[PATCH 3 of 3] run-tests: redefine --with-hg so it takes the 'hg' script to run

Greg Ward greg-hg at gerg.ca
Fri May 29 21:24:41 CDT 2009


# HG changeset patch
# User Greg Ward <greg-hg at gerg.ca>
# Date 1243649896 14400
# Node ID 5287aaabf1e1424bc6064ad2f80673e58e71af09
# Parent  e17b060db459d9ebac71fc5d9b6bce14b8d7986a
run-tests: redefine --with-hg so it takes the 'hg' script to run.

- in parseargs(), check that --with-hg value is valid
- add handy --local option for "--with-hg=../hg"
- ensure that we always set PATH and PYTHONPATH (not just
  when doing a temporary install)
- override any existing PYTHONPATH, so test success does not
  depend on whatever happens to be in the caller's environment
- give tests a little more control by exporting $PYTHON to the
  environment; needed by test-convert and test-mergetool when
  they run hg with a stripped-down $PATH

diff --git a/tests/run-tests.py b/tests/run-tests.py
--- a/tests/run-tests.py
+++ b/tests/run-tests.py
@@ -44,6 +44,12 @@
     'port': ('HGTEST_PORT', 20059),
 }
 
+_origchdir = os.chdir
+def _chdir(d):
+    vlog("chdir " + d)
+    _origchdir(d)
+os.chdir = _chdir
+
 def parseargs():
     parser = optparse.OptionParser("%prog [options] [tests]")
     parser.add_option("-C", "--annotate", action="store_true",
@@ -81,7 +87,11 @@
     parser.add_option("-n", "--nodiff", action="store_true",
         help="skip showing test changes")
     parser.add_option("--with-hg", type="string",
-        help="test existing install at given location")
+        metavar="HG",
+        help="test using specified hg script rather than a "
+             "temporary installation")
+    parser.add_option("--local", action="store_true",
+        help="shortcut for --with-hg=<testdir>/../hg")
     parser.add_option("--pure", action="store_true",
         help="use pure Python code instead of C extensions")
 
@@ -90,11 +100,32 @@
     parser.set_defaults(**defaults)
     (options, args) = parser.parse_args()
 
-    global vlog
+    if options.with_hg:
+        if not (os.path.isfile(options.with_hg) and
+                os.access(options.with_hg, os.X_OK)):
+            parser.error('--with-hg must specify an executable hg script')
+        if not os.path.basename(options.with_hg) == 'hg':
+            sys.stderr.write('warning: --with-hg should specify an hg script')
+    if options.local:
+        testdir = os.path.dirname(os.path.realpath(sys.argv[0]))
+        hgbin = os.path.join(os.path.dirname(testdir), 'hg')
+        if not os.access(hgbin, os.X_OK):
+            parser.error('--local specified, but %r not found or not executable'
+                         % hgbin)
+        options.with_hg = hgbin
+
     options.anycoverage = (options.cover or
                            options.cover_stdlib or
                            options.annotate)
 
+    if options.anycoverage and options.with_hg:
+        # I'm not sure if this is a fundamental limitation or just a
+        # bug.  But I don't want to waste people's time and energy doing
+        # test runs that don't give the results they want.
+        parser.error("sorry, coverage options do not work when --with-hg "
+                     "or --local specified")
+
+    global vlog
     if options.verbose:
         if options.jobs > 1 or options.child is not None:
             pid = "[%d]" % os.getpid()
@@ -227,16 +258,6 @@
         sys.exit(1)
     os.chdir(TESTDIR)
 
-    os.environ["PATH"] = "%s%s%s" % (BINDIR, os.pathsep, os.environ["PATH"])
-
-    pydir = os.pathsep.join([PYTHONDIR, TESTDIR])
-    pythonpath = os.environ.get("PYTHONPATH")
-    if pythonpath:
-        pythonpath = pydir + os.pathsep + pythonpath
-    else:
-        pythonpath = pydir
-    os.environ["PYTHONPATH"] = pythonpath
-
     usecorrectpython()
 
     vlog("# Installing dummy diffstat")
