@@ -271,6 +271,12 @@ pub fn test_main_static_x(args: &[~str], tests: &[TestDescAndFn]) {
271
271
tests)
272
272
}
273
273
274
+ pub enum ColorConfig {
275
+ AutoColor ,
276
+ AlwaysColor ,
277
+ NeverColor ,
278
+ }
279
+
274
280
pub struct TestOpts {
275
281
pub filter : Option < Regex > ,
276
282
pub run_ignored : bool ,
@@ -282,6 +288,7 @@ pub struct TestOpts {
282
288
pub test_shard : Option < ( uint , uint ) > ,
283
289
pub logfile : Option < Path > ,
284
290
pub nocapture : bool ,
291
+ pub color : ColorConfig ,
285
292
}
286
293
287
294
impl TestOpts {
@@ -298,6 +305,7 @@ impl TestOpts {
298
305
test_shard : None ,
299
306
logfile : None ,
300
307
nocapture : false ,
308
+ color : AutoColor ,
301
309
}
302
310
}
303
311
}
@@ -324,7 +332,11 @@ fn optgroups() -> Vec<getopts::OptGroup> {
324
332
getopts:: optopt( "" , "test-shard" , "run shard A, of B shards, worth of the testsuite" ,
325
333
"A.B" ) ,
326
334
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" ) )
328
340
}
329
341
330
342
fn usage ( binary : & str ) {
@@ -406,6 +418,16 @@ pub fn parse_opts(args: &[String]) -> Option<OptRes> {
406
418
nocapture = os:: getenv ( "RUST_TEST_NOCAPTURE" ) . is_some ( ) ;
407
419
}
408
420
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
+
409
431
let test_opts = TestOpts {
410
432
filter : filter,
411
433
run_ignored : run_ignored,
@@ -417,6 +439,7 @@ pub fn parse_opts(args: &[String]) -> Option<OptRes> {
417
439
test_shard : test_shard,
418
440
logfile : logfile,
419
441
nocapture : nocapture,
442
+ color : color,
420
443
} ;
421
444
422
445
Some ( Ok ( test_opts) )
@@ -492,7 +515,7 @@ impl<T: Writer> ConsoleTestState<T> {
492
515
Ok ( ConsoleTestState {
493
516
out : out,
494
517
log_out : log_out,
495
- use_color : use_color ( ) ,
518
+ use_color : use_color ( opts ) ,
496
519
total : 0 u,
497
520
passed : 0 u,
498
521
failed : 0 u,
@@ -867,8 +890,12 @@ fn should_sort_failures_before_printing_them() {
867
890
assert ! ( apos < bpos) ;
868
891
}
869
892
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
+ }
872
899
}
873
900
874
901
#[ deriving( Clone ) ]
0 commit comments