@@ -249,7 +249,8 @@ fn run_test(
249249 outdir : DirState ,
250250 path : PathBuf ,
251251) -> Result < ( ) , TestFailure > {
252- let ( test, line_offset) = make_test ( test, Some ( cratename) , as_test_harness, opts, edition) ;
252+ let ( test, line_offset, supports_color) =
253+ make_test ( test, Some ( cratename) , as_test_harness, opts, edition) ;
253254
254255 let output_file = outdir. path ( ) . join ( "rust_out" ) ;
255256
@@ -294,38 +295,19 @@ fn run_test(
294295 path. to_str ( ) . expect ( "target path must be valid unicode" ) . to_string ( )
295296 }
296297 } ) ;
297- match options. error_format {
298- ErrorOutputType :: HumanReadable ( kind) => {
299- let ( _, color_config) = kind. unzip ( ) ;
300- match color_config {
301- ColorConfig :: Never => {
302- compiler. arg ( "--color" ) . arg ( "never" ) ;
303- }
304- ColorConfig :: Always => {
305- compiler. arg ( "--color" ) . arg ( "always" ) ;
306- }
307- ColorConfig :: Auto => {
308- #[ cfg( windows) ]
309- {
310- // This specific check is because old windows consoles require a connection
311- // to be able to display colors (and they don't support ANSI), which we
312- // cannot in here, so in case this is an old windows console, we can't
313- // display colors.
314- use crate :: termcolor:: { ColorChoice , StandardStream , WriteColor } ;
315- if StandardStream :: stdout ( ColorChoice :: Auto ) . is_synchronous ( ) {
316- compiler. arg ( "--color" ) . arg ( "never" ) ;
317- } else {
318- compiler. arg ( "--color" ) . arg ( "always" ) ;
319- }
320- }
321- #[ cfg( not( windows) ) ]
322- {
323- compiler. arg ( "--color" ) . arg ( "always" ) ;
324- }
325- }
298+ if let ErrorOutputType :: HumanReadable ( kind) = options. error_format {
299+ let ( _, color_config) = kind. unzip ( ) ;
300+ match color_config {
301+ ColorConfig :: Never => {
302+ compiler. arg ( "--color" ) . arg ( "never" ) ;
303+ }
304+ ColorConfig :: Always => {
305+ compiler. arg ( "--color" ) . arg ( "always" ) ;
306+ }
307+ ColorConfig :: Auto => {
308+ compiler. arg ( "--color" ) . arg ( if supports_color { "always" } else { "never" } ) ;
326309 }
327310 }
328- _ => { }
329311 }
330312
331313 compiler. arg ( "-" ) ;
@@ -396,18 +378,19 @@ fn run_test(
396378}
397379
398380/// Transforms a test into code that can be compiled into a Rust binary, and returns the number of
399- /// lines before the test code begins.
381+ /// lines before the test code begins as well as if the output stream supports colors or not .
400382pub fn make_test (
401383 s : & str ,
402384 cratename : Option < & str > ,
403385 dont_insert_main : bool ,
404386 opts : & TestOptions ,
405387 edition : Edition ,
406- ) -> ( String , usize ) {
388+ ) -> ( String , usize , bool ) {
407389 let ( crate_attrs, everything_else, crates) = partition_source ( s) ;
408390 let everything_else = everything_else. trim ( ) ;
409391 let mut line_offset = 0 ;
410392 let mut prog = String :: new ( ) ;
393+ let mut supports_color = false ;
411394
412395 if opts. attrs . is_empty ( ) && !opts. display_warnings {
413396 // If there aren't any attributes supplied by #![doc(test(attr(...)))], then allow some
@@ -433,7 +416,7 @@ pub fn make_test(
433416 // crate already is included.
434417 let result = rustc_driver:: catch_fatal_errors ( || {
435418 rustc_span:: with_session_globals ( edition, || {
436- use rustc_errors:: emitter:: EmitterWriter ;
419+ use rustc_errors:: emitter:: { Emitter , EmitterWriter } ;
437420 use rustc_errors:: Handler ;
438421 use rustc_parse:: maybe_new_parser_from_source_str;
439422 use rustc_session:: parse:: ParseSess ;
@@ -447,6 +430,9 @@ pub fn make_test(
447430 let sm = Lrc :: new ( SourceMap :: new ( FilePathMapping :: empty ( ) ) ) ;
448431 let emitter =
449432 EmitterWriter :: new ( box io:: sink ( ) , None , false , false , false , None , false ) ;
433+
434+ supports_color = emitter. supports_color ( ) ;
435+
450436 // FIXME(misdreavus): pass `-Z treat-err-as-bug` to the doctest parser
451437 let handler = Handler :: with_emitter ( false , None , box emitter) ;
452438 let sess = ParseSess :: with_span_handler ( handler, sm) ;
@@ -516,7 +502,7 @@ pub fn make_test(
516502 Err ( ErrorReported ) => {
517503 // If the parser panicked due to a fatal error, pass the test code through unchanged.
518504 // The error will be reported during compilation.
519- return ( s. to_owned ( ) , 0 ) ;
505+ return ( s. to_owned ( ) , 0 , false ) ;
520506 }
521507 } ;
522508
@@ -566,7 +552,7 @@ pub fn make_test(
566552
567553 debug ! ( "final doctest:\n {}" , prog) ;
568554
569- ( prog, line_offset)
555+ ( prog, line_offset, supports_color )
570556}
571557
572558// FIXME(aburka): use a real parser to deal with multiline attributes
0 commit comments