]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-34783: Add test_cmd_line_script.test_nonexisting_script() (GH-9535)
authorVictor Stinner <vstinner@redhat.com>
Mon, 24 Sep 2018 15:06:29 +0000 (08:06 -0700)
committerGitHub <noreply@github.com>
Mon, 24 Sep 2018 15:06:29 +0000 (08:06 -0700)
Make sure that "./python script.py" does not crash if the script
file doesn't exist.

Lib/test/test_cmd_line_script.py

index a9c1309ad6d32beac64a2cf2037b3d50d2c253a6..2595ca98c7addb1e5ef3c10e016c7d861ddcd011 100644 (file)
@@ -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()