11use regex:: Regex ;
22use similar:: TextDiff ;
3- use std:: path:: Path ;
3+ use std:: path:: { Path , PathBuf } ;
44
55use crate :: drop_bomb:: DropBomb ;
6+ use crate :: fs_wrapper;
67
78#[ cfg( test) ]
89mod tests;
@@ -17,6 +18,7 @@ pub fn diff() -> Diff {
1718pub struct Diff {
1819 expected : Option < String > ,
1920 expected_name : Option < String > ,
21+ expected_file : Option < PathBuf > ,
2022 actual : Option < String > ,
2123 actual_name : Option < String > ,
2224 normalizers : Vec < ( String , String ) > ,
@@ -30,6 +32,7 @@ impl Diff {
3032 Self {
3133 expected : None ,
3234 expected_name : None ,
35+ expected_file : None ,
3336 actual : None ,
3437 actual_name : None ,
3538 normalizers : Vec :: new ( ) ,
@@ -40,9 +43,10 @@ impl Diff {
4043 /// Specify the expected output for the diff from a file.
4144 pub fn expected_file < P : AsRef < Path > > ( & mut self , path : P ) -> & mut Self {
4245 let path = path. as_ref ( ) ;
43- let content = std :: fs :: read_to_string ( path) . expect ( "failed to read file" ) ;
46+ let content = fs_wrapper :: read_to_string ( path) ;
4447 let name = path. to_string_lossy ( ) . to_string ( ) ;
4548
49+ self . expected_file = Some ( path. into ( ) ) ;
4650 self . expected = Some ( content) ;
4751 self . expected_name = Some ( name) ;
4852 self
@@ -58,10 +62,7 @@ impl Diff {
5862 /// Specify the actual output for the diff from a file.
5963 pub fn actual_file < P : AsRef < Path > > ( & mut self , path : P ) -> & mut Self {
6064 let path = path. as_ref ( ) ;
61- let content = match std:: fs:: read_to_string ( path) {
62- Ok ( c) => c,
63- Err ( e) => panic ! ( "failed to read `{}`: {:?}" , path. display( ) , e) ,
64- } ;
65+ let content = fs_wrapper:: read_to_string ( path) ;
6566 let name = path. to_string_lossy ( ) . to_string ( ) ;
6667
6768 self . actual = Some ( content) ;
@@ -104,6 +105,15 @@ impl Diff {
104105 . to_string ( ) ;
105106
106107 if !output. is_empty ( ) {
108+ // If we can bless (meaning we have a file to write into and the `RUSTC_BLESS_TEST`
109+ // environment variable set), then we write into the file and return.
110+ if let Some ( ref expected_file) = self . expected_file {
111+ if std:: env:: var ( "RUSTC_BLESS_TEST" ) . is_ok ( ) {
112+ println ! ( "Blessing `{}`" , expected_file. display( ) ) ;
113+ fs_wrapper:: write ( expected_file, actual) ;
114+ return ;
115+ }
116+ }
107117 panic ! (
108118 "test failed: `{}` is different from `{}`\n \n {}" ,
109119 expected_name, actual_name, output
0 commit comments