parent
c022abc831
commit
cbaba4a10a
@ -229,13 +229,21 @@ def run_repl(hr=None, **kwargs):
|
|||||||
|
|
||||||
|
|
||||||
def run_icommand(source, **kwargs):
|
def run_icommand(source, **kwargs):
|
||||||
hr = HyREPL(**kwargs)
|
|
||||||
if os.path.exists(source):
|
if os.path.exists(source):
|
||||||
|
# Emulate Python cmdline behavior by setting `sys.path` relative
|
||||||
|
# to the executed file's location.
|
||||||
|
if sys.path[0] == '':
|
||||||
|
sys.path[0] = os.path.realpath(os.path.split(source)[0])
|
||||||
|
else:
|
||||||
|
sys.path.insert(0, os.path.split(source)[0])
|
||||||
|
|
||||||
with io.open(source, "r", encoding='utf-8') as f:
|
with io.open(source, "r", encoding='utf-8') as f:
|
||||||
source = f.read()
|
source = f.read()
|
||||||
filename = source
|
filename = source
|
||||||
else:
|
else:
|
||||||
filename = '<input>'
|
filename = '<input>'
|
||||||
|
|
||||||
|
hr = HyREPL(**kwargs)
|
||||||
hr.runsource(source, filename=filename, symbol='single')
|
hr.runsource(source, filename=filename, symbol='single')
|
||||||
return run_repl(hr)
|
return run_repl(hr)
|
||||||
|
|
||||||
@ -333,9 +341,18 @@ def cmdline_handler(scriptname, argv):
|
|||||||
|
|
||||||
else:
|
else:
|
||||||
# User did "hy <filename>"
|
# User did "hy <filename>"
|
||||||
|
filename = options.args[0]
|
||||||
|
|
||||||
|
# Emulate Python cmdline behavior by setting `sys.path` relative
|
||||||
|
# to the executed file's location.
|
||||||
|
if sys.path[0] == '':
|
||||||
|
sys.path[0] = os.path.realpath(os.path.split(filename)[0])
|
||||||
|
else:
|
||||||
|
sys.path.insert(0, os.path.split(filename)[0])
|
||||||
|
|
||||||
try:
|
try:
|
||||||
sys.argv = options.args
|
sys.argv = options.args
|
||||||
runpy.run_path(options.args[0], run_name='__main__')
|
runpy.run_path(filename, run_name='__main__')
|
||||||
return 0
|
return 0
|
||||||
except FileNotFoundError as e:
|
except FileNotFoundError as e:
|
||||||
print("hy: Can't open file '{0}': [Errno {1}] {2}".format(
|
print("hy: Can't open file '{0}': [Errno {1}] {2}".format(
|
||||||
|
3
tests/resources/relative_import.hy
Normal file
3
tests/resources/relative_import.hy
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
(import bin.printenv)
|
||||||
|
(import sys)
|
||||||
|
(print sys.path)
|
@ -214,6 +214,10 @@ def test_bin_hy_icmd_file():
|
|||||||
output, _ = run_cmd("hy -i resources/icmd_test_file.hy", "(ideas)")
|
output, _ = run_cmd("hy -i resources/icmd_test_file.hy", "(ideas)")
|
||||||
assert "Hy!" in output
|
assert "Hy!" in output
|
||||||
|
|
||||||
|
file_relative_path = os.path.realpath(os.path.split('tests/resources/relative_import.hy')[0])
|
||||||
|
|
||||||
|
output, _ = run_cmd("hy -i tests/resources/relative_import.hy None")
|
||||||
|
assert file_relative_path in output
|
||||||
|
|
||||||
def test_bin_hy_icmd_and_spy():
|
def test_bin_hy_icmd_and_spy():
|
||||||
output, _ = run_cmd("hy -i \"(+ [] [])\" --spy", "(+ 1 1)")
|
output, _ = run_cmd("hy -i \"(+ [] [])\" --spy", "(+ 1 1)")
|
||||||
@ -346,6 +350,20 @@ def test_bin_hy_file_main_file():
|
|||||||
assert "This is a __main__.hy" in output
|
assert "This is a __main__.hy" in output
|
||||||
|
|
||||||
|
|
||||||
|
def test_bin_hy_file_sys_path():
|
||||||
|
"""The test resource `relative_import.hy` will perform an absolute import
|
||||||
|
of a module in its directory: a directory that is not on the `sys.path` of
|
||||||
|
the script executing the module (i.e. `hy`). We want to make sure that Hy
|
||||||
|
adopts the file's location in `sys.path`, instead of the runner's current
|
||||||
|
dir (e.g. '' in `sys.path`).
|
||||||
|
"""
|
||||||
|
file_path, _ = os.path.split('tests/resources/relative_import.hy')
|
||||||
|
file_relative_path = os.path.realpath(file_path)
|
||||||
|
|
||||||
|
output, _ = run_cmd("hy tests/resources/relative_import.hy")
|
||||||
|
assert file_relative_path in output
|
||||||
|
|
||||||
|
|
||||||
def test_bin_hy_module_main_args():
|
def test_bin_hy_module_main_args():
|
||||||
output, _ = run_cmd("hy -m tests.resources.bin.main test 123")
|
output, _ = run_cmd("hy -m tests.resources.bin.main test 123")
|
||||||
assert "test" in output
|
assert "test" in output
|
||||||
|
Loading…
x
Reference in New Issue
Block a user