From 4659ccff704431e23bd3635c9d6beb33551d62b8 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 14 Sep 2016 10:57:00 +0200 Subject: Issue #28114: Add unit tests on os.spawn*() --- Lib/test/test_os.py | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py index 95c7482..d82a4a7 100644 --- a/Lib/test/test_os.py +++ b/Lib/test/test_os.py @@ -1413,6 +1413,7 @@ def _execvpe_mockup(defpath=None): os.execve = orig_execve os.defpath = orig_defpath + class ExecTests(unittest.TestCase): @unittest.skipIf(USING_LINUXTHREADS, "avoid triggering a linuxthreads bug: see issue #4970") @@ -2163,6 +2164,91 @@ class PidTests(unittest.TestCase): self.assertEqual(status, (pid, 0)) +class SpawnTests(unittest.TestCase): + def create_args(self, with_env=False): + self.exitcode = 17 + + filename = support.TESTFN + self.addCleanup(support.unlink, filename) + + if not with_env: + code = 'import sys; sys.exit(%s)' % self.exitcode + else: + self.env = dict(os.environ) + # create an unique key + self.key = str(uuid.uuid4()) + self.env[self.key] = self.key + # read the variable from os.environ to check that it exists + code = ('import sys, os; magic = os.environ[%r]; sys.exit(%s)' + % (self.key, self.exitcode)) + + with open(filename, "w") as fp: + fp.write(code) + + return [sys.executable, filename] + + @unittest.skipUnless(hasattr(os, 'spawnl'), 'need os.spawnl') + def test_spawnl(self): + args = self.create_args() + exitcode = os.spawnl(os.P_WAIT, args[0], *args) + self.assertEqual(exitcode, self.exitcode) + + @unittest.skipUnless(hasattr(os, 'spawnle'), 'need os.spawnle') + def test_spawnle(self): + args = self.create_args(True) + exitcode = os.spawnle(os.P_WAIT, args[0], *args, self.env) + self.assertEqual(exitcode, self.exitcode) + + @unittest.skipUnless(hasattr(os, 'spawnlp'), 'need os.spawnlp') + def test_spawnlp(self): + args = self.create_args() + exitcode = os.spawnlp(os.P_WAIT, args[0], *args) + self.assertEqual(exitcode, self.exitcode) + + @unittest.skipUnless(hasattr(os, 'spawnlpe'), 'need os.spawnlpe') + def test_spawnlpe(self): + args = self.create_args(True) + exitcode = os.spawnlpe(os.P_WAIT, args[0], *args, self.env) + self.assertEqual(exitcode, self.exitcode) + + @unittest.skipUnless(hasattr(os, 'spawnv'), 'need os.spawnv') + def test_spawnv(self): + args = self.create_args() + exitcode = os.spawnv(os.P_WAIT, args[0], args) + self.assertEqual(exitcode, self.exitcode) + + @unittest.skipUnless(hasattr(os, 'spawnve'), 'need os.spawnve') + def test_spawnve(self): + args = self.create_args(True) + exitcode = os.spawnve(os.P_WAIT, args[0], args, self.env) + self.assertEqual(exitcode, self.exitcode) + + @unittest.skipUnless(hasattr(os, 'spawnvp'), 'need os.spawnvp') + def test_spawnvp(self): + args = self.create_args() + exitcode = os.spawnvp(os.P_WAIT, args[0], args) + self.assertEqual(exitcode, self.exitcode) + + @unittest.skipUnless(hasattr(os, 'spawnvpe'), 'need os.spawnvpe') + def test_spawnvpe(self): + args = self.create_args(True) + exitcode = os.spawnvpe(os.P_WAIT, args[0], args, self.env) + self.assertEqual(exitcode, self.exitcode) + + @unittest.skipUnless(hasattr(os, 'spawnv'), 'need os.spawnv') + def test_nowait(self): + args = self.create_args() + pid = os.spawnv(os.P_NOWAIT, args[0], args) + result = os.waitpid(pid, 0) + self.assertEqual(result[0], pid) + status = result[1] + if hasattr(os, 'WIFEXITED'): + self.assertTrue(os.WIFEXITED(status)) + self.assertEqual(os.WEXITSTATUS(status), self.exitcode) + else: + self.assertEqual(status, self.exitcode << 8) + + # The introduction of this TestCase caused at least two different errors on # *nix buildbots. Temporarily skip this to let the buildbots move along. @unittest.skip("Skip due to platform/environment differences on *NIX buildbots") -- cgit v0.12