diff options
author | Victor Stinner <vstinner@redhat.com> | 2018-09-24 15:06:29 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-09-24 15:06:29 (GMT) |
commit | a46467ff198c42c8f34768c7be4b4562f6f44736 (patch) | |
tree | 823c779fa60ab3434048a4523e3c06fe3f57d27d | |
parent | 1b77f929f8d18e9a97335a8ec5e838e5be506568 (diff) | |
download | cpython-a46467ff198c42c8f34768c7be4b4562f6f44736.zip cpython-a46467ff198c42c8f34768c7be4b4562f6f44736.tar.gz cpython-a46467ff198c42c8f34768c7be4b4562f6f44736.tar.bz2 |
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.
-rw-r--r-- | Lib/test/test_cmd_line_script.py | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/Lib/test/test_cmd_line_script.py b/Lib/test/test_cmd_line_script.py index a9c1309..2595ca9 100644 --- a/Lib/test/test_cmd_line_script.py +++ b/Lib/test/test_cmd_line_script.py @@ -630,6 +630,25 @@ class CmdLineTest(unittest.TestCase): traceback_lines = stderr.decode().splitlines() self.assertIn("No module named script_pkg", traceback_lines[-1]) + def test_nonexisting_script(self): + # bpo-34783: "./python script.py" must not crash + # if the script file doesn't exist. + script = 'nonexistingscript.py' + self.assertFalse(os.path.exists(script)) + # Only test the base name, since the error message can use + # a relative path, whereas sys.executable can be an asolution path. + program = os.path.basename(sys.executable) + + proc = spawn_python(script, text=True, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + out, err = proc.communicate() + # "./python" must be in the error message: + # "./python: can't open file (...)" + self.assertIn(program, err) + self.assertNotEqual(proc.returncode, 0) + + def test_main(): support.run_unittest(CmdLineTest) support.reap_children() |