22
22
23
23
#![ allow( clippy:: disallowed_methods, clippy:: print_stdout, clippy:: print_stderr) ]
24
24
25
- use anyhow:: { anyhow, ensure , Context , Error } ;
25
+ use anyhow:: { anyhow, Context , Error } ;
26
26
use rustfix:: apply_suggestions;
27
27
use std:: collections:: HashSet ;
28
28
use std:: env;
29
29
use std:: ffi:: OsString ;
30
30
use std:: fs;
31
- use std:: path:: { Path , PathBuf } ;
31
+ use std:: path:: Path ;
32
32
use std:: process:: { Command , Output } ;
33
33
use tempfile:: tempdir;
34
- use tracing:: { debug , info, warn } ;
34
+ use tracing:: info;
35
35
36
36
mod fixmode {
37
37
pub const EVERYTHING : & str = "yolo" ;
@@ -151,7 +151,7 @@ fn diff(expected: &str, actual: &str) -> String {
151
151
res
152
152
}
153
153
154
- fn test_rustfix_with_file < P : AsRef < Path > > ( file : P , mode : & str ) -> Result < ( ) , Error > {
154
+ fn test_rustfix_with_file < P : AsRef < Path > > ( file : P , mode : & str ) {
155
155
let file: & Path = file. as_ref ( ) ;
156
156
let json_file = file. with_extension ( "json" ) ;
157
157
let fixed_file = file. with_extension ( "fixed.rs" ) ;
@@ -162,26 +162,25 @@ fn test_rustfix_with_file<P: AsRef<Path>>(file: P, mode: &str) -> Result<(), Err
162
162
rustfix:: Filter :: MachineApplicableOnly
163
163
} ;
164
164
165
- debug ! ( "next up: {:?}" , file) ;
166
- let code = fs:: read_to_string ( file) ?;
165
+ let code = fs:: read_to_string ( file) . unwrap ( ) ;
167
166
let errors = compile_and_get_json_errors ( file)
168
- . with_context ( || format ! ( "could not compile {}" , file. display( ) ) ) ? ;
167
+ . with_context ( || format ! ( "could not compile {}" , file. display( ) ) ) . unwrap ( ) ;
169
168
let suggestions =
170
169
rustfix:: get_suggestions_from_json ( & errors, & HashSet :: new ( ) , filter_suggestions)
171
- . context ( "could not load suggestions" ) ? ;
170
+ . context ( "could not load suggestions" ) . unwrap ( ) ;
172
171
173
172
if std:: env:: var ( settings:: RECORD_JSON ) . is_ok ( ) {
174
- fs:: write ( file. with_extension ( "recorded.json" ) , & errors) ? ;
173
+ fs:: write ( file. with_extension ( "recorded.json" ) , & errors) . unwrap ( ) ;
175
174
}
176
175
177
176
if std:: env:: var ( settings:: CHECK_JSON ) . is_ok ( ) {
178
177
let expected_json = fs:: read_to_string ( & json_file)
179
- . with_context ( || format ! ( "could not load json fixtures for {}" , file. display( ) ) ) ? ;
178
+ . with_context ( || format ! ( "could not load json fixtures for {}" , file. display( ) ) ) . unwrap ( ) ;
180
179
let expected_suggestions =
181
180
rustfix:: get_suggestions_from_json ( & expected_json, & HashSet :: new ( ) , filter_suggestions)
182
- . context ( "could not load expected suggestions" ) ? ;
181
+ . context ( "could not load expected suggestions" ) . unwrap ( ) ;
183
182
184
- ensure ! (
183
+ assert ! (
185
184
expected_suggestions == suggestions,
186
185
"got unexpected suggestions from clippy:\n {}" ,
187
186
diff(
@@ -192,86 +191,58 @@ fn test_rustfix_with_file<P: AsRef<Path>>(file: P, mode: &str) -> Result<(), Err
192
191
}
193
192
194
193
let fixed = apply_suggestions ( & code, & suggestions)
195
- . with_context ( || format ! ( "could not apply suggestions to {}" , file. display( ) ) ) ?
194
+ . with_context ( || format ! ( "could not apply suggestions to {}" , file. display( ) ) ) . unwrap ( )
196
195
. replace ( '\r' , "" ) ;
197
196
198
197
if std:: env:: var ( settings:: RECORD_FIXED_RUST ) . is_ok ( ) {
199
- fs:: write ( file. with_extension ( "recorded.rs" ) , & fixed) ? ;
198
+ fs:: write ( file. with_extension ( "recorded.rs" ) , & fixed) . unwrap ( ) ;
200
199
}
201
200
202
201
if let Some ( bless_name) = std:: env:: var_os ( settings:: BLESS ) {
203
202
if bless_name == file. file_name ( ) . unwrap ( ) {
204
- std:: fs:: write ( & json_file, & errors) ? ;
205
- std:: fs:: write ( & fixed_file, & fixed) ? ;
203
+ std:: fs:: write ( & json_file, & errors) . unwrap ( ) ;
204
+ std:: fs:: write ( & fixed_file, & fixed) . unwrap ( ) ;
206
205
}
207
206
}
208
207
209
208
let expected_fixed = fs:: read_to_string ( & fixed_file)
210
- . with_context ( || format ! ( "could read fixed file for {}" , file. display( ) ) ) ?
209
+ . with_context ( || format ! ( "could read fixed file for {}" , file. display( ) ) ) . unwrap ( )
211
210
. replace ( '\r' , "" ) ;
212
- ensure ! (
211
+ assert ! (
213
212
fixed. trim( ) == expected_fixed. trim( ) ,
214
213
"file {} doesn't look fixed:\n {}" ,
215
214
file. display( ) ,
216
215
diff( fixed. trim( ) , expected_fixed. trim( ) )
217
216
) ;
218
217
219
- compiles_without_errors ( & fixed_file) ? ;
218
+ compiles_without_errors ( & fixed_file) . unwrap ( ) ;
220
219
221
- Ok ( ( ) )
222
220
}
223
221
224
- fn get_fixture_files ( p : & str ) -> Result < Vec < PathBuf > , Error > {
225
- Ok ( fs:: read_dir ( p) ?
226
- . map ( |e| e. unwrap ( ) . path ( ) )
227
- . filter ( |p| p. is_file ( ) )
228
- . filter ( |p| {
229
- let x = p. to_string_lossy ( ) ;
230
- x. ends_with ( ".rs" ) && !x. ends_with ( ".fixed.rs" ) && !x. ends_with ( ".recorded.rs" )
231
- } )
232
- . collect ( ) )
233
- }
234
-
235
- fn assert_fixtures ( dir : & str , mode : & str ) {
236
- let files = get_fixture_files ( dir)
237
- . with_context ( || format ! ( "couldn't load dir `{dir}`" ) )
238
- . unwrap ( ) ;
239
- let mut failures = 0 ;
240
-
241
- let is_not_nightly = !version ( ) . 1 ;
242
-
243
- for file in & files {
244
- if file
245
- . file_stem ( )
246
- . unwrap ( )
247
- . to_str ( )
248
- . unwrap ( )
249
- . ends_with ( ".nightly" )
250
- && is_not_nightly
251
- {
252
- info ! ( "skipped: {file:?}" ) ;
253
- continue ;
254
- }
255
- if let Err ( err) = test_rustfix_with_file ( file, mode) {
256
- println ! ( "failed: {}" , file. display( ) ) ;
257
- warn ! ( "{:?}" , err) ;
258
- failures += 1 ;
222
+ macro_rules! run_test {
223
+ ( $name: ident, $file: expr) => {
224
+ #[ test]
225
+ #[ allow( non_snake_case) ]
226
+ fn $name( ) {
227
+ let ( _, nightly) = version( ) ;
228
+ if !$file. ends_with( ".nightly.rs" ) || nightly {
229
+ let file = Path :: new( concat!( "./tests/everything/" , $file) ) ;
230
+ assert!( file. is_file( ) , "could not load {}" , $file) ;
231
+ test_rustfix_with_file( file, fixmode:: EVERYTHING ) ;
232
+ }
259
233
}
260
- info ! ( "passed: {:?}" , file) ;
261
- }
262
-
263
- if failures > 0 {
264
- panic ! (
265
- "{} out of {} fixture asserts failed\n \
266
- (run with `env RUST_LOG=parse_and_replace=info` to get more details)",
267
- failures,
268
- files. len( ) ,
269
- ) ;
270
- }
234
+ } ;
271
235
}
272
236
273
- #[ test]
274
- fn everything ( ) {
275
- tracing_subscriber:: fmt:: init ( ) ;
276
- assert_fixtures ( "./tests/everything" , fixmode:: EVERYTHING ) ;
237
+ run_test ! {
238
+ closure_immutable_outer_variable,
239
+ "closure-immutable-outer-variable.rs"
277
240
}
241
+ run_test ! { dedup_suggestions, "dedup-suggestions.rs" }
242
+ run_test ! { E0178 , "E0178.rs" }
243
+ run_test ! { handle_insert_only, "handle-insert-only.rs" }
244
+ run_test ! { lt_generic_comp, "lt-generic-comp.rs" }
245
+ run_test ! { multiple_solutions, "multiple-solutions.nightly.rs" }
246
+ run_test ! { replace_only_one_char, "replace-only-one-char.rs" }
247
+ run_test ! { str_lit_type_mismatch, "str-lit-type-mismatch.rs" }
248
+ run_test ! { use_insert, "use-insert.rs" }
0 commit comments