File: test_bin.py

package info (click to toggle)
hy 0.10.1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,220 kB
  • ctags: 484
  • sloc: python: 3,737; makefile: 207; sh: 20
file content (172 lines) | stat: -rw-r--r-- 5,177 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Copyright (c) 2013 Julien Danjou <julien@danjou.info>
# Copyright (c) 2013 Will Kahn-Greene <willg@bluesock.org>
# Copyright (c) 2014 Bob Tolbert <bob@tolbert.org>
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
import os
import subprocess
from hy._compat import PY3


def run_cmd(cmd, stdin_data=None):
    p = subprocess.Popen(cmd,
                         stdin=subprocess.PIPE,
                         stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE,
                         shell=True)
    stdout = ""
    stderr = ""
    if stdin_data is not None:
        p.stdin.write(stdin_data.encode('ASCII'))
        p.stdin.flush()
        p.stdin.close()
    # Read stdout and stderr otherwise if the PIPE buffer is full, we might
    # wait for ever…
    while p.poll() is None:
        stdout += p.stdout.read().decode('utf-8')
        stderr += p.stderr.read().decode('utf-8')
    return p.returncode, stdout, stderr


def test_bin_hy():
    ret = run_cmd("hy", "")
    assert ret[0] == 0


def test_bin_hy_stdin():
    ret = run_cmd("hy", '(koan)')
    assert ret[0] == 0
    assert "monk" in ret[1]


def test_bin_hy_cmd():
    ret = run_cmd("hy -c \"(koan)\"")
    assert ret[0] == 0
    assert "monk" in ret[1]

    ret = run_cmd("hy -c \"(koan\"")
    assert ret[0] == 1
    assert "Premature end of input" in ret[2]


def test_bin_hy_icmd():
    ret = run_cmd("hy -i \"(koan)\"", "(ideas)")
    assert ret[0] == 0
    output = ret[1]

    assert "monk" in output
    assert "figlet" in output


def test_bin_hy_icmd_and_spy():
    ret = run_cmd("hy -i \"(+ [] [])\" --spy", "(+ 1 1)")
    assert ret[0] == 0
    output = ret[1]

    assert "([] + [])" in output


def test_bin_hy_missing_file():
    ret = run_cmd("hy foobarbaz")
    assert ret[0] == 2
    assert "No such file" in ret[2]


def test_bin_hy_file_with_args():
    ret = run_cmd("hy tests/resources/argparse_ex.hy -h")
    assert ret[0] == 0
    assert "usage" in ret[1]
    ret = run_cmd("hy tests/resources/argparse_ex.hy -c bar")
    assert ret[0] == 0
    assert "got c" in ret[1]
    ret = run_cmd("hy tests/resources/argparse_ex.hy -i foo")
    assert ret[0] == 0
    assert "foo" in ret[1]
    ret = run_cmd("hy tests/resources/argparse_ex.hy -i foo -c bar")
    assert ret[0] == 0
    assert "foo" in ret[1]


def test_bin_hyc():
    ret = run_cmd("hyc")
    assert ret[0] == 2
    assert "usage" in ret[2]
    ret = run_cmd("hyc -h")
    assert ret[0] == 0
    assert "usage" in ret[1]
    ret = run_cmd("hyc tests/resources/argparse_ex.hy")
    assert ret[0] == 0
    assert "Compiling" in ret[1]
    assert os.path.exists("tests/resources/argparse_ex.pyc")


def test_bin_hyc_missing_file():
    ret = run_cmd("hyc foobarbaz")
    assert ret[0] == 2
    assert "[Errno 2]" in ret[2]


def test_hy2py():
    i = 0
    for dirpath, dirnames, filenames in os.walk("tests/native_tests"):
        for f in filenames:
            if f.endswith(".hy"):
                if f == "py3_only_tests.hy" and not PY3:
                    continue
                else:
                    i += 1
                    ret = run_cmd("hy2py -s -a "
                                  + os.path.join(dirpath, f))
                    assert ret[0] == 0, f
                    assert len(ret[1]) > 1, f
                    assert len(ret[2]) == 0, f
    assert i


def test_bin_hy_builtins():
    import hy.cmdline  # NOQA

    assert str(exit) == "Use (exit) or Ctrl-D (i.e. EOF) to exit"
    assert str(quit) == "Use (quit) or Ctrl-D (i.e. EOF) to exit"


def test_bin_hy_main():
    ret = run_cmd("hy tests/resources/bin/main.hy")
    assert ret[0] == 0
    assert "Hello World" in ret[1]


def test_bin_hy_main_args():
    ret = run_cmd("hy tests/resources/bin/main.hy test 123")
    assert ret[0] == 0
    assert "test" in ret[1]
    assert "123" in ret[1]


def test_bin_hy_main_exitvalue():
    ret = run_cmd("hy tests/resources/bin/main.hy exit1")
    assert ret[0] == 1


def test_bin_hy_no_main():
    ret = run_cmd("hy tests/resources/bin/nomain.hy")
    assert ret[0] == 0
    assert "This Should Still Work" in ret[1]