@@ -6,15 +6,16 @@ use std::iter::Peekable;
6
6
use std:: mem;
7
7
use std:: path:: { Path , PathBuf } ;
8
8
use std:: process:: { Command , Stdio } ;
9
- use std:: str:: Chars ;
9
+ use std:: str:: { Chars , FromStr } ;
10
10
use std:: thread;
11
11
12
12
use crate :: config:: { Color , Config , EmitMode , FileName , NewlineStyle , ReportTactic } ;
13
13
use crate :: formatting:: { ReportedErrors , SourceFile } ;
14
- use crate :: is_nightly_channel;
15
14
use crate :: rustfmt_diff:: { make_diff, print_diff, DiffLine , Mismatch , ModifiedChunk , OutputWriter } ;
16
15
use crate :: source_file;
17
- use crate :: { FormatReport , FormatReportFormatterBuilder , Input , Session } ;
16
+ use crate :: {
17
+ is_nightly_channel, FileLines , FormatReport , FormatReportFormatterBuilder , Input , Session ,
18
+ } ;
18
19
19
20
mod configuration_snippet;
20
21
@@ -180,7 +181,7 @@ fn system_tests() {
180
181
run_test_with ( & TestSetting :: default ( ) , || {
181
182
// Get all files in the tests/source directory.
182
183
let files = get_test_files ( Path :: new ( "tests/source" ) , true ) ;
183
- let ( _reports, count, fails) = check_files ( files, & None ) ;
184
+ let ( _reports, count, fails) = check_files ( files, & None , None ) ;
184
185
185
186
// Display results.
186
187
println ! ( "Ran {} system tests." , count) ;
@@ -199,7 +200,7 @@ fn system_tests() {
199
200
fn coverage_tests ( ) {
200
201
init_log ( ) ;
201
202
let files = get_test_files ( Path :: new ( "tests/coverage/source" ) , true ) ;
202
- let ( _reports, count, fails) = check_files ( files, & None ) ;
203
+ let ( _reports, count, fails) = check_files ( files, & None , Some ( EmitMode :: Coverage ) ) ;
203
204
204
205
println ! ( "Ran {} tests in coverage mode." , count) ;
205
206
assert_eq ! ( fails, 0 , "{} tests failed" , fails) ;
@@ -210,15 +211,23 @@ fn checkstyle_test() {
210
211
init_log ( ) ;
211
212
let filename = "tests/writemode/source/fn-single-line.rs" ;
212
213
let expected_filename = "tests/writemode/target/checkstyle.xml" ;
213
- assert_output ( Path :: new ( filename) , Path :: new ( expected_filename) ) ;
214
+ assert_output (
215
+ Path :: new ( filename) ,
216
+ Path :: new ( expected_filename) ,
217
+ EmitMode :: Checkstyle ,
218
+ ) ;
214
219
}
215
220
216
221
#[ test]
217
222
fn json_test ( ) {
218
223
init_log ( ) ;
219
224
let filename = "tests/writemode/source/json.rs" ;
220
225
let expected_filename = "tests/writemode/target/output.json" ;
221
- assert_output ( Path :: new ( filename) , Path :: new ( expected_filename) ) ;
226
+ assert_output (
227
+ Path :: new ( filename) ,
228
+ Path :: new ( expected_filename) ,
229
+ EmitMode :: Json ,
230
+ ) ;
222
231
}
223
232
224
233
#[ test]
@@ -230,9 +239,7 @@ fn modified_test() {
230
239
let filename = "tests/writemode/source/modified.rs" ;
231
240
let mut data = Vec :: new ( ) ;
232
241
let mut config = Config :: default ( ) ;
233
- config
234
- . set ( )
235
- . emit_mode ( crate :: config:: EmitMode :: ModifiedLines ) ;
242
+ config. set_emit_mode ( crate :: config:: EmitMode :: ModifiedLines ) ;
236
243
237
244
{
238
245
let mut session = Session :: new ( config, Some ( & mut data) ) ;
@@ -281,8 +288,9 @@ fn modified_test() {
281
288
282
289
// Helper function for comparing the results of rustfmt
283
290
// to a known output file generated by one of the write modes.
284
- fn assert_output ( source : & Path , expected_filename : & Path ) {
285
- let config = read_config ( source) ;
291
+ fn assert_output ( source : & Path , expected_filename : & Path , emit_mode : EmitMode ) {
292
+ let mut config = read_config ( source) ;
293
+ config. set_emit_mode ( emit_mode) ;
286
294
let ( _, source_file, _) = format_file ( source, config. clone ( ) ) ;
287
295
288
296
// Populate output by writing to a vec.
@@ -317,7 +325,7 @@ fn idempotence_tests() {
317
325
}
318
326
// Get all files in the tests/target directory.
319
327
let files = get_test_files ( Path :: new ( "tests/target" ) , true ) ;
320
- let ( _reports, count, fails) = check_files ( files, & None ) ;
328
+ let ( _reports, count, fails) = check_files ( files, & None , None ) ;
321
329
322
330
// Display results.
323
331
println ! ( "Ran {} idempotent tests." , count) ;
@@ -349,7 +357,7 @@ fn self_tests() {
349
357
}
350
358
files. push ( PathBuf :: from ( "src/lib.rs" ) ) ;
351
359
352
- let ( reports, count, fails) = check_files ( files, & Some ( PathBuf :: from ( "rustfmt.toml" ) ) ) ;
360
+ let ( reports, count, fails) = check_files ( files, & Some ( PathBuf :: from ( "rustfmt.toml" ) ) , None ) ;
353
361
let mut warnings = 0 ;
354
362
355
363
// Display results.
@@ -376,7 +384,7 @@ fn stdin_formatting_smoke_test() {
376
384
init_log ( ) ;
377
385
let input = Input :: Text ( "fn main () {}" . to_owned ( ) ) ;
378
386
let mut config = Config :: default ( ) ;
379
- config. set ( ) . emit_mode ( EmitMode :: Stdout ) ;
387
+ config. set_emit_mode ( EmitMode :: Stdout ) ;
380
388
let mut buf: Vec < u8 > = vec ! [ ] ;
381
389
{
382
390
let mut session = Session :: new ( config, Some ( & mut buf) ) ;
@@ -484,7 +492,11 @@ fn format_lines_errors_are_reported_with_tabs() {
484
492
485
493
// For each file, run rustfmt and collect the output.
486
494
// Returns the number of files checked and the number of failures.
487
- fn check_files ( files : Vec < PathBuf > , opt_config : & Option < PathBuf > ) -> ( Vec < FormatReport > , u32 , u32 ) {
495
+ fn check_files (
496
+ files : Vec < PathBuf > ,
497
+ opt_config : & Option < PathBuf > ,
498
+ emit_mode : Option < EmitMode > ,
499
+ ) -> ( Vec < FormatReport > , u32 , u32 ) {
488
500
let mut count = 0 ;
489
501
let mut fails = 0 ;
490
502
let mut reports = vec ! [ ] ;
@@ -502,7 +514,7 @@ fn check_files(files: Vec<PathBuf>, opt_config: &Option<PathBuf>) -> (Vec<Format
502
514
503
515
debug ! ( "Testing '{}'..." , file_name. display( ) ) ;
504
516
505
- match idempotent_check ( & file_name, & opt_config) {
517
+ match idempotent_check ( & file_name, & opt_config, emit_mode ) {
506
518
Ok ( ref report) if report. has_warnings ( ) => {
507
519
print ! ( "{}" , FormatReportFormatterBuilder :: new( & report) . build( ) ) ;
508
520
fails += 1 ;
@@ -559,16 +571,22 @@ fn read_config(filename: &Path) -> Config {
559
571
} ;
560
572
561
573
for ( key, val) in & sig_comments {
562
- if key != "target" && key != "config" && key != "unstable" {
563
- config. override_value ( key, val) ;
564
- if config. is_default ( key) {
565
- warn ! ( "Default value {} used explicitly for {}" , val, key) ;
574
+ match key. as_ref ( ) {
575
+ "target" | "config" | "unstable" => continue ,
576
+ "file_lines" => {
577
+ config. set_file_lines ( FileLines :: from_str ( val) . unwrap ( ) ) ;
578
+ }
579
+ _ => {
580
+ config. override_value ( key, val) . unwrap ( ) ;
581
+ if config. is_default ( key) {
582
+ warn ! ( "Default value {} used explicitly for {}" , val, key) ;
583
+ }
566
584
}
567
585
}
568
586
}
569
587
570
588
// Don't generate warnings for to-do items.
571
- config. set ( ) . report_todo ( ReportTactic :: Never ) ;
589
+ config. set_report_todo ( ReportTactic :: Never ) ;
572
590
573
591
config
574
592
}
@@ -592,13 +610,17 @@ enum IdempotentCheckError {
592
610
fn idempotent_check (
593
611
filename : & PathBuf ,
594
612
opt_config : & Option < PathBuf > ,
613
+ emit_mode : Option < EmitMode > ,
595
614
) -> Result < FormatReport , IdempotentCheckError > {
596
615
let sig_comments = read_significant_comments ( filename) ;
597
- let config = if let Some ( ref config_file_path) = opt_config {
616
+ let mut config = if let Some ( ref config_file_path) = opt_config {
598
617
Config :: from_toml_path ( config_file_path) . expect ( "`rustfmt.toml` not found" )
599
618
} else {
600
619
read_config ( filename)
601
620
} ;
621
+ if let Some ( emit_mode) = emit_mode {
622
+ config. set_emit_mode ( emit_mode) ;
623
+ }
602
624
let ( parsing_errors, source_file, format_report) = format_file ( filename, config) ;
603
625
if parsing_errors {
604
626
return Err ( IdempotentCheckError :: Parse ) ;
0 commit comments