[PATCH 2 of 6] run-tests: move run into Test class

Gregory Szorc gregory.szorc at gmail.com
Sat Mar 28 22:15:07 CDT 2015


# HG changeset patch
# User Gregory Szorc <gregory.szorc at gmail.com>
# Date 1427576905 25200
#      Sat Mar 28 14:08:25 2015 -0700
# Node ID ffe2862b3d4c2e9783bf0d1e6f22c764e0542735
# Parent  3c820c7b3ab9657c8b2d0a769ad2385c09371eac
run-tests: move run into Test class

Future patches will change how replacements work. Since the logic in
run() is strongly tied to the operation of individual tests and since
there is potential to make the implementation simpler by giving the
function access to Test attributes, move it into Test.

diff --git a/tests/run-tests.py b/tests/run-tests.py
--- a/tests/run-tests.py
+++ b/tests/run-tests.py
@@ -75,8 +75,10 @@ processlock = threading.Lock()
 # zombies but it's pretty harmless even if we do.
 if sys.version_info < (2, 5):
     subprocess._cleanup = lambda: None
 
+wifexited = getattr(os, "WIFEXITED", lambda x: False)
+
 closefds = os.name == 'posix'
 def Popen4(cmd, wd, timeout, env=None):
     processlock.acquire()
     p = subprocess.Popen(cmd, shell=True, bufsize=-1, cwd=wd, env=env,
@@ -719,8 +721,53 @@ class Test(unittest.TestCase):
         # unittest differentiates between errored and failed.
         # Failed is denoted by AssertionError (by default at least).
         raise AssertionError(msg)
 
+    def _runcommand(self, cmd, wd, replacements, env, debug=False,
+                    timeout=None):
+        """Run command in a sub-process, capturing the output (stdout and
+        stderr).
+
+        Return a tuple (exitcode, output). output is None in debug mode.
+        """
+        if debug:
+            proc = subprocess.Popen(cmd, shell=True, cwd=wd, env=env)
+            ret = proc.wait()
+            return (ret, None)
+
+        proc = Popen4(cmd, wd, timeout, env)
+        def cleanup():
+            terminate(proc)
+            ret = proc.wait()
+            if ret == 0:
+                ret = signal.SIGTERM << 8
+            killdaemons(env['DAEMON_PIDS'])
+            return ret
+
+        output = ''
+        proc.tochild.close()
+
+        try:
+            output = proc.fromchild.read()
+        except KeyboardInterrupt:
+            vlog('# Handling keyboard interrupt')
+            cleanup()
+            raise
+
+        ret = proc.wait()
+        if wifexited(ret):
+            ret = os.WEXITSTATUS(ret)
+
+        if proc.timeout:
+            ret = 'timeout'
+
+        if ret:
+            killdaemons(env['DAEMON_PIDS'])
+
+        for s, r in replacements:
+            output = re.sub(s, r, output)
+        return ret, output.splitlines(True)
+
 class PythonTest(Test):
     """A Python-based test."""
 
     @property
@@ -732,10 +779,10 @@ class PythonTest(Test):
         cmd = '%s%s "%s"' % (PYTHON, py3kswitch, self.path)
         vlog("# Running", cmd)
         if os.name == 'nt':
             replacements.append((r'\r\n', '\n'))
-        result = run(cmd, self._testtmp, replacements, env,
-                   debug=self._debug, timeout=self._timeout)
+        result = self._runcommand(cmd, self._testtmp, replacements, env,
+                                  debug=self._debug, timeout=self._timeout)
         if self._aborted:
             raise KeyboardInterrupt()
 
         return result
@@ -780,10 +827,11 @@ class TTest(Test):
 
         cmd = '%s "%s"' % (self._shell, fname)
         vlog("# Running", cmd)
 
-        exitcode, output = run(cmd, self._testtmp, replacements, env,
-                               debug=self._debug, timeout=self._timeout)
+        exitcode, output = self._runcommand(cmd, self._testtmp, replacements,
+                                            env, debug=self._debug,
+                                            timeout=self._timeout)
 
         if self._aborted:
             raise KeyboardInterrupt()
 
@@ -1074,51 +1122,8 @@ class TTest(Test):
     @staticmethod
     def _stringescape(s):
         return TTest.ESCAPESUB(TTest._escapef, s)
 
-
-wifexited = getattr(os, "WIFEXITED", lambda x: False)
-def run(cmd, wd, replacements, env, debug=False, timeout=None):
-    """Run command in a sub-process, capturing the output (stdout and stderr).
-    Return a tuple (exitcode, output).  output is None in debug mode."""
-    if debug:
-        proc = subprocess.Popen(cmd, shell=True, cwd=wd, env=env)
-        ret = proc.wait()
-        return (ret, None)
-
-    proc = Popen4(cmd, wd, timeout, env)
-    def cleanup():
-        terminate(proc)
-        ret = proc.wait()
-        if ret == 0:
-            ret = signal.SIGTERM << 8
-        killdaemons(env['DAEMON_PIDS'])
-        return ret
-
-    output = ''
-    proc.tochild.close()
-
-    try:
-        output = proc.fromchild.read()
-    except KeyboardInterrupt:
-        vlog('# Handling keyboard interrupt')
-        cleanup()
-        raise
-
-    ret = proc.wait()
-    if wifexited(ret):
-        ret = os.WEXITSTATUS(ret)
-
-    if proc.timeout:
-        ret = 'timeout'
-
-    if ret:
-        killdaemons(env['DAEMON_PIDS'])
-
-    for s, r in replacements:
-        output = re.sub(s, r, output)
-    return ret, output.splitlines(True)
-
 iolock = threading.RLock()
 
 class SkipTest(Exception):
     """Raised to indicate that a test is to be skipped."""


More information about the Mercurial-devel mailing list