run-tests: move run into Test class
authorGregory Szorc <gregory.szorc@gmail.com>
Sat, 28 Mar 2015 14:08:25 -0700
changeset 24508 fbe2fb71a6e6
parent 24507 a0668a587c04
child 24509 27092bb70293
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.
tests/run-tests.py
--- a/tests/run-tests.py	Sat Mar 28 19:39:03 2015 -0700
+++ b/tests/run-tests.py	Sat Mar 28 14:08:25 2015 -0700
@@ -76,6 +76,8 @@
 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()
@@ -720,6 +722,51 @@
         # 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."""
 
@@ -733,8 +780,8 @@
         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()
 
@@ -781,8 +828,9 @@
         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()
@@ -1075,49 +1123,6 @@
     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):