changeset 8094:60a9e3cf0cf4

run-tests: factor out main(); reduce use of globals a bit.
author Greg Ward <greg-hg@gerg.ca>
date Mon, 20 Apr 2009 21:42:20 -0400
parents 70d8f70264c4
children f5428d4ffd97
files tests/run-tests.py
diffstat 1 files changed, 63 insertions(+), 59 deletions(-) [+]
line wrap: on
line diff
--- a/tests/run-tests.py	Mon Apr 20 21:10:39 2009 -0400
+++ b/tests/run-tests.py	Mon Apr 20 21:42:20 2009 -0400
@@ -183,7 +183,7 @@
         else:
             print "WARNING: Did not find prerequisite tool: "+p
 
-def cleanup_exit():
+def cleanup_exit(options):
     if not options.keep_tmpdir:
         if verbose:
             print "# Cleaning up HGTMP", HGTMP
@@ -206,7 +206,7 @@
         shutil.copyfile(sys.executable, my_python)
         shutil.copymode(sys.executable, my_python)
 
-def install_hg():
+def install_hg(options):
     global python
     vlog("# Performing temporary installation of HG")
     installerrs = os.path.join("tests", "install.err")
@@ -281,7 +281,7 @@
     hgpath.close()
     return path
 
-def output_coverage():
+def output_coverage(options):
     vlog("# Producing coverage report")
     omit = [BINDIR, TESTDIR, PYTHONDIR]
     if not options.cover_stdlib:
@@ -339,7 +339,7 @@
                        % options.timeout)
     return ret, splitnewlines(output)
 
-def run_one(test, skips, fails):
+def run_one(options, test, skips, fails):
     '''tristate output:
     None -> skipped
     True -> passed
@@ -496,7 +496,7 @@
         return None
     return ret == 0
 
-def run_children(tests):
+def run_children(options, expecthg, tests):
     if not options.with_hg:
         install_hg()
         if hgpkg != expecthg:
@@ -561,14 +561,14 @@
         tested, skipped, failed)
     sys.exit(failures != 0)
 
-def run_tests(tests):
+def run_tests(options, expecthg, tests):
     global DAEMON_PIDS, HGRCPATH
     DAEMON_PIDS = os.environ["DAEMON_PIDS"] = os.path.join(HGTMP, 'daemon.pids')
     HGRCPATH = os.environ["HGRCPATH"] = os.path.join(HGTMP, '.hgrc')
 
     try:
         if not options.with_hg:
-            install_hg()
+            install_hg(options)
 
             if hgpkg != expecthg:
                 print '# Testing unexpected mercurial: %s' % hgpkg
@@ -602,7 +602,7 @@
             if options.retest and not os.path.exists(test + ".err"):
                 skipped += 1
                 continue
-            ret = run_one(test, skips, fails)
+            ret = run_one(options, test, skips, fails)
             if ret is None:
                 skipped += 1
             elif not ret:
@@ -639,7 +639,7 @@
                 tested, skipped, failed)
 
         if coverage:
-            output_coverage()
+            output_coverage(options)
     except KeyboardInterrupt:
         failed = True
         print "\ninterrupted!"
@@ -647,63 +647,67 @@
     if failed:
         sys.exit(1)
 
-(options, args) = parse_args()
-if not options.child:
-    os.umask(022)
+def main():
+    (options, args) = parse_args()
+    if not options.child:
+        os.umask(022)
 
-    check_required_tools()
+        check_required_tools()
 
-# Reset some environment variables to well-known values so that
-# the tests produce repeatable output.
-os.environ['LANG'] = os.environ['LC_ALL'] = 'C'
-os.environ['TZ'] = 'GMT'
-os.environ["EMAIL"] = "Foo Bar <foo.bar@example.com>"
-os.environ['CDPATH'] = ''
+    # Reset some environment variables to well-known values so that
+    # the tests produce repeatable output.
+    os.environ['LANG'] = os.environ['LC_ALL'] = 'C'
+    os.environ['TZ'] = 'GMT'
+    os.environ["EMAIL"] = "Foo Bar <foo.bar@example.com>"
+    os.environ['CDPATH'] = ''
 
-TESTDIR = os.environ["TESTDIR"] = os.getcwd()
-HGTMP = os.environ['HGTMP'] = os.path.realpath(tempfile.mkdtemp('', 'hgtests.',
-                                               options.tmpdir))
-DAEMON_PIDS = None
-HGRCPATH = None
+    global TESTDIR, HGTMP, INST, BINDIR, PYTHONDIR, COVERAGE_FILE
+    TESTDIR = os.environ["TESTDIR"] = os.getcwd()
+    HGTMP = os.environ['HGTMP'] = os.path.realpath(tempfile.mkdtemp('', 'hgtests.',
+                                                   options.tmpdir))
+    DAEMON_PIDS = None
+    HGRCPATH = None
 
-os.environ["HGEDITOR"] = sys.executable + ' -c "import sys; sys.exit(0)"'
-os.environ["HGMERGE"] = "internal:merge"
-os.environ["HGUSER"]   = "test"
-os.environ["HGENCODING"] = "ascii"
-os.environ["HGENCODINGMODE"] = "strict"
-os.environ["HGPORT"] = str(options.port)
-os.environ["HGPORT1"] = str(options.port + 1)
-os.environ["HGPORT2"] = str(options.port + 2)
+    os.environ["HGEDITOR"] = sys.executable + ' -c "import sys; sys.exit(0)"'
+    os.environ["HGMERGE"] = "internal:merge"
+    os.environ["HGUSER"]   = "test"
+    os.environ["HGENCODING"] = "ascii"
+    os.environ["HGENCODINGMODE"] = "strict"
+    os.environ["HGPORT"] = str(options.port)
+    os.environ["HGPORT1"] = str(options.port + 1)
+    os.environ["HGPORT2"] = str(options.port + 2)
 
-if options.with_hg:
-    INST = options.with_hg
-else:
-    INST = os.path.join(HGTMP, "install")
-BINDIR = os.environ["BINDIR"] = os.path.join(INST, "bin")
-PYTHONDIR = os.path.join(INST, "lib", "python")
-COVERAGE_FILE = os.path.join(TESTDIR, ".coverage")
+    if options.with_hg:
+        INST = options.with_hg
+    else:
+        INST = os.path.join(HGTMP, "install")
+    BINDIR = os.environ["BINDIR"] = os.path.join(INST, "bin")
+    PYTHONDIR = os.path.join(INST, "lib", "python")
+    COVERAGE_FILE = os.path.join(TESTDIR, ".coverage")
 
-expecthg = os.path.join(HGTMP, 'install', 'lib', 'python', 'mercurial')
-hgpkg = None
+    expecthg = os.path.join(HGTMP, 'install', 'lib', 'python', 'mercurial')
+    hgpkg = None
 
-if len(args) == 0:
-    args = os.listdir(".")
-    args.sort()
+    if len(args) == 0:
+        args = os.listdir(".")
+        args.sort()
 
-tests = []
-for test in args:
-    if (test.startswith("test-") and '~' not in test and
-        ('.' not in test or test.endswith('.py') or
-         test.endswith('.bat'))):
-        tests.append(test)
+    tests = []
+    for test in args:
+        if (test.startswith("test-") and '~' not in test and
+            ('.' not in test or test.endswith('.py') or
+             test.endswith('.bat'))):
+            tests.append(test)
+
+    vlog("# Using TESTDIR", TESTDIR)
+    vlog("# Using HGTMP", HGTMP)
 
-vlog("# Using TESTDIR", TESTDIR)
-vlog("# Using HGTMP", HGTMP)
+    try:
+        if len(tests) > 1 and options.jobs > 1:
+            run_children(options, expecthg, tests)
+        else:
+            run_tests(options, expecthg, tests)
+    finally:
+        cleanup_exit(options)
 
-try:
-    if len(tests) > 1 and options.jobs > 1:
-        run_children(tests)
-    else:
-        run_tests(tests)
-finally:
-    cleanup_exit()
+main()