Skip to content

Commit 37f2784

Browse files
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
1 parent e5fdda6 commit 37f2784

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
@@ -227,6 +227,7 @@ def main():
227227
epilog='''
228228
Help text and arguments for individual test script:''',
229229
formatter_class=argparse.RawTextHelpFormatter)
230+
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)")
230231
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.')
231232
parser.add_argument('--coverage', action='store_true', help='generate a basic coverage report for the RPC interface')
232233
parser.add_argument('--ci', action='store_true', help='Run checks and code that are usually only enabled in a continuous integration environment')
@@ -239,7 +240,14 @@ def main():
239240
parser.add_argument('--tmpdirprefix', '-t', default=tempfile.gettempdir(), help="Root directory for datadirs")
240241
parser.add_argument('--failfast', action='store_true', help='stop execution after the first test failure')
241242
parser.add_argument('--filter', help='filter scripts to run by regular expression')
243+
242244
args, unknown_args = parser.parse_known_args()
245+
if not args.ansi:
246+
global BOLD, GREEN, RED, GREY
247+
BOLD = ("", "")
248+
GREEN = ("", "")
249+
RED = ("", "")
250+
GREY = ("", "")
243251

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

346-
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):
355+
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):
347356
args = args or []
348357

349358
# Warn if bitcoind is already running (unix only)
@@ -385,6 +394,7 @@ def run_tests(*, test_list, src_dir, build_dir, tmpdir, jobs=1, enable_coverage=
385394
test_list=test_list,
386395
flags=flags,
387396
timeout_duration=40 * 60 if runs_ci else float('inf'), # in seconds
397+
use_term_control=use_term_control,
388398
)
389399
start_time = time.time()
390400
test_results = []
@@ -468,7 +478,7 @@ class TestHandler:
468478
Trigger the test scripts passed in via the list.
469479
"""
470480

471-
def __init__(self, *, num_tests_parallel, tests_dir, tmpdir, test_list, flags, timeout_duration):
481+
def __init__(self, *, num_tests_parallel, tests_dir, tmpdir, test_list, flags, timeout_duration, use_term_control):
472482
assert num_tests_parallel >= 1
473483
self.num_jobs = num_tests_parallel
474484
self.tests_dir = tests_dir
@@ -478,6 +488,7 @@ def __init__(self, *, num_tests_parallel, tests_dir, tmpdir, test_list, flags, t
478488
self.flags = flags
479489
self.num_running = 0
480490
self.jobs = []
491+
self.use_term_control = use_term_control
481492

482493
def get_next(self):
483494
while self.num_running < self.num_jobs and self.test_list:
@@ -529,11 +540,13 @@ def get_next(self):
529540
status = "Failed"
530541
self.num_running -= 1
531542
self.jobs.remove(job)
532-
clearline = '\r' + (' ' * dot_count) + '\r'
533-
print(clearline, end='', flush=True)
543+
if self.use_term_control:
544+
clearline = '\r' + (' ' * dot_count) + '\r'
545+
print(clearline, end='', flush=True)
534546
dot_count = 0
535547
return TestResult(name, status, int(time.time() - start_time)), testdir, stdout, stderr
536-
print('.', end='', flush=True)
548+
if self.use_term_control:
549+
print('.', end='', flush=True)
537550
dot_count += 1
538551

539552
def kill_and_join(self):

0 commit comments

Comments
 (0)