[PATCH 1 of 3 coverage] run-tests: obtain code coverage via Python API

Gregory Szorc gregory.szorc at gmail.com
Sat Mar 28 07:48:44 UTC 2015


# HG changeset patch
# User Gregory Szorc <gregory.szorc at gmail.com>
# Date 1427523439 25200
#      Fri Mar 27 23:17:19 2015 -0700
# Node ID 1a91ee1edd9b042b2083880f608dece8a170a82b
# Parent  efa094701a05d58d505c3b0c3b3c73dba4e51e97
run-tests: obtain code coverage via Python API

Before, we were invoking the "coverage" program provided by the
"coverage" module. This patch changes the code to go through the
Python API. This makes the next patch a little bit easier to reason
about.

A side effect of this patch is that writing code coverage reports
will be slightly faster, as we won't have to redundantly load
coverage data.

diff --git a/tests/run-tests.py b/tests/run-tests.py
--- a/tests/run-tests.py
+++ b/tests/run-tests.py
@@ -1980,29 +1980,29 @@ class TestRunner(object):
         return self._hgpath
 
     def _outputcoverage(self):
         """Produce code coverage output."""
+        from coverage import coverage
+
         vlog('# Producing coverage report')
+        # chdir is the easiest way to get short, relative paths in the
+        # output.
         os.chdir(self._pythondir)
+        covdir = os.path.join(self._installdir, '..')
+        cov = coverage(data_file=os.path.join(covdir, '.coverage'))
+        cov.load()
 
-        def covrun(*args):
-            cmd = 'coverage %s' % ' '.join(args)
-            vlog('# Running: %s' % cmd)
-            os.system(cmd)
+        omit = [os.path.join(x, '*') for x in [self._bindir, self._testdir]]
+        cov.report(ignore_errors=True, omit=omit)
 
-        covrun('-c')
-        omit = ','.join(os.path.join(x, '*') for x in
-                        [self._bindir, self._testdir])
-        covrun('-i', '-r', '"--omit=%s"' % omit) # report
         if self.options.htmlcov:
             htmldir = os.path.join(self._testdir, 'htmlcov')
-            covrun('-i', '-b', '"--directory=%s"' % htmldir,
-                   '"--omit=%s"' % omit)
+            cov.html_report(directory=htmldir, omit=omit)
         if self.options.annotate:
             adir = os.path.join(self._testdir, 'annotated')
             if not os.path.isdir(adir):
                 os.mkdir(adir)
-            covrun('-i', '-a', '"--directory=%s"' % adir, '"--omit=%s"' % omit)
+            cov.annotate(directory=adir, omit=omit)
 
     def _findprogram(self, program):
         """Search PATH for a executable program"""
         for p in os.environ.get('PATH', os.defpath).split(os.pathsep):


More information about the Mercurial-devel mailing list