Commits

Ned Batchelder  committed 0617c1c

Don't measure the coverage package itself. Inspired by, but does not fix, issue #8.

  • Participants
  • Parent commits e387072

Comments (0)

Files changed (3)

File coverage/control.py

 
         self.data = CoverageData(
             basename=data_file, suffix=data_suffix,
-            collector="coverage v%s" % __version__)
+            collector="coverage v%s" % __version__
+            )
 
         # The default exclude pattern.
         self.exclude('# *pragma[: ]*[nN][oO] *[cC][oO][vV][eE][rR]')
             os_file = self.file_locator.canonical_filename(os.__file__)
             self.pylib_prefix = os.path.split(os_file)[0]
 
+        here = self.file_locator.canonical_filename(__file__)
+        self.cover_prefix = os.path.split(here)[0]
+
     def _should_trace(self, filename, frame):
         """Decide whether to trace execution in `filename`
         
         if not self.cover_pylib:
             if canonical.startswith(self.pylib_prefix):
                 return False
-        
+
+        # We exclude the coverage code itself, since a little of it will be
+        # measured otherwise.
+        if canonical.startswith(self.cover_prefix):
+            return False
+
         return canonical
 
     def use_cache(self, usecache):

File test/coverage_coverage.py

     shutil.rmtree(HTML_DIR)
 
 cov = coverage.coverage()
+# Cheap trick: the coverage code itself is excluded from measurement, but if
+# we clobber the cover_prefix in the coverage object, we can defeat the
+# self-detection.
+cov.cover_prefix = "Please measure coverage.py!"
 cov.erase()
 cov.start()
 

File test/test_coverage.py

 
         # Name                                                Stmts   Exec  Cover
         # -----------------------------------------------------------------------
-        # c:/ned/coverage/trunk/coverage/__init__               616      3     0%
         # c:/ned/coverage/trunk/test/modules/covmod1              2      2   100%
         # c:/ned/coverage/trunk/test/zipmods.zip/covmodzip1       2      2   100%
-        # c:/python25/lib/atexit                                 33      5    15%
-        # c:/python25/lib/ntpath                                250     12     4%
-        # c:/python25/lib/threading                             562      1     0%
         # mycode                                                  4      4   100%
         # -----------------------------------------------------------------------
-        # TOTAL                                                1467     27     1%
+        # TOTAL                                                   8      8   100%
 
-        self.assert_("/coverage/" in report1)
+        self.assert_("/coverage/__init__/" not in report1)
         self.assert_("/test/modules/covmod1 " in report1)
         self.assert_("/test/zipmods.zip/covmodzip1 " in report1)
         self.assert_("mycode " in report1)
 
         # Try reporting just one module
         report2 = self.run_command("coverage -r mycode.py").replace('\\', '/')
+
+        # Name     Stmts   Exec  Cover
+        # ----------------------------
+        # mycode       4      4   100%
+
         self.assert_("/coverage/" not in report2)
         self.assert_("/test/modules/covmod1 " not in report2)
         self.assert_("/test/zipmods.zip/covmodzip1 " not in report2)
 
         # Try reporting while omitting some modules
         report3 = self.run_command("coverage -r -o %s" % prefix).replace('\\', '/')
-        self.assert_("/coverage/" in report3)
+
+        # Name     Stmts   Exec  Cover
+        # ----------------------------
+        # mycode       4      4   100%
+
+        self.assert_("/coverage/" not in report3)
         self.assert_("/test/modules/covmod1 " not in report3)
         self.assert_("/test/zipmods.zip/covmodzip1 " not in report3)
         self.assert_("mycode " in report3)