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

login
register
mail settings
Submitter Gregory Szorc
Date March 25, 2014, 5:12 a.m.
Message ID <9d7b3768e916df9eb4ac.1395724375@77.1.168.192.in-addr.arpa>
Download mbox | patch
Permalink /patch/4058/
State Accepted
Commit 727ec3c9b6131fc200239895478e57967f19ad40
Headers show

Comments

Gregory Szorc - March 25, 2014, 5:12 a.m.
# HG changeset patch
# User Gregory Szorc <gregory.szorc@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.

Patch

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)