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
#[ cfg( test) ]
6
6
mod tests;
@@ -13,6 +13,7 @@ pub fn diff() -> Diff {
13
13
pub struct Diff {
14
14
expected : Option < String > ,
15
15
expected_name : Option < String > ,
16
+ expected_file : Option < PathBuf > ,
16
17
actual : Option < String > ,
17
18
actual_name : Option < String > ,
18
19
normalizers : Vec < ( String , String ) > ,
@@ -24,6 +25,7 @@ impl Diff {
24
25
Self {
25
26
expected : None ,
26
27
expected_name : None ,
28
+ expected_file : None ,
27
29
actual : None ,
28
30
actual_name : None ,
29
31
normalizers : Vec :: new ( ) ,
@@ -36,6 +38,7 @@ impl Diff {
36
38
let content = std:: fs:: read_to_string ( path) . expect ( "failed to read file" ) ;
37
39
let name = path. to_string_lossy ( ) . to_string ( ) ;
38
40
41
+ self . expected_file = Some ( path. into ( ) ) ;
39
42
self . expected = Some ( content) ;
40
43
self . expected_name = Some ( name) ;
41
44
self
@@ -97,6 +100,15 @@ impl Diff {
97
100
. to_string ( ) ;
98
101
99
102
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
+ }
100
112
panic ! (
101
113
"test failed: `{}` is different from `{}`\n \n {}" ,
102
114
expected_name, actual_name, output
0 commit comments