Patchwork [023,of,179,tests-refactor] run-tests: capture reference output in Test.__init__

login
register
mail settings
Submitter Gregory Szorc
Date May 2, 2014, 6:37 p.m.
Message ID <005aea2ea48076ce7dc1.1399055860@vm-ubuntu-main.gateway.sonic.net>
Download mbox | patch
Permalink /patch/4518/
State Accepted
Commit 6b3d66e4d3bed7d3bf5c7b96eec9d218f193729a
Headers show

Comments

Gregory Szorc - May 2, 2014, 6:37 p.m.
# HG changeset patch
# User Gregory Szorc <gregory.szorc@gmail.com>
# Date 1397958640 25200
#      Sat Apr 19 18:50:40 2014 -0700
# Branch stable
# Node ID 005aea2ea48076ce7dc1c14a99b99b4f58a90786
# Parent  95d37bc21eea1e2fe294cd5a181e28b194f7392c
run-tests: capture reference output in Test.__init__

Reference output should be constant and doesn't need to be computed at
test execution time. We calculate it earlier.

This patch is the first in a mini series that will change how the
TestResult object works.

Patch

diff --git a/tests/run-tests.py b/tests/run-tests.py
--- a/tests/run-tests.py
+++ b/tests/run-tests.py
@@ -544,29 +544,40 @@  def outputcoverage(options):
 
 class Test(object):
     """Encapsulates a single, runnable test.
 
     Test instances can be run multiple times via run(). However, multiple
     runs cannot be run concurrently.
     """
 
-    def __init__(self, path, options, count):
+    def __init__(self, path, options, count, refpath):
         self._path = path
         self._options = options
         self._count = count
 
+        # If we're not in --debug mode and reference output file exists,
+        # check test output against it.
+        if options.debug:
+            self._refout = None # to match "out is None"
+        elif os.path.exists(refpath):
+            f = open(refpath, 'r')
+            self._refout = f.read().splitlines(True)
+            f.close()
+        else:
+            self._refout = []
+
         self._threadtmp = os.path.join(HGTMP, 'child%d' % count)
         os.mkdir(self._threadtmp)
 
     def __del__(self):
         if self._threadtmp and not self._options.keep_tmpdir:
             shutil.rmtree(self._threadtmp, True)
 
-    def run(self, result, refpath):
+    def run(self, result):
         testtmp = os.path.join(self._threadtmp, os.path.basename(self._path))
         os.mkdir(testtmp)
         replacements, port = self._getreplacements(testtmp)
         env = self._getenv(testtmp, port)
         createhgrc(env['HGRCPATH'], self._options)
 
         starttime = time.time()
 
@@ -582,26 +593,17 @@  class Test(object):
             updateduration()
             result.interrupted = True
         except Exception, e:
             updateduration()
             result.exception = e
 
         killdaemons(env['DAEMON_PIDS'])
 
-        # If we're not in --debug mode and reference output file exists,
-        # check test output against it.
-        if self._options.debug:
-            result.refout = None # to match "out is None"
-        elif os.path.exists(refpath):
-            f = open(refpath, 'r')
-            result.refout = f.read().splitlines(True)
-            f.close()
-        else:
-            result.refout = []
+        result.refout = self._refout
 
         if not self._options.keep_tmpdir:
             shutil.rmtree(testtmp)
 
     def _run(self, testtmp, replacements, env):
         raise NotImplemented('Subclasses must implement Test.run()')
 
     def _getreplacements(self, testtmp):
@@ -1077,19 +1079,19 @@  def runone(options, test, count):
     else:
         return skip("unknown test type")
 
     vlog("# Test", test)
 
     if os.path.exists(err):
         os.remove(err)       # Remove any previous output files
 
-    t = runner(testpath, options, count)
+    t = runner(testpath, options, count, ref)
     res = TestResult()
-    t.run(res, ref)
+    t.run(res)
     del t # For cleanup side-effects.
 
     if res.interrupted:
         log('INTERRUPTED: %s (after %d seconds)' % (test, res.duration))
         raise KeyboardInterrupt()
 
     if res.exception:
         return fail('Exception during execution: %s' % res.exception, 255)