diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/backtest.py | 54 | ||||
-rw-r--r-- | test/coveragetest.py | 9 | ||||
-rw-r--r-- | test/farm/run/src/chdir.py | 4 | ||||
-rw-r--r-- | test/farm/run/src/showtrace.py | 13 | ||||
-rw-r--r-- | test/test_api.py | 4 | ||||
-rw-r--r-- | test/test_cmdline.py | 4 | ||||
-rw-r--r-- | test/test_codeunit.py | 2 | ||||
-rw-r--r-- | test/test_coverage.py | 5 | ||||
-rw-r--r-- | test/test_data.py | 6 | ||||
-rw-r--r-- | test/test_execfile.py | 4 | ||||
-rw-r--r-- | test/test_farm.py | 8 |
11 files changed, 93 insertions, 20 deletions
diff --git a/test/backtest.py b/test/backtest.py new file mode 100644 index 00000000..21a14d6d --- /dev/null +++ b/test/backtest.py @@ -0,0 +1,54 @@ +"""Add things to old Pythons so I can pretend they are newer, for tests.""" + +# pylint: disable-msg=W0622 +# (Redefining built-in blah) +# The whole point of this file is to redefine built-ins, so shut up about it. + +import os, sys + +# 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. + + """ + + if sys.hexversion > 0x03000000 and cmd.startswith("coverage "): + # We don't have a coverage command on 3.x, so fix it up to call the + # script. Eventually we won't need this. + cmd = "python " + sys.prefix + os.sep + "Scripts" + os.sep + cmd + + proc = subprocess.Popen(cmd, shell=True, + stdin=subprocess.PIPE, stdout=subprocess.PIPE, + stderr=subprocess.STDOUT + ) + retcode = proc.wait() + + # Get the output, and canonicalize it to strings with newlines. + output = proc.stdout.read() + if not isinstance(output, str): + output = output.decode('utf-8') + output = output.replace('\r', '') + + return retcode, output + +# No more execfile in Py3k +try: + execfile = execfile +except NameError: + def execfile(filename, globs): + """A Python 3 implementation of execfile.""" + exec(compile(open(filename).read(), filename, 'exec'), globs) diff --git a/test/coveragetest.py b/test/coveragetest.py index 1502770d..638c10d6 100644 --- a/test/coveragetest.py +++ b/test/coveragetest.py @@ -1,10 +1,10 @@ """Base test case class for coverage testing.""" import imp, os, random, shutil, sys, tempfile, textwrap, unittest -from cStringIO import StringIO import coverage -from coverage.backward import set, run_command # pylint: disable-msg=W0622 +from coverage.backward import set, StringIO # pylint: disable-msg=W0622 +from backtest import run_command class Tee(object): @@ -168,7 +168,8 @@ class CoverageTest(unittest.TestCase): """ try: callableObj(*args, **kwargs) - except excClass, exc: + except excClass: + _, exc, _ = sys.exc_info() excMsg = str(exc) if not msg: # No message provided: it passes. @@ -214,7 +215,7 @@ class CoverageTest(unittest.TestCase): os.environ['PYTHONPATH'] = pypath _, output = run_command(cmd) - print output + print(output) return output def assert_equal_sets(self, s1, s2): diff --git a/test/farm/run/src/chdir.py b/test/farm/run/src/chdir.py index 23c5afa8..d8287ed7 100644 --- a/test/farm/run/src/chdir.py +++ b/test/farm/run/src/chdir.py @@ -1,5 +1,5 @@ import os -print "Line One" +print("Line One") os.chdir("subdir") -print "Line Two" +print("Line Two") diff --git a/test/farm/run/src/showtrace.py b/test/farm/run/src/showtrace.py index 3708ac08..c3b4356c 100644 --- a/test/farm/run/src/showtrace.py +++ b/test/farm/run/src/showtrace.py @@ -3,13 +3,16 @@ import sys -# Print the argument as a label for the output. -print sys.argv[1], - # Show what the trace function is. If a C-based function is used, then f_trace # is None. trace_fn = sys._getframe(0).f_trace if trace_fn is None: - print "None" + trace_name = "None" else: - print trace_fn.im_class.__name__ + # Get the name of the tracer class. Py3k has a different way to get it. + try: + trace_name = trace_fn.im_class.__name__ + except AttributeError: + trace_name = trace_fn.__self__.__class__.__name__ + +print("%s %s" % (sys.argv[1], trace_name)) diff --git a/test/test_api.py b/test/test_api.py index f19099e1..460c5b36 100644 --- a/test/test_api.py +++ b/test/test_api.py @@ -1,9 +1,11 @@ """Tests for Coverage's api.""" import os, re, sys, textwrap -from cStringIO import StringIO import coverage +from coverage.backward import StringIO + +sys.path.insert(0, os.path.split(__file__)[0]) # Force relative import for Py3k from coveragetest import CoverageTest diff --git a/test/test_cmdline.py b/test/test_cmdline.py index f06e9158..0c262108 100644 --- a/test/test_cmdline.py +++ b/test/test_cmdline.py @@ -1,8 +1,10 @@ """Test cmdline.py for coverage.""" -import re, shlex, textwrap, unittest +import os, re, shlex, sys, textwrap, unittest import mock import coverage + +sys.path.insert(0, os.path.split(__file__)[0]) # Force relative import for Py3k from coveragetest import CoverageTest diff --git a/test/test_codeunit.py b/test/test_codeunit.py index 46853b76..36ca1fcd 100644 --- a/test/test_codeunit.py +++ b/test/test_codeunit.py @@ -4,6 +4,8 @@ import os, sys from coverage.codeunit import code_unit_factory from coverage.files import FileLocator + +sys.path.insert(0, os.path.split(__file__)[0]) # Force relative import for Py3k from coveragetest import CoverageTest # pylint: disable-msg=F0401 diff --git a/test/test_coverage.py b/test/test_coverage.py index b364d312..f70b67e3 100644 --- a/test/test_coverage.py +++ b/test/test_coverage.py @@ -3,11 +3,12 @@ # http://nedbatchelder.com/code/coverage import os, sys, unittest -from cStringIO import StringIO import coverage +from coverage.backward import StringIO coverage.use_cache(0) +sys.path.insert(0, os.path.split(__file__)[0]) # Force relative import for Py3k from coveragetest import CoverageTest @@ -1891,7 +1892,7 @@ class ExceptionTest(CoverageTest): if __name__ == '__main__': - print "Testing under Python version: %s" % sys.version + print("Testing under Python version: %s" % sys.version) unittest.main() diff --git a/test/test_data.py b/test/test_data.py index 44c123fe..a2557a91 100644 --- a/test/test_data.py +++ b/test/test_data.py @@ -1,7 +1,11 @@ """Tests for coverage.data""" -import cPickle as pickle +import os, sys + +from coverage.backward import pickle from coverage.data import CoverageData + +sys.path.insert(0, os.path.split(__file__)[0]) # Force relative import for Py3k from coveragetest import CoverageTest diff --git a/test/test_execfile.py b/test/test_execfile.py index aee4eeb3..d4fe2245 100644 --- a/test/test_execfile.py +++ b/test/test_execfile.py @@ -1,8 +1,10 @@ """Tests for coverage.execfile""" -import os +import os, sys from coverage.execfile import run_python_file + +sys.path.insert(0, os.path.split(__file__)[0]) # Force relative import for Py3k from coveragetest import CoverageTest here = os.path.dirname(__file__) diff --git a/test/test_farm.py b/test/test_farm.py index 6e8f3467..6200611d 100644 --- a/test/test_farm.py +++ b/test/test_farm.py @@ -1,7 +1,9 @@ """Run tests in the farm subdirectory. Designed for nose.""" import filecmp, fnmatch, glob, os, shutil, sys -from coverage.backward import run_command + +sys.path.insert(0, os.path.split(__file__)[0]) # Force relative import for Py3k +from backtest import run_command, execfile # pylint: disable-msg=W0622 def test_farm(clean_only=False): @@ -133,7 +135,7 @@ class FarmTestCase(object): if not cmd: continue retcode, output = run_command(cmd) - print output, + print(output.rstrip()) if outfile: open(outfile, "a+").write(output) if retcode: @@ -278,7 +280,7 @@ def main(): for test in test_farm(clean_only=True): test[0].run_fully() else: - print "Need an operation: run, out, clean" + print("Need an operation: run, out, clean") # So that we can run just one farm run.py at a time. if __name__ == '__main__': |