run-tests: add --with-python3 to define a Python 3 interpreter
authorGregory Szorc <gregory.szorc@gmail.com>
Fri, 18 Mar 2016 16:17:56 -0700
changeset 28582 cdbc25306696
parent 28581 3c8f0a605504
child 28583 260ce2eed951
run-tests: add --with-python3 to define a Python 3 interpreter Currently, very few parts of Mercurial run under Python 3, notably the test harness. We want to write tests that run Python 3. For example, we want to extend test-check-py3-compat.t to parse and load Python files. However, we have a problem: finding appropriate files requires running `hg files` and this requires Python 2 until `hg` works with Python 3. As a temporary workaround, we add --with-python3 to the test harness to allow us to define the path to a Python 3 interpreter. This interpreter is made available to the test environment via $PYTHON3 so tests can run things with Python 3 while the test harness and `hg` invocations continue to run from Python 2. To round out the feature, a "py3exe" hghave check has been added.
tests/hghave.py
tests/run-tests.py
--- a/tests/hghave.py	Fri Mar 18 11:06:03 2016 -0700
+++ b/tests/hghave.py	Fri Mar 18 16:17:56 2016 -0700
@@ -443,6 +443,10 @@
 def has_py3k():
     return 3 == sys.version_info[0]
 
+@check("py3exe", "a Python 3.x interpreter is available")
+def has_python3exe():
+    return 'PYTHON3' in os.environ
+
 @check("pure", "running with pure Python code")
 def has_pure():
     return any([
--- a/tests/run-tests.py	Fri Mar 18 11:06:03 2016 -0700
+++ b/tests/run-tests.py	Fri Mar 18 16:17:56 2016 -0700
@@ -255,6 +255,11 @@
                       help="use specified chg wrapper in place of hg")
     parser.add_option("-3", "--py3k-warnings", action="store_true",
         help="enable Py3k warnings on Python 2.6+")
+    # This option should be deleted once test-check-py3-compat.t and other
+    # Python 3 tests run with Python 3.
+    parser.add_option("--with-python3", metavar="PYTHON3",
+                      help="Python 3 interpreter (if running under Python 2)"
+                           " (TEMPORARY)")
     parser.add_option('--extra-config-opt', action="append",
                       help='set the given config opt in the test hgrc')
     parser.add_option('--random', action="store_true",
@@ -353,6 +358,27 @@
         if PYTHON3:
             parser.error(
                 '--py3k-warnings can only be used on Python 2.6 and 2.7')
+    if options.with_python3:
+        if PYTHON3:
+            parser.error('--with-python3 cannot be used when executing with '
+                         'Python 3')
+
+        # Verify Python3 executable is acceptable.
+        proc = subprocess.Popen([options.with_python3, b'--version'],
+                                stdout=subprocess.PIPE,
+                                stderr=subprocess.STDOUT)
+        out, _err = proc.communicate()
+        ret = proc.wait()
+        if ret != 0:
+            parser.error('could not determine version of python 3')
+        if not out.startswith('Python '):
+            parser.error('unexpected output from python3 --version: %s' %
+                         out)
+        vers = version.LooseVersion(out[len('Python '):])
+        if vers < version.LooseVersion('3.5.0'):
+            parser.error('--with-python3 version must be 3.5.0 or greater; '
+                         'got %s' % out)
+
     if options.blacklist:
         options.blacklist = parselistfiles(options.blacklist, 'blacklist')
     if options.whitelist:
@@ -1987,6 +2013,9 @@
         osenvironb[b"BINDIR"] = self._bindir
         osenvironb[b"PYTHON"] = PYTHON
 
+        if self.options.with_python3:
+            osenvironb[b'PYTHON3'] = self.options.with_python3
+
         fileb = _bytespath(__file__)
         runtestdir = os.path.abspath(os.path.dirname(fileb))
         osenvironb[b'RUNTESTDIR'] = runtestdir