Skip to content

Commit 74dd4dc

Browse files
committed
Enable colors in FileCheck and Swift tests
If we're attached to a tty, pass `--color` to FileCheck and `--color-diagnostics` to Swift invocations in tests.
1 parent e380c66 commit 74dd4dc

File tree

5 files changed

+23
-1
lines changed

5 files changed

+23
-1
lines changed

test/lit.cfg

+8-1
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,8 @@ config.abi_symbol_checker = make_path(config.swift_utils, 'swift-abi-symbol-chec
365365
config.link = lit.util.which('link', config.environment.get('PATH', '')) or \
366366
lit.util.which('lld-link', config.environment.get('PATH', ''))
367367

368+
config.color_output = lit_config.params.get('color_output', None) is not None
369+
368370
# Find the resource directory. Assume it's near the swift compiler if not set.
369371
test_resource_dir = lit_config.params.get('test_resource_dir')
370372
config.resource_dir_opt = ""
@@ -1953,6 +1955,10 @@ config.substitutions.append(('%cache-tool', config.swift_cache_tool))
19531955
config.target_swift_frontend += " -typo-correction-limit 10 "
19541956
subst_target_swift_frontend_mock_sdk += " -typo-correction-limit 10 "
19551957

1958+
# Enable colors if we have them.
1959+
if config.color_output:
1960+
config.target_swift_frontend += " -color-diagnostics"
1961+
19561962
config.substitutions.append(('%module-target-triple',
19571963
target_specific_module_triple))
19581964
config.substitutions.append(('%module-target-future', target_future))
@@ -2728,7 +2734,7 @@ if hasattr(config, 'target_link_sdk_future_version'):
27282734
config.substitutions.append(('%target-link-sdk-future-version',
27292735
config.target_link_sdk_future_version))
27302736

2731-
run_filecheck = '%s %s --allow-unused-prefixes --sanitize BUILD_DIR=%s --sanitize SOURCE_DIR=%s --use-filecheck %s %s' % (
2737+
run_filecheck = '%s %s --allow-unused-prefixes --sanitize BUILD_DIR=%s --sanitize SOURCE_DIR=%s --use-filecheck %s %s %s' % (
27322738
shell_quote(sys.executable),
27332739
shell_quote(config.PathSanitizingFileCheck),
27342740
# LLVM Lit performs realpath with the config path, so all paths are relative
@@ -2740,6 +2746,7 @@ run_filecheck = '%s %s --allow-unused-prefixes --sanitize BUILD_DIR=%s --sanitiz
27402746
shell_quote(lit.util.abs_path_preserve_drive(swift_obj_root).replace("\\", "/")),
27412747
shell_quote(lit.util.abs_path_preserve_drive(config.swift_src_root).replace("\\", "/")),
27422748
shell_quote(config.filecheck),
2749+
'--color' if config.color_output else '',
27432750
'--enable-windows-compatibility' if kIsWindows else '')
27442751

27452752
config.substitutions.append(('%FileCheck', run_filecheck))

utils/build-script

+4
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,10 @@ def apply_default_arguments(toolchain, args):
313313
if ninja_required and toolchain.ninja is None:
314314
args.build_ninja = True
315315

316+
# Enable test colors if we're on a tty and aren't generating for Xcode.
317+
if args.color_in_tests and sys.stdout.isatty() and args.cmake_generator != "Xcode":
318+
args.lit_args += " --param color_output"
319+
316320
# Set the default stdlib-deployment-targets, if none were provided.
317321
if args.stdlib_deployment_targets is None:
318322
stdlib_targets = default_stdlib_deployment_targets(args)

utils/build_swift/build_swift/driver_arguments.py

+3
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,9 @@ def create_argument_parser():
572572
metavar='LITARGS',
573573
help='lit args to use when testing')
574574

575+
option('--color-in-tests', toggle_true, default=True,
576+
help='Enable color output in lit tests')
577+
575578
option('--coverage-db', store_path,
576579
help='coverage database to use when prioritizing testing')
577580

utils/build_swift/tests/expected_options.py

+2
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@
140140
'cmake_generator': 'Ninja',
141141
'cmark_assertions': True,
142142
'cmark_build_variant': 'Debug',
143+
'color_in_tests': True,
143144
'compiler_vendor': defaults.COMPILER_VENDOR,
144145
'coverage_db': None,
145146
'cross_compile_append_host_target_to_destdir': True,
@@ -580,6 +581,7 @@ class BuildScriptImplOption(_BaseOption):
580581
EnableOption('--build-swift-remote-mirror'),
581582
EnableOption('--build-swift-external-generic-metadata-builder'),
582583
EnableOption('--cross-compile-append-host-target-to-destdir'),
584+
EnableOption('--color-in-tests'),
583585
EnableOption('--distcc'),
584586
EnableOption('--sccache'),
585587
EnableOption('--enable-asan'),

utils/run-test

+6
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ def main():
127127
parser.add_argument("--build-jobs",
128128
type=int,
129129
help="The number of parallel build jobs to use")
130+
parser.add_argument("--color", choices=["true", "false"], default="true",
131+
help="Whether to enable color output (default: true)")
130132
parser.add_argument("--target",
131133
type=argparse.types.ShellSplitType(),
132134
action=argparse.actions.AppendAction,
@@ -265,6 +267,10 @@ def main():
265267
for param in args.param:
266268
test_args += ['--param', param]
267269

270+
# Enable colors if we're on a tty.
271+
if args.color == 'true' and sys.stdout.isatty():
272+
test_args += ['--param', 'color_output']
273+
268274
if args.result_dir:
269275
test_args += ['--param', 'swift_test_results_dir=%s' % args.result_dir,
270276
'--xunit-xml-output=%s' % os.path.join(args.result_dir,

0 commit comments

Comments
 (0)