changeset 21454:046587aa1c8a

run-tests: refactor testtmp testtmp is now a member variable of our test class. It's value is computed during instance creation and the directory is managed via the lifetime of the test object.
author Gregory Szorc <gregory.szorc@gmail.com>
date Sun, 20 Apr 2014 15:40:22 -0700
parents aaf52b78327e
children 0f0bace82149
files tests/run-tests.py
diffstat 1 files changed, 29 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/tests/run-tests.py	Sun Apr 20 15:19:21 2014 -0700
+++ b/tests/run-tests.py	Sun Apr 20 15:40:22 2014 -0700
@@ -362,6 +362,7 @@
         self._duration = None
         self._result = None
         self._skipped = None
+        self._testtmp = None
 
         # If we're not in --debug mode and reference output file exists,
         # check test output against it.
@@ -393,6 +394,10 @@
         self._result = None
         self._skipped = None
 
+        self._testtmp = os.path.join(self._threadtmp,
+                                     os.path.basename(self._path))
+        os.mkdir(self._testtmp)
+
     def run(self):
         """Run this test instance.
 
@@ -429,10 +434,8 @@
         if os.path.exists(self._errpath):
             os.remove(self._errpath)
 
-        testtmp = os.path.join(self._threadtmp, os.path.basename(self._path))
-        os.mkdir(testtmp)
-        replacements, port = self._getreplacements(testtmp)
-        env = self._getenv(testtmp, port)
+        replacements, port = self._getreplacements()
+        env = self._getenv(port)
         self._daemonpids.append(env['DAEMON_PIDS'])
         self._createhgrc(env['HGRCPATH'])
 
@@ -440,7 +443,7 @@
 
         starttime = time.time()
         try:
-            ret, out = self._run(testtmp, replacements, env)
+            ret, out = self._run(replacements, env)
             self._duration = time.time() - starttime
             self._finished = True
             self._ret = ret
@@ -455,9 +458,6 @@
 
         killdaemons(env['DAEMON_PIDS'])
 
-        if not options.keep_tmpdir:
-            shutil.rmtree(testtmp)
-
         def describe(ret):
             if ret < 0:
                 return 'killed by signal: %d' % -ret
@@ -529,6 +529,9 @@
 
     def tearDown(self):
         """Tasks to perform after run()."""
+        if not self._options.keep_tmpdir:
+            shutil.rmtree(self._testtmp)
+
         vlog("# Ret was:", self._ret)
 
         # Don't print progress in unittest mode because that is handled
@@ -541,11 +544,11 @@
 
         self._runner.times.append((self.name, self._duration))
 
-    def _run(self, testtmp, replacements, env):
+    def _run(self, replacements, env):
         # This should be implemented in child classes to run tests.
         return self._skip('unknown test type')
 
-    def _getreplacements(self, testtmp):
+    def _getreplacements(self):
         port = self._options.port + self._count * 3
         r = [
             (r':%s\b' % port, ':$HGPORT'),
@@ -557,16 +560,16 @@
             r.append(
                 (''.join(c.isalpha() and '[%s%s]' % (c.lower(), c.upper()) or
                     c in '/\\' and r'[/\\]' or c.isdigit() and c or '\\' + c
-                    for c in testtmp), '$TESTTMP'))
+                    for c in self._testtmp), '$TESTTMP'))
         else:
-            r.append((re.escape(testtmp), '$TESTTMP'))
+            r.append((re.escape(self._testtmp), '$TESTTMP'))
 
         return r, port
 
-    def _getenv(self, testtmp, port):
+    def _getenv(self, port):
         env = os.environ.copy()
-        env['TESTTMP'] = testtmp
-        env['HOME'] = testtmp
+        env['TESTTMP'] = self._testtmp
+        env['HOME'] = self._testtmp
         env["HGPORT"] = str(port)
         env["HGPORT1"] = str(port + 1)
         env["HGPORT2"] = str(port + 2)
@@ -666,13 +669,13 @@
 
 class PythonTest(Test):
     """A Python-based test."""
-    def _run(self, testtmp, replacements, env):
+    def _run(self, replacements, env):
         py3kswitch = self._options.py3k_warnings and ' -3' or ''
         cmd = '%s%s "%s"' % (PYTHON, py3kswitch, self._path)
         vlog("# Running", cmd)
         if os.name == 'nt':
             replacements.append((r'\r\n', '\n'))
-        return run(cmd, testtmp, self._options, replacements, env,
+        return run(cmd, self._testtmp, self._options, replacements, env,
                    self._runner.abort)
 
 class TTest(Test):
@@ -686,15 +689,15 @@
     ESCAPEMAP = dict((chr(i), r'\x%02x' % i) for i in range(256)).update(
                      {'\\': '\\\\', '\r': r'\r'})
 
-    def _run(self, testtmp, replacements, env):
+    def _run(self, replacements, env):
         f = open(self._path)
         lines = f.readlines()
         f.close()
 
-        salt, script, after, expected = self._parsetest(lines, testtmp)
+        salt, script, after, expected = self._parsetest(lines)
 
         # Write out the generated script.
-        fname = '%s.sh' % testtmp
+        fname = '%s.sh' % self._testtmp
         f = open(fname, 'w')
         for l in script:
             f.write(l)
@@ -703,8 +706,8 @@
         cmd = '%s "%s"' % (self._options.shell, fname)
         vlog("# Running", cmd)
 
-        exitcode, output = run(cmd, testtmp, self._options, replacements, env,
-                               self._runner.abort)
+        exitcode, output = run(cmd, self._testtmp, self._options, replacements,
+                               env, self._runner.abort)
         # Do not merge output if skipped. Return hghave message instead.
         # Similarly, with --debug, output is None.
         if exitcode == self.SKIPPED_STATUS or output is None:
@@ -712,12 +715,12 @@
 
         return self._processoutput(exitcode, output, salt, after, expected)
 
-    def _hghave(self, reqs, testtmp):
+    def _hghave(self, reqs):
         # TODO do something smarter when all other uses of hghave are gone.
         tdir = self._testdir.replace('\\', '/')
         proc = Popen4('%s -c "%s/hghave %s"' %
                       (self._options.shell, tdir, ' '.join(reqs)),
-                      testtmp, 0)
+                      self._testtmp, 0)
         stdout, stderr = proc.communicate()
         ret = proc.wait()
         if wifexited(ret):
@@ -728,7 +731,7 @@
 
         return ret == 0
 
-    def _parsetest(self, lines, testtmp):
+    def _parsetest(self, lines):
         # We generate a shell script which outputs unique markers to line
         # up script results with our source. These markers include input
         # line number and the last return code.
@@ -773,7 +776,7 @@
                     after.setdefault(pos, []).append('  !!! invalid #if\n')
                 if skipping is not None:
                     after.setdefault(pos, []).append('  !!! nested #if\n')
-                skipping = not self._hghave(lsplit[1:], testtmp)
+                skipping = not self._hghave(lsplit[1:])
                 after.setdefault(pos, []).append(l)
             elif l.startswith('#else'):
                 if skipping is None: