Skip to content

Commit 585db41

Browse files
author
MarcoFalke
committed
Merge #12437: [Trivial] Simplify if-else blocks and more descriptive variable naming
97bcd36 [Trivial] Simplify if-else blocks and more descriptive variable naming (Jeff Rade) Pull request description: Was looking through `test_runner.py` to start work on [#11964](bitcoin/bitcoin#11964). Made a few changes to make the file more readable and keep these separate from future PR. Tree-SHA512: 7508f4ee39672d18718d8f80b61b89918eac7b4c75953682b812b73013f18ebd81adc7953f3b6c98c5c598adeb1998f5455f123b5566d1cc03631c7924b4103a
2 parents af20f9b + 97bcd36 commit 585db41

File tree

1 file changed

+32
-32
lines changed

1 file changed

+32
-32
lines changed

test/functional/test_runner.py

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@
5252
TEST_EXIT_PASSED = 0
5353
TEST_EXIT_SKIPPED = 77
5454

55+
# 20 minutes represented in seconds
56+
TRAVIS_TIMEOUT_DURATION = 20 * 60
57+
5558
BASE_SCRIPTS= [
5659
# Scripts that are run by the travis build process.
5760
# Longest test should go first, to favor running tests in parallel
@@ -236,26 +239,24 @@ def main():
236239
if tests:
237240
# Individual tests have been specified. Run specified tests that exist
238241
# in the ALL_SCRIPTS list. Accept the name with or without .py extension.
239-
tests = [re.sub("\.py$", "", t) + ".py" for t in tests]
242+
tests = [re.sub("\.py$", "", test) + ".py" for test in tests]
240243
test_list = []
241-
for t in tests:
242-
if t in ALL_SCRIPTS:
243-
test_list.append(t)
244+
for test in tests:
245+
if test in ALL_SCRIPTS:
246+
test_list.append(test)
244247
else:
245-
print("{}WARNING!{} Test '{}' not found in full test list.".format(BOLD[1], BOLD[0], t))
248+
print("{}WARNING!{} Test '{}' not found in full test list.".format(BOLD[1], BOLD[0], test))
249+
elif args.extended:
250+
# Include extended tests
251+
test_list = ALL_SCRIPTS
246252
else:
247-
# No individual tests have been specified.
248-
# Run all base tests, and optionally run extended tests.
253+
# Run base tests only
249254
test_list = BASE_SCRIPTS
250-
if args.extended:
251-
# place the EXTENDED_SCRIPTS first since the three longest ones
252-
# are there and the list is shorter
253-
test_list = EXTENDED_SCRIPTS + test_list
254255

255256
# Remove the test cases that the user has explicitly asked to exclude.
256257
if args.exclude:
257-
tests_excl = [re.sub("\.py$", "", t) + ".py" for t in args.exclude.split(',')]
258-
for exclude_test in tests_excl:
258+
exclude_tests = [re.sub("\.py$", "", test) + ".py" for test in args.exclude.split(',')]
259+
for exclude_test in exclude_tests:
259260
if exclude_test in test_list:
260261
test_list.remove(exclude_test)
261262
else:
@@ -320,7 +321,7 @@ def run_tests(test_list, src_dir, build_dir, exeext, tmpdir, jobs=1, enable_cove
320321

321322
#Run Tests
322323
job_queue = TestHandler(jobs, tests_dir, tmpdir, test_list, flags)
323-
time0 = time.time()
324+
start_time = time.time()
324325
test_results = []
325326

326327
max_len_name = len(max(test_list, key=len))
@@ -346,7 +347,7 @@ def run_tests(test_list, src_dir, build_dir, exeext, tmpdir, jobs=1, enable_cove
346347
combined_logs, _ = subprocess.Popen([sys.executable, os.path.join(tests_dir, 'combine_logs.py'), '-c', testdir], universal_newlines=True, stdout=subprocess.PIPE).communicate()
347348
print("\n".join(deque(combined_logs.splitlines(), combined_logs_len)))
348349

349-
print_results(test_results, max_len_name, (int(time.time() - time0)))
350+
print_results(test_results, max_len_name, (int(time.time() - start_time)))
350351

351352
if coverage:
352353
coverage.report_rpc_coverage()
@@ -403,15 +404,15 @@ def get_next(self):
403404
while self.num_running < self.num_jobs and self.test_list:
404405
# Add tests
405406
self.num_running += 1
406-
t = self.test_list.pop(0)
407+
test = self.test_list.pop(0)
407408
portseed = len(self.test_list) + self.portseed_offset
408409
portseed_arg = ["--portseed={}".format(portseed)]
409410
log_stdout = tempfile.SpooledTemporaryFile(max_size=2**16)
410411
log_stderr = tempfile.SpooledTemporaryFile(max_size=2**16)
411-
test_argv = t.split()
412+
test_argv = test.split()
412413
testdir = "{}/{}_{}".format(self.tmpdir, re.sub(".py$", "", test_argv[0]), portseed)
413414
tmpdir_arg = ["--tmpdir={}".format(testdir)]
414-
self.jobs.append((t,
415+
self.jobs.append((test,
415416
time.time(),
416417
subprocess.Popen([sys.executable, self.tests_dir + test_argv[0]] + test_argv[1:] + self.flags + portseed_arg + tmpdir_arg,
417418
universal_newlines=True,
@@ -425,15 +426,14 @@ def get_next(self):
425426
while True:
426427
# Return first proc that finishes
427428
time.sleep(.5)
428-
for j in self.jobs:
429-
(name, time0, proc, testdir, log_out, log_err) = j
430-
if os.getenv('TRAVIS') == 'true' and int(time.time() - time0) > 20 * 60:
431-
# In travis, timeout individual tests after 20 minutes (to stop tests hanging and not
432-
# providing useful output.
429+
for job in self.jobs:
430+
(name, start_time, proc, testdir, log_out, log_err) = job
431+
if os.getenv('TRAVIS') == 'true' and int(time.time() - start_time) > TRAVIS_TIMEOUT_DURATION:
432+
# In travis, timeout individual tests (to stop tests hanging and not providing useful output).
433433
proc.send_signal(signal.SIGINT)
434434
if proc.poll() is not None:
435435
log_out.seek(0), log_err.seek(0)
436-
[stdout, stderr] = [l.read().decode('utf-8') for l in (log_out, log_err)]
436+
[stdout, stderr] = [file.read().decode('utf-8') for file in (log_out, log_err)]
437437
log_out.close(), log_err.close()
438438
if proc.returncode == TEST_EXIT_PASSED and stderr == "":
439439
status = "Passed"
@@ -442,9 +442,9 @@ def get_next(self):
442442
else:
443443
status = "Failed"
444444
self.num_running -= 1
445-
self.jobs.remove(j)
445+
self.jobs.remove(job)
446446

447-
return TestResult(name, status, int(time.time() - time0)), testdir, stdout, stderr
447+
return TestResult(name, status, int(time.time() - start_time)), testdir, stdout, stderr
448448
print('.', end='', flush=True)
449449

450450
class TestResult():
@@ -490,7 +490,7 @@ def check_script_list(src_dir):
490490
Check that there are no scripts in the functional tests directory which are
491491
not being run by pull-tester.py."""
492492
script_dir = src_dir + '/test/functional/'
493-
python_files = set([t for t in os.listdir(script_dir) if t[-3:] == ".py"])
493+
python_files = set([file for file in os.listdir(script_dir) if file.endswith(".py")])
494494
missed_tests = list(python_files - set(map(lambda x: x.split()[0], ALL_SCRIPTS + NON_SCRIPTS)))
495495
if len(missed_tests) != 0:
496496
print("%sWARNING!%s The following scripts are not being run: %s. Check the test lists in test_runner.py." % (BOLD[1], BOLD[0], str(missed_tests)))
@@ -526,7 +526,7 @@ def report_rpc_coverage(self):
526526

527527
if uncovered:
528528
print("Uncovered RPC commands:")
529-
print("".join((" - %s\n" % i) for i in sorted(uncovered)))
529+
print("".join((" - %s\n" % command) for command in sorted(uncovered)))
530530
else:
531531
print("All RPC commands covered.")
532532

@@ -550,17 +550,17 @@ def _get_uncovered_rpc_commands(self):
550550
if not os.path.isfile(coverage_ref_filename):
551551
raise RuntimeError("No coverage reference found")
552552

553-
with open(coverage_ref_filename, 'r') as f:
554-
all_cmds.update([i.strip() for i in f.readlines()])
553+
with open(coverage_ref_filename, 'r') as file:
554+
all_cmds.update([line.strip() for line in file.readlines()])
555555

556556
for root, dirs, files in os.walk(self.dir):
557557
for filename in files:
558558
if filename.startswith(coverage_file_prefix):
559559
coverage_filenames.add(os.path.join(root, filename))
560560

561561
for filename in coverage_filenames:
562-
with open(filename, 'r') as f:
563-
covered_cmds.update([i.strip() for i in f.readlines()])
562+
with open(filename, 'r') as file:
563+
covered_cmds.update([line.strip() for line in file.readlines()])
564564

565565
return all_cmds - covered_cmds
566566

0 commit comments

Comments
 (0)