diff tests/run-tests.py @ 47588:eb611ecb435c

run-tests: rely on an actual executable in PATH instead of alias for `hg` The alias approach is poorly inherited by other process that the test might spawn. To solve this we use the same approach as for `python`/`python3` we write an executable file explicitly. Doing this fixes `which hg` invocation that now returns the same location as `hg`. Using chg server side has some minor effect on some stdout/stderr ordering when using `chg` as the server too. Differential Revision: https://phab.mercurial-scm.org/D11053
author Pierre-Yves David <pierre-yves.david@octobus.net>
date Fri, 09 Jul 2021 22:37:24 +0200
parents a8e33ab50c4f
children f0fbe8f4faa6
line wrap: on
line diff
--- a/tests/run-tests.py	Fri Jul 09 20:42:26 2021 +0200
+++ b/tests/run-tests.py	Fri Jul 09 22:37:24 2021 +0200
@@ -359,6 +359,21 @@
     return os.path.realpath(os.path.expanduser(path))
 
 
+def which(exe):
+    if PYTHON3:
+        # shutil.which only accept bytes from 3.8
+        cmd = _bytes2sys(exe)
+        real_exec = shutil.which(cmd)
+        return _sys2bytes(real_exec)
+    else:
+        # let us do the os work
+        for p in osenvironb[b'PATH'].split(os.pathsep):
+            f = os.path.join(p, exe)
+            if os.path.isfile(f):
+                return f
+        return None
+
+
 def parselistfiles(files, listtype, warn=True):
     entries = dict()
     for filename in files:
@@ -1829,8 +1844,6 @@
 
         if self._debug:
             script.append(b'set -x\n')
-        if self._hgcommand != b'hg':
-            script.append(b'alias hg="%s"\n' % self._hgcommand)
         if os.getenv('MSYSTEM'):
             script.append(b'alias pwd="pwd -W"\n')
 
@@ -3436,6 +3449,7 @@
                 if self.options.rhg:
                     assert self._installdir
                     self._installrhg()
+                self._use_correct_mercurial()
 
                 log(
                     'running %d tests using %d parallel processes'
@@ -3628,6 +3642,25 @@
                 if not self._findprogram(pyexename):
                     print("WARNING: Cannot find %s in search path" % pyexename)
 
+    def _use_correct_mercurial(self):
+        target_exec = os.path.join(self._custom_bin_dir, b'hg')
+        if self._hgcommand != b'hg':
+            # shutil.which only accept bytes from 3.8
+            real_exec = which(self._hgcommand)
+            if real_exec is None:
+                raise ValueError('could not find exec path for "%s"', real_exec)
+            if real_exec == target_exec:
+                # do not overwrite something with itself
+                return
+            if WINDOWS:
+                with open(target_exec, 'wb') as f:
+                    f.write(b'#!/bin/sh\n')
+                    escaped_exec = shellquote(_bytes2sys(real_exec))
+                    f.write(b'%s "$@"\n' % _sys2bytes(escaped_exec))
+            else:
+                os.symlink(real_exec, target_exec)
+            self._createdfiles.append(target_exec)
+
     def _installhg(self):
         """Install hg into the test environment.