@@ -512,13 +533,14 @@
                          % (verb, actualhg, expecthg))
 
 def runchildren(options, tests):
-    if not options.with_hg:
+    if INST:
         installhg(options)
         _checkhglib("Testing")
 
     optcopy = dict(options.__dict__)
     optcopy['jobs'] = 1
-    optcopy['with_hg'] = INST
+    if optcopy['with_hg'] is None:
+        optcopy['with_hg'] = os.path.join(BINDIR, "hg")
     opts = []
     for opt, value in optcopy.iteritems():
         name = '--' + opt.replace('_', '-')
@@ -579,7 +601,7 @@
     HGRCPATH = os.environ["HGRCPATH"] = os.path.join(HGTMP, '.hgrc')
 
     try:
-        if not options.with_hg:
+        if INST:
             installhg(options)
             _checkhglib("Testing")
 
@@ -687,11 +709,26 @@
     os.environ["HGPORT2"] = str(options.port + 2)
 
     if options.with_hg:
-        INST = options.with_hg
+        INST = None
+        BINDIR = os.path.dirname(os.path.realpath(options.with_hg))
+        # do not set PYTHONDIR: any code that uses it only works
+        # when we install our own Mercurial library
     else:
         INST = os.path.join(HGTMP, "install")
-    BINDIR = os.environ["BINDIR"] = os.path.join(INST, "bin")
-    PYTHONDIR = os.path.join(INST, "lib", "python")
+        BINDIR = os.environ["BINDIR"] = os.path.join(INST, "bin")
+        PYTHONDIR = os.path.join(INST, "lib", "python")
+
+    os.environ["BINDIR"] = BINDIR
+    os.environ["PYTHON"] = PYTHON
+
+    if not options.child:
+        path = [BINDIR] + os.environ["PATH"].split(os.pathsep)
+        os.environ["PATH"] = os.pathsep.join(path)
+
+        # Deliberately override existing PYTHONPATH: do not want success
+        # to depend on what happens to be in caller's environment.
+        os.environ["PYTHONPATH"] = PYTHONDIR
+
     COVERAGE_FILE = os.path.join(TESTDIR, ".coverage")
 
     if len(args) == 0:
@@ -710,6 +747,8 @@
 
     vlog("# Using TESTDIR", TESTDIR)
     vlog("# Using HGTMP", HGTMP)
+    vlog("# Using PATH", os.environ["PATH"])
+    vlog("# Using PYTHONPATH", os.environ["PYTHONPATH"])
 
     try:
         if len(tests) > 1 and options.jobs > 1:
diff --git a/tests/test-convert b/tests/test-convert
--- a/tests/test-convert
+++ b/tests/test-convert
@@ -47,4 +47,6 @@
 
 echo % converting empty dir should fail "nicely"
 mkdir emptydir
-PATH=$BINDIR hg convert emptydir 2>&1 | sed 's,file://.*/emptydir,.../emptydir,g'
+# override $PATH to ensure p4 not visible; use $PYTHON in case we're
+# running from a devel copy, not a temp installation
+PATH=$BINDIR $PYTHON $BINDIR/hg convert emptydir 2>&1 | sed 's,file://.*/emptydir,.../emptydir,g'
diff --git a/tests/test-merge-tools b/tests/test-merge-tools
--- a/tests/test-merge-tools
+++ b/tests/test-merge-tools
@@ -60,7 +60,9 @@
 echo "# default is internal merge:"
 beforemerge
 echo "# hg merge -r 2"
-PATH=$BINDIR hg merge -r 2
+# override $PATH to ensure hgmerge not visible; use $PYTHON in case we're
+# running from a devel copy, not a temp installation
+PATH=$BINDIR $PYTHON $BINDIR/hg merge -r 2
 aftermerge
 
 echo "# simplest hgrc using false for merge:"


More information about the Mercurial-devel mailing list