Ned Batchelder avatar Ned Batchelder committed 935338c

You can include files in the Python installation, and they will be measured.

Comments (0)

Files changed (3)

   different than the number reported on the individual file pages.  This is
   now fixed.
 
+- An explicit include directive to measure files in the Python installation
+  wouldn't work because of the standard library exclusion.  Now the include
+  directive takes precendence, and the files will be measured.  Fixes
+  `issue 138`_.
+
 .. _issue 122: http://bitbucket.org/ned/coveragepy/issue/122/for-else-always-reports-missing-branch
+.. _issue 138: https://bitbucket.org/ned/coveragepy/issue/138/include-should-take-precedence-over-is
 
 
 Version 3.5 --- 29 June 2011

coverage/control.py

 
         canonical = self.file_locator.canonical_filename(filename)
 
-        # If the user specified source, then that's authoritative about what to
-        # measure.  If they didn't, then we have to exclude the stdlib and
-        # coverage.py directories.
+        # If the user specified source or include, then that's authoritative
+        # about the outer bound of what to measure and we don't have to apply
+        # any canned exclusions. If they didn't, then we have to exclude the
+        # stdlib and coverage.py directories.
         if self.source_match:
             if not self.source_match.match(canonical):
                 return False
+        elif self.include_match:
+            if not self.include_match.match(canonical):
+                return False
         else:
             # If we aren't supposed to trace installed code, then check if this
             # is near the Python standard library and skip it if so.
             if self.cover_match and self.cover_match.match(canonical):
                 return False
 
-        # Check the file against the include and omit patterns.
-        if self.include_match and not self.include_match.match(canonical):
-            return False
+        # Check the file against the omit pattern.
         if self.omit_match and self.omit_match.match(canonical):
             return False
 
 
     def test_ignore_stdlib(self):
         self.make_file("mymain.py", """\
-            import mymod, colorsys
+            import colorsys
             a = 1
             hls = colorsys.rgb_to_hls(1.0, 0.5, 0.0)
             """)
 
-        self.make_file("mymod.py", """\
-            fooey = 17
-            """)
-
         # Measure without the stdlib.
         cov1 = coverage.coverage()
         self.assertEqual(cov1.config.cover_pylib, False)
         _, statements, missing, _ = cov2.analysis("colorsys.py")
         self.assertNotEqual(statements, missing)
 
+    def test_include_can_measure_stdlib(self):
+        self.make_file("mymain.py", """\
+            import colorsys, random
+            a = 1
+            r, g, b = [random.random() for _ in range(3)]
+            hls = colorsys.rgb_to_hls(r, g, b)
+            """)
+
+        # Measure without the stdlib, but include colorsys.
+        cov1 = coverage.coverage(cover_pylib=False, include=["*/colorsys.py"])
+        cov1.start()
+        self.import_local_file("mymain")        # pragma: recursive coverage
+        cov1.stop()                             # pragma: recursive coverage
+
+        # some statements were marked executed in colorsys.py
+        _, statements, missing, _ = cov1.analysis("colorsys.py")
+        self.assertNotEqual(statements, missing)
+        # but none were in random.py
+        _, statements, missing, _ = cov1.analysis("random.py")
+        self.assertEqual(statements, missing)
+
     def test_exclude_list(self):
         cov = coverage.coverage()
         cov.clear_exclude()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.