diff options
-rw-r--r-- | CHANGES.txt | 4 | ||||
-rw-r--r-- | coverage/__init__.py | 61 | ||||
-rw-r--r-- | tests/test_api.py | 75 |
3 files changed, 4 insertions, 136 deletions
diff --git a/CHANGES.txt b/CHANGES.txt index 1abf564f..155d44ea 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -6,6 +6,10 @@ Change history for Coverage.py Latest ------ +- The original module-level function interface to coverage is no longer + supported. You must now create a ``coverage.Coverage`` object, and use + methods on it. + - The ``coverage combine`` command now accepts any number of directories as arguments, and will combine all the data files from those directories. This means you don't have to copy the files to one directory before combining. diff --git a/coverage/__init__.py b/coverage/__init__.py index 0aa1d45c..4b6b46a5 100644 --- a/coverage/__init__.py +++ b/coverage/__init__.py @@ -16,67 +16,6 @@ from coverage.plugin import CoveragePlugin # Backward compatibility. coverage = Coverage -# Module-level functions. The original API to this module was based on -# functions defined directly in the module, with a singleton of the Coverage() -# class. That design hampered programmability, so the current API uses -# explicitly-created Coverage objects. But for backward compatibility, here we -# define the top-level functions to create the singleton when they are first -# called. - -# Singleton object for use with module-level functions. The singleton is -# created as needed when one of the module-level functions is called. -_the_coverage = None - -def _singleton_method(name): - """Return a function to the `name` method on a singleton `Coverage` object. - - The singleton object is created the first time one of these functions is - called. - - """ - # Disable pylint message, because a bunch of variables look unused, but - # they're accessed via locals(). - # pylint: disable=unused-variable - - def wrapper(*args, **kwargs): - """Singleton wrapper around a coverage method.""" - global _the_coverage - if not _the_coverage: - _the_coverage = Coverage(auto_data=True) - return getattr(_the_coverage, name)(*args, **kwargs) - - import inspect - meth = getattr(Coverage, name) - args, varargs, kw, defaults = inspect.getargspec(meth) - argspec = inspect.formatargspec(args[1:], varargs, kw, defaults) - docstring = meth.__doc__ - wrapper.__doc__ = ("""\ - A first-use-singleton wrapper around Coverage.%(name)s. - - This wrapper is provided for backward compatibility with legacy code. - New code should use Coverage.%(name)s directly. - - %(name)s%(argspec)s: - - %(docstring)s - """ % locals() - ) - - return wrapper - - -# Define the module-level functions. -use_cache = _singleton_method('use_cache') -start = _singleton_method('start') -stop = _singleton_method('stop') -erase = _singleton_method('erase') -exclude = _singleton_method('exclude') -analysis = _singleton_method('analysis') -analysis2 = _singleton_method('analysis2') -report = _singleton_method('report') -annotate = _singleton_method('annotate') - - # On Windows, we encode and decode deep enough that something goes wrong and # the encodings.utf_8 module is loaded and then unloaded, I don't know why. # Adding a reference here prevents it from being unloaded. Yuk. diff --git a/tests/test_api.py b/tests/test_api.py index a21372ad..baf1f016 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -2,7 +2,6 @@ import fnmatch import os -import re import sys import textwrap @@ -12,80 +11,6 @@ from coverage.backward import StringIO from tests.coveragetest import CoverageTest -class SingletonApiTest(CoverageTest): - """Tests of the old-fashioned singleton API.""" - - def setUp(self): - super(SingletonApiTest, self).setUp() - # These tests use the singleton module interface. Prevent it from - # writing .coverage files at exit. - coverage.use_cache(0) - - def do_report_work(self, modname): - """Create a module named `modname`, then measure it.""" - coverage.erase() - - self.make_file(modname+".py", """\ - a = 1 - b = 2 - if b == 3: - c = 4 - d = 5 - e = 6 - f = 7 - """) - - # Import the Python file, executing it. - self.start_import_stop(coverage, modname) - - def test_simple(self): - coverage.erase() - - self.make_file("mycode.py", """\ - a = 1 - b = 2 - if b == 3: - c = 4 - d = 5 - """) - - # Import the Python file, executing it. - self.start_import_stop(coverage, "mycode") - - _, statements, missing, missingtext = coverage.analysis("mycode.py") - self.assertEqual(statements, [1,2,3,4,5]) - self.assertEqual(missing, [4]) - self.assertEqual(missingtext, "4") - - def test_report(self): - self.do_report_work("mycode2") - coverage.report(["mycode2.py"]) - self.assertEqual(self.stdout(), textwrap.dedent("""\ - Name Stmts Miss Cover Missing - ------------------------------------------ - mycode2.py 7 3 57% 4-6 - """)) - - def test_report_file(self): - # The file= argument of coverage.report makes the report go there. - self.do_report_work("mycode3") - fout = StringIO() - coverage.report(["mycode3.py"], file=fout) - self.assertEqual(self.stdout(), "") - self.assertEqual(fout.getvalue(), textwrap.dedent("""\ - Name Stmts Miss Cover Missing - ------------------------------------------ - mycode3.py 7 3 57% 4-6 - """)) - - def test_report_default(self): - # Calling report() with no morfs will report on whatever was executed. - self.do_report_work("mycode4") - coverage.report() - rpt = re.sub(r"\s+", " ", self.stdout()) - self.assertIn("mycode4.py 7 3 57% 4-6", rpt) - - class ApiTest(CoverageTest): """Api-oriented tests for Coverage.""" |