9
9
// except according to those terms.
10
10
11
11
use common:: { Config , TestPaths } ;
12
- use common:: { UI_FIXED , UI_STDERR , UI_STDOUT } ;
12
+ use common:: { expected_output_path , UI_FIXED , UI_STDERR , UI_STDOUT } ;
13
13
use common:: { CompileFail , ParseFail , Pretty , RunFail , RunPass , RunPassValgrind } ;
14
14
use common:: { Codegen , DebugInfoLldb , DebugInfoGdb , Rustdoc , CodegenUnits } ;
15
15
use common:: { Incremental , RunMake , Ui , MirOpt } ;
@@ -20,7 +20,7 @@ use json;
20
20
use regex:: Regex ;
21
21
use rustfix:: { apply_suggestions, get_suggestions_from_json, Filter } ;
22
22
use header:: TestProps ;
23
- use util:: logv;
23
+ use crate :: util:: { logv, PathBufExt } ;
24
24
25
25
use std:: collections:: HashMap ;
26
26
use std:: collections:: HashSet ;
@@ -2167,6 +2167,18 @@ actual:\n\
2167
2167
// compiler flags set in the test cases:
2168
2168
cmd. env_remove ( "RUSTFLAGS" ) ;
2169
2169
2170
+ if self . config . bless {
2171
+ cmd. env ( "RUSTC_BLESS_TEST" , "--bless" ) ;
2172
+ // Assume this option is active if the environment variable is "defined", with _any_ value.
2173
+ // As an example, a `Makefile` can use this option by:
2174
+ //
2175
+ // ifdef RUSTC_BLESS_TEST
2176
+ // cp "$(TMPDIR)"/actual_something.ext expected_something.ext
2177
+ // else
2178
+ // $(DIFF) expected_something.ext "$(TMPDIR)"/actual_something.ext
2179
+ // endif
2180
+ }
2181
+
2170
2182
if self . config . target . contains ( "msvc" ) {
2171
2183
// We need to pass a path to `lib.exe`, so assume that `cc` is `cl.exe`
2172
2184
// and that `lib.exe` lives next to it.
@@ -2323,16 +2335,17 @@ actual:\n\
2323
2335
}
2324
2336
2325
2337
if errors > 0 {
2326
- println ! ( "To update references, run this command from build directory: " ) ;
2338
+ println ! ( "To update references, rerun the tests and pass the `--bless` flag " ) ;
2327
2339
let relative_path_to_file =
2328
- self . testpaths . relative_dir
2329
- . join ( self . testpaths . file . file_name ( ) . unwrap ( ) ) ;
2330
- println ! ( "{}/update-references.sh '{}' '{}'" ,
2331
- self . config. src_base. display( ) ,
2332
- self . config. build_base. display( ) ,
2333
- relative_path_to_file. display( ) ) ;
2334
- self . fatal_proc_rec ( & format ! ( "{} errors occurred comparing output." , errors) ,
2335
- & proc_res) ;
2340
+ self . testpaths . relative_dir . join ( self . testpaths . file . file_name ( ) . unwrap ( ) ) ;
2341
+ println ! (
2342
+ "To only update this specific test, also pass `--test-args {}`" ,
2343
+ relative_path_to_file. display( ) ,
2344
+ ) ;
2345
+ self . fatal_proc_rec (
2346
+ & format ! ( "{} errors occurred comparing output." , errors) ,
2347
+ & proc_res,
2348
+ ) ;
2336
2349
}
2337
2350
2338
2351
if self . props . run_pass {
@@ -2566,11 +2579,14 @@ actual:\n\
2566
2579
}
2567
2580
2568
2581
fn expected_output_path ( & self , kind : & str ) -> PathBuf {
2569
- let extension = match self . revision {
2570
- Some ( r) => format ! ( "{}.{}" , r, kind) ,
2571
- None => kind. to_string ( ) ,
2572
- } ;
2573
- self . testpaths . file . with_extension ( extension)
2582
+ let mut path =
2583
+ expected_output_path ( & self . testpaths , self . revision , kind) ;
2584
+
2585
+ if !path. exists ( ) {
2586
+ path = expected_output_path ( & self . testpaths , self . revision , kind) ;
2587
+ }
2588
+
2589
+ path
2574
2590
}
2575
2591
2576
2592
fn load_expected_output ( & self , path : & Path ) -> String {
@@ -2594,35 +2610,68 @@ actual:\n\
2594
2610
} )
2595
2611
}
2596
2612
2613
+ fn delete_file ( & self , file : & PathBuf ) {
2614
+ if !file. exists ( ) {
2615
+ // Deleting a nonexistant file would error.
2616
+ return ;
2617
+ }
2618
+ if let Err ( e) = fs:: remove_file ( file) {
2619
+ self . fatal ( & format ! ( "failed to delete `{}`: {}" , file. display( ) , e, ) ) ;
2620
+ }
2621
+ }
2622
+
2597
2623
fn compare_output ( & self , kind : & str , actual : & str , expected : & str ) -> usize {
2598
2624
if actual == expected {
2599
2625
return 0 ;
2600
2626
}
2601
2627
2602
- println ! ( "normalized {}:\n {}\n " , kind, actual) ;
2603
- println ! ( "expected {}:\n {}\n " , kind, expected) ;
2604
- println ! ( "diff of {}:\n " , kind) ;
2605
-
2606
- for diff in diff:: lines ( expected, actual) {
2607
- match diff {
2608
- diff:: Result :: Left ( l) => println ! ( "-{}" , l) ,
2609
- diff:: Result :: Both ( l, _) => println ! ( " {}" , l) ,
2610
- diff:: Result :: Right ( r) => println ! ( "+{}" , r) ,
2628
+ if !self . config . bless {
2629
+ if expected. is_empty ( ) {
2630
+ println ! ( "normalized {}:\n {}\n " , kind, actual) ;
2631
+ } else {
2632
+ println ! ( "diff of {}:\n " , kind) ;
2633
+ for diff in diff:: lines ( expected, actual) {
2634
+ match diff {
2635
+ diff:: Result :: Left ( l) => println ! ( "-{}" , l) ,
2636
+ diff:: Result :: Both ( l, _) => println ! ( " {}" , l) ,
2637
+ diff:: Result :: Right ( r) => println ! ( "+{}" , r) ,
2638
+ }
2639
+ }
2611
2640
}
2612
2641
}
2613
2642
2614
- let output_file = self . output_base_name ( ) . with_extension ( kind) ;
2615
- match File :: create ( & output_file) . and_then ( |mut f| f. write_all ( actual. as_bytes ( ) ) ) {
2616
- Ok ( ( ) ) => { }
2617
- Err ( e) => {
2618
- self . fatal ( & format ! ( "failed to write {} to `{}`: {}" ,
2619
- kind, output_file. display( ) , e) )
2643
+ let output_file = self
2644
+ . output_base_name ( )
2645
+ . with_extra_extension ( self . revision . unwrap_or ( "" ) )
2646
+ . with_extra_extension ( kind) ;
2647
+
2648
+ let mut files = vec ! [ output_file] ;
2649
+ if self . config . bless {
2650
+ files. push ( expected_output_path (
2651
+ self . testpaths ,
2652
+ self . revision ,
2653
+ kind,
2654
+ ) ) ;
2655
+ }
2656
+
2657
+ for output_file in & files {
2658
+ if actual. is_empty ( ) {
2659
+ self . delete_file ( output_file) ;
2660
+ } else if let Err ( err) = fs:: write ( & output_file, & actual) {
2661
+ self . fatal ( & format ! (
2662
+ "failed to write {} to `{}`: {}" ,
2663
+ kind,
2664
+ output_file. display( ) ,
2665
+ err,
2666
+ ) ) ;
2620
2667
}
2621
2668
}
2622
2669
2623
2670
println ! ( "\n The actual {0} differed from the expected {0}." , kind) ;
2624
- println ! ( "Actual {} saved to {}" , kind, output_file. display( ) ) ;
2625
- 1
2671
+ for output_file in files {
2672
+ println ! ( "Actual {} saved to {}" , kind, output_file. display( ) ) ;
2673
+ }
2674
+ if self . config . bless { 0 } else { 1 }
2626
2675
}
2627
2676
}
2628
2677
0 commit comments