Add tests for importing __main__.hy files

Closes hylang/hy#1466.
This commit is contained in:
Brandon T. Willard 2018-08-22 01:38:15 -05:00
parent e9e7171b56
commit 4839acadf7
3 changed files with 39 additions and 3 deletions

View File

@ -24,9 +24,31 @@ from hy.importer import hy_parse, HyLoader, cache_from_source
def test_basics():
"Make sure the basics of the importer work"
basic_namespace = runpy.run_path("tests/resources/importer/basic.hy",
run_name='__main__')
assert 'square' in basic_namespace
assert os.path.isfile('tests/resources/__init__.py')
resources_mod = importlib.import_module('tests.resources')
assert hasattr(resources_mod, 'kwtest')
assert os.path.isfile('tests/resources/bin/__init__.hy')
bin_mod = importlib.import_module('tests.resources.bin')
assert hasattr(bin_mod, '_null_fn_for_import_test')
def test_runpy():
# XXX: `runpy` won't update cached bytecode! Don't know if that's
# intentional or not.
basic_ns = runpy.run_path('tests/resources/importer/basic.hy')
assert 'square' in basic_ns
main_ns = runpy.run_path('tests/resources/bin')
assert main_ns['visited_main'] == 1
del main_ns
main_ns = runpy.run_module('tests.resources.bin')
assert main_ns['visited_main'] == 1
with pytest.raises(IOError):
runpy.run_path('tests/resources/foobarbaz.py')
def test_stringer():

View File

@ -0,0 +1,2 @@
(print "This is a __main__.hy")
(setv visited_main True)

View File

@ -328,6 +328,18 @@ def test_bin_hy_module_main():
assert "Hello World" in output
def test_bin_hy_module_main_file():
output, _ = run_cmd("hy -m tests.resources.bin")
assert "This is a __main__.hy" in output
output, _ = run_cmd("hy -m .tests.resources.bin", expect=1)
def test_bin_hy_file_main_file():
output, _ = run_cmd("hy tests/resources/bin")
assert "This is a __main__.hy" in output
def test_bin_hy_module_main_args():
output, _ = run_cmd("hy -m tests.resources.bin.main test 123")
assert "test" in output