summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2015-02-08 11:48:10 -0500
committerNed Batchelder <ned@nedbatchelder.com>2015-02-08 11:48:10 -0500
commit89ec8f6da41555dce7bee84b47cfa2b7f2e0213e (patch)
treed3950ec152ef0a94b0b43c4573a5c3870c412756
parent8b605085e10910fc3b2f413bf7b6d0853f5e3015 (diff)
downloadpython-coveragepy-89ec8f6da41555dce7bee84b47cfa2b7f2e0213e.tar.gz
Re-use CheckUniqueFilenames for test_collector.py
-rw-r--r--coverage/test_helpers.py1
-rw-r--r--tests/helpers.py25
-rw-r--r--tests/test_collector.py29
-rw-r--r--tests/test_plugins.py32
4 files changed, 42 insertions, 45 deletions
diff --git a/coverage/test_helpers.py b/coverage/test_helpers.py
index a42fe3b..65d9977 100644
--- a/coverage/test_helpers.py
+++ b/coverage/test_helpers.py
@@ -117,6 +117,7 @@ class StdStreamCapturingMixin(TestCase):
self.old_stdout = sys.stdout
self.captured_stdout = StringIO()
sys.stdout = Tee(sys.stdout, self.captured_stdout)
+
self.old_stderr = sys.stderr
self.captured_stderr = StringIO()
sys.stderr = self.captured_stderr
diff --git a/tests/helpers.py b/tests/helpers.py
new file mode 100644
index 0000000..db20d79
--- /dev/null
+++ b/tests/helpers.py
@@ -0,0 +1,25 @@
+"""Helpers for coverage.py tests."""
+
+
+class CheckUniqueFilenames(object):
+ """Asserts the uniqueness of filenames passed to a function."""
+ def __init__(self, wrapped):
+ self.filenames = set()
+ self.wrapped = wrapped
+
+ @classmethod
+ def hook(cls, cov, method_name):
+ """Replace a method with our checking wrapper."""
+ method = getattr(cov, method_name)
+ hook = cls(method)
+ setattr(cov, method_name, hook.wrapper)
+ return hook
+
+ def wrapper(self, filename, *args, **kwargs):
+ """The replacement method. Check that we don't have dupes."""
+ assert filename not in self.filenames, (
+ "Filename %r passed to %r twice" % (filename, self.wrapped)
+ )
+ self.filenames.add(filename)
+ ret = self.wrapped(filename, *args, **kwargs)
+ return ret
diff --git a/tests/test_collector.py b/tests/test_collector.py
index 5a8325c..2636009 100644
--- a/tests/test_collector.py
+++ b/tests/test_collector.py
@@ -1,11 +1,11 @@
"""Tests of coverage/collector.py and other collectors."""
-import re
+import os.path
import coverage
-from coverage.backward import StringIO
from tests.coveragetest import CoverageTest
+from tests.helpers import CheckUniqueFilenames
class CollectorTest(CoverageTest):
@@ -13,8 +13,6 @@ class CollectorTest(CoverageTest):
def test_should_trace_cache(self):
# The tracers should only invoke should_trace once for each file name.
- # TODO: Might be better to do this with a mocked _should_trace,
- # rather than by examining debug output.
# Make some files that invoke each other.
self.make_file("f1.py", """\
@@ -35,22 +33,15 @@ class CollectorTest(CoverageTest):
func(i)
""")
- # Trace one file, but not the other, and get the debug output.
- debug_out = StringIO()
- cov = coverage.coverage(include=["f1.py"], debug=['trace'])
- cov._debug_file = debug_out
+ # Trace one file, but not the other. CheckUniqueFilenames will assert
+ # that _should_trace hasn't been called twice for the same file.
+ cov = coverage.coverage(include=["f1.py"])
+ should_trace_hook = CheckUniqueFilenames.hook(cov, '_should_trace')
# Import the Python file, executing it.
self.start_import_stop(cov, "f2")
- # Grab all the filenames mentioned in debug output, there should be no
- # duplicates.
- trace_lines = [
- l for l in debug_out.getvalue().splitlines()
- if l.startswith(("Tracing ", "Not tracing "))
- ]
- filenames = [re.search(r"'[^']+'", l).group() for l in trace_lines]
- self.assertEqual(len(filenames), len(set(filenames)))
-
- # Double-check that the tracing messages are in there somewhere.
- self.assertGreater(len(filenames), 5)
+ # Double-check that our files were checked.
+ abs_files = set(os.path.abspath(f) for f in should_trace_hook.filenames)
+ self.assertIn(os.path.abspath("f1.py"), abs_files)
+ self.assertIn(os.path.abspath("f2.py"), abs_files)
diff --git a/tests/test_plugins.py b/tests/test_plugins.py
index 48b0498..190096b 100644
--- a/tests/test_plugins.py
+++ b/tests/test_plugins.py
@@ -10,6 +10,7 @@ from coverage.control import Plugins
import coverage.plugin
from tests.coveragetest import CoverageTest
+from tests.helpers import CheckUniqueFilenames
class FakeConfig(object):
@@ -215,8 +216,8 @@ class FileTracerTest(CoverageTest):
""")
cov = coverage.Coverage()
- should_trace_hook = CheckUnique.hook(cov, '_should_trace')
- check_include_hook = CheckUnique.hook(cov, '_check_include_omit_etc')
+ should_trace_hook = CheckUniqueFilenames.hook(cov, '_should_trace')
+ check_include_hook = CheckUniqueFilenames.hook(cov, '_check_include_omit_etc')
cov.config["run:plugins"] = ["tests.plugin1"]
# Import the Python file, executing it.
@@ -250,7 +251,7 @@ class FileTracerTest(CoverageTest):
from render import helper, render
assert render("foo_7.html", 4) == "[foo_7.html @ 4]"
- # Render foo_7.html again to trigger the callback snoopers.
+ # Render foo_7.html again to try the CheckUniqueFilenames asserts.
render("foo_7.html", 4)
assert helper(42) == 43
@@ -259,8 +260,8 @@ class FileTracerTest(CoverageTest):
""")
cov = coverage.Coverage()
- should_trace_hook = CheckUnique.hook(cov, '_should_trace')
- check_include_hook = CheckUnique.hook(cov, '_check_include_omit_etc')
+ should_trace_hook = CheckUniqueFilenames.hook(cov, '_should_trace')
+ check_include_hook = CheckUniqueFilenames.hook(cov, '_check_include_omit_etc')
cov.config["run:plugins"] = ["tests.plugin2"]
self.start_import_stop(cov, "caller")
@@ -274,24 +275,3 @@ class FileTracerTest(CoverageTest):
_, statements, missing, _ = cov.analysis("bar_4.html")
self.assertEqual(statements, [1, 2, 3, 4])
self.assertEqual(missing, [1, 4])
-
-
-class CheckUnique(object):
- """Asserts the uniqueness of filenames passed to a function."""
- def __init__(self, wrapped):
- self.filenames = set()
- self.wrapped = wrapped
-
- @classmethod
- def hook(cls, cov, method_name):
- """Replace a method with our checking wrapper."""
- method = getattr(cov, method_name)
- hook = cls(method)
- setattr(cov, method_name, hook.wrapper)
- return hook
-
- def wrapper(self, filename, *args, **kwargs):
- """The replacement method. Check that we don't have dupes."""
- assert filename not in self.filenames
- self.filenames.add(filename)
- return self.wrapped(filename, *args, **kwargs)