diff options
-rw-r--r-- | coverage/codeunit.py | 62 | ||||
-rw-r--r-- | coverage/control.py | 59 | ||||
-rw-r--r-- | coverage/report.py | 6 | ||||
-rw-r--r-- | tests/test_codeunit.py | 53 |
4 files changed, 80 insertions, 100 deletions
diff --git a/coverage/codeunit.py b/coverage/codeunit.py index a607ae1a..e9efa396 100644 --- a/coverage/codeunit.py +++ b/coverage/codeunit.py @@ -3,72 +3,12 @@ import os import sys -from coverage.backward import string_class, unicode_class +from coverage.backward import unicode_class from coverage.files import get_python_source, FileLocator -from coverage.misc import CoverageException from coverage.parser import PythonParser from coverage.phystokens import source_token_lines, source_encoding -def code_units_factory(morfs, file_locator=None, get_plugin=None): - """Construct a list of CodeUnits from modules or filenames. - - `morfs` is a module or filename, or a list of the same. - - `file_locator` is a FileLocator that can help resolve filenames. - - `get_plugin` is a function taking a filename, and returning a plugin - responsible for the file. It can also return None if there is no plugin - claiming the file. - - Returns a list of CodeUnit objects. - - """ - # Be sure we have a list. - if not isinstance(morfs, (list, tuple)): - morfs = [morfs] - - code_units = [] - for morf in morfs: - file_reporter = code_unit_factory(morf, file_locator, get_plugin) - code_units.append(file_reporter) - - return code_units - - -def code_unit_factory(morf, file_locator=None, get_plugin=None): - """Construct a CodeUnit from a module or filename. - - `morfs` is a module or a filename. - - `file_locator` is a FileLocator that can help resolve filenames. - - `get_plugin` is a function taking a filename, and returning a plugin - responsible for the file. It can also return None if there is no plugin - claiming the file. - - Returns a CodeUnit object. - - """ - plugin = None - - if isinstance(morf, string_class) and get_plugin: - plugin = get_plugin(morf) - - if plugin: - file_reporter = plugin.file_reporter(morf) - if file_reporter is None: - raise CoverageException( - "Plugin %r did not provide a file reporter for %r." % ( - plugin.plugin_name, morf - ) - ) - else: - file_reporter = PythonCodeUnit(morf, file_locator) - - return file_reporter - - class CodeUnit(object): """Code unit: a filename or module. diff --git a/coverage/control.py b/coverage/control.py index 95592508..0ca1e95c 100644 --- a/coverage/control.py +++ b/coverage/control.py @@ -10,7 +10,7 @@ import sys from coverage.annotate import AnnotateReporter from coverage.backward import string_class, iitems -from coverage.codeunit import code_unit_factory, CodeUnit, PythonCodeUnit +from coverage.codeunit import CodeUnit, PythonCodeUnit from coverage.collector import Collector from coverage.config import CoverageConfig from coverage.data import CoverageData @@ -750,20 +750,59 @@ class Coverage(object): Returns an `Analysis` object. """ - def get_plugin(filename): - """For code_unit_factory to use to find the plugin for a file.""" - plugin = None - plugin_name = self.data.plugin_data().get(filename) - if plugin_name: - plugin = self.plugins.get(plugin_name) - return plugin - self._harvest_data() if not isinstance(it, CodeUnit): - it = code_unit_factory(it, self.file_locator, get_plugin) + it = self._get_file_reporter(it) return Analysis(self, it) + def _get_file_reporter(self, morf): + """Get a FileReporter for a module or filename.""" + plugin = None + + if isinstance(morf, string_class): + plugin_name = self.data.plugin_data().get(morf) + if plugin_name: + plugin = self.plugins.get(plugin_name) + + if plugin: + file_reporter = plugin.file_reporter(morf) + if file_reporter is None: + raise CoverageException( + "Plugin %r did not provide a file reporter for %r." % ( + plugin.plugin_name, morf + ) + ) + else: + file_reporter = PythonCodeUnit(morf, self.file_locator) + + return file_reporter + + def _get_file_reporters(self, morfs=None): + """Get a list of FileReporters for a list of modules or filenames. + + For each module or filename in `morfs`, find a FileReporter. Return + the list of FileReporters. + + If `morfs` is a single module or filename, this returns a list of one + FileReporter. If `morfs` is empty or None, then the list of all files + measured is used to find the FileReporters. + + """ + if not morfs: + morfs = self.data.measured_files() + + # Be sure we have a list. + if not isinstance(morfs, (list, tuple)): + morfs = [morfs] + + file_reporters = [] + for morf in morfs: + file_reporter = self._get_file_reporter(morf) + file_reporters.append(file_reporter) + + return file_reporters + def report( self, morfs=None, show_missing=True, ignore_errors=None, file=None, # pylint: disable=redefined-builtin diff --git a/coverage/report.py b/coverage/report.py index 85d552e1..93b6c928 100644 --- a/coverage/report.py +++ b/coverage/report.py @@ -2,7 +2,6 @@ import os -from coverage.codeunit import code_units_factory from coverage.files import prep_patterns, FnmatchMatcher from coverage.misc import CoverageException, NoSource, NotPython @@ -33,10 +32,7 @@ class Reporter(object): `morfs` is a list of modules or filenames. """ - morfs = morfs or self.coverage.data.measured_files() - file_locator = self.coverage.file_locator - get_plugin = self.coverage.data.plugin_data().get - self.code_units = code_units_factory(morfs, file_locator, get_plugin) + self.code_units = self.coverage._get_file_reporters(morfs) if self.config.include: patterns = prep_patterns(self.config.include) diff --git a/tests/test_codeunit.py b/tests/test_codeunit.py index 66635092..c8389870 100644 --- a/tests/test_codeunit.py +++ b/tests/test_codeunit.py @@ -3,7 +3,7 @@ import os import sys -from coverage.codeunit import CodeUnit, PythonCodeUnit, code_units_factory +from coverage.codeunit import CodeUnit, PythonCodeUnit from tests.coveragetest import CoverageTest @@ -55,32 +55,36 @@ class CodeUnitTest(CoverageTest): import aa.bb import aa.bb.cc - cu = code_units_factory([aa, aa.bb, aa.bb.cc]) - self.assertEqual(cu[0].name, "aa") - self.assertEqual(cu[1].name, "aa.bb") - self.assertEqual(cu[2].name, "aa.bb.cc") - self.assertEqual(cu[0].flat_rootname(), "aa") - self.assertEqual(cu[1].flat_rootname(), "aa_bb") - self.assertEqual(cu[2].flat_rootname(), "aa_bb_cc") - self.assertEqual(cu[0].source(), "# aa\n") - self.assertEqual(cu[1].source(), "# bb\n") - self.assertEqual(cu[2].source(), "") # yes, empty + acu = PythonCodeUnit(aa) + bcu = PythonCodeUnit(aa.bb) + ccu = PythonCodeUnit(aa.bb.cc) + self.assertEqual(acu.name, "aa") + self.assertEqual(bcu.name, "aa.bb") + self.assertEqual(ccu.name, "aa.bb.cc") + self.assertEqual(acu.flat_rootname(), "aa") + self.assertEqual(bcu.flat_rootname(), "aa_bb") + self.assertEqual(ccu.flat_rootname(), "aa_bb_cc") + self.assertEqual(acu.source(), "# aa\n") + self.assertEqual(bcu.source(), "# bb\n") + self.assertEqual(ccu.source(), "") # yes, empty def test_module_files(self): import aa.afile import aa.bb.bfile import aa.bb.cc.cfile - cu = code_units_factory([aa.afile, aa.bb.bfile, aa.bb.cc.cfile]) - self.assertEqual(cu[0].name, "aa.afile") - self.assertEqual(cu[1].name, "aa.bb.bfile") - self.assertEqual(cu[2].name, "aa.bb.cc.cfile") - self.assertEqual(cu[0].flat_rootname(), "aa_afile") - self.assertEqual(cu[1].flat_rootname(), "aa_bb_bfile") - self.assertEqual(cu[2].flat_rootname(), "aa_bb_cc_cfile") - self.assertEqual(cu[0].source(), "# afile.py\n") - self.assertEqual(cu[1].source(), "# bfile.py\n") - self.assertEqual(cu[2].source(), "# cfile.py\n") + acu = PythonCodeUnit(aa.afile) + bcu = PythonCodeUnit(aa.bb.bfile) + ccu = PythonCodeUnit(aa.bb.cc.cfile) + self.assertEqual(acu.name, "aa.afile") + self.assertEqual(bcu.name, "aa.bb.bfile") + self.assertEqual(ccu.name, "aa.bb.cc.cfile") + self.assertEqual(acu.flat_rootname(), "aa_afile") + self.assertEqual(bcu.flat_rootname(), "aa_bb_bfile") + self.assertEqual(ccu.flat_rootname(), "aa_bb_cc_cfile") + self.assertEqual(acu.source(), "# afile.py\n") + self.assertEqual(bcu.source(), "# bfile.py\n") + self.assertEqual(ccu.source(), "# cfile.py\n") def test_comparison(self): acu = CodeUnit("aa/afile.py") @@ -104,6 +108,7 @@ class CodeUnitTest(CoverageTest): # in the path is actually the .egg zip file. self.assert_doesnt_exist(egg1.__file__) - cu = code_units_factory([egg1, egg1.egg1]) - self.assertEqual(cu[0].source(), u"") - self.assertEqual(cu[1].source().split("\n")[0], u"# My egg file!") + ecu = PythonCodeUnit(egg1) + eecu = PythonCodeUnit(egg1.egg1) + self.assertEqual(ecu.source(), u"") + self.assertEqual(eecu.source().split("\n")[0], u"# My egg file!") |