[PATCH v2] run-tests: $TESTDIR can be something else than $PWD

matthieu.laneuville at octobus.net matthieu.laneuville at octobus.net
Fri Nov 3 03:40:37 EDT 2017


# HG changeset patch
# User Matthieu Laneuville <matthieu.laneuville at octobus.net>
# Date 1501648065 -32400
#      Wed Aug 02 13:27:45 2017 +0900
# Node ID 753f33aed3c6b3fc4d02f5235b1468450ee41fab
# Parent  a7e49a5b3e6f38338660c0d98f9c593f70064e95
run-tests: $TESTDIR can be something else than $PWD

$TESTDIR is expected to be the directory were the test lives, and is often used
to reference helper functions. However, it is now set to $PWD at test invocation
time, so if `run-tests.py` is called from another folder, the test will fail.

The solution is to force $TESTDIR to be the base directory of the test itself,
irrespective of where the runner is called from.

diff -r a7e49a5b3e6f -r 753f33aed3c6 tests/run-tests.py
--- a/tests/run-tests.py	Wed Nov 01 17:08:33 2017 +0100
+++ b/tests/run-tests.py	Wed Aug 02 13:27:45 2017 +0900
@@ -2353,6 +2353,12 @@ class TestRunner(object):
 
         self._testdir = osenvironb[b'TESTDIR'] = getattr(
             os, 'getcwdb', os.getcwd)()
+        # assume all tests in same folder for now
+        if testdescs:
+            pathname = os.path.dirname(testdescs[0]['path'])
+            if pathname and not osenvironb[b'TESTDIR'].endswith(b'/'):
+                osenvironb[b'TESTDIR'] += b'/'
+            osenvironb[b'TESTDIR'] += pathname
         if self.options.outputdir:
             self._outputdir = canonpath(_bytespath(self.options.outputdir))
         else:
diff -r a7e49a5b3e6f -r 753f33aed3c6 tests/test-run-tests.t
--- a/tests/test-run-tests.t	Wed Nov 01 17:08:33 2017 +0100
+++ b/tests/test-run-tests.t	Wed Aug 02 13:27:45 2017 +0900
@@ -1261,6 +1261,22 @@ support for running a test outside the c
   .
   # Ran 1 tests, 0 skipped, 0 failed.
 
+support for running run-tests.py from another directory
+  $ mkdir tmp && cd tmp
+  $ cat > useful-file.sh << EOF
+  > important command
+  > EOF
+
+  $ cat > test-folder.t << EOF
+  >   $ cat \$TESTDIR/useful-file.sh
+  >   important command
+  > EOF
+
+  $ cd ..
+  $ $PYTHON $TESTDIR/run-tests.py tmp/test-folder.t
+  .
+  # Ran 1 tests, 0 skipped, 0 failed.
+
 support for bisecting failed tests automatically
   $ hg init bisect
   $ cd bisect


More information about the Mercurial-devel mailing list