Refactor test_bin

This commit is contained in:
Kodi Arfer 2017-03-17 09:31:54 -07:00
parent ae1dd78c53
commit 32e76caafe

View File

@ -29,110 +29,87 @@ from hy._compat import PY3
hy_dir = os.environ.get('HY_DIR', '') hy_dir = os.environ.get('HY_DIR', '')
def run_cmd(cmd, stdin_data=None): def run_cmd(cmd, stdin_data=None, expect=0):
p = subprocess.Popen(os.path.join(hy_dir, cmd), p = subprocess.Popen(os.path.join(hy_dir, cmd),
stdin=subprocess.PIPE, stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, stderr=subprocess.PIPE,
shell=True) shell=True)
stdout = ""
stderr = ""
if stdin_data is not None: if stdin_data is not None:
p.stdin.write(stdin_data.encode('ASCII')) p.stdin.write(stdin_data.encode('ASCII'))
p.stdin.flush() p.stdin.flush()
p.stdin.close() p.stdin.close()
# Read stdout and stderr otherwise if the PIPE buffer is full, we might # Read stdout and stderr otherwise if the PIPE buffer is full, we might
# wait for ever… # wait for ever…
stdout = ""
stderr = ""
while p.poll() is None: while p.poll() is None:
stdout += p.stdout.read().decode('utf-8') stdout += p.stdout.read().decode('utf-8')
stderr += p.stderr.read().decode('utf-8') stderr += p.stderr.read().decode('utf-8')
return p.returncode, stdout, stderr assert p.returncode == expect
return stdout, stderr
def test_bin_hy(): def test_bin_hy():
ret = run_cmd("hy", "") run_cmd("hy", "")
assert ret[0] == 0
def test_bin_hy_stdin(): def test_bin_hy_stdin():
ret = run_cmd("hy", '(koan)') output, _ = run_cmd("hy", '(koan)')
assert ret[0] == 0 assert "monk" in output
assert "monk" in ret[1]
def test_bin_hy_cmd(): def test_bin_hy_cmd():
ret = run_cmd("hy -c \"(koan)\"") output, _ = run_cmd("hy -c \"(koan)\"")
assert ret[0] == 0 assert "monk" in output
assert "monk" in ret[1]
ret = run_cmd("hy -c \"(koan\"") _, err = run_cmd("hy -c \"(koan\"", expect=1)
assert ret[0] == 1 assert "Premature end of input" in err
assert "Premature end of input" in ret[2]
def test_bin_hy_icmd(): def test_bin_hy_icmd():
ret = run_cmd("hy -i \"(koan)\"", "(ideas)") output, _ = run_cmd("hy -i \"(koan)\"", "(ideas)")
assert ret[0] == 0
output = ret[1]
assert "monk" in output assert "monk" in output
assert "figlet" in output assert "figlet" in output
def test_bin_hy_icmd_file(): def test_bin_hy_icmd_file():
ret = run_cmd("hy -i resources/icmd_test_file.hy", "(ideas)") output, _ = run_cmd("hy -i resources/icmd_test_file.hy", "(ideas)")
assert ret[0] == 0
output = ret[1]
assert "Hy!" in output assert "Hy!" in output
def test_bin_hy_icmd_and_spy(): def test_bin_hy_icmd_and_spy():
ret = run_cmd("hy -i \"(+ [] [])\" --spy", "(+ 1 1)") output, _ = run_cmd("hy -i \"(+ [] [])\" --spy", "(+ 1 1)")
assert ret[0] == 0
output = ret[1]
assert "([] + [])" in output assert "([] + [])" in output
def test_bin_hy_missing_file(): def test_bin_hy_missing_file():
ret = run_cmd("hy foobarbaz") _, err = run_cmd("hy foobarbaz", expect=2)
assert ret[0] == 2 assert "No such file" in err
assert "No such file" in ret[2]
def test_bin_hy_file_with_args(): def test_bin_hy_file_with_args():
ret = run_cmd("hy tests/resources/argparse_ex.hy -h") assert "usage" in run_cmd("hy tests/resources/argparse_ex.hy -h")[0]
assert ret[0] == 0 assert "got c" in run_cmd("hy tests/resources/argparse_ex.hy -c bar")[0]
assert "usage" in ret[1] assert "foo" in run_cmd("hy tests/resources/argparse_ex.hy -i foo")[0]
ret = run_cmd("hy tests/resources/argparse_ex.hy -c bar") assert "foo" in run_cmd("hy tests/resources/argparse_ex.hy -i foo -c bar")[0] # noqa
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(): def test_bin_hyc():
ret = run_cmd("hyc") _, err = run_cmd("hyc", expect=2)
assert ret[0] == 2 assert "usage" in err
assert "usage" in ret[2]
ret = run_cmd("hyc -h") output, _ = run_cmd("hyc -h")
assert ret[0] == 0 assert "usage" in output
assert "usage" in ret[1]
ret = run_cmd("hyc tests/resources/argparse_ex.hy") output, _ = run_cmd("hyc tests/resources/argparse_ex.hy")
assert ret[0] == 0 assert "Compiling" in output
assert "Compiling" in ret[1]
assert os.path.exists("tests/resources/argparse_ex.pyc") assert os.path.exists("tests/resources/argparse_ex.pyc")
def test_bin_hyc_missing_file(): def test_bin_hyc_missing_file():
ret = run_cmd("hyc foobarbaz") _, err = run_cmd("hyc foobarbaz", expect=2)
assert ret[0] == 2 assert "[Errno 2]" in err
assert "[Errno 2]" in ret[2]
def test_hy2py(): def test_hy2py():
@ -144,10 +121,10 @@ def test_hy2py():
continue continue
else: else:
i += 1 i += 1
ret = run_cmd("hy2py -s -a " + os.path.join(dirpath, f)) output, err = run_cmd("hy2py -s -a " +
assert ret[0] == 0, f os.path.join(dirpath, f))
assert len(ret[1]) > 1, f assert len(output) > 1, f
assert len(ret[2]) == 0, f assert len(err) == 0, f
assert i assert i
@ -159,48 +136,40 @@ def test_bin_hy_builtins():
def test_bin_hy_main(): def test_bin_hy_main():
ret = run_cmd("hy tests/resources/bin/main.hy") output, _ = run_cmd("hy tests/resources/bin/main.hy")
assert ret[0] == 0 assert "Hello World" in output
assert "Hello World" in ret[1]
def test_bin_hy_main_args(): def test_bin_hy_main_args():
ret = run_cmd("hy tests/resources/bin/main.hy test 123") output, _ = run_cmd("hy tests/resources/bin/main.hy test 123")
assert ret[0] == 0 assert "test" in output
assert "test" in ret[1] assert "123" in output
assert "123" in ret[1]
def test_bin_hy_main_exitvalue(): def test_bin_hy_main_exitvalue():
ret = run_cmd("hy tests/resources/bin/main.hy exit1") run_cmd("hy tests/resources/bin/main.hy exit1", expect=1)
assert ret[0] == 1
def test_bin_hy_no_main(): def test_bin_hy_no_main():
ret = run_cmd("hy tests/resources/bin/nomain.hy") output, _ = run_cmd("hy tests/resources/bin/nomain.hy")
assert ret[0] == 0 assert "This Should Still Work" in output
assert "This Should Still Work" in ret[1]
def test_bin_hy_module_main(): def test_bin_hy_module_main():
ret = run_cmd("hy -m tests.resources.bin.main") output, _ = run_cmd("hy -m tests.resources.bin.main")
assert ret[0] == 0 assert "Hello World" in output
assert "Hello World" in ret[1]
def test_bin_hy_module_main_args(): def test_bin_hy_module_main_args():
ret = run_cmd("hy -m tests.resources.bin.main test 123") output, _ = run_cmd("hy -m tests.resources.bin.main test 123")
assert ret[0] == 0 assert "test" in output
assert "test" in ret[1] assert "123" in output
assert "123" in ret[1]
def test_bin_hy_module_main_exitvalue(): def test_bin_hy_module_main_exitvalue():
ret = run_cmd("hy -m tests.resources.bin.main exit1") run_cmd("hy -m tests.resources.bin.main exit1", expect=1)
assert ret[0] == 1
def test_bin_hy_module_no_main(): def test_bin_hy_module_no_main():
ret = run_cmd("hy -m tests.resources.bin.nomain") output, _ = run_cmd("hy -m tests.resources.bin.nomain")
assert ret[0] == 0 assert "This Should Still Work" in output
assert "This Should Still Work" in ret[1]