1
+ //! Tests that verify rustfix applies the appropriate changes to a file.
2
+ //!
3
+ //! This test works by reading a series of `*.rs` files in the
4
+ //! `tests/everything` directory. For each `.rs` file, it runs `rustc` to
5
+ //! collect JSON diagnostics from the file. It feeds that JSON data into
6
+ //! rustfix and applies the recommended suggestions to the `.rs` file. It then
7
+ //! compares the result with the corresponding `.fixed.rs` file. If they don't
8
+ //! match, then the test fails.
9
+ //!
10
+ //! There are several debugging environment variables for this test that you can set:
11
+ //!
12
+ //! - `RUST_LOG=parse_and_replace=debug`: Print debug information.
13
+ //! - `RUSTFIX_TEST_RECORD_JSON=1`: Records the JSON output to
14
+ //! `*.recorded.json` files. You can then move that to `.json` or whatever
15
+ //! you need.
16
+ //! - `RUSTFIX_TEST_RECORD_FIXED_RUST=1`: Records the fixed result to
17
+ //! `*.recorded.rs` files. You can then move that to `.rs` or whatever you
18
+ //! need.
19
+
1
20
#![ allow( clippy:: disallowed_methods, clippy:: print_stdout, clippy:: print_stderr) ]
2
21
3
22
use anyhow:: { anyhow, ensure, Context , Error } ;
@@ -79,15 +98,6 @@ fn compiles_without_errors(file: &Path) -> Result<(), Error> {
79
98
}
80
99
}
81
100
82
- fn read_file ( path : & Path ) -> Result < String , Error > {
83
- use std:: io:: Read ;
84
-
85
- let mut buffer = String :: new ( ) ;
86
- let mut file = fs:: File :: open ( path) ?;
87
- file. read_to_string ( & mut buffer) ?;
88
- Ok ( buffer)
89
- }
90
-
91
101
fn diff ( expected : & str , actual : & str ) -> String {
92
102
use similar:: { ChangeTag , TextDiff } ;
93
103
use std:: fmt:: Write ;
@@ -104,11 +114,7 @@ fn diff(expected: &str, actual: &str) -> String {
104
114
ChangeTag :: Delete => "-" ,
105
115
} ;
106
116
if !different {
107
- write ! (
108
- & mut res,
109
- "differences found (+ == actual, - == expected):\n "
110
- )
111
- . unwrap ( ) ;
117
+ writeln ! ( & mut res, "differences found (+ == actual, - == expected):" ) . unwrap ( ) ;
112
118
different = true ;
113
119
}
114
120
write ! ( & mut res, "{} {}" , prefix, change. value( ) ) . unwrap ( ) ;
@@ -133,23 +139,19 @@ fn test_rustfix_with_file<P: AsRef<Path>>(file: P, mode: &str) -> Result<(), Err
133
139
} ;
134
140
135
141
debug ! ( "next up: {:?}" , file) ;
136
- let code = read_file ( file) . context ( format ! ( "could not read {}" , file . display ( ) ) ) ?;
142
+ let code = fs :: read_to_string ( file) ?;
137
143
let errors =
138
144
compile_and_get_json_errors ( file) . context ( format ! ( "could compile {}" , file. display( ) ) ) ?;
139
145
let suggestions =
140
146
rustfix:: get_suggestions_from_json ( & errors, & HashSet :: new ( ) , filter_suggestions)
141
147
. context ( "could not load suggestions" ) ?;
142
148
143
149
if std:: env:: var ( settings:: RECORD_JSON ) . is_ok ( ) {
144
- use std:: io:: Write ;
145
- let mut recorded_json = fs:: File :: create ( & file. with_extension ( "recorded.json" ) ) . context (
146
- format ! ( "could not create recorded.json for {}" , file. display( ) ) ,
147
- ) ?;
148
- recorded_json. write_all ( errors. as_bytes ( ) ) ?;
150
+ fs:: write ( file. with_extension ( "recorded.json" ) , & errors) ?;
149
151
}
150
152
151
153
if std:: env:: var ( settings:: CHECK_JSON ) . is_ok ( ) {
152
- let expected_json = read_file ( & json_file) . context ( format ! (
154
+ let expected_json = fs :: read_to_string ( & json_file) . context ( format ! (
153
155
"could not load json fixtures for {}" ,
154
156
file. display( )
155
157
) ) ?;
@@ -171,13 +173,11 @@ fn test_rustfix_with_file<P: AsRef<Path>>(file: P, mode: &str) -> Result<(), Err
171
173
. context ( format ! ( "could not apply suggestions to {}" , file. display( ) ) ) ?;
172
174
173
175
if std:: env:: var ( settings:: RECORD_FIXED_RUST ) . is_ok ( ) {
174
- use std:: io:: Write ;
175
- let mut recorded_rust = fs:: File :: create ( & file. with_extension ( "recorded.rs" ) ) ?;
176
- recorded_rust. write_all ( fixed. as_bytes ( ) ) ?;
176
+ fs:: write ( file. with_extension ( "recorded.rs" ) , & fixed) ?;
177
177
}
178
178
179
- let expected_fixed =
180
- read_file ( & fixed_file ) . context ( format ! ( "could read fixed file for {}" , file. display( ) ) ) ?;
179
+ let expected_fixed = fs :: read_to_string ( & fixed_file )
180
+ . context ( format ! ( "could read fixed file for {}" , file. display( ) ) ) ?;
181
181
ensure ! (
182
182
fixed. trim( ) == expected_fixed. trim( ) ,
183
183
"file {} doesn't look fixed:\n {}" ,
@@ -191,8 +191,7 @@ fn test_rustfix_with_file<P: AsRef<Path>>(file: P, mode: &str) -> Result<(), Err
191
191
}
192
192
193
193
fn get_fixture_files ( p : & str ) -> Result < Vec < PathBuf > , Error > {
194
- Ok ( fs:: read_dir ( & p) ?
195
- . into_iter ( )
194
+ Ok ( fs:: read_dir ( p) ?
196
195
. map ( |e| e. unwrap ( ) . path ( ) )
197
196
. filter ( |p| p. is_file ( ) )
198
197
. filter ( |p| {
@@ -203,7 +202,7 @@ fn get_fixture_files(p: &str) -> Result<Vec<PathBuf>, Error> {
203
202
}
204
203
205
204
fn assert_fixtures ( dir : & str , mode : & str ) {
206
- let files = get_fixture_files ( & dir)
205
+ let files = get_fixture_files ( dir)
207
206
. context ( format ! ( "couldn't load dir `{}`" , dir) )
208
207
. unwrap ( ) ;
209
208
let mut failures = 0 ;
0 commit comments