Skip to content

Commit 41e5814

Browse files
authored
Give an explanatory error if user passes a .py file to cli. (#198)
Old habits die hard, more than once in development I've mistakenly passed a `.py` file to the cli, which will then spit out a long traceback. For the benefit of new users and old, write a helpful message to stderr and exit with an error code if the user asks to process a `.py` file. Example: ``` $ spy --redshift foo.py Error: foo.py is a .py file, not a .spy file. ```
2 parents e23918b + 77c912c commit 41e5814

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

spy/cli.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,10 @@ async def inner_main(args: Arguments) -> None:
307307
do_pyparse(str(args.filename))
308308
return
309309

310+
if args.filename.suffix == '.py':
311+
print(f"Error: {args.filename} is a .py file, not a .spy file.", file=sys.stderr)
312+
sys.exit(1)
313+
310314
modname = args.filename.stem
311315
srcdir = args.filename.parent
312316
vm = await SPyVM.async_new()

spy/tests/test_cli.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,16 @@ def run_external(self, python_exe, *args: Any) -> Any:
7575
raise Exception("run_external failed")
7676
return exit_code, decolorize(stdout)
7777

78+
def test_py_file_error(self):
79+
# Create a .py file instead of .spy
80+
py_file = self.tmpdir.join('test.py')
81+
py_file.write("print('This is a Python file')")
82+
83+
# Test that passing a .py file produces an error
84+
res = self.runner.invoke(app, [str(py_file)])
85+
assert res.exit_code == 1
86+
assert "Error:" in res.output and ".py file, not a .spy file" in res.output
87+
7888
def test_pyparse(self):
7989
res, stdout = self.run('--pyparse', self.main_spy)
8090
assert stdout.startswith('py:Module(')

0 commit comments

Comments
 (0)