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