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
+ use cargo_test_macro:: cargo_test;
26
27
use rustfix:: apply_suggestions;
27
28
use std:: collections:: HashSet ;
28
29
use std:: env;
29
30
use std:: ffi:: OsString ;
30
31
use std:: fs;
31
- use std:: path:: { Path , PathBuf } ;
32
+ use std:: path:: Path ;
32
33
use std:: process:: { Command , Output } ;
33
34
use tempfile:: tempdir;
34
- use tracing:: { debug , info, warn } ;
35
+ use tracing:: info;
35
36
36
37
mod fixmode {
37
38
pub const EVERYTHING : & str = "yolo" ;
@@ -45,26 +46,6 @@ mod settings {
45
46
pub const BLESS : & str = "RUSTFIX_TEST_BLESS" ;
46
47
}
47
48
48
- static mut VERSION : ( u32 , bool ) = ( 0 , false ) ;
49
-
50
- // Temporarily copy from `cargo_test_macro::version`.
51
- fn version ( ) -> ( u32 , bool ) {
52
- static INIT : std:: sync:: Once = std:: sync:: Once :: new ( ) ;
53
- INIT . call_once ( || {
54
- let output = Command :: new ( "rustc" )
55
- . arg ( "-V" )
56
- . output ( )
57
- . expect ( "cargo should run" ) ;
58
- let stdout = std:: str:: from_utf8 ( & output. stdout ) . expect ( "utf8" ) ;
59
- let vers = stdout. split_whitespace ( ) . skip ( 1 ) . next ( ) . unwrap ( ) ;
60
- let is_nightly = option_env ! ( "CARGO_TEST_DISABLE_NIGHTLY" ) . is_none ( )
61
- && ( vers. contains ( "-nightly" ) || vers. contains ( "-dev" ) ) ;
62
- let minor = vers. split ( '.' ) . skip ( 1 ) . next ( ) . unwrap ( ) . parse ( ) . unwrap ( ) ;
63
- unsafe { VERSION = ( minor, is_nightly) }
64
- } ) ;
65
- unsafe { VERSION }
66
- }
67
-
68
49
fn compile ( file : & Path ) -> Result < Output , Error > {
69
50
let tmp = tempdir ( ) ?;
70
51
@@ -151,7 +132,7 @@ fn diff(expected: &str, actual: &str) -> String {
151
132
res
152
133
}
153
134
154
- fn test_rustfix_with_file < P : AsRef < Path > > ( file : P , mode : & str ) -> Result < ( ) , Error > {
135
+ fn test_rustfix_with_file < P : AsRef < Path > > ( file : P , mode : & str ) {
155
136
let file: & Path = file. as_ref ( ) ;
156
137
let json_file = file. with_extension ( "json" ) ;
157
138
let fixed_file = file. with_extension ( "fixed.rs" ) ;
@@ -162,26 +143,25 @@ fn test_rustfix_with_file<P: AsRef<Path>>(file: P, mode: &str) -> Result<(), Err
162
143
rustfix:: Filter :: MachineApplicableOnly
163
144
} ;
164
145
165
- debug ! ( "next up: {:?}" , file) ;
166
- let code = fs:: read_to_string ( file) ?;
146
+ let code = fs:: read_to_string ( file) . unwrap ( ) ;
167
147
let errors = compile_and_get_json_errors ( file)
168
- . with_context ( || format ! ( "could not compile {}" , file. display( ) ) ) ? ;
148
+ . with_context ( || format ! ( "could not compile {}" , file. display( ) ) ) . unwrap ( ) ;
169
149
let suggestions =
170
150
rustfix:: get_suggestions_from_json ( & errors, & HashSet :: new ( ) , filter_suggestions)
171
- . context ( "could not load suggestions" ) ? ;
151
+ . context ( "could not load suggestions" ) . unwrap ( ) ;
172
152
173
153
if std:: env:: var ( settings:: RECORD_JSON ) . is_ok ( ) {
174
- fs:: write ( file. with_extension ( "recorded.json" ) , & errors) ? ;
154
+ fs:: write ( file. with_extension ( "recorded.json" ) , & errors) . unwrap ( ) ;
175
155
}
176
156
177
157
if std:: env:: var ( settings:: CHECK_JSON ) . is_ok ( ) {
178
158
let expected_json = fs:: read_to_string ( & json_file)
179
- . with_context ( || format ! ( "could not load json fixtures for {}" , file. display( ) ) ) ? ;
159
+ . with_context ( || format ! ( "could not load json fixtures for {}" , file. display( ) ) ) . unwrap ( ) ;
180
160
let expected_suggestions =
181
161
rustfix:: get_suggestions_from_json ( & expected_json, & HashSet :: new ( ) , filter_suggestions)
182
- . context ( "could not load expected suggestions" ) ? ;
162
+ . context ( "could not load expected suggestions" ) . unwrap ( ) ;
183
163
184
- ensure ! (
164
+ assert ! (
185
165
expected_suggestions == suggestions,
186
166
"got unexpected suggestions from clippy:\n {}" ,
187
167
diff(
@@ -192,86 +172,64 @@ fn test_rustfix_with_file<P: AsRef<Path>>(file: P, mode: &str) -> Result<(), Err
192
172
}
193
173
194
174
let fixed = apply_suggestions ( & code, & suggestions)
195
- . with_context ( || format ! ( "could not apply suggestions to {}" , file. display( ) ) ) ?
175
+ . with_context ( || format ! ( "could not apply suggestions to {}" , file. display( ) ) ) . unwrap ( )
196
176
. replace ( '\r' , "" ) ;
197
177
198
178
if std:: env:: var ( settings:: RECORD_FIXED_RUST ) . is_ok ( ) {
199
- fs:: write ( file. with_extension ( "recorded.rs" ) , & fixed) ? ;
179
+ fs:: write ( file. with_extension ( "recorded.rs" ) , & fixed) . unwrap ( ) ;
200
180
}
201
181
202
182
if let Some ( bless_name) = std:: env:: var_os ( settings:: BLESS ) {
203
183
if bless_name == file. file_name ( ) . unwrap ( ) {
204
- std:: fs:: write ( & json_file, & errors) ? ;
205
- std:: fs:: write ( & fixed_file, & fixed) ? ;
184
+ std:: fs:: write ( & json_file, & errors) . unwrap ( ) ;
185
+ std:: fs:: write ( & fixed_file, & fixed) . unwrap ( ) ;
206
186
}
207
187
}
208
188
209
189
let expected_fixed = fs:: read_to_string ( & fixed_file)
210
- . with_context ( || format ! ( "could read fixed file for {}" , file. display( ) ) ) ?
190
+ . with_context ( || format ! ( "could read fixed file for {}" , file. display( ) ) ) . unwrap ( )
211
191
. replace ( '\r' , "" ) ;
212
- ensure ! (
192
+ assert ! (
213
193
fixed. trim( ) == expected_fixed. trim( ) ,
214
194
"file {} doesn't look fixed:\n {}" ,
215
195
file. display( ) ,
216
196
diff( fixed. trim( ) , expected_fixed. trim( ) )
217
197
) ;
218
198
219
- compiles_without_errors ( & fixed_file) ? ;
199
+ compiles_without_errors ( & fixed_file) . unwrap ( ) ;
220
200
221
- Ok ( ( ) )
222
201
}
223
202
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 ;
203
+ macro_rules! run_test {
204
+ ( $name: ident, $file: expr) => {
205
+ #[ allow( non_snake_case) ]
206
+ #[ cargo_test]
207
+ fn $name( ) {
208
+ let file = Path :: new( concat!( "./tests/everything/" , $file) ) ;
209
+ assert!( file. is_file( ) , "could not load {}" , $file) ;
210
+ test_rustfix_with_file( file, fixmode:: EVERYTHING ) ;
254
211
}
255
- if let Err ( err) = test_rustfix_with_file ( file, mode) {
256
- println ! ( "failed: {}" , file. display( ) ) ;
257
- warn ! ( "{:?}" , err) ;
258
- failures += 1 ;
212
+ } ;
213
+ ( $name: ident, $file: expr, nightly=$reason: expr) => {
214
+ #[ allow( non_snake_case) ]
215
+ #[ cargo_test( nightly, reason = $reason) ]
216
+ fn $name( ) {
217
+ let file = Path :: new( concat!( "./tests/everything/" , $file) ) ;
218
+ assert!( file. is_file( ) , "could not load {}" , $file) ;
219
+ test_rustfix_with_file( file, fixmode:: EVERYTHING ) ;
259
220
}
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
- }
221
+ } ;
271
222
}
272
223
273
- #[ test]
274
- fn everything ( ) {
275
- tracing_subscriber:: fmt:: init ( ) ;
276
- assert_fixtures ( "./tests/everything" , fixmode:: EVERYTHING ) ;
224
+ run_test ! {
225
+ closure_immutable_outer_variable,
226
+ "closure-immutable-outer-variable.rs"
277
227
}
228
+ run_test ! { dedup_suggestions, "dedup-suggestions.rs" }
229
+ run_test ! { E0178 , "E0178.rs" }
230
+ run_test ! { handle_insert_only, "handle-insert-only.rs" }
231
+ run_test ! { lt_generic_comp, "lt-generic-comp.rs" }
232
+ run_test ! { multiple_solutions, "multiple-solutions.nightly.rs" , nightly="requires nightly" }
233
+ run_test ! { replace_only_one_char, "replace-only-one-char.rs" }
234
+ run_test ! { str_lit_type_mismatch, "str-lit-type-mismatch.rs" }
235
+ run_test ! { use_insert, "use-insert.rs" }
0 commit comments