Add a --color flag to test binaries
It uses the same behavior as rustc's.
This commit is contained in:
committed by
Alex Crichton
parent
9faf5a3483
commit
5eb4d19dc3
@@ -292,6 +292,7 @@ pub fn test_opts(config: &Config) -> test::TestOpts {
|
|||||||
save_metrics: config.save_metrics.clone(),
|
save_metrics: config.save_metrics.clone(),
|
||||||
test_shard: config.test_shard.clone(),
|
test_shard: config.test_shard.clone(),
|
||||||
nocapture: false,
|
nocapture: false,
|
||||||
|
color: test::AutoColor,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -271,6 +271,12 @@ pub fn test_main_static_x(args: &[~str], tests: &[TestDescAndFn]) {
|
|||||||
tests)
|
tests)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub enum ColorConfig {
|
||||||
|
AutoColor,
|
||||||
|
AlwaysColor,
|
||||||
|
NeverColor,
|
||||||
|
}
|
||||||
|
|
||||||
pub struct TestOpts {
|
pub struct TestOpts {
|
||||||
pub filter: Option<Regex>,
|
pub filter: Option<Regex>,
|
||||||
pub run_ignored: bool,
|
pub run_ignored: bool,
|
||||||
@@ -282,6 +288,7 @@ pub struct TestOpts {
|
|||||||
pub test_shard: Option<(uint,uint)>,
|
pub test_shard: Option<(uint,uint)>,
|
||||||
pub logfile: Option<Path>,
|
pub logfile: Option<Path>,
|
||||||
pub nocapture: bool,
|
pub nocapture: bool,
|
||||||
|
pub color: ColorConfig,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TestOpts {
|
impl TestOpts {
|
||||||
@@ -298,6 +305,7 @@ impl TestOpts {
|
|||||||
test_shard: None,
|
test_shard: None,
|
||||||
logfile: None,
|
logfile: None,
|
||||||
nocapture: false,
|
nocapture: false,
|
||||||
|
color: AutoColor,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -324,7 +332,11 @@ fn optgroups() -> Vec<getopts::OptGroup> {
|
|||||||
getopts::optopt("", "test-shard", "run shard A, of B shards, worth of the testsuite",
|
getopts::optopt("", "test-shard", "run shard A, of B shards, worth of the testsuite",
|
||||||
"A.B"),
|
"A.B"),
|
||||||
getopts::optflag("", "nocapture", "don't capture stdout/stderr of each \
|
getopts::optflag("", "nocapture", "don't capture stdout/stderr of each \
|
||||||
task, allow printing directly"))
|
task, allow printing directly"),
|
||||||
|
getopts::optopt("", "color", "Configure coloring of output:
|
||||||
|
auto = colorize if stdout is a tty and tests are run on serially (default);
|
||||||
|
always = always colorize output;
|
||||||
|
never = never colorize output;", "auto|always|never"))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn usage(binary: &str) {
|
fn usage(binary: &str) {
|
||||||
@@ -406,6 +418,16 @@ pub fn parse_opts(args: &[String]) -> Option<OptRes> {
|
|||||||
nocapture = os::getenv("RUST_TEST_NOCAPTURE").is_some();
|
nocapture = os::getenv("RUST_TEST_NOCAPTURE").is_some();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let color = match matches.opt_str("color").as_ref().map(|s| s.as_slice()) {
|
||||||
|
Some("auto") | None => AutoColor,
|
||||||
|
Some("always") => AlwaysColor,
|
||||||
|
Some("never") => NeverColor,
|
||||||
|
|
||||||
|
Some(v) => return Some(Err(format!("argument for --color must be \
|
||||||
|
auto, always, or never (was {})",
|
||||||
|
v))),
|
||||||
|
};
|
||||||
|
|
||||||
let test_opts = TestOpts {
|
let test_opts = TestOpts {
|
||||||
filter: filter,
|
filter: filter,
|
||||||
run_ignored: run_ignored,
|
run_ignored: run_ignored,
|
||||||
@@ -417,6 +439,7 @@ pub fn parse_opts(args: &[String]) -> Option<OptRes> {
|
|||||||
test_shard: test_shard,
|
test_shard: test_shard,
|
||||||
logfile: logfile,
|
logfile: logfile,
|
||||||
nocapture: nocapture,
|
nocapture: nocapture,
|
||||||
|
color: color,
|
||||||
};
|
};
|
||||||
|
|
||||||
Some(Ok(test_opts))
|
Some(Ok(test_opts))
|
||||||
@@ -492,7 +515,7 @@ impl<T: Writer> ConsoleTestState<T> {
|
|||||||
Ok(ConsoleTestState {
|
Ok(ConsoleTestState {
|
||||||
out: out,
|
out: out,
|
||||||
log_out: log_out,
|
log_out: log_out,
|
||||||
use_color: use_color(),
|
use_color: use_color(opts),
|
||||||
total: 0u,
|
total: 0u,
|
||||||
passed: 0u,
|
passed: 0u,
|
||||||
failed: 0u,
|
failed: 0u,
|
||||||
@@ -867,8 +890,12 @@ fn should_sort_failures_before_printing_them() {
|
|||||||
assert!(apos < bpos);
|
assert!(apos < bpos);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn use_color() -> bool {
|
fn use_color(opts: &TestOpts) -> bool {
|
||||||
get_concurrency() == 1 && io::stdout().get_ref().isatty()
|
match opts.color {
|
||||||
|
AutoColor => get_concurrency() == 1 && io::stdout().get_ref().isatty(),
|
||||||
|
AlwaysColor => true,
|
||||||
|
NeverColor => false,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[deriving(Clone)]
|
#[deriving(Clone)]
|
||||||
|
|||||||
Reference in New Issue
Block a user