Skip to content

Commit a46467f

Browse files
authored
bpo-34783: Add test_cmd_line_script.test_nonexisting_script() (GH-9535)
Make sure that "./python script.py" does not crash if the script file doesn't exist.
1 parent 1b77f92 commit a46467f

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

Lib/test/test_cmd_line_script.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,25 @@ def test_consistent_sys_path_for_module_execution(self):
630630
traceback_lines = stderr.decode().splitlines()
631631
self.assertIn("No module named script_pkg", traceback_lines[-1])
632632

633+
def test_nonexisting_script(self):
634+
# bpo-34783: "./python script.py" must not crash
635+
# if the script file doesn't exist.
636+
script = 'nonexistingscript.py'
637+
self.assertFalse(os.path.exists(script))
638+
# Only test the base name, since the error message can use
639+
# a relative path, whereas sys.executable can be an asolution path.
640+
program = os.path.basename(sys.executable)
641+
642+
proc = spawn_python(script, text=True,
643+
stdout=subprocess.PIPE,
644+
stderr=subprocess.PIPE)
645+
out, err = proc.communicate()
646+
# "./python" must be in the error message:
647+
# "./python: can't open file (...)"
648+
self.assertIn(program, err)
649+
self.assertNotEqual(proc.returncode, 0)
650+
651+
633652
def test_main():
634653
support.run_unittest(CmdLineTest)
635654
support.reap_children()

0 commit comments

Comments
 (0)