run-tests: make code coverage work on Python 3
authorGregory Szorc <gregory.szorc@gmail.com>
Thu, 17 Oct 2019 20:40:12 -0700
changeset 43283 96eb9ef777a8
parent 43282 47946f08e463
child 43284 ce6dd1cee4c8
run-tests: make code coverage work on Python 3 This code path was obviously not tested on Python 3 because it blew up in several places due to str/bytes mismatch. For internal code, we normalize paths to bytes. For code calling into `coverage`, we normalize paths to str, which is what `coverage` seems to expect. After this, `run-tests.py -H` works on Python 3! Differential Revision: https://phab.mercurial-scm.org/D7133
tests/run-tests.py
--- a/tests/run-tests.py	Thu Oct 17 22:40:24 2019 +0100
+++ b/tests/run-tests.py	Thu Oct 17 20:40:12 2019 -0700
@@ -3514,21 +3514,23 @@
                 print('WARNING: cannot fix hg.bat reference to python.exe')
 
         if self.options.anycoverage:
-            custom = os.path.join(osenvironb[b'RUNTESTDIR'], 'sitecustomize.py')
-            target = os.path.join(self._pythondir, 'sitecustomize.py')
+            custom = os.path.join(
+                osenvironb[b'RUNTESTDIR'], b'sitecustomize.py'
+            )
+            target = os.path.join(self._pythondir, b'sitecustomize.py')
             vlog('# Installing coverage trigger to %s' % target)
             shutil.copyfile(custom, target)
-            rc = os.path.join(self._testdir, '.coveragerc')
+            rc = os.path.join(self._testdir, b'.coveragerc')
             vlog('# Installing coverage rc to %s' % rc)
-            os.environ['COVERAGE_PROCESS_START'] = rc
-            covdir = os.path.join(self._installdir, '..', 'coverage')
+            osenvironb[b'COVERAGE_PROCESS_START'] = rc
+            covdir = os.path.join(self._installdir, b'..', b'coverage')
             try:
                 os.mkdir(covdir)
             except OSError as e:
                 if e.errno != errno.EEXIST:
                     raise
 
-            os.environ['COVERAGE_DIR'] = covdir
+            osenvironb[b'COVERAGE_DIR'] = covdir
 
     def _checkhglib(self, verb):
         """Ensure that the 'mercurial' package imported by python is
@@ -3602,22 +3604,25 @@
         # chdir is the easiest way to get short, relative paths in the
         # output.
         os.chdir(self._hgroot)
-        covdir = os.path.join(self._installdir, '..', 'coverage')
+        covdir = os.path.join(_strpath(self._installdir), '..', 'coverage')
         cov = coverage(data_file=os.path.join(covdir, 'cov'))
 
         # Map install directory paths back to source directory.
-        cov.config.paths['srcdir'] = ['.', self._pythondir]
+        cov.config.paths['srcdir'] = ['.', _strpath(self._pythondir)]
 
         cov.combine()
 
-        omit = [os.path.join(x, '*') for x in [self._bindir, self._testdir]]
+        omit = [
+            _strpath(os.path.join(x, b'*'))
+            for x in [self._bindir, self._testdir]
+        ]
         cov.report(ignore_errors=True, omit=omit)
 
         if self.options.htmlcov:
-            htmldir = os.path.join(self._outputdir, 'htmlcov')
+            htmldir = os.path.join(_strpath(self._outputdir), 'htmlcov')
             cov.html_report(directory=htmldir, omit=omit)
         if self.options.annotate:
-            adir = os.path.join(self._outputdir, 'annotated')
+            adir = os.path.join(_strpath(self._outputdir), 'annotated')
             if not os.path.isdir(adir):
                 os.mkdir(adir)
             cov.annotate(directory=adir, omit=omit)