diff --git a/tests/importer/test_importer.py b/tests/importer/test_importer.py index d9c3565..db1e2a3 100644 --- a/tests/importer/test_importer.py +++ b/tests/importer/test_importer.py @@ -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(): diff --git a/tests/resources/bin/__main__.hy b/tests/resources/bin/__main__.hy new file mode 100644 index 0000000..2f4951d --- /dev/null +++ b/tests/resources/bin/__main__.hy @@ -0,0 +1,2 @@ +(print "This is a __main__.hy") +(setv visited_main True) diff --git a/tests/test_bin.py b/tests/test_bin.py index 6ccaea1..b267bb1 100644 --- a/tests/test_bin.py +++ b/tests/test_bin.py @@ -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