1
1
use regex:: Regex ;
2
2
use similar:: TextDiff ;
3
- use std:: path:: Path ;
3
+ use std:: path:: { Path , PathBuf } ;
4
4
5
5
use crate :: drop_bomb:: DropBomb ;
6
6
@@ -17,6 +17,7 @@ pub fn diff() -> Diff {
17
17
pub struct Diff {
18
18
expected : Option < String > ,
19
19
expected_name : Option < String > ,
20
+ expected_file : Option < PathBuf > ,
20
21
actual : Option < String > ,
21
22
actual_name : Option < String > ,
22
23
normalizers : Vec < ( String , String ) > ,
@@ -30,6 +31,7 @@ impl Diff {
30
31
Self {
31
32
expected : None ,
32
33
expected_name : None ,
34
+ expected_file : None ,
33
35
actual : None ,
34
36
actual_name : None ,
35
37
normalizers : Vec :: new ( ) ,
@@ -43,6 +45,7 @@ impl Diff {
43
45
let content = std:: fs:: read_to_string ( path) . expect ( "failed to read file" ) ;
44
46
let name = path. to_string_lossy ( ) . to_string ( ) ;
45
47
48
+ self . expected_file = Some ( path. into ( ) ) ;
46
49
self . expected = Some ( content) ;
47
50
self . expected_name = Some ( name) ;
48
51
self
@@ -104,6 +107,15 @@ impl Diff {
104
107
. to_string ( ) ;
105
108
106
109
if !output. is_empty ( ) {
110
+ // If we can bless (meaning we have a file to write into and the `RUSTC_BLESS_TEST`
111
+ // environment variable set), then we write into the file and return.
112
+ if let Some ( ref expected_file) = self . expected_file {
113
+ if std:: env:: var ( "RUSTC_BLESS_TEST" ) . is_ok ( ) {
114
+ println ! ( "Blessing `{}`" , expected_file. display( ) ) ;
115
+ std:: fs:: write ( expected_file, actual) . unwrap ( ) ;
116
+ return ;
117
+ }
118
+ }
107
119
panic ! (
108
120
"test failed: `{}` is different from `{}`\n \n {}" ,
109
121
expected_name, actual_name, output
0 commit comments