diff options
Diffstat (limited to 'tests/test_oddball.py')
-rw-r--r-- | tests/test_oddball.py | 61 |
1 files changed, 47 insertions, 14 deletions
diff --git a/tests/test_oddball.py b/tests/test_oddball.py index 9fdc654d..05c724b6 100644 --- a/tests/test_oddball.py +++ b/tests/test_oddball.py @@ -1,9 +1,12 @@ +# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0 +# For details: https://bitbucket.org/ned/coveragepy/src/default/NOTICE.txt + """Oddball cases for testing coverage.py""" -import os import sys import coverage +from coverage.files import abs_file from tests.coveragetest import CoverageTest from tests import osinfo @@ -107,7 +110,7 @@ class RecursionTest(CoverageTest): i = 11 """) - cov = coverage.coverage() + cov = coverage.Coverage() self.start_import_stop(cov, "recur") pytrace = (cov.collector.tracer_name() == "PyTracer") @@ -209,7 +212,7 @@ class PyexpatTest(CoverageTest): self.make_file("outer.py", "\n"*100 + "import trydom\na = 102\n") - cov = coverage.coverage() + cov = coverage.Coverage() cov.erase() # Import the Python file, executing it. @@ -307,9 +310,10 @@ class ExceptionTest(CoverageTest): for callnames, lines_expected in runs: # Make the list of functions we'll call for this test. - calls = [getattr(sys.modules[cn], cn) for cn in callnames.split()] + callnames = callnames.split() + calls = [getattr(sys.modules[cn], cn) for cn in callnames] - cov = coverage.coverage() + cov = coverage.Coverage() cov.start() # Call our list of functions: invoke the first, with the rest as # an argument. @@ -318,16 +322,13 @@ class ExceptionTest(CoverageTest): # Clean the line data and compare to expected results. # The filenames are absolute, so keep just the base. - cov._harvest_data() # private! sshhh... - lines = cov.data.line_data() clean_lines = {} - for f, llist in lines.items(): - # f is a path to a Python module, so we drop the '.py' to get - # a callname. - basename = os.path.basename(f) - assert basename.endswith(".py") - if basename[:-3] in callnames: - clean_lines[basename] = llist + data = cov.get_data() + for callname in callnames: + filename = callname + ".py" + lines = data.lines(abs_file(filename)) + clean_lines[filename] = sorted(lines) + self.assertEqual(clean_lines, lines_expected) @@ -400,3 +401,35 @@ class GettraceTest(CoverageTest): f = 12 ''', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], "") + + +class ExecTest(CoverageTest): + """Tests of exec.""" + def test_correct_filename(self): + # https://bitbucket.org/ned/coveragepy/issues/380/code-executed-by-exec-excluded-from + # Bug was that exec'd files would have their lines attributed to the + # calling file. Make two files, both with ~30 lines, but no lines in + # common. Line 30 in to_exec.py was recorded as line 30 in main.py, + # but now it's fixed. :) + self.make_file("to_exec.py", """\ + \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n + print("var is {0}".format(var)) # line 31 + """) + self.make_file("main.py", """\ + namespace = {'var': 17} + with open("to_exec.py") as to_exec_py: + code = compile(to_exec_py.read(), 'to_exec.py', 'exec') + exec(code, globals(), namespace) + \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n + print("done") # line 35 + """) + + cov = coverage.Coverage() + self.start_import_stop(cov, "main") + + _, statements, missing, _ = cov.analysis("main.py") + self.assertEqual(statements, [1, 2, 3, 4, 35]) + self.assertEqual(missing, []) + _, statements, missing, _ = cov.analysis("to_exec.py") + self.assertEqual(statements, [31]) + self.assertEqual(missing, []) |