[PATCH 1 of 4] run-tests: Pass arguments into argument parser

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


# HG changeset patch
# User Gregory Szorc <gregory.szorc at gmail.com>
# Date 1395722253 25200
#      Mon Mar 24 21:37:33 2014 -0700
# Node ID b7d99c46e39a0871297c38bbd292a2764fccb1f1
# Parent  3879ac3858ffd9bb46e19fcc3a2b31d7bb2b54c5
run-tests: Pass arguments into argument parser

Before, arguments were not passed into the optparse.OptionParser
instance and were coming from sys.argv. This patch enables consumers to
define the list of arguments to parse without having to adjust sys.argv.

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():
+def parseargs(args):
     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,17 +205,17 @@ def parseargs():
     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)
-    (options, args) = parser.parse_args()
+    (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)
         if not (os.path.isfile(options.with_hg) and
@@ -1171,18 +1171,18 @@ def runtests(options, tests):
     if failed:
         return 1
     if warned:
         return 80
 
 testtypes = [('.py', pytest, '.out'),
              ('.t', tsttest, '')]
 
-def main():
-    (options, args) = parseargs()
+def main(args):
+    (options, args) = parseargs(args)
     os.umask(022)
 
     checktools()
 
     if not args:
         if options.changed:
             proc = Popen4('hg st --rev "%s" -man0 .' % options.changed,
                           None, 0)
@@ -1294,9 +1294,9 @@ def main():
 
     try:
         sys.exit(runtests(options, tests) or 0)
     finally:
         time.sleep(.1)
         cleanup(options)
 
 if __name__ == '__main__':
-    main()
+    main(sys.argv[1:])


More information about the Mercurial-devel mailing list