diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2015-02-08 11:48:10 -0500 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2015-02-08 11:48:10 -0500 |
commit | c16eb3ec0d917d8f5c6320a01ea6965665a1f607 (patch) | |
tree | a5eb267f563c8e4b73a1057959921a0a0791b040 | |
parent | a2331114926f37823f0dc45dc44308a94c0d454e (diff) | |
download | python-coveragepy-git-c16eb3ec0d917d8f5c6320a01ea6965665a1f607.tar.gz |
Re-use CheckUniqueFilenames for test_collector.py
-rw-r--r-- | coverage/test_helpers.py | 1 | ||||
-rw-r--r-- | tests/helpers.py | 25 | ||||
-rw-r--r-- | tests/test_collector.py | 29 | ||||
-rw-r--r-- | tests/test_plugins.py | 32 |
4 files changed, 42 insertions, 45 deletions
diff --git a/coverage/test_helpers.py b/coverage/test_helpers.py index a42fe3b4..65d99779 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 00000000..db20d798 --- /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 5a8325c7..26360091 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 48b0498c..190096bd 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) |