Skip to content

Commit 91bffff

Browse files
committed
Use argparse in rpc_tests.py
This commit replaces the roll-your-own argument parsing in rpc_tests.py with Python's standard library argparse.
1 parent 1581ecb commit 91bffff

File tree

1 file changed

+44
-39
lines changed

1 file changed

+44
-39
lines changed

qa/pull-tester/rpc-tests.py

Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
2222
"""
2323

24+
import argparse
2425
import configparser
2526
import os
2627
import time
@@ -30,6 +31,19 @@
3031
import tempfile
3132
import re
3233

34+
# Parse arguments and pass through unrecognised args
35+
parser = argparse.ArgumentParser(add_help=False)
36+
parser.add_argument('--coverage', action='store_true')
37+
parser.add_argument('-extended', action='store_true')
38+
parser.add_argument('--help', '-h', '-?', action='store_true')
39+
parser.add_argument('--parallel', type=int, default=4)
40+
parser.add_argument('-win', action='store_true')
41+
(args, unknown_args) = parser.parse_known_args()
42+
43+
#Create a set to store arguments and create the passon string
44+
tests = set(arg for arg in unknown_args if arg[:2] != "--")
45+
passon_args = [arg for arg in unknown_args if arg[:2] == "--"]
46+
3347
BOLD = ("","")
3448
if os.name == 'posix':
3549
# primitive formatting on supported
@@ -47,35 +61,14 @@
4761

4862
RPC_TESTS_DIR = config["environment"]["SRCDIR"] + '/qa/rpc-tests/'
4963

50-
ENABLE_COVERAGE=0
51-
52-
#Create a set to store arguments and create the passon string
53-
opts = set()
54-
passon_args = []
55-
PASSON_REGEX = re.compile("^--")
56-
PARALLEL_REGEX = re.compile('^-parallel=')
57-
58-
print_help = False
59-
run_parallel = 4
60-
61-
for arg in sys.argv[1:]:
62-
if arg == "--help" or arg == "-h" or arg == "-?":
63-
print_help = True
64-
break
65-
if arg == '--coverage':
66-
ENABLE_COVERAGE = 1
67-
elif PASSON_REGEX.match(arg):
68-
passon_args.append(arg)
69-
elif PARALLEL_REGEX.match(arg):
70-
run_parallel = int(arg.split(sep='=', maxsplit=1)[1])
71-
else:
72-
opts.add(arg)
64+
print_help = args.help
65+
run_parallel = args.parallel
7366

7467
#Set env vars
7568
if "BITCOIND" not in os.environ:
7669
os.environ["BITCOIND"] = config["environment"]["BUILDDIR"] + '/src/bitcoind' + config["environment"]["EXEEXT"]
7770

78-
if config["environment"]["EXEEXT"] == ".exe" and "-win" not in opts:
71+
if config["environment"]["EXEEXT"] == ".exe" and not args.win:
7972
# https://github.com/bitcoin/bitcoin/commit/d52802551752140cf41f0d9a225a43e84404d3e9
8073
# https://github.com/bitcoin/bitcoin/pull/5677#issuecomment-136646964
8174
print("Win tests currently disabled by default. Use -win option to enable")
@@ -95,7 +88,7 @@
9588
# ENABLE_ZMQ=0
9689
raise
9790

98-
testScripts = [
91+
BASE_SCRIPTS= [
9992
# longest test should go first, to favor running tests in parallel
10093
'wallet-hd.py',
10194
'walletbackup.py',
@@ -152,10 +145,9 @@
152145
'rpcnamedargs.py',
153146
'listsinceblock.py',
154147
]
155-
if ENABLE_ZMQ:
156-
testScripts.append('zmq_test.py')
148+
ZMQ_SCRIPTS = ["zmq_test.py"]
157149

158-
testScriptsExt = [
150+
EXTENDED_SCRIPTS = [
159151
'pruning.py',
160152
# vv Tests less than 20m vv
161153
'smartfees.py',
@@ -184,26 +176,39 @@
184176
'replace-by-fee.py',
185177
]
186178

179+
ALL_SCRIPTS = BASE_SCRIPTS + ZMQ_SCRIPTS + EXTENDED_SCRIPTS
187180

188181
def runtests():
189-
test_list = []
190-
if '-extended' in opts:
191-
test_list = testScripts + testScriptsExt
192-
elif len(opts) == 0 or (len(opts) == 1 and "-win" in opts):
193-
test_list = testScripts
182+
# Build list of tests
183+
if len(tests) != 0:
184+
# Individual tests have been specified. Run specified tests that exist
185+
# in the ALL_SCRIPTS list. Accept the name with or without .py extension.
186+
test_list = [t for t in ALL_SCRIPTS if
187+
(t in tests or re.sub(".py$", "", t) in tests)]
188+
if len(test_list) == 0:
189+
print("No valid test scripts specified. Check that your test is in one "
190+
"of the test lists in rpc-tests.py or run rpc-tests.py with no arguments to run all tests")
191+
sys.exit(0)
194192
else:
195-
for t in testScripts + testScriptsExt:
196-
if t in opts or re.sub(".py$", "", t) in opts:
197-
test_list.append(t)
198-
199-
if print_help:
193+
# No individual tests have been specified. Run base tests, and
194+
# optionally ZMQ tests and extended tests.
195+
test_list = BASE_SCRIPTS
196+
if ENABLE_ZMQ:
197+
test_list += ZMQ_SCRIPTS
198+
if args.extended:
199+
test_list += EXTENDED_SCRIPTS
200+
# TODO: BASE_SCRIPTS and EXTENDED_SCRIPTS are sorted by runtime
201+
# (for parallel running efficiency). This combined list will is no
202+
# longer sorted.
203+
204+
if args.help:
200205
# Only print help of the first script and exit
201206
subprocess.check_call((RPC_TESTS_DIR + test_list[0]).split() + ['-h'])
202207
sys.exit(0)
203208

204209
coverage = None
205210

206-
if ENABLE_COVERAGE:
211+
if args.coverage:
207212
coverage = RPCCoverage()
208213
print("Initializing coverage directory at %s\n" % coverage.dir)
209214
flags = ["--srcdir=%s/src" % config["environment"]["BUILDDIR"]] + passon_args

0 commit comments

Comments
 (0)