@@ -20,7 +20,7 @@ use std::path::{Path, PathBuf};
20
20
21
21
use rustfmt:: * ;
22
22
use rustfmt:: filemap:: { write_system_newlines, FileMap } ;
23
- use rustfmt:: config:: { Config , ReportTactic , WriteMode } ;
23
+ use rustfmt:: config:: { Config , ReportTactic } ;
24
24
use rustfmt:: rustfmt_diff:: * ;
25
25
26
26
const DIFF_CONTEXT_SIZE : usize = 3 ;
@@ -44,7 +44,7 @@ fn system_tests() {
44
44
// Turn a DirEntry into a String that represents the relative path to the
45
45
// file.
46
46
let files = files. map ( get_path_string) ;
47
- let ( _reports, count, fails) = check_files ( files, None ) ;
47
+ let ( _reports, count, fails) = check_files ( files) ;
48
48
49
49
// Display results.
50
50
println ! ( "Ran {} system tests." , count) ;
@@ -55,26 +55,26 @@ fn system_tests() {
55
55
// the only difference is the coverage mode
56
56
#[ test]
57
57
fn coverage_tests ( ) {
58
- let files = fs:: read_dir ( "tests/coverage- source" ) . expect ( "Couldn't read source dir" ) ;
58
+ let files = fs:: read_dir ( "tests/coverage/ source" ) . expect ( "Couldn't read source dir" ) ;
59
59
let files = files. map ( get_path_string) ;
60
- let ( _reports, count, fails) = check_files ( files, Some ( WriteMode :: Coverage ) ) ;
60
+ let ( _reports, count, fails) = check_files ( files) ;
61
61
62
62
println ! ( "Ran {} tests in coverage mode." , count) ;
63
63
assert ! ( fails == 0 , "{} tests failed" , fails) ;
64
64
}
65
65
66
66
#[ test]
67
67
fn checkstyle_test ( ) {
68
- let filename = "tests/source/fn-single-line.rs" ;
69
- let expected_filename = "tests/writemode/checkstyle.xml" ;
70
- assert_output ( filename, expected_filename, Some ( WriteMode :: Checkstyle ) ) ;
68
+ let filename = "tests/writemode/ source/fn-single-line.rs" ;
69
+ let expected_filename = "tests/writemode/target/ checkstyle.xml" ;
70
+ assert_output ( filename, expected_filename) ;
71
71
}
72
72
73
73
74
74
// Helper function for comparing the results of rustfmt
75
75
// to a known output file generated by one of the write modes.
76
- fn assert_output ( source : & str , expected_filename : & str , write_mode : Option < WriteMode > ) {
77
- let config = read_config ( & source, write_mode ) ;
76
+ fn assert_output ( source : & str , expected_filename : & str ) {
77
+ let config = read_config ( & source) ;
78
78
let ( file_map, _report) = format_file ( source, & config) ;
79
79
80
80
// Populate output by writing to a vec.
@@ -104,7 +104,7 @@ fn idempotence_tests() {
104
104
let files = fs:: read_dir ( "tests/target" )
105
105
. expect ( "Couldn't read target dir" )
106
106
. map ( get_path_string) ;
107
- let ( _reports, count, fails) = check_files ( files, None ) ;
107
+ let ( _reports, count, fails) = check_files ( files) ;
108
108
109
109
// Display results.
110
110
println ! ( "Ran {} idempotent tests." , count) ;
@@ -122,7 +122,7 @@ fn self_tests() {
122
122
// Hack because there's no `IntoIterator` impl for `[T; N]`.
123
123
let files = files. chain ( Some ( "src/lib.rs" . to_owned ( ) ) . into_iter ( ) ) ;
124
124
125
- let ( reports, count, fails) = check_files ( files, None ) ;
125
+ let ( reports, count, fails) = check_files ( files) ;
126
126
let mut warnings = 0 ;
127
127
128
128
// Display results.
@@ -141,7 +141,7 @@ fn self_tests() {
141
141
142
142
// For each file, run rustfmt and collect the output.
143
143
// Returns the number of files checked and the number of failures.
144
- fn check_files < I > ( files : I , write_mode : Option < WriteMode > ) -> ( Vec < FormatReport > , u32 , u32 )
144
+ fn check_files < I > ( files : I ) -> ( Vec < FormatReport > , u32 , u32 )
145
145
where I : Iterator < Item = String >
146
146
{
147
147
let mut count = 0 ;
@@ -151,7 +151,7 @@ fn check_files<I>(files: I, write_mode: Option<WriteMode>) -> (Vec<FormatReport>
151
151
for file_name in files. filter ( |f| f. ends_with ( ".rs" ) ) {
152
152
println ! ( "Testing '{}'..." , file_name) ;
153
153
154
- match idempotent_check ( file_name, write_mode ) {
154
+ match idempotent_check ( file_name) {
155
155
Ok ( report) => reports. push ( report) ,
156
156
Err ( msg) => {
157
157
print_mismatches ( msg) ;
@@ -176,7 +176,7 @@ fn print_mismatches(result: HashMap<String, Vec<Mismatch>>) {
176
176
assert ! ( t. reset( ) . unwrap( ) ) ;
177
177
}
178
178
179
- fn read_config ( filename : & str , write_mode : Option < WriteMode > ) -> Config {
179
+ fn read_config ( filename : & str ) -> Config {
180
180
let sig_comments = read_significant_comments ( & filename) ;
181
181
let mut config = get_config ( sig_comments. get ( "config" ) . map ( |x| & ( * x) [ ..] ) ) ;
182
182
@@ -189,10 +189,6 @@ fn read_config(filename: &str, write_mode: Option<WriteMode>) -> Config {
189
189
// Don't generate warnings for to-do items.
190
190
config. report_todo = ReportTactic :: Never ;
191
191
192
- if let Some ( mode) = write_mode {
193
- config. write_mode = mode
194
- }
195
-
196
192
config
197
193
}
198
194
@@ -201,11 +197,9 @@ fn format_file<P: Into<PathBuf>>(filename: P, config: &Config) -> (FileMap, Form
201
197
format_input ( input, & config)
202
198
}
203
199
204
- pub fn idempotent_check ( filename : String ,
205
- write_mode : Option < WriteMode > )
206
- -> Result < FormatReport , HashMap < String , Vec < Mismatch > > > {
200
+ pub fn idempotent_check ( filename : String ) -> Result < FormatReport , HashMap < String , Vec < Mismatch > > > {
207
201
let sig_comments = read_significant_comments ( & filename) ;
208
- let config = read_config ( & filename, write_mode ) ;
202
+ let config = read_config ( & filename) ;
209
203
let ( file_map, format_report) = format_file ( filename, & config) ;
210
204
211
205
let mut write_result = HashMap :: new ( ) ;
@@ -220,7 +214,7 @@ pub fn idempotent_check(filename: String,
220
214
221
215
let target = sig_comments. get ( "target" ) . map ( |x| & ( * x) [ ..] ) ;
222
216
223
- handle_result ( write_result, target, write_mode ) . map ( |_| format_report)
217
+ handle_result ( write_result, target) . map ( |_| format_report)
224
218
}
225
219
226
220
// Reads test config file from comments and reads its contents.
@@ -268,14 +262,13 @@ fn read_significant_comments(file_name: &str) -> HashMap<String, String> {
268
262
// Compare output to input.
269
263
// TODO: needs a better name, more explanation.
270
264
fn handle_result ( result : HashMap < String , String > ,
271
- target : Option < & str > ,
272
- write_mode : Option < WriteMode > )
265
+ target : Option < & str > )
273
266
-> Result < ( ) , HashMap < String , Vec < Mismatch > > > {
274
267
let mut failures = HashMap :: new ( ) ;
275
268
276
269
for ( file_name, fmt_text) in result {
277
270
// If file is in tests/source, compare to file with same name in tests/target.
278
- let target = get_target ( & file_name, target, write_mode ) ;
271
+ let target = get_target ( & file_name, target) ;
279
272
let mut f = fs:: File :: open ( & target) . expect ( "Couldn't open target" ) ;
280
273
281
274
let mut text = String :: new ( ) ;
@@ -297,29 +290,20 @@ fn handle_result(result: HashMap<String, String>,
297
290
}
298
291
299
292
// Map source file paths to their target paths.
300
- fn get_target ( file_name : & str , target : Option < & str > , write_mode : Option < WriteMode > ) -> String {
301
- let file_path = Path :: new ( file_name) ;
302
- let ( source_path_prefix, target_path_prefix) = match write_mode {
303
- Some ( WriteMode :: Coverage ) => {
304
- ( Path :: new ( "tests/coverage-source/" ) , "tests/coverage-target/" )
293
+ fn get_target ( file_name : & str , target : Option < & str > ) -> String {
294
+ if file_name. contains ( "source" ) {
295
+ let target_file_name = file_name. replace ( "source" , "target" ) ;
296
+ if let Some ( replace_name) = target {
297
+ Path :: new ( & target_file_name)
298
+ . with_file_name ( replace_name)
299
+ . into_os_string ( )
300
+ . into_string ( )
301
+ . unwrap ( )
302
+ } else {
303
+ target_file_name
305
304
}
306
- _ => ( Path :: new ( "tests/source/" ) , "tests/target/" ) ,
307
- } ;
308
-
309
- if file_path. starts_with ( source_path_prefix) {
310
- let mut components = file_path. components ( ) ;
311
- // Can't skip(2) as the resulting iterator can't as_path()
312
- components. next ( ) ;
313
- components. next ( ) ;
314
-
315
- let new_target = match components. as_path ( ) . to_str ( ) {
316
- Some ( string) => string,
317
- None => file_name,
318
- } ;
319
- let base = target. unwrap_or ( new_target) ;
320
-
321
- format ! ( "{}{}" , target_path_prefix, base)
322
305
} else {
306
+ // This is either and idempotence check or a self check
323
307
file_name. to_owned ( )
324
308
}
325
309
}
0 commit comments