run-tests: rely on an actual executable in PATH instead of alias for `hg`
authorPierre-Yves David <pierre-yves.david@octobus.net>
Fri, 09 Jul 2021 22:37:24 +0200
changeset 47588 eb611ecb435c
parent 47587 be496e3489b9
child 47589 f5c24c124e07
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
tests/run-tests.py
tests/test-bundle2-exchange.t
tests/test-run-tests.t
tests/test-ssh-bundle1.t
tests/test-ssh.t
tests/test-transaction-rollback-on-sigpipe.t
--- 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.
 
--- a/tests/test-bundle2-exchange.t	Fri Jul 09 20:42:26 2021 +0200
+++ b/tests/test-bundle2-exchange.t	Fri Jul 09 22:37:24 2021 +0200
@@ -751,10 +751,12 @@
   $ hg -R main push ssh://user@dummy/other -r e7ec4e813ba6
   pushing to ssh://user@dummy/other
   searching for changes
+  remote: Fail early! (no-py3 chg !)
   remote: adding changesets
   remote: adding manifests
   remote: adding file changes
-  remote: Fail early!
+  remote: Fail early! (py3 !)
+  remote: Fail early! (no-py3 no-chg !)
   remote: transaction abort!
   remote: Cleaning up the mess...
   remote: rollback completed
--- a/tests/test-run-tests.t	Fri Jul 09 20:42:26 2021 +0200
+++ b/tests/test-run-tests.t	Fri Jul 09 22:37:24 2021 +0200
@@ -648,14 +648,12 @@
 
   $ rt --debug 2>&1 | grep -v pwd
   running 2 tests using 1 parallel processes 
-  + alias hg=hg.exe (windows !)
   + echo *SALT* 0 0 (glob)
   *SALT* 0 0 (glob)
   + echo babar
   babar
   + echo *SALT* 10 0 (glob)
   *SALT* 10 0 (glob)
-  .+ alias hg=hg.exe (windows !)
   *+ echo *SALT* 0 0 (glob)
   *SALT* 0 0 (glob)
   + echo babar
--- a/tests/test-ssh-bundle1.t	Fri Jul 09 20:42:26 2021 +0200
+++ b/tests/test-ssh-bundle1.t	Fri Jul 09 22:37:24 2021 +0200
@@ -304,8 +304,10 @@
   remote: adding changesets
   remote: adding manifests
   remote: adding file changes
-  remote: added 1 changesets with 1 changes to 1 files
+  remote: added 1 changesets with 1 changes to 1 files (py3 !)
+  remote: added 1 changesets with 1 changes to 1 files (no-py3 no-chg !)
   remote: KABOOM
+  remote: added 1 changesets with 1 changes to 1 files (no-py3 chg !)
   $ hg -R ../remote heads
   changeset:   5:1383141674ec
   tag:         tip
@@ -474,8 +476,10 @@
   remote: adding changesets
   remote: adding manifests
   remote: adding file changes
-  remote: added 1 changesets with 1 changes to 1 files
+  remote: added 1 changesets with 1 changes to 1 files (py3 !)
+  remote: added 1 changesets with 1 changes to 1 files (no-py3 no-chg !)
   remote: KABOOM
+  remote: added 1 changesets with 1 changes to 1 files (no-py3 chg !)
   local stdout
 
 debug output
--- a/tests/test-ssh.t	Fri Jul 09 20:42:26 2021 +0200
+++ b/tests/test-ssh.t	Fri Jul 09 22:37:24 2021 +0200
@@ -301,9 +301,11 @@
   remote: adding changesets
   remote: adding manifests
   remote: adding file changes
-  remote: added 1 changesets with 1 changes to 1 files
+  remote: added 1 changesets with 1 changes to 1 files (py3 !)
+  remote: added 1 changesets with 1 changes to 1 files (no-py3 no-chg !)
   remote: KABOOM
   remote: KABOOM IN PROCESS
+  remote: added 1 changesets with 1 changes to 1 files (no-py3 chg !)
   $ hg -R ../remote heads
   changeset:   5:1383141674ec
   tag:         tip
@@ -527,9 +529,11 @@
   remote: adding changesets
   remote: adding manifests
   remote: adding file changes
-  remote: added 1 changesets with 1 changes to 1 files
+  remote: added 1 changesets with 1 changes to 1 files (py3 !)
+  remote: added 1 changesets with 1 changes to 1 files (no-py3 no-chg !)
   remote: KABOOM
   remote: KABOOM IN PROCESS
+  remote: added 1 changesets with 1 changes to 1 files (no-py3 chg !)
   local stdout
 
 debug output
--- a/tests/test-transaction-rollback-on-sigpipe.t	Fri Jul 09 20:42:26 2021 +0200
+++ b/tests/test-transaction-rollback-on-sigpipe.t	Fri Jul 09 22:37:24 2021 +0200
@@ -35,9 +35,12 @@
   $ hg push -e "\"$PYTHON\" \"$TESTDIR/dummyssh\"" --remotecmd "$remotecmd"
   pushing to ssh://user@dummy/$TESTTMP/remote
   searching for changes
-  remote: adding changesets
-  remote: adding manifests
-  remote: adding file changes
+  remote: adding changesets (py3 !)
+  remote: adding manifests (py3 !)
+  remote: adding file changes (py3 !)
+  remote: adding changesets (no-py3 no-chg !)
+  remote: adding manifests (no-py3 no-chg !)
+  remote: adding file changes (no-py3 no-chg !)
   abort: stream ended unexpectedly (got 0 bytes, expected 4)
   [255]
   $ cat $SIGPIPE_REMOTE_DEBUG_FILE