Patchwork [044,of,179,tests-refactor] run-tests: refactor runone() into gettest() and scheduletests()

login
register
mail settings
Submitter Gregory Szorc
Date May 2, 2014, 6:38 p.m.
Message ID <f254b3678507045ba565.1399055881@vm-ubuntu-main.gateway.sonic.net>
Download mbox | patch
Permalink /patch/4539/
State Accepted
Commit de25e968b4d838652129afe30f9b7d1af53e4633
Headers show

Comments

Gregory Szorc - May 2, 2014, 6:38 p.m.
# HG changeset patch
# User Gregory Szorc <gregory.szorc@gmail.com>
# Date 1397970175 25200
#      Sat Apr 19 22:02:55 2014 -0700
# Branch stable
# Node ID f254b3678507045ba5656ed4fbead44a6425b9ff
# Parent  17b756f8c979486ff5ef6a597566743226f82422
run-tests: refactor runone() into gettest() and scheduletests()

runone() has been removed. Replacing it are gettest() (which returns a
Test instance) and a small amount of boilerplate in scheduletests().

Patch

diff --git a/tests/run-tests.py b/tests/run-tests.py
--- a/tests/run-tests.py
+++ b/tests/run-tests.py
@@ -701,17 +701,18 @@  class Test(object):
             sys.stdout.flush()
             iolock.release()
 
         times.append((self._test, duration))
 
         return res
 
     def _run(self, testtmp, replacements, env):
-        raise NotImplemented('Subclasses must implement Test.run()')
+        # This should be implemented in child classes to run tests.
+        return self._skip('unknown test type')
 
     def _getreplacements(self, testtmp):
         port = self._options.port + self._count * 3
         r = [
             (r':%s\b' % port, ':$HGPORT'),
             (r':%s\b' % (port + 1), ':$HGPORT1'),
             (r':%s\b' % (port + 2), ':$HGPORT2'),
             ]
@@ -1076,16 +1077,36 @@  class TTest(Test):
             if el.endswith(" (re)\n"):
                 return TTest.rematch(el[:-6], l)
             if el.endswith(" (glob)\n"):
                 return TTest.globmatch(el[:-8], l)
             if os.altsep and l.replace('\\', '/') == el:
                 return '+glob'
         return False
 
+def gettest(testdir, test, options, count):
+    """Obtain a Test by looking at its filename.
+
+    Returns a Test instance. The Test may not be runnable if it doesn't map
+    to a known type.
+    """
+
+    lctest = test.lower()
+    refpath = os.path.join(testdir, test)
+
+    runner = Test
+
+    for ext, cls, out in testtypes:
+        if lctest.endswith(ext):
+            runner = cls
+            refpath = os.path.join(testdir, test + out)
+            break
+
+    return runner(testdir, test, options, count, refpath)
+
 wifexited = getattr(os, "WIFEXITED", lambda x: False)
 def run(cmd, wd, options, replacements, env):
     """Run command in a sub-process, capturing the output (stdout and stderr).
     Return a tuple (exitcode, output).  output is None in debug mode."""
     # TODO: Use subprocess.Popen if we're running on Python 2.4
     if options.debug:
         proc = subprocess.Popen(cmd, shell=True, cwd=wd, env=env)
         ret = proc.wait()
@@ -1122,41 +1143,16 @@  def run(cmd, wd, options, replacements, 
 
     if abort:
         raise KeyboardInterrupt()
 
     for s, r in replacements:
         output = re.sub(s, r, output)
     return ret, output.splitlines(True)
 
-def runone(options, test, count):
-    '''returns a result element: (code, test, msg)'''
-
-    def skip(msg):
-        if options.verbose:
-            log("\nSkipping %s: %s" % (testpath, msg))
-        return 's', test, msg
-
-    lctest = test.lower()
-
-    for ext, cls, out in testtypes:
-        if lctest.endswith(ext):
-            runner = cls
-            ref = os.path.join(TESTDIR, test + out)
-            break
-    else:
-        return skip("unknown test type")
-
-    t = runner(TESTDIR, test, options, count, ref)
-    result = t.run()
-
-    del t # For cleanup side-effects.
-
-    return result
-
 _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
@@ -1188,17 +1184,19 @@  def scheduletests(options, tests):
     jobs = options.jobs
     done = queue.Queue()
     running = 0
     count = 0
     global abort
 
     def job(test, count):
         try:
-            done.put(runone(options, test, count))
+            t = gettest(TESTDIR, test, options, count)
+            done.put(t.run())
+            del t # For side-effects.
         except KeyboardInterrupt:
             pass
         except: # re-raises
             done.put(('!', test, 'run-test raised an error, see traceback'))
             raise
 
     try:
         while tests or running: