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
+ use crate :: fs_wrapper;
6
7
7
8
#[ cfg( test) ]
8
9
mod tests;
@@ -17,6 +18,7 @@ pub fn diff() -> Diff {
17
18
pub struct Diff {
18
19
expected : Option < String > ,
19
20
expected_name : Option < String > ,
21
+ expected_file : Option < PathBuf > ,
20
22
actual : Option < String > ,
21
23
actual_name : Option < String > ,
22
24
normalizers : Vec < ( String , String ) > ,
@@ -30,6 +32,7 @@ impl Diff {
30
32
Self {
31
33
expected : None ,
32
34
expected_name : None ,
35
+ expected_file : None ,
33
36
actual : None ,
34
37
actual_name : None ,
35
38
normalizers : Vec :: new ( ) ,
@@ -40,9 +43,10 @@ impl Diff {
40
43
/// Specify the expected output for the diff from a file.
41
44
pub fn expected_file < P : AsRef < Path > > ( & mut self , path : P ) -> & mut Self {
42
45
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) ;
44
47
let name = path. to_string_lossy ( ) . to_string ( ) ;
45
48
49
+ self . expected_file = Some ( path. into ( ) ) ;
46
50
self . expected = Some ( content) ;
47
51
self . expected_name = Some ( name) ;
48
52
self
@@ -58,10 +62,7 @@ impl Diff {
58
62
/// Specify the actual output for the diff from a file.
59
63
pub fn actual_file < P : AsRef < Path > > ( & mut self , path : P ) -> & mut Self {
60
64
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) ;
65
66
let name = path. to_string_lossy ( ) . to_string ( ) ;
66
67
67
68
self . actual = Some ( content) ;
@@ -104,6 +105,15 @@ impl Diff {
104
105
. to_string ( ) ;
105
106
106
107
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
+ }
107
117
panic ! (
108
118
"test failed: `{}` is different from `{}`\n \n {}" ,
109
119
expected_name, actual_name, output
0 commit comments