[PATCH 070 of 179 tests-refactor] run-tests: move program searching into TestRunner

Gregory Szorc gregory.szorc at gmail.com
Fri May 2 13:38:27 CDT 2014


# HG changeset patch
# User Gregory Szorc <gregory.szorc at gmail.com>
# Date 1398012279 25200
#      Sun Apr 20 09:44:39 2014 -0700
# Branch stable
# Node ID 03c84727362c8b0e94d0bb127953cbc7bcf0657f
# Parent  ba68428f7499929e1e412d1d6400f80283908a7e
run-tests: move program searching into TestRunner

diff --git a/tests/run-tests.py b/tests/run-tests.py
--- a/tests/run-tests.py
+++ b/tests/run-tests.py
@@ -96,18 +96,16 @@ def Popen4(cmd, wd, timeout, env=None):
 SKIPPED_STATUS = 80
 SKIPPED_PREFIX = 'skipped: '
 FAILED_PREFIX  = 'hghave check failed: '
 PYTHON = sys.executable.replace('\\', '/')
 IMPL_PATH = 'PYTHONPATH'
 if 'java' in sys.platform:
     IMPL_PATH = 'JYTHONPATH'
 
-requiredtools = [os.path.basename(sys.executable), "diff", "grep", "unzip",
-                 "gunzip", "bunzip2", "sed"]
 defaults = {
     'jobs': ('HGTEST_JOBS', 1),
     'timeout': ('HGTEST_TIMEOUT', 180),
     'port': ('HGTEST_PORT', 20059),
     'shell': ('HGTEST_SHELL', 'sh'),
 }
 
 def parselistfiles(files, listtype, warn=True):
@@ -328,24 +326,16 @@ def log(*msg):
     if verbose:
         print verbose,
     for m in msg:
         print m,
     print
     sys.stdout.flush()
     iolock.release()
 
-def findprogram(program):
-    """Search PATH for a executable program"""
-    for p in os.environ.get('PATH', os.defpath).split(os.pathsep):
-        name = os.path.join(p, program)
-        if os.name == 'nt' or os.access(name, os.X_OK):
-            return name
-    return None
-
 def createhgrc(path, options):
     # create a fresh hgrc
     hgrc = open(path, 'w')
     hgrc.write('[ui]\n')
     hgrc.write('slash = True\n')
     hgrc.write('interactive = False\n')
     hgrc.write('[defaults]\n')
     hgrc.write('backout = -d "0 0"\n')
@@ -355,28 +345,16 @@ def createhgrc(path, options):
     if options.extra_config_opt:
         for opt in options.extra_config_opt:
             section, key = opt.split('.', 1)
             assert '=' in key, ('extra config opt %s must '
                                 'have an = for assignment' % opt)
             hgrc.write('[%s]\n%s\n' % (section, key))
     hgrc.close()
 
-def checktools():
-    # Before we go any further, check for pre-requisite tools
-    # stuff from coreutils (cat, rm, etc) are not tested
-    for p in requiredtools:
-        if os.name == 'nt' and not p.endswith('.exe'):
-            p += '.exe'
-        found = findprogram(p)
-        if found:
-            vlog("# Found prerequisite", p, "at", found)
-        else:
-            print "WARNING: Did not find prerequisite tool: "+p
-
 def terminate(proc):
     """Terminate subprocess (with fallback for Python versions < 2.6)"""
     vlog('# Terminating process %d' % proc.pid)
     try:
         getattr(proc, 'terminate', lambda : os.kill(proc.pid, signal.SIGTERM))()
     except OSError:
         pass
 
@@ -994,16 +972,26 @@ def _gethgpath():
 iolock = threading.Lock()
 
 class TestRunner(object):
     """Holds context for executing tests.
 
     Tests rely on a lot of state. This object holds it for them.
     """
 
+    REQUIREDTOOLS = [
+        os.path.basename(sys.executable),
+        'diff',
+        'grep',
+        'unzip',
+        'gunzip',
+        'bunzip2',
+        'sed',
+    ]
+
     TESTTYPES = [
         ('.py', PythonTest, '.out'),
         ('.t', TTest, ''),
     ]
 
     def __init__(self):
         self.options = None
         self.testdir = None
@@ -1139,33 +1127,33 @@ class TestRunner(object):
             mypython = os.path.join(self.tmpbindir, pyexename)
             try:
                 if os.readlink(mypython) == sys.executable:
                     return
                 os.unlink(mypython)
             except OSError, err:
                 if err.errno != errno.ENOENT:
                     raise
-            if findprogram(pyexename) != sys.executable:
+            if self._findprogram(pyexename) != sys.executable:
                 try:
                     os.symlink(sys.executable, mypython)
                     self._createdfiles.append(mypython)
                 except OSError, err:
                     # child processes may race, which is harmless
                     if err.errno != errno.EEXIST:
                         raise
         else:
             exedir, exename = os.path.split(sys.executable)
             vlog("# Modifying search path to find %s as %s in '%s'" %
                  (exename, pyexename, exedir))
             path = os.environ['PATH'].split(os.pathsep)
             while exedir in path:
                 path.remove(exedir)
             os.environ['PATH'] = os.pathsep.join([exedir] + path)
-            if not findprogram(pyexename):
+            if not self._findprogram(pyexename):
                 print "WARNING: Cannot find %s in search path" % pyexename
 
     def installhg(self):
         vlog("# Performing temporary installation of HG")
         installerrs = os.path.join("tests", "install.err")
         compiler = ''
         if self.options.compiler:
             compiler = '--compiler ' + self.options.compiler
@@ -1324,25 +1312,45 @@ class TestRunner(object):
                     t = threading.Thread(target=job, name=test,
                                          args=(test, count))
                     t.start()
                     running += 1
                     count += 1
         except KeyboardInterrupt:
             self.abort[0] = True
 
+    def _findprogram(self, program):
+        """Search PATH for a executable program"""
+        for p in os.environ.get('PATH', os.defpath).split(os.pathsep):
+            name = os.path.join(p, program)
+            if os.name == 'nt' or os.access(name, os.X_OK):
+                return name
+        return None
+
+    def checktools(self):
+        # Before we go any further, check for pre-requisite tools
+        # stuff from coreutils (cat, rm, etc) are not tested
+        for p in self.REQUIREDTOOLS:
+            if os.name == 'nt' and not p.endswith('.exe'):
+                p += '.exe'
+            found = self._findprogram(p)
+            if found:
+                vlog("# Found prerequisite", p, "at", found)
+            else:
+                print "WARNING: Did not find prerequisite tool: %s " % p
+
 def main(args, runner=None, parser=None):
     runner = runner or TestRunner()
 
     parser = parser or getparser()
     (options, args) = parseargs(args, parser)
     runner.options = options
     os.umask(022)
 
-    checktools()
+    runner.checktools()
 
     tests = runner.findtests(args)
 
     if options.random:
         random.shuffle(tests)
     else:
         # keywords for slow tests
         slow = 'svn gendoc check-code-hg'.split()


More information about the Mercurial-devel mailing list