Skip to content

Commit e00501e

Browse files
author
MarcoFalke
committed
Merge bitcoin#16561: tests: Use colors and dots in test_runner.py output only if standard output is a terminal
37f2784 tests: Use colors and dots in test_runner.py output only if standard output is a terminal -- allows for using the test runner output as input to other programs (practicalswift) Pull request description: Use colors and dots in `test_runner.py` output only if standard output is a terminal -- allows for using the test runner output as input to other programs. I found the need for this when parsing `test_runner.py` output while investigating intermittent functional test failures. Before: ``` $ test/functional/test_runner.py wallet_hd.py > output 2>&1 $ less output Temporary test directory at /tmp/test_runner_₿_🏃_20190807_074115 ESC[1mWARNING!ESC[0m There is already a bitcoind process running on this system. Tests may fail unexpectedly due to resource contention! Remaining jobs: [wallet_hd.py] .......................................^M ^M1/1 - ESC[1mwallet_hd.pyESC[0m passed, Duration: 20 s ESC[1mTEST | STATUS | DURATION ESC[0mESC[0;32mwallet_hd.py | ✓ Passed | 20 s ESC[0mESC[1m ALL | ✓ Passed | 20 s (accumulated) ESC[0mRuntime: 20 s ``` After: ``` $ test/functional/test_runner.py wallet_hd.py > output 2>&1 $ less output Temporary test directory at /tmp/test_runner_₿_🏃_20190807_074244 1/1 - wallet_hd.py passed, Duration: 20 s WARNING! There is already a bitcoind process running on this system. Tests may fail unexpectedly due to resource contention! Remaining jobs: [wallet_hd.py] TEST | STATUS | DURATION wallet_hd.py | ✓ Passed | 20 s ALL | ✓ Passed | 20 s (accumulated) Runtime: 20 s ``` ACKs for top commit: laanwj: ACK 37f2784 Tree-SHA512: f15d95f9e07de2954c326d63d7a4bcd2971faeaa00386600dec2fb915ec89475aeef1dbc968b2c12aa5e988d4b3ed1974d6da0b6a3f1e1a105cfd90e8cb97cf6
2 parents 8fc7f0c + 37f2784 commit e00501e

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

test/functional/test_runner.py

+18-5
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ def main():
228228
epilog='''
229229
Help text and arguments for individual test script:''',
230230
formatter_class=argparse.RawTextHelpFormatter)
231+
parser.add_argument('--ansi', action='store_true', default=sys.stdout.isatty(), help="Use ANSI colors and dots in output (enabled by default when standard output is a TTY)")
231232
parser.add_argument('--combinedlogslen', '-c', type=int, default=0, metavar='n', help='On failure, print a log (of length n lines) to the console, combined from the test framework and all test nodes.')
232233
parser.add_argument('--coverage', action='store_true', help='generate a basic coverage report for the RPC interface')
233234
parser.add_argument('--ci', action='store_true', help='Run checks and code that are usually only enabled in a continuous integration environment')
@@ -240,7 +241,14 @@ def main():
240241
parser.add_argument('--tmpdirprefix', '-t', default=tempfile.gettempdir(), help="Root directory for datadirs")
241242
parser.add_argument('--failfast', action='store_true', help='stop execution after the first test failure')
242243
parser.add_argument('--filter', help='filter scripts to run by regular expression')
244+
243245
args, unknown_args = parser.parse_known_args()
246+
if not args.ansi:
247+
global BOLD, GREEN, RED, GREY
248+
BOLD = ("", "")
249+
GREEN = ("", "")
250+
RED = ("", "")
251+
GREY = ("", "")
244252

245253
# args to be passed on always start with two dashes; tests are the remaining unknown args
246254
tests = [arg for arg in unknown_args if arg[:2] != "--"]
@@ -342,9 +350,10 @@ def main():
342350
combined_logs_len=args.combinedlogslen,
343351
failfast=args.failfast,
344352
runs_ci=args.ci,
353+
use_term_control=args.ansi,
345354
)
346355

347-
def run_tests(*, test_list, src_dir, build_dir, tmpdir, jobs=1, enable_coverage=False, args=None, combined_logs_len=0, failfast=False, runs_ci):
356+
def run_tests(*, test_list, src_dir, build_dir, tmpdir, jobs=1, enable_coverage=False, args=None, combined_logs_len=0, failfast=False, runs_ci, use_term_control):
348357
args = args or []
349358

350359
# Warn if bitcoind is already running (unix only)
@@ -386,6 +395,7 @@ def run_tests(*, test_list, src_dir, build_dir, tmpdir, jobs=1, enable_coverage=
386395
test_list=test_list,
387396
flags=flags,
388397
timeout_duration=40 * 60 if runs_ci else float('inf'), # in seconds
398+
use_term_control=use_term_control,
389399
)
390400
start_time = time.time()
391401
test_results = []
@@ -469,7 +479,7 @@ class TestHandler:
469479
Trigger the test scripts passed in via the list.
470480
"""
471481

472-
def __init__(self, *, num_tests_parallel, tests_dir, tmpdir, test_list, flags, timeout_duration):
482+
def __init__(self, *, num_tests_parallel, tests_dir, tmpdir, test_list, flags, timeout_duration, use_term_control):
473483
assert num_tests_parallel >= 1
474484
self.num_jobs = num_tests_parallel
475485
self.tests_dir = tests_dir
@@ -479,6 +489,7 @@ def __init__(self, *, num_tests_parallel, tests_dir, tmpdir, test_list, flags, t
479489
self.flags = flags
480490
self.num_running = 0
481491
self.jobs = []
492+
self.use_term_control = use_term_control
482493

483494
def get_next(self):
484495
while self.num_running < self.num_jobs and self.test_list:
@@ -530,11 +541,13 @@ def get_next(self):
530541
status = "Failed"
531542
self.num_running -= 1
532543
self.jobs.remove(job)
533-
clearline = '\r' + (' ' * dot_count) + '\r'
534-
print(clearline, end='', flush=True)
544+
if self.use_term_control:
545+
clearline = '\r' + (' ' * dot_count) + '\r'
546+
print(clearline, end='', flush=True)
535547
dot_count = 0
536548
return TestResult(name, status, int(time.time() - start_time)), testdir, stdout, stderr
537-
print('.', end='', flush=True)
549+
if self.use_term_control:
550+
print('.', end='', flush=True)
538551
dot_count += 1
539552

540553
def kill_and_join(self):

0 commit comments

Comments
 (0)