[PATCH 3 of 4] run-tests: allow option parser to be extended

Gregory Szorc gregory.szorc at gmail.com
Tue Mar 25 00:12:55 CDT 2014


# HG changeset patch
# User Gregory Szorc <gregory.szorc at gmail.com>
# Date 1395723148 25200
#      Mon Mar 24 21:52:28 2014 -0700
# Node ID 9d7b3768e916df9eb4acbbbce6cb55b63f152b59
# Parent  9c1dd166d28cb1f0dd4d3fa36d4afe150fe7efee
run-tests: allow option parser to be extended

This patch moves the OptionParser population into its own function so
consumers may modify the OptionParser before arguments are evaluated.
This will allow consumers to add custom options, set different defaults,
etc.

diff --git a/tests/run-tests.py b/tests/run-tests.py
--- a/tests/run-tests.py
+++ b/tests/run-tests.py
@@ -128,17 +128,17 @@ def parselistfiles(files, listtype, warn
         for line in f.readlines():
             line = line.split('#', 1)[0].strip()
             if line:
                 entries[line] = filename
 
         f.close()
     return entries
 
-def parseargs(args):
+def getparser():
     parser = optparse.OptionParser("%prog [options] [tests]")
 
     # keep these sorted
     parser.add_option("--blacklist", action="append",
         help="skip tests listed in the specified blacklist file")
     parser.add_option("--whitelist", action="append",
         help="always run tests listed in the specified whitelist file")
     parser.add_option("--changed", type="string",
@@ -205,16 +205,20 @@ def parseargs(args):
     parser.add_option('--extra-config-opt', action="append",
                       help='set the given config opt in the test hgrc')
     parser.add_option('--random', action="store_true",
                       help='run tests in random order')
 
     for option, (envvar, default) in defaults.items():
         defaults[option] = type(default)(os.environ.get(envvar, default))
     parser.set_defaults(**defaults)
+
+    return parser
+
+def parseargs(args, parser):
     (options, args) = parser.parse_args(args)
 
     # jython is always pure
     if 'java' in sys.platform or '__pypy__' in sys.modules:
         options.pure = True
 
     if options.with_hg:
         options.with_hg = os.path.expanduser(options.with_hg)
@@ -1171,18 +1175,19 @@ def runtests(options, tests):
     if failed:
         return 1
     if warned:
         return 80
 
 testtypes = [('.py', pytest, '.out'),
              ('.t', tsttest, '')]
 
-def main(args):
-    (options, args) = parseargs(args)
+def main(args, parser=None):
+    parser = parser or getparser()
+    (options, args) = parseargs(args, parser)
     os.umask(022)
 
     checktools()
 
     if not args:
         if options.changed:
             proc = Popen4('hg st --rev "%s" -man0 .' % options.changed,
                           None, 0)


More information about the Mercurial-devel mailing list