1. Ned Batchelder
  2. coverage.py

Commits

Ned Batchelder  committed 55fc9b0

Refactor the --omit and --include support during reporting, and add a test for --include.

  • Participants
  • Parent commits 369b96f
  • Branches default

Comments (0)

Files changed (3)

File coverage/codeunit.py

View file
  • Ignore whitespace
 """Code unit (module) handling for Coverage."""
 
-import fnmatch, glob, os
+import glob, os
 
 from coverage.backward import string_class, StringIO
 from coverage.misc import CoverageException
 
 
-def code_unit_factory(morfs, file_locator, omit=None, include=None):
+def code_unit_factory(morfs, file_locator):
     """Construct a list of CodeUnits from polymorphic inputs.
 
     `morfs` is a module or a filename, or a list of same.
 
     `file_locator` is a FileLocator that can help resolve filenames.
 
-    `include` is a list of filename patterns. Only CodeUnits that match those
-    patterns will be included in the list. `omit` is a list of patterns to omit
-    from the list.
-
     Returns a list of CodeUnit objects.
 
     """
-
     # Be sure we have a list.
     if not isinstance(morfs, (list, tuple)):
         morfs = [morfs]
 
     code_units = [CodeUnit(morf, file_locator) for morf in morfs]
 
-    if include:
-        assert not isinstance(include, string_class) # common mistake
-        patterns = [file_locator.abs_file(p) for p in include]
-        filtered = []
-        for cu in code_units:
-            for pattern in patterns:
-                if fnmatch.fnmatch(cu.filename, pattern):
-                    filtered.append(cu)
-                    break
-        code_units = filtered
+    return code_units
 
-    if omit:
-        assert not isinstance(omit, string_class) # common mistake
-        patterns = [file_locator.abs_file(p) for p in omit]
-        filtered = []
-        for cu in code_units:
-            for pattern in patterns:
-                if fnmatch.fnmatch(cu.filename, pattern):
-                    break
-            else:
-                filtered.append(cu)
-        code_units = filtered
-
-    return code_units
 
 class CodeUnit(object):
     """Code unit: a filename or module.
     `relative` is a boolean.
 
     """
-
     def __init__(self, morf, file_locator):
         self.file_locator = file_locator
 

File coverage/report.py

View file
  • Ignore whitespace
 """Reporter foundation for Coverage."""
 
-import os
+import fnmatch, os
+from coverage.backward import string_class
 from coverage.codeunit import code_unit_factory
 from coverage.misc import CoverageException, NoSource
 
 
         """
         morfs = morfs or self.coverage.data.executed_files()
-        self.code_units = code_unit_factory(
-                            morfs, self.coverage.file_locator, omit, include
-                            )
+        file_locator = self.coverage.file_locator
+        self.code_units = code_unit_factory(morfs, file_locator)
+
+        if include:
+            assert not isinstance(include, string_class) # common mistake
+            patterns = [file_locator.abs_file(p) for p in include]
+            filtered = []
+            for cu in self.code_units:
+                for pattern in patterns:
+                    if fnmatch.fnmatch(cu.filename, pattern):
+                        filtered.append(cu)
+                        break
+            self.code_units = filtered
+
+        if omit:
+            assert not isinstance(omit, string_class) # common mistake
+            patterns = [file_locator.abs_file(p) for p in omit]
+            filtered = []
+            for cu in self.code_units:
+                for pattern in patterns:
+                    if fnmatch.fnmatch(cu.filename, pattern):
+                        break
+                else:
+                    filtered.append(cu)
+            self.code_units = filtered
+
         self.code_units.sort()
 
     def report_files(self, report_fn, morfs, directory=None,

File test/test_summary.py

View file
  • Ignore whitespace
         self.assertTrue("mycode " in report)
         self.assertEqual(self.last_line_squeezed(report), "mycode 4 0 100%")
 
+    def test_report_including(self):
+        # Try reporting while including some modules
+        self.run_command("coverage run mycode.py")
+        report = self.report_from_command("coverage report --include=mycode*")
+
+        # Name     Stmts   Miss  Cover
+        # ----------------------------
+        # mycode       4      0   100%
+
+        self.assertEqual(self.line_count(report), 3)
+        self.assertFalse("/coverage/" in report)
+        self.assertFalse("/test/modules/covmod1 " in report)
+        self.assertFalse("/test/zipmods.zip/covmodzip1 " in report)
+        self.assertTrue("mycode " in report)
+        self.assertEqual(self.last_line_squeezed(report), "mycode 4 0 100%")
+
     def test_report_branches(self):
         self.make_file("mybranch.py", """\
             def branch(x):