[PATCH 054 of 179 tests-refactor] run-tests: add options to runner

Gregory Szorc gregory.szorc at gmail.com
Fri May 2 13:38:11 CDT 2014


# HG changeset patch
# User Gregory Szorc <gregory.szorc at gmail.com>
# Date 1397975624 25200
#      Sat Apr 19 23:33:44 2014 -0700
# Branch stable
# Node ID 0e44bd11854a7a0842de2649cf83a2443ac062d4
# Parent  608bcc158f377ff9a34af05433b3cc75466ed609
run-tests: add options to runner

diff --git a/tests/run-tests.py b/tests/run-tests.py
--- a/tests/run-tests.py
+++ b/tests/run-tests.py
@@ -381,25 +381,27 @@ def terminate(proc):
         getattr(proc, 'terminate', lambda : os.kill(proc.pid, signal.SIGTERM))()
     except OSError:
         pass
 
 def killdaemons(pidfile):
     return killmod.killdaemons(pidfile, tryhard=False, remove=True,
                                logfn=vlog)
 
-def cleanup(runner, options):
-    if not options.keep_tmpdir:
-        vlog("# Cleaning up HGTMP", runner.hgtmp)
-        shutil.rmtree(runner.hgtmp, True)
-        for f in createdfiles:
-            try:
-                os.remove(f)
-            except OSError:
-                pass
+def cleanup(runner):
+    if runner.options.keep_tmpdir:
+        return
+
+    vlog("# Cleaning up HGTMP", runner.hgtmp)
+    shutil.rmtree(runner.hgtmp, True)
+    for f in createdfiles:
+        try:
+            os.remove(f)
+        except OSError:
+            pass
 
 def usecorrectpython(runner):
     # some tests run python interpreter. they must use same
     # interpreter we use or bad things will happen.
     pyexename = sys.platform == 'win32' and 'python.exe' or 'python'
     if getattr(os, 'symlink', None):
         vlog("# Making python executable in test path a symlink to '%s'" %
              sys.executable)
@@ -425,23 +427,23 @@ def usecorrectpython(runner):
              (exename, pyexename, exedir))
         path = os.environ['PATH'].split(os.pathsep)
         while exedir in path:
             path.remove(exedir)
         os.environ['PATH'] = os.pathsep.join([exedir] + path)
         if not findprogram(pyexename):
             print "WARNING: Cannot find %s in search path" % pyexename
 
-def installhg(runner, options):
+def installhg(runner):
     vlog("# Performing temporary installation of HG")
     installerrs = os.path.join("tests", "install.err")
     compiler = ''
-    if options.compiler:
-        compiler = '--compiler ' + options.compiler
-    pure = options.pure and "--pure" or ""
+    if runner.options.compiler:
+        compiler = '--compiler ' + runner.options.compiler
+    pure = runner.options.pure and "--pure" or ""
     py3 = ''
     if sys.version_info[0] == 3:
         py3 = '--c2to3'
 
     # Run installer in hg root
     script = os.path.realpath(sys.argv[0])
     hgroot = os.path.dirname(os.path.dirname(script))
     os.chdir(hgroot)
@@ -458,29 +460,29 @@ def installhg(runner, options):
            ' --install-scripts="%(bindir)s" %(nohome)s >%(logfile)s 2>&1'
            % {'exe': sys.executable, 'py3': py3, 'pure': pure,
               'compiler': compiler, 'base': os.path.join(runner.hgtmp, "build"),
               'prefix': runner.inst, 'libdir': runner.pythondir,
               'bindir': runner.bindir,
               'nohome': nohome, 'logfile': installerrs})
     vlog("# Running", cmd)
     if os.system(cmd) == 0:
-        if not options.verbose:
+        if not runner.options.verbose:
             os.remove(installerrs)
     else:
         f = open(installerrs)
         for line in f:
             print line,
         f.close()
         sys.exit(1)
     os.chdir(runner.testdir)
 
     usecorrectpython(runner)
 
-    if options.py3k_warnings and not options.anycoverage:
+    if runner.options.py3k_warnings and not runner.options.anycoverage:
         vlog("# Updating hg command to enable Py3k Warnings switch")
         f = open(os.path.join(runner.bindir, 'hg'), 'r')
         lines = [line.rstrip() for line in f]
         lines[0] += ' -3'
         f.close()
         f = open(os.path.join(runner.bindir, 'hg'), 'w')
         for line in lines:
             f.write(line + '\n')
@@ -497,17 +499,17 @@ def installhg(runner, options):
             data = data.replace('"%~dp0..\python" "%~dp0hg" %*',
                                 '"%~dp0python" "%~dp0hg" %*')
             f = open(hgbat, 'wb')
             f.write(data)
             f.close()
         else:
             print 'WARNING: cannot fix hg.bat reference to python.exe'
 
-    if options.anycoverage:
+    if runner.options.anycoverage:
         custom = os.path.join(runner.testdir, 'sitecustomize.py')
         target = os.path.join(runner.pythondir, 'sitecustomize.py')
         vlog('# Installing coverage trigger to %s' % target)
         shutil.copyfile(custom, target)
         rc = os.path.join(runner.testdir, '.coveragerc')
         vlog('# Installing coverage rc to %s' % rc)
         os.environ['COVERAGE_PROCESS_START'] = rc
         fn = os.path.join(runner.inst, '..', '.coverage')
@@ -516,62 +518,62 @@ def installhg(runner, options):
 def outputtimes(options):
     vlog('# Producing time report')
     times.sort(key=lambda t: (t[1], t[0]), reverse=True)
     cols = '%7.3f   %s'
     print '\n%-7s   %s' % ('Time', 'Test')
     for test, timetaken in times:
         print cols % (timetaken, test)
 
-def outputcoverage(runner, options):
+def outputcoverage(runner):
 
     vlog('# Producing coverage report')
     os.chdir(runner.pythondir)
 
     def covrun(*args):
         cmd = 'coverage %s' % ' '.join(args)
         vlog('# Running: %s' % cmd)
         os.system(cmd)
 
     covrun('-c')
     omit = ','.join(os.path.join(x, '*') for x in
                     [runner.bindir, runner.testdir])
     covrun('-i', '-r', '"--omit=%s"' % omit) # report
-    if options.htmlcov:
+    if runner.options.htmlcov:
         htmldir = os.path.join(runner.testdir, 'htmlcov')
         covrun('-i', '-b', '"--directory=%s"' % htmldir, '"--omit=%s"' % omit)
-    if options.annotate:
+    if runner.options.annotate:
         adir = os.path.join(runner.testdir, 'annotated')
         if not os.path.isdir(adir):
             os.mkdir(adir)
         covrun('-i', '-a', '"--directory=%s"' % adir, '"--omit=%s"' % omit)
 
 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, runner, test, options, count, refpath):
+    def __init__(self, runner, test, count, refpath):
         path = os.path.join(runner.testdir, test)
         errpath = os.path.join(runner.testdir, '%s.err' % test)
 
         self._testdir = runner.testdir
         self._test = test
         self._path = path
-        self._options = options
+        self._options = runner.options
         self._count = count
         self._daemonpids = []
         self._refpath = refpath
         self._errpath = errpath
 
         # If we're not in --debug mode and reference output file exists,
         # check test output against it.
-        if options.debug:
+        if runner.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 = []
 
@@ -1080,17 +1082,17 @@ 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(runner, test, options, count):
+def gettest(runner, test, 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(runner.testdir, test)
@@ -1098,17 +1100,17 @@ def gettest(runner, test, options, count
     testcls = Test
 
     for ext, cls, out in testtypes:
         if lctest.endswith(ext):
             testcls = cls
             refpath = os.path.join(runner.testdir, test + out)
             break
 
-    return testcls(runner, test, options, count, refpath)
+    return testcls(runner, test, 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)
@@ -1178,100 +1180,100 @@ def _checkhglib(runner, verb):
                          '         (expected %s)\n'
                          % (verb, actualhg, expecthg))
 
 results = {'.':[], '!':[], '~': [], 's':[], 'i':[]}
 times = []
 iolock = threading.Lock()
 abort = False
 
-def scheduletests(runner, options, tests):
-    jobs = options.jobs
+def scheduletests(runner, tests):
+    jobs = runner.options.jobs
     done = queue.Queue()
     running = 0
     count = 0
     global abort
 
     def job(test, count):
         try:
-            t = gettest(runner, test, options, count)
+            t = gettest(runner, test, 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:
             if not done.empty() or running == jobs or not tests:
                 try:
                     code, test, msg = done.get(True, 1)
                     results[code].append((test, msg))
-                    if options.first and code not in '.si':
+                    if runner.options.first and code not in '.si':
                         break
                 except queue.Empty:
                     continue
                 running -= 1
             if tests and not running == jobs:
                 test = tests.pop(0)
-                if options.loop:
+                if runner.options.loop:
                     tests.append(test)
                 t = threading.Thread(target=job, name=test, args=(test, count))
                 t.start()
                 running += 1
                 count += 1
     except KeyboardInterrupt:
         abort = True
 
-def runtests(runner, options, tests):
+def runtests(runner, tests):
     try:
         if runner.inst:
-            installhg(runner, options)
+            installhg(runner)
             _checkhglib(runner, "Testing")
         else:
             usecorrectpython(runner)
 
-        if options.restart:
+        if runner.options.restart:
             orig = list(tests)
             while tests:
                 if os.path.exists(tests[0] + ".err"):
                     break
                 tests.pop(0)
             if not tests:
                 print "running all tests"
                 tests = orig
 
-        scheduletests(runner, options, tests)
+        scheduletests(runner, tests)
 
         failed = len(results['!'])
         warned = len(results['~'])
         tested = len(results['.']) + failed + warned
         skipped = len(results['s'])
         ignored = len(results['i'])
 
         print
-        if not options.noskips:
+        if not runner.options.noskips:
             for s in results['s']:
                 print "Skipped %s: %s" % s
         for s in results['~']:
             print "Warned %s: %s" % s
         for s in results['!']:
             print "Failed %s: %s" % s
         _checkhglib(runner, "Tested")
         print "# Ran %d tests, %d skipped, %d warned, %d failed." % (
             tested, skipped + ignored, warned, failed)
         if results['!']:
             print 'python hash seed:', os.environ['PYTHONHASHSEED']
-        if options.time:
-            outputtimes(options)
+        if runner.options.time:
+            outputtimes(runner.options)
 
-        if options.anycoverage:
-            outputcoverage(runner, options)
+        if runner.options.anycoverage:
+            outputcoverage(runner)
     except KeyboardInterrupt:
         failed = True
         print "\ninterrupted!"
 
     if failed:
         return 1
     if warned:
         return 80
@@ -1412,15 +1414,15 @@ def main(args, parser=None):
     runner.coveragefile = os.path.join(runner.testdir, ".coverage")
 
     vlog("# Using TESTDIR", runner.testdir)
     vlog("# Using HGTMP", runner.hgtmp)
     vlog("# Using PATH", os.environ["PATH"])
     vlog("# Using", IMPL_PATH, os.environ[IMPL_PATH])
 
     try:
-        return runtests(runner, options, tests) or 0
+        return runtests(runner, tests) or 0
     finally:
         time.sleep(.1)
-        cleanup(runner, options)
+        cleanup(runner)
 
 if __name__ == '__main__':
     sys.exit(main(sys.argv[1:]))


More information about the Mercurial-devel mailing list