Skip to content

Commit 2219ec2

Browse files
committed
Add a --color flag to test binaries
It uses the same behavior as rustc's.
1 parent 443a1cd commit 2219ec2

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

src/compiletest/compiletest.rs

+1
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ pub fn test_opts(config: &Config) -> test::TestOpts {
286286
save_metrics: config.save_metrics.clone(),
287287
test_shard: config.test_shard.clone(),
288288
nocapture: false,
289+
color: test::AutoColor,
289290
}
290291
}
291292

src/libtest/lib.rs

+31-4
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,12 @@ pub fn test_main_static_x(args: &[~str], tests: &[TestDescAndFn]) {
271271
tests)
272272
}
273273

274+
pub enum ColorConfig {
275+
AutoColor,
276+
AlwaysColor,
277+
NeverColor,
278+
}
279+
274280
pub struct TestOpts {
275281
pub filter: Option<Regex>,
276282
pub run_ignored: bool,
@@ -282,6 +288,7 @@ pub struct TestOpts {
282288
pub test_shard: Option<(uint,uint)>,
283289
pub logfile: Option<Path>,
284290
pub nocapture: bool,
291+
pub color: ColorConfig,
285292
}
286293

287294
impl TestOpts {
@@ -298,6 +305,7 @@ impl TestOpts {
298305
test_shard: None,
299306
logfile: None,
300307
nocapture: false,
308+
color: AutoColor,
301309
}
302310
}
303311
}
@@ -324,7 +332,11 @@ fn optgroups() -> Vec<getopts::OptGroup> {
324332
getopts::optopt("", "test-shard", "run shard A, of B shards, worth of the testsuite",
325333
"A.B"),
326334
getopts::optflag("", "nocapture", "don't capture stdout/stderr of each \
327-
task, allow printing directly"))
335+
task, allow printing directly"),
336+
getopts::optopt("", "color", "Configure coloring of output:
337+
auto = colorize if stdout is a tty and tests are run on serially (default);
338+
always = always colorize output;
339+
never = never colorize output;", "auto|always|never"))
328340
}
329341

330342
fn usage(binary: &str) {
@@ -406,6 +418,16 @@ pub fn parse_opts(args: &[String]) -> Option<OptRes> {
406418
nocapture = os::getenv("RUST_TEST_NOCAPTURE").is_some();
407419
}
408420

421+
let color = match matches.opt_str("color").as_ref().map(|s| s.as_slice()) {
422+
Some("auto") | None => AutoColor,
423+
Some("always") => AlwaysColor,
424+
Some("never") => NeverColor,
425+
426+
Some(v) => return Some(Err(format!("argument for --color must be \
427+
auto, always, or never (was {})",
428+
v))),
429+
};
430+
409431
let test_opts = TestOpts {
410432
filter: filter,
411433
run_ignored: run_ignored,
@@ -417,6 +439,7 @@ pub fn parse_opts(args: &[String]) -> Option<OptRes> {
417439
test_shard: test_shard,
418440
logfile: logfile,
419441
nocapture: nocapture,
442+
color: color,
420443
};
421444

422445
Some(Ok(test_opts))
@@ -492,7 +515,7 @@ impl<T: Writer> ConsoleTestState<T> {
492515
Ok(ConsoleTestState {
493516
out: out,
494517
log_out: log_out,
495-
use_color: use_color(),
518+
use_color: use_color(opts),
496519
total: 0u,
497520
passed: 0u,
498521
failed: 0u,
@@ -867,8 +890,12 @@ fn should_sort_failures_before_printing_them() {
867890
assert!(apos < bpos);
868891
}
869892

870-
fn use_color() -> bool {
871-
get_concurrency() == 1 && io::stdout().get_ref().isatty()
893+
fn use_color(opts: &TestOpts) -> bool {
894+
match opts.color {
895+
AutoColor => get_concurrency() == 1 && io::stdout().get_ref().isatty(),
896+
AlwaysColor => true,
897+
NeverColor => false,
898+
}
872899
}
873900

874901
#[deriving(Clone)]

0 commit comments

Comments
 (0)