[PATCH 2 of 4] run-tests: factor out _checkhglib() to check import path of 'mercurial'

Greg Ward greg-hg at gerg.ca
Tue May 19 09:19:29 CDT 2009


# HG changeset patch
# User Greg Ward <greg-hg at gerg.ca>
# Date 1242741455 14400
# Node ID 15f2f75a69ce89fd1d636f5bf81c4687978ff2ba
# Parent  5c250c2a62d8c8433a16f6e6475c4d0deb7a1504
run-tests: factor out _checkhglib() to check import path of 'mercurial'.

- rename _hgpath() to _gethgpath() and move it down next to _checkhglib()
- change _gethgpath() so it caches its result: replaces global 'hgpkg'
  that anyone can use with global '_hgpath' that is only for _gethgpath()
- no need to pass 'expecthg' to runchildren() or runtests() anymore

Also: do not change global PYTHON when doing coverage; this seems to
be unnecessary, since we create a dummy 'hg' script that handles
coverage for us, and it made things brittle.  (E.g. the rest of this
patch makes the call to _hgpath() come later, and it was broken by
enabling coverage.)

diff --git a/tests/run-tests.py b/tests/run-tests.py
--- a/tests/run-tests.py
+++ b/tests/run-tests.py
@@ -200,7 +200,6 @@
         shutil.copymode(sys.executable, mypython)
 
 def installhg(options):
-    global PYTHON
     vlog("# Performing temporary installation of HG")
     installerrs = os.path.join("tests", "install.err")
     pure = options.pure and "--pure" or ""
@@ -234,8 +233,6 @@
     os.environ["PYTHONPATH"] = pythonpath
 
     usecorrectpython()
-    global hgpkg
-    hgpkg = _hgpath()
 
     vlog("# Installing dummy diffstat")
     f = open(os.path.join(BINDIR, 'diffstat'), 'w')
@@ -264,15 +261,6 @@
                  os.path.join(BINDIR, '_hg.py')))
         f.close()
         os.chmod(os.path.join(BINDIR, 'hg'), 0700)
-        PYTHON = '"%s" "%s" -x' % (sys.executable,
-                                   os.path.join(TESTDIR,'coverage.py'))
-
-def _hgpath():
-    cmd = '%s -c "import mercurial; print mercurial.__path__[0]"'
-    hgpath = os.popen(cmd % PYTHON)
-    path = hgpath.read().strip()
-    hgpath.close()
-    return path
 
 def outputcoverage(options):
     vlog("# Producing coverage report")
@@ -488,11 +476,35 @@
         return None
     return ret == 0
 
-def runchildren(options, expecthg, tests):
+_hgpath = None
+
+def _gethgpath():
+    """Return the path to the mercurial package that is actually found by
+    the current Python interpreter."""
+    global _hgpath
+    if _hgpath is not None:
+        return _hgpath
+
+    cmd = '%s -c "import mercurial; print mercurial.__path__[0]"'
+    pipe = os.popen(cmd % PYTHON)
+    try:
+        _hgpath = pipe.read().strip()
+    finally:
+        pipe.close()
+    return _hgpath
+
+def _checkhglib(verb):
+    """Ensure that the 'mercurial' package imported by python is
+    the one we expect it to be.  If not, print a message to stdout."""
+    expecthg = os.path.join(HGTMP, 'install', 'lib', 'python', 'mercurial')
+    actualhg = _gethgpath()
+    if actualhg != expecthg:
+        print '# %s unexpected mercurial: %s' % (verb, actualhg)
+
+def runchildren(options, tests):
     if not options.with_hg:
         installhg(options)
-        if hgpkg != expecthg:
-            print '# Testing unexpected mercurial: %s' % hgpkg
+        _checkhglib("Testing")
 
     optcopy = dict(options.__dict__)
     optcopy['jobs'] = 1
@@ -546,13 +558,12 @@
     for s in fails:
         print "Failed %s: %s" % (s[0], s[1])
 
-    if hgpkg != expecthg:
-        print '# Tested unexpected mercurial: %s' % hgpkg
+    _checkhglib("Tested")
     print "# Ran %d tests, %d skipped, %d failed." % (
         tested, skipped, failed)
     sys.exit(failures != 0)
 
-def runtests(options, expecthg, tests):
+def runtests(options, tests):
     global DAEMON_PIDS, HGRCPATH
     DAEMON_PIDS = os.environ["DAEMON_PIDS"] = os.path.join(HGTMP, 'daemon.pids')
     HGRCPATH = os.environ["HGRCPATH"] = os.path.join(HGTMP, '.hgrc')
@@ -560,9 +571,7 @@
     try:
         if not options.with_hg:
             installhg(options)
-
-            if hgpkg != expecthg:
-                print '# Testing unexpected mercurial: %s' % hgpkg
+            _checkhglib("Testing")
 
         if options.timeout > 0:
             try:
@@ -624,8 +633,7 @@
                 print "Skipped %s: %s" % s
             for s in fails:
                 print "Failed %s: %s" % s
-            if hgpkg != expecthg:
-                print '# Tested unexpected mercurial: %s' % hgpkg
+            _checkhglib("Tested")
             print "# Ran %d tests, %d skipped, %d failed." % (
                 tested, skipped, failed)
 
@@ -676,8 +684,6 @@
     PYTHONDIR = os.path.join(INST, "lib", "python")
     COVERAGE_FILE = os.path.join(TESTDIR, ".coverage")
 
-    expecthg = os.path.join(HGTMP, 'install', 'lib', 'python', 'mercurial')
-
     if len(args) == 0:
         args = os.listdir(".")
         args.sort()
@@ -694,9 +700,9 @@
 
     try:
         if len(tests) > 1 and options.jobs > 1:
-            runchildren(options, expecthg, tests)
+            runchildren(options, tests)
         else:
-            runtests(options, expecthg, tests)
+            runtests(options, tests)
     finally:
         cleanup(options)
 


More information about the Mercurial-devel mailing list