@@ -354,13 +354,10 @@ impl<'test> TestCx<'test> {
354
354
}
355
355
} else {
356
356
if proc_res. status . success ( ) {
357
- {
358
- self . error ( & format ! ( "{} test did not emit an error" , self . config. mode) ) ;
359
- if self . config . mode == crate :: common:: TestMode :: Ui {
360
- println ! ( "note: by default, ui tests are expected not to compile" ) ;
361
- }
362
- proc_res. fatal ( None , || ( ) ) ;
363
- } ;
357
+ let err = & format ! ( "{} test did not emit an error" , self . config. mode) ;
358
+ let extra_note = ( self . config . mode == crate :: common:: TestMode :: Ui )
359
+ . then_some ( "note: by default, ui tests are expected not to compile" ) ;
360
+ self . fatal_proc_rec_general ( err, extra_note, proc_res, || ( ) ) ;
364
361
}
365
362
366
363
if !self . props . dont_check_failure_status {
@@ -606,7 +603,10 @@ impl<'test> TestCx<'test> {
606
603
) ;
607
604
} else {
608
605
for pattern in missing_patterns {
609
- self . error ( & format ! ( "error pattern '{}' not found!" , pattern) ) ;
606
+ println ! (
607
+ "\n {prefix}: error pattern '{pattern}' not found!" ,
608
+ prefix = self . error_prefix( )
609
+ ) ;
610
610
}
611
611
self . fatal_proc_rec ( "multiple error patterns not found" , proc_res) ;
612
612
}
@@ -824,10 +824,11 @@ impl<'test> TestCx<'test> {
824
824
// - only known line - meh, but suggested
825
825
// - others are not worth suggesting
826
826
if !unexpected. is_empty ( ) {
827
- self . error ( & format ! (
828
- "{} diagnostics reported in JSON output but not expected in test file" ,
829
- unexpected. len( ) ,
830
- ) ) ;
827
+ println ! (
828
+ "\n {prefix}: {n} diagnostics reported in JSON output but not expected in test file" ,
829
+ prefix = self . error_prefix( ) ,
830
+ n = unexpected. len( ) ,
831
+ ) ;
831
832
for error in & unexpected {
832
833
print_error ( error) ;
833
834
let mut suggestions = Vec :: new ( ) ;
@@ -857,10 +858,11 @@ impl<'test> TestCx<'test> {
857
858
}
858
859
}
859
860
if !not_found. is_empty ( ) {
860
- self . error ( & format ! (
861
- "{} diagnostics expected in test file but not reported in JSON output" ,
862
- not_found. len( )
863
- ) ) ;
861
+ println ! (
862
+ "\n {prefix}: {n} diagnostics expected in test file but not reported in JSON output" ,
863
+ prefix = self . error_prefix( ) ,
864
+ n = not_found. len( ) ,
865
+ ) ;
864
866
for error in & not_found {
865
867
print_error ( error) ;
866
868
let mut suggestions = Vec :: new ( ) ;
@@ -1995,33 +1997,52 @@ impl<'test> TestCx<'test> {
1995
1997
output_base_name ( self . config , self . testpaths , self . safe_revision ( ) )
1996
1998
}
1997
1999
1998
- fn error ( & self , err : & str ) {
2000
+ /// Prefix to print before error messages. Normally just `error`, but also
2001
+ /// includes the revision name for tests that use revisions.
2002
+ #[ must_use]
2003
+ fn error_prefix ( & self ) -> String {
1999
2004
match self . revision {
2000
- Some ( rev) => println ! ( "\n error in revision `{}`: {}" , rev , err ) ,
2001
- None => println ! ( "\n error: {}" , err ) ,
2005
+ Some ( rev) => format ! ( "error in revision `{rev}`" ) ,
2006
+ None => format ! ( "error" ) ,
2002
2007
}
2003
2008
}
2004
2009
2005
2010
#[ track_caller]
2006
2011
fn fatal ( & self , err : & str ) -> ! {
2007
- self . error ( err ) ;
2012
+ println ! ( " \n {prefix}: {err}" , prefix = self . error_prefix ( ) ) ;
2008
2013
error ! ( "fatal error, panic: {:?}" , err) ;
2009
2014
panic ! ( "fatal error" ) ;
2010
2015
}
2011
2016
2012
2017
fn fatal_proc_rec ( & self , err : & str , proc_res : & ProcRes ) -> ! {
2013
- self . error ( err) ;
2014
- proc_res. fatal ( None , || ( ) ) ;
2018
+ self . fatal_proc_rec_general ( err, None , proc_res, || ( ) ) ;
2015
2019
}
2016
2020
2017
- fn fatal_proc_rec_with_ctx (
2021
+ /// Underlying implementation of [`Self::fatal_proc_rec`], providing some
2022
+ /// extra capabilities not needed by most callers.
2023
+ fn fatal_proc_rec_general (
2018
2024
& self ,
2019
2025
err : & str ,
2026
+ extra_note : Option < & str > ,
2020
2027
proc_res : & ProcRes ,
2021
- on_failure : impl FnOnce ( Self ) ,
2028
+ callback_before_unwind : impl FnOnce ( ) ,
2022
2029
) -> ! {
2023
- self . error ( err) ;
2024
- proc_res. fatal ( None , || on_failure ( * self ) ) ;
2030
+ println ! ( "\n {prefix}: {err}" , prefix = self . error_prefix( ) ) ;
2031
+
2032
+ // Some callers want to print additional notes after the main error message.
2033
+ if let Some ( note) = extra_note {
2034
+ println ! ( "{note}" ) ;
2035
+ }
2036
+
2037
+ // Print the details and output of the subprocess that caused this test to fail.
2038
+ println ! ( "{}" , proc_res. format_info( ) ) ;
2039
+
2040
+ // Some callers want print more context or show a custom diff before the unwind occurs.
2041
+ callback_before_unwind ( ) ;
2042
+
2043
+ // Use resume_unwind instead of panic!() to prevent a panic message + backtrace from
2044
+ // compiletest, which is unnecessary noise.
2045
+ std:: panic:: resume_unwind ( Box :: new ( ( ) ) ) ;
2025
2046
}
2026
2047
2027
2048
// codegen tests (using FileCheck)
@@ -2080,7 +2101,7 @@ impl<'test> TestCx<'test> {
2080
2101
if cfg ! ( target_os = "freebsd" ) { "ISO-8859-1" } else { "UTF-8" }
2081
2102
}
2082
2103
2083
- fn compare_to_default_rustdoc ( & mut self , out_dir : & Utf8Path ) {
2104
+ fn compare_to_default_rustdoc ( & self , out_dir : & Utf8Path ) {
2084
2105
if !self . config . has_html_tidy {
2085
2106
return ;
2086
2107
}
@@ -2957,7 +2978,8 @@ pub struct ProcRes {
2957
2978
}
2958
2979
2959
2980
impl ProcRes {
2960
- pub fn print_info ( & self ) {
2981
+ #[ must_use]
2982
+ pub fn format_info ( & self ) -> String {
2961
2983
fn render ( name : & str , contents : & str ) -> String {
2962
2984
let contents = json:: extract_rendered ( contents) ;
2963
2985
let contents = contents. trim_end ( ) ;
@@ -2973,24 +2995,13 @@ impl ProcRes {
2973
2995
}
2974
2996
}
2975
2997
2976
- println ! (
2998
+ format ! (
2977
2999
"status: {}\n command: {}\n {}\n {}\n " ,
2978
3000
self . status,
2979
3001
self . cmdline,
2980
3002
render( "stdout" , & self . stdout) ,
2981
3003
render( "stderr" , & self . stderr) ,
2982
- ) ;
2983
- }
2984
-
2985
- pub fn fatal ( & self , err : Option < & str > , on_failure : impl FnOnce ( ) ) -> ! {
2986
- if let Some ( e) = err {
2987
- println ! ( "\n error: {}" , e) ;
2988
- }
2989
- self . print_info ( ) ;
2990
- on_failure ( ) ;
2991
- // Use resume_unwind instead of panic!() to prevent a panic message + backtrace from
2992
- // compiletest, which is unnecessary noise.
2993
- std:: panic:: resume_unwind ( Box :: new ( ( ) ) ) ;
3004
+ )
2994
3005
}
2995
3006
}
2996
3007
0 commit comments