run-tests: obtain code coverage via Python API
authorGregory Szorc <gregory.szorc@gmail.com>
Fri, 27 Mar 2015 23:17:19 -0700
changeset 24504 7046ecabd9a8
parent 24503 944749de6f3a
child 24505 031947baf4d0
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.
tests/run-tests.py
--- a/tests/run-tests.py	Sat Mar 28 12:58:44 2015 -0700
+++ b/tests/run-tests.py	Fri Mar 27 23:17:19 2015 -0700
@@ -1985,27 +1985,27 @@
 
     def _outputcoverage(self):
         """Produce code coverage output."""
-        vlog('# Producing coverage report')
-        os.chdir(self._pythondir)
+        from coverage import coverage
 
-        def covrun(*args):
-            cmd = 'coverage %s' % ' '.join(args)
-            vlog('# Running: %s' % cmd)
-            os.system(cmd)
+        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()
 
-        covrun('-c')
-        omit = ','.join(os.path.join(x, '*') for x in
-                        [self._bindir, self._testdir])
-        covrun('-i', '-r', '"--omit=%s"' % omit) # report
+        omit = [os.path.join(x, '*') for x in [self._bindir, self._testdir]]
+        cov.report(ignore_errors=True, omit=omit)
+
         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"""