Merge #12437: [Trivial] Simplify if-else blocks and more descriptive variable naming

97bcd36811 [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](https://github.com/bitcoin/bitcoin/issues/11964).  Made a few changes to make the file more readable and keep these separate from future PR.

Tree-SHA512: 7508f4ee39672d18718d8f80b61b89918eac7b4c75953682b812b73013f18ebd81adc7953f3b6c98c5c598adeb1998f5455f123b5566d1cc03631c7924b4103a
This commit is contained in:
MarcoFalke 2018-03-17 11:05:37 -04:00
commit 585db41e9a
No known key found for this signature in database
GPG key ID: D2EA4850E7528B25

View file

@ -52,6 +52,9 @@ if os.name == 'posix':
TEST_EXIT_PASSED = 0 TEST_EXIT_PASSED = 0
TEST_EXIT_SKIPPED = 77 TEST_EXIT_SKIPPED = 77
# 20 minutes represented in seconds
TRAVIS_TIMEOUT_DURATION = 20 * 60
BASE_SCRIPTS= [ BASE_SCRIPTS= [
# Scripts that are run by the travis build process. # Scripts that are run by the travis build process.
# Longest test should go first, to favor running tests in parallel # Longest test should go first, to favor running tests in parallel
@ -236,26 +239,24 @@ def main():
if tests: if tests:
# Individual tests have been specified. Run specified tests that exist # Individual tests have been specified. Run specified tests that exist
# in the ALL_SCRIPTS list. Accept the name with or without .py extension. # in the ALL_SCRIPTS list. Accept the name with or without .py extension.
tests = [re.sub("\.py$", "", t) + ".py" for t in tests] tests = [re.sub("\.py$", "", test) + ".py" for test in tests]
test_list = [] test_list = []
for t in tests: for test in tests:
if t in ALL_SCRIPTS: if test in ALL_SCRIPTS:
test_list.append(t) test_list.append(test)
else: else:
print("{}WARNING!{} Test '{}' not found in full test list.".format(BOLD[1], BOLD[0], t)) print("{}WARNING!{} Test '{}' not found in full test list.".format(BOLD[1], BOLD[0], test))
elif args.extended:
# Include extended tests
test_list = ALL_SCRIPTS
else: else:
# No individual tests have been specified. # Run base tests only
# Run all base tests, and optionally run extended tests.
test_list = BASE_SCRIPTS test_list = BASE_SCRIPTS
if args.extended:
# place the EXTENDED_SCRIPTS first since the three longest ones
# are there and the list is shorter
test_list = EXTENDED_SCRIPTS + test_list
# Remove the test cases that the user has explicitly asked to exclude. # Remove the test cases that the user has explicitly asked to exclude.
if args.exclude: if args.exclude:
tests_excl = [re.sub("\.py$", "", t) + ".py" for t in args.exclude.split(',')] exclude_tests = [re.sub("\.py$", "", test) + ".py" for test in args.exclude.split(',')]
for exclude_test in tests_excl: for exclude_test in exclude_tests:
if exclude_test in test_list: if exclude_test in test_list:
test_list.remove(exclude_test) test_list.remove(exclude_test)
else: else:
@ -320,7 +321,7 @@ def run_tests(test_list, src_dir, build_dir, exeext, tmpdir, jobs=1, enable_cove
#Run Tests #Run Tests
job_queue = TestHandler(jobs, tests_dir, tmpdir, test_list, flags) job_queue = TestHandler(jobs, tests_dir, tmpdir, test_list, flags)
time0 = time.time() start_time = time.time()
test_results = [] test_results = []
max_len_name = len(max(test_list, key=len)) 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
combined_logs, _ = subprocess.Popen([sys.executable, os.path.join(tests_dir, 'combine_logs.py'), '-c', testdir], universal_newlines=True, stdout=subprocess.PIPE).communicate() combined_logs, _ = subprocess.Popen([sys.executable, os.path.join(tests_dir, 'combine_logs.py'), '-c', testdir], universal_newlines=True, stdout=subprocess.PIPE).communicate()
print("\n".join(deque(combined_logs.splitlines(), combined_logs_len))) print("\n".join(deque(combined_logs.splitlines(), combined_logs_len)))
print_results(test_results, max_len_name, (int(time.time() - time0))) print_results(test_results, max_len_name, (int(time.time() - start_time)))
if coverage: if coverage:
coverage.report_rpc_coverage() coverage.report_rpc_coverage()
@ -403,15 +404,15 @@ class TestHandler:
while self.num_running < self.num_jobs and self.test_list: while self.num_running < self.num_jobs and self.test_list:
# Add tests # Add tests
self.num_running += 1 self.num_running += 1
t = self.test_list.pop(0) test = self.test_list.pop(0)
portseed = len(self.test_list) + self.portseed_offset portseed = len(self.test_list) + self.portseed_offset
portseed_arg = ["--portseed={}".format(portseed)] portseed_arg = ["--portseed={}".format(portseed)]
log_stdout = tempfile.SpooledTemporaryFile(max_size=2**16) log_stdout = tempfile.SpooledTemporaryFile(max_size=2**16)
log_stderr = tempfile.SpooledTemporaryFile(max_size=2**16) log_stderr = tempfile.SpooledTemporaryFile(max_size=2**16)
test_argv = t.split() test_argv = test.split()
testdir = "{}/{}_{}".format(self.tmpdir, re.sub(".py$", "", test_argv[0]), portseed) testdir = "{}/{}_{}".format(self.tmpdir, re.sub(".py$", "", test_argv[0]), portseed)
tmpdir_arg = ["--tmpdir={}".format(testdir)] tmpdir_arg = ["--tmpdir={}".format(testdir)]
self.jobs.append((t, self.jobs.append((test,
time.time(), time.time(),
subprocess.Popen([sys.executable, self.tests_dir + test_argv[0]] + test_argv[1:] + self.flags + portseed_arg + tmpdir_arg, subprocess.Popen([sys.executable, self.tests_dir + test_argv[0]] + test_argv[1:] + self.flags + portseed_arg + tmpdir_arg,
universal_newlines=True, universal_newlines=True,
@ -425,15 +426,14 @@ class TestHandler:
while True: while True:
# Return first proc that finishes # Return first proc that finishes
time.sleep(.5) time.sleep(.5)
for j in self.jobs: for job in self.jobs:
(name, time0, proc, testdir, log_out, log_err) = j (name, start_time, proc, testdir, log_out, log_err) = job
if os.getenv('TRAVIS') == 'true' and int(time.time() - time0) > 20 * 60: if os.getenv('TRAVIS') == 'true' and int(time.time() - start_time) > TRAVIS_TIMEOUT_DURATION:
# In travis, timeout individual tests after 20 minutes (to stop tests hanging and not # In travis, timeout individual tests (to stop tests hanging and not providing useful output).
# providing useful output.
proc.send_signal(signal.SIGINT) proc.send_signal(signal.SIGINT)
if proc.poll() is not None: if proc.poll() is not None:
log_out.seek(0), log_err.seek(0) log_out.seek(0), log_err.seek(0)
[stdout, stderr] = [l.read().decode('utf-8') for l in (log_out, log_err)] [stdout, stderr] = [file.read().decode('utf-8') for file in (log_out, log_err)]
log_out.close(), log_err.close() log_out.close(), log_err.close()
if proc.returncode == TEST_EXIT_PASSED and stderr == "": if proc.returncode == TEST_EXIT_PASSED and stderr == "":
status = "Passed" status = "Passed"
@ -442,9 +442,9 @@ class TestHandler:
else: else:
status = "Failed" status = "Failed"
self.num_running -= 1 self.num_running -= 1
self.jobs.remove(j) self.jobs.remove(job)
return TestResult(name, status, int(time.time() - time0)), testdir, stdout, stderr return TestResult(name, status, int(time.time() - start_time)), testdir, stdout, stderr
print('.', end='', flush=True) print('.', end='', flush=True)
class TestResult(): class TestResult():
@ -490,7 +490,7 @@ def check_script_list(src_dir):
Check that there are no scripts in the functional tests directory which are Check that there are no scripts in the functional tests directory which are
not being run by pull-tester.py.""" not being run by pull-tester.py."""
script_dir = src_dir + '/test/functional/' script_dir = src_dir + '/test/functional/'
python_files = set([t for t in os.listdir(script_dir) if t[-3:] == ".py"]) python_files = set([file for file in os.listdir(script_dir) if file.endswith(".py")])
missed_tests = list(python_files - set(map(lambda x: x.split()[0], ALL_SCRIPTS + NON_SCRIPTS))) missed_tests = list(python_files - set(map(lambda x: x.split()[0], ALL_SCRIPTS + NON_SCRIPTS)))
if len(missed_tests) != 0: if len(missed_tests) != 0:
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))) 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 @@ class RPCCoverage():
if uncovered: if uncovered:
print("Uncovered RPC commands:") print("Uncovered RPC commands:")
print("".join((" - %s\n" % i) for i in sorted(uncovered))) print("".join((" - %s\n" % command) for command in sorted(uncovered)))
else: else:
print("All RPC commands covered.") print("All RPC commands covered.")
@ -550,8 +550,8 @@ class RPCCoverage():
if not os.path.isfile(coverage_ref_filename): if not os.path.isfile(coverage_ref_filename):
raise RuntimeError("No coverage reference found") raise RuntimeError("No coverage reference found")
with open(coverage_ref_filename, 'r') as f: with open(coverage_ref_filename, 'r') as file:
all_cmds.update([i.strip() for i in f.readlines()]) all_cmds.update([line.strip() for line in file.readlines()])
for root, dirs, files in os.walk(self.dir): for root, dirs, files in os.walk(self.dir):
for filename in files: for filename in files:
@ -559,8 +559,8 @@ class RPCCoverage():
coverage_filenames.add(os.path.join(root, filename)) coverage_filenames.add(os.path.join(root, filename))
for filename in coverage_filenames: for filename in coverage_filenames:
with open(filename, 'r') as f: with open(filename, 'r') as file:
covered_cmds.update([i.strip() for i in f.readlines()]) covered_cmds.update([line.strip() for line in file.readlines()])
return all_cmds - covered_cmds return all_cmds - covered_cmds