diff options
-rw-r--r-- | coverage/backward.py | 27 | ||||
-rw-r--r-- | test/coveragetest.py | 5 | ||||
-rw-r--r-- | test/modules/aa/zfile.py | 1 | ||||
-rw-r--r-- | test/test_coverage.py | 8 | ||||
-rw-r--r-- | test/test_farm.py | 17 |
5 files changed, 36 insertions, 22 deletions
diff --git a/coverage/backward.py b/coverage/backward.py index 506649d..91f07bf 100644 --- a/coverage/backward.py +++ b/coverage/backward.py @@ -4,6 +4,7 @@ # (Redefining built-in blah) # The whole point of this file is to redefine built-ins, so shut up about it. +import os # Python 2.3 doesn't have `set` try: @@ -22,3 +23,29 @@ except NameError: lst = list(iterable) lst.sort() return lst + +# Py2k and 3k don't agree on how to run commands in a subprocess. +try: + import subprocess +except ImportError: + def run_command(cmd): + """Run a command in a subprocess. + + Returns the exit code and the combined stdout and stderr. + + """ + _, stdouterr = os.popen4(cmd) + return 0, stdouterr.read() +else: + def run_command(cmd): + """Run a command in a subprocess. + + Returns the exit code and the combined stdout and stderr. + + """ + proc = subprocess.Popen(cmd, shell=True, + stdin=subprocess.PIPE, stdout=subprocess.PIPE, + stderr=subprocess.STDOUT + ) + retcode = proc.wait() + return retcode, proc.stdout.read() diff --git a/test/coveragetest.py b/test/coveragetest.py index baace80..9588a7f 100644 --- a/test/coveragetest.py +++ b/test/coveragetest.py @@ -4,7 +4,7 @@ import imp, os, random, shutil, sys, tempfile, textwrap, unittest from cStringIO import StringIO import coverage -from coverage.backward import set # pylint: disable-msg=W0622 +from coverage.backward import set, run_command # pylint: disable-msg=W0622 class Tee(object): @@ -209,8 +209,7 @@ class CoverageTest(unittest.TestCase): pypath += testmods + os.pathsep + zipfile os.environ['PYTHONPATH'] = pypath - stdin_unused, stdouterr = os.popen4(cmd) - output = stdouterr.read() + _, output = run_command(cmd) print output return output diff --git a/test/modules/aa/zfile.py b/test/modules/aa/zfile.py new file mode 100644 index 0000000..924f9b7 --- /dev/null +++ b/test/modules/aa/zfile.py @@ -0,0 +1 @@ +# zfile.py diff --git a/test/test_coverage.py b/test/test_coverage.py index 1084b98..64fe1d5 100644 --- a/test/test_coverage.py +++ b/test/test_coverage.py @@ -1586,7 +1586,7 @@ class ProcessTest(CoverageTest): self.assert_(not os.path.exists(".coverage")) out = self.run_command("coverage -x mycode.py") self.assert_(os.path.exists(".coverage")) - self.assertEqual(out, 'done\n') + self.assertEqual(out.strip(), 'done') def testReport(self): self.makeFile("mycode.py", """\ @@ -1597,7 +1597,7 @@ class ProcessTest(CoverageTest): """) out = self.run_command("coverage -x mycode.py") - self.assertEqual(out, 'done\n') + self.assertEqual(out.strip(), 'done') report1 = self.run_command("coverage -r").replace('\\', '/') # Name Stmts Exec Cover @@ -1656,11 +1656,11 @@ class ProcessTest(CoverageTest): """) out = self.run_command("coverage -x -p b_or_c.py b") - self.assertEqual(out, 'done\n') + self.assertEqual(out.strip(), 'done') self.assert_(not os.path.exists(".coverage")) out = self.run_command("coverage -x -p b_or_c.py c") - self.assertEqual(out, 'done\n') + self.assertEqual(out.strip(), 'done') self.assert_(not os.path.exists(".coverage")) # After two -p runs, there should be two .coverage.machine.123 files. diff --git a/test/test_farm.py b/test/test_farm.py index d33dcdc..4323f23 100644 --- a/test/test_farm.py +++ b/test/test_farm.py @@ -1,11 +1,7 @@ """Run tests in the farm subdirectory. Designed for nose.""" import filecmp, fnmatch, glob, os, shutil, sys - -try: - import subprocess -except ImportError: - subprocess = None +from coverage.backward import run_command def test_farm(clean_only=False): @@ -135,16 +131,7 @@ class FarmTestCase(object): for cmd in cmds.split("\n"): if not cmd.strip(): continue - if subprocess: - proc = subprocess.Popen(cmd, shell=True, - stdin=subprocess.PIPE, stdout=subprocess.PIPE, - stderr=subprocess.STDOUT) - retcode = proc.wait() - output = proc.stdout.read() - else: - _, stdouterr = os.popen4(cmd) - output = stdouterr.read() - retcode = 0 # Can't tell if the process failed. + retcode, output = run_command(cmd) print output, if outfile: open(outfile, "a+").write(output) |