diff --git a/hy/cmdline.py b/hy/cmdline.py index 5178855..199c454 100644 --- a/hy/cmdline.py +++ b/hy/cmdline.py @@ -37,7 +37,7 @@ from hy.lex.states import Idle, LexException from hy.lex.machine import Machine from hy.compiler import hy_compile from hy.core import process -from hy.importer import ast_compile +from hy.importer import ast_compile, import_buffer_to_module import hy.completer @@ -137,8 +137,13 @@ def ideas_macro(tree): def run_command(source): - hr = HyREPL() - hr.runsource(source, filename='', symbol='single') + try: + import_buffer_to_module("__main__", source) + except LexException as exc: + # TODO: This would be better if we had line, col info. + print(source) + print(repr(exc)) + return 1 return 0 diff --git a/hy/importer.py b/hy/importer.py index 548eb1f..1287d8b 100644 --- a/hy/importer.py +++ b/hy/importer.py @@ -78,6 +78,13 @@ def import_file_to_module(module_name, fpath): return mod +def import_buffer_to_module(module_name, buf): + _ast = import_buffer_to_ast(buf) + mod = imp.new_module(module_name) + eval(ast_compile(_ast, "", "exec"), mod.__dict__) + return mod + + def hy_eval(hytree, namespace): foo = HyObject() foo.start_line = 0 diff --git a/tests/test_bin.py b/tests/test_bin.py index ff05516..47f9c57 100644 --- a/tests/test_bin.py +++ b/tests/test_bin.py @@ -1,8 +1,44 @@ import subprocess -def test_bin_hy(): - p = subprocess.Popen("echo | bin/hy", +def run_cmd(cmd): + p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) p.wait() - assert p.returncode == 0 + return p.returncode, p.stdout, p.stderr + + +def test_bin_hy(): + ret = run_cmd("echo | bin/hy") + assert ret[0] == 0 + + +def test_bin_hy_stdin(): + ret = run_cmd("echo \"(koan)\" | bin/hy") + assert ret[0] == 0 + assert "monk" in ret[1].read().decode("utf-8") + + +def test_bin_hy_cmd(): + ret = run_cmd("bin/hy -c \"(koan)\"") + assert ret[0] == 0 + assert "monk" in ret[1].read().decode("utf-8") + + ret = run_cmd("bin/hy -c \"(koan\"") + assert ret[0] == 1 + assert "LexException" in ret[1].read().decode("utf-8") + + +def test_bin_hy_icmd(): + ret = run_cmd("echo \"(ideas)\" | bin/hy -i \"(koan)\"") + assert ret[0] == 0 + output = ret[1].read().decode("utf-8") + + assert "monk" in output + assert "figlet" in output + + +def test_bin_hy_file(): + ret = run_cmd("bin/hy eg/nonfree/halting-problem/halting.hy") + assert ret[0] == 0 + assert "27" in ret[1].read().decode("utf-8")