11use regex:: Regex ;
22use similar:: TextDiff ;
3- use std:: path:: Path ;
3+ use std:: path:: { Path , PathBuf } ;
44
55#[ cfg( test) ]
66mod tests;
@@ -13,6 +13,7 @@ pub fn diff() -> Diff {
1313pub struct Diff {
1414 expected : Option < String > ,
1515 expected_name : Option < String > ,
16+ expected_file : Option < PathBuf > ,
1617 actual : Option < String > ,
1718 actual_name : Option < String > ,
1819 normalizers : Vec < ( String , String ) > ,
@@ -24,6 +25,7 @@ impl Diff {
2425 Self {
2526 expected : None ,
2627 expected_name : None ,
28+ expected_file : None ,
2729 actual : None ,
2830 actual_name : None ,
2931 normalizers : Vec :: new ( ) ,
@@ -36,6 +38,7 @@ impl Diff {
3638 let content = std:: fs:: read_to_string ( path) . expect ( "failed to read file" ) ;
3739 let name = path. to_string_lossy ( ) . to_string ( ) ;
3840
41+ self . expected_file = Some ( path. into ( ) ) ;
3942 self . expected = Some ( content) ;
4043 self . expected_name = Some ( name) ;
4144 self
@@ -97,6 +100,15 @@ impl Diff {
97100 . to_string ( ) ;
98101
99102 if !output. is_empty ( ) {
103+ // If we can bless (meaning we have a file to write into and the `RUSTC_BLESS_TEST`
104+ // environment variable set), then we write into the file and return.
105+ if let Some ( ref expected_file) = self . expected_file {
106+ if std:: env:: var ( "RUSTC_BLESS_TEST" ) . is_ok ( ) {
107+ println ! ( "Blessing `{}`" , expected_file. display( ) ) ;
108+ std:: fs:: write ( expected_file, actual) . unwrap ( ) ;
109+ return ;
110+ }
111+ }
100112 panic ! (
101113 "test failed: `{}` is different from `{}`\n \n {}" ,
102114 expected_name, actual_name, output
0 commit comments