Skip to content

Commit d34d80c

Browse files
committed
Explicitly set hidden options
1 parent d081610 commit d34d80c

File tree

6 files changed

+46
-30
lines changed

6 files changed

+46
-30
lines changed

src/test/configuration_snippet.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -280,9 +280,7 @@ fn get_code_blocks() -> Vec<ConfigCodeBlock> {
280280
}
281281

282282
for name in hash_set {
283-
if !Config::is_hidden_option(&name) {
284-
panic!("{} does not have a configuration guide", name);
285-
}
283+
panic!("{} does not have a configuration guide", name);
286284
}
287285

288286
code_blocks

src/test/mod.rs

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@ use std::iter::Peekable;
66
use std::mem;
77
use std::path::{Path, PathBuf};
88
use std::process::{Command, Stdio};
9-
use std::str::Chars;
9+
use std::str::{Chars, FromStr};
1010
use std::thread;
1111

1212
use crate::config::{Color, Config, EmitMode, FileName, NewlineStyle, ReportTactic};
1313
use crate::formatting::{ReportedErrors, SourceFile};
14-
use crate::is_nightly_channel;
1514
use crate::rustfmt_diff::{make_diff, print_diff, DiffLine, Mismatch, ModifiedChunk, OutputWriter};
1615
use crate::source_file;
17-
use crate::{FormatReport, FormatReportFormatterBuilder, Input, Session};
16+
use crate::{
17+
is_nightly_channel, FileLines, FormatReport, FormatReportFormatterBuilder, Input, Session,
18+
};
1819

1920
mod configuration_snippet;
2021

@@ -180,7 +181,7 @@ fn system_tests() {
180181
run_test_with(&TestSetting::default(), || {
181182
// Get all files in the tests/source directory.
182183
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);
184185

185186
// Display results.
186187
println!("Ran {} system tests.", count);
@@ -199,7 +200,7 @@ fn system_tests() {
199200
fn coverage_tests() {
200201
init_log();
201202
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));
203204

204205
println!("Ran {} tests in coverage mode.", count);
205206
assert_eq!(fails, 0, "{} tests failed", fails);
@@ -210,15 +211,23 @@ fn checkstyle_test() {
210211
init_log();
211212
let filename = "tests/writemode/source/fn-single-line.rs";
212213
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+
);
214219
}
215220

216221
#[test]
217222
fn json_test() {
218223
init_log();
219224
let filename = "tests/writemode/source/json.rs";
220225
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+
);
222231
}
223232

224233
#[test]
@@ -230,9 +239,7 @@ fn modified_test() {
230239
let filename = "tests/writemode/source/modified.rs";
231240
let mut data = Vec::new();
232241
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);
236243

237244
{
238245
let mut session = Session::new(config, Some(&mut data));
@@ -281,8 +288,9 @@ fn modified_test() {
281288

282289
// Helper function for comparing the results of rustfmt
283290
// 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);
286294
let (_, source_file, _) = format_file(source, config.clone());
287295

288296
// Populate output by writing to a vec.
@@ -317,7 +325,7 @@ fn idempotence_tests() {
317325
}
318326
// Get all files in the tests/target directory.
319327
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);
321329

322330
// Display results.
323331
println!("Ran {} idempotent tests.", count);
@@ -349,7 +357,7 @@ fn self_tests() {
349357
}
350358
files.push(PathBuf::from("src/lib.rs"));
351359

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);
353361
let mut warnings = 0;
354362

355363
// Display results.
@@ -376,7 +384,7 @@ fn stdin_formatting_smoke_test() {
376384
init_log();
377385
let input = Input::Text("fn main () {}".to_owned());
378386
let mut config = Config::default();
379-
config.set().emit_mode(EmitMode::Stdout);
387+
config.set_emit_mode(EmitMode::Stdout);
380388
let mut buf: Vec<u8> = vec![];
381389
{
382390
let mut session = Session::new(config, Some(&mut buf));
@@ -484,7 +492,11 @@ fn format_lines_errors_are_reported_with_tabs() {
484492

485493
// For each file, run rustfmt and collect the output.
486494
// 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) {
488500
let mut count = 0;
489501
let mut fails = 0;
490502
let mut reports = vec![];
@@ -502,7 +514,7 @@ fn check_files(files: Vec<PathBuf>, opt_config: &Option<PathBuf>) -> (Vec<Format
502514

503515
debug!("Testing '{}'...", file_name.display());
504516

505-
match idempotent_check(&file_name, &opt_config) {
517+
match idempotent_check(&file_name, &opt_config, emit_mode) {
506518
Ok(ref report) if report.has_warnings() => {
507519
print!("{}", FormatReportFormatterBuilder::new(&report).build());
508520
fails += 1;
@@ -559,16 +571,22 @@ fn read_config(filename: &Path) -> Config {
559571
};
560572

561573
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+
}
566584
}
567585
}
568586
}
569587

570588
// Don't generate warnings for to-do items.
571-
config.set().report_todo(ReportTactic::Never);
589+
config.set_report_todo(ReportTactic::Never);
572590

573591
config
574592
}
@@ -592,13 +610,17 @@ enum IdempotentCheckError {
592610
fn idempotent_check(
593611
filename: &PathBuf,
594612
opt_config: &Option<PathBuf>,
613+
emit_mode: Option<EmitMode>,
595614
) -> Result<FormatReport, IdempotentCheckError> {
596615
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 {
598617
Config::from_toml_path(config_file_path).expect("`rustfmt.toml` not found")
599618
} else {
600619
read_config(filename)
601620
};
621+
if let Some(emit_mode) = emit_mode {
622+
config.set_emit_mode(emit_mode);
623+
}
602624
let (parsing_errors, source_file, format_report) = format_file(filename, config);
603625
if parsing_errors {
604626
return Err(IdempotentCheckError::Parse);

tests/coverage/source/comments.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// rustfmt-emit_mode: coverage
21
/// Here's a doc comment!
32
fn main() {
43
// foo is bar

tests/coverage/target/comments.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
XX XXXXXXXXXXXXXXXXXX XXXXXXXX
21
/// Here's a doc comment!
32
fn main() {
43
XX XXX XX XXX

tests/writemode/source/fn-single-line.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// rustfmt-fn_single_line: true
2-
// rustfmt-emit_mode: checkstyle
32
// Test single-line functions.
43

54
fn foo_expr() {

tests/writemode/source/json.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// rustfmt-fn_single_line: true
2-
// rustfmt-emit_mode: json
32
// Test single-line functions.
43

54
fn foo_expr() {

0 commit comments

Comments
 (0)