Skip to content

Commit 75a375f

Browse files
authored
fix self_tests (#4033)
1 parent ec3a26e commit 75a375f

File tree

11 files changed

+67
-64
lines changed

11 files changed

+67
-64
lines changed

rustfmt-core/rustfmt-bin/build.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,13 @@ fn main() {
2828
// (git not installed or if this is not a git repository) just return an empty string.
2929
fn commit_info() -> String {
3030
match (channel(), commit_hash(), commit_date()) {
31-
(channel, Some(hash), Some(date)) => {
32-
format!("{}-{} ({} {})", option_env!("CARGO_PKG_VERSION")
33-
.unwrap_or("unknown"), channel, hash.trim_end(), date)
34-
},
31+
(channel, Some(hash), Some(date)) => format!(
32+
"{}-{} ({} {})",
33+
option_env!("CARGO_PKG_VERSION").unwrap_or("unknown"),
34+
channel,
35+
hash.trim_end(),
36+
date
37+
),
3538
_ => String::new(),
3639
}
3740
}

rustfmt-core/rustfmt-bin/src/bin/main.rs

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ extern crate lazy_static;
55
use std::collections::HashMap;
66
use std::env;
77
use std::fmt;
8-
use std::io::{self, stdout, Error as IoError, Read, Write, stdin};
8+
use std::io::{self, stdin, stdout, Error as IoError, Read, Write};
99
use std::path::{Path, PathBuf};
1010
use std::str::FromStr;
1111

@@ -41,7 +41,7 @@ fn main() {
4141

4242
/// Format Rust code
4343
#[derive(Debug, StructOpt, Clone)]
44-
#[structopt(name = "rustfmt", version = include_str!(concat!(env!("OUT_DIR"), "/version-info.txt")))]
44+
#[structopt(name = "rustfmt", version = include_str!(concat!(env!("OUT_DIR"),"/version-info.txt")))]
4545
struct Opt {
4646
/// Run in 'check' mode.
4747
///
@@ -50,8 +50,8 @@ struct Opt {
5050
#[structopt(short, long)]
5151
check: bool,
5252
/// Specify the format of rustfmt's output.
53-
#[cfg_attr(nightly, structopt(long, name= "files|stdout|checkstyle|json"))]
54-
#[cfg_attr(not(nightly), structopt(long, name= "files|stdout"))]
53+
#[cfg_attr(nightly, structopt(long, name = "files|stdout|checkstyle|json"))]
54+
#[cfg_attr(not(nightly), structopt(long, name = "files|stdout"))]
5555
emit: Option<Emit>,
5656
/// A path to the configuration file.
5757
#[structopt(long = "config-path", parse(from_os_str))]
@@ -92,7 +92,6 @@ struct Opt {
9292
verbose: bool,
9393

9494
// Nightly-only options.
95-
9695
/// Limit formatting to specified ranges.
9796
///
9897
/// If you want to restrict reformatting to specific sets of lines, you can
@@ -124,7 +123,6 @@ struct Opt {
124123
error_on_unformatted: bool,
125124

126125
// Positional arguments.
127-
128126
#[structopt(parse(from_os_str))]
129127
files: Vec<PathBuf>,
130128
}
@@ -148,22 +146,23 @@ impl FromStr for InlineConfig {
148146

149147
s.split(',')
150148
.map(
151-
|key_val| match key_val.char_indices().find(|(_, ch)| *ch == '=') {
152-
Some((middle, _)) => {
153-
let (key, val) = (&key_val[..middle], &key_val[middle + 1..]);
154-
if !Config::is_valid_key_val(key, val) {
155-
Err(format_err!("invalid key=val pair: `{}`", key_val))
156-
} else {
157-
Ok((key.to_string(), val.to_string()))
149+
|key_val| match key_val.char_indices().find(|(_, ch)| *ch == '=') {
150+
Some((middle, _)) => {
151+
let (key, val) = (&key_val[..middle], &key_val[middle + 1..]);
152+
if !Config::is_valid_key_val(key, val) {
153+
Err(format_err!("invalid key=val pair: `{}`", key_val))
154+
} else {
155+
Ok((key.to_string(), val.to_string()))
156+
}
158157
}
159-
}
160158

161-
None => Err(format_err!(
159+
None => Err(format_err!(
162160
"--config expects comma-separated list of key=val pairs, found `{}`",
163161
key_val
164162
)),
165-
},
166-
).collect::<Result<HashMap<_, _>, _>>()
163+
},
164+
)
165+
.collect::<Result<HashMap<_, _>, _>>()
167166
.map(|map| InlineConfig(map, false))
168167
}
169168
}
@@ -183,7 +182,10 @@ impl FromStr for PrintConfig {
183182
"default" => Ok(PrintConfig::Default),
184183
"minimal" => Ok(PrintConfig::Minimal),
185184
"current" => Ok(PrintConfig::Current),
186-
_ => Err(format!("expected one of [current,default,minimal], found `{}`", s)),
185+
_ => Err(format!(
186+
"expected one of [current,default,minimal], found `{}`",
187+
s
188+
)),
187189
}
188190
}
189191
}
@@ -280,7 +282,6 @@ impl Opt {
280282
}
281283
}
282284

283-
284285
/// Rustfmt operations errors.
285286
#[derive(Error, Debug)]
286287
pub enum OperationError {
@@ -347,7 +348,9 @@ impl CliOptions for Opt {
347348
fn execute(mut opt: Opt) -> Result<i32> {
348349
opt.verify()?;
349350

350-
if opt.inline_config.as_ref().map_or(false, |inline_configs| inline_configs.iter().any(InlineConfig::is_help)) {
351+
if opt.inline_config.as_ref().map_or(false, |inline_configs| {
352+
inline_configs.iter().any(InlineConfig::is_help)
353+
}) {
351354
Config::print_docs(&mut stdout(), cfg!(nightly));
352355
return Ok(0);
353356
}
@@ -369,10 +372,12 @@ fn print_default_config() -> Result<i32> {
369372
}
370373

371374
fn print_config(opt: &Opt, print_config: PrintConfig) -> Result<i32> {
372-
let (config, config_path) =
373-
load_config(env::current_dir().ok().as_ref().map(PathBuf::as_path), Some(opt))?;
374-
let actual_config = FileConfigPairIter::new(&opt, config_path.is_some())
375-
.find_map(|pair| match pair.config {
375+
let (config, config_path) = load_config(
376+
env::current_dir().ok().as_ref().map(PathBuf::as_path),
377+
Some(opt),
378+
)?;
379+
let actual_config =
380+
FileConfigPairIter::new(&opt, config_path.is_some()).find_map(|pair| match pair.config {
376381
FileConfig::Local(config, Some(_)) => Some(config),
377382
_ => None,
378383
});
@@ -436,8 +441,7 @@ struct FileConfigPairIter<'a> {
436441
opt: &'a Opt,
437442
}
438443

439-
impl<'a> FileConfigPairIter<'a>
440-
{
444+
impl<'a> FileConfigPairIter<'a> {
441445
fn new(opt: &'a Opt, has_config_from_commandline: bool) -> Self {
442446
FileConfigPairIter {
443447
has_config_from_commandline,
@@ -447,8 +451,7 @@ impl<'a> FileConfigPairIter<'a>
447451
}
448452
}
449453

450-
impl<'a> Iterator for FileConfigPairIter<'a>
451-
{
454+
impl<'a> Iterator for FileConfigPairIter<'a> {
452455
type Item = FileConfigPair<'a>;
453456

454457
fn next(&mut self) -> Option<Self::Item> {
@@ -461,13 +464,11 @@ impl<'a> Iterator for FileConfigPairIter<'a>
461464
FileConfig::Local(local_config, config_path)
462465
};
463466

464-
Some(FileConfigPair {file, config})
467+
Some(FileConfigPair { file, config })
465468
}
466469
}
467470

468-
fn format(
469-
opt: Opt,
470-
) -> Result<i32> {
471+
fn format(opt: Opt) -> Result<i32> {
471472
if opt.files.is_empty() {
472473
let mut buf = String::new();
473474
stdin().read_to_string(&mut buf)?;

rustfmt-core/rustfmt-config/src/config_type.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,4 +340,3 @@ macro_rules! is_nightly_channel {
340340
option_env!("CFG_RELEASE_CHANNEL").map_or(true, |c| c == "nightly" || c == "dev")
341341
};
342342
}
343-

rustfmt-core/rustfmt-config/src/file_lines.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,10 @@ impl FileLines {
251251
Some(ref map) => map,
252252
};
253253

254-
match canonicalize_path_string(file_name).ok().and_then(|file| map.get(&file)) {
254+
match canonicalize_path_string(file_name)
255+
.ok()
256+
.and_then(|file| map.get(&file))
257+
{
255258
Some(ranges) => ranges.iter().any(f),
256259
None => false,
257260
}

rustfmt-core/rustfmt-config/src/lib.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -428,10 +428,7 @@ mod test {
428428

429429
let used_options = config.used_options();
430430
let toml = used_options.to_toml().unwrap();
431-
assert_eq!(
432-
toml,
433-
"merge_derives = false\n"
434-
);
431+
assert_eq!(toml, "merge_derives = false\n");
435432
}
436433

437434
#[test]

rustfmt-core/rustfmt-emitter/src/diff.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use rustfmt_configuration::Config;
22

3-
use crate::rustfmt_diff::{make_diff, print_diff};
43
use super::*;
4+
use crate::rustfmt_diff::{make_diff, print_diff};
55

66
pub struct DiffEmitter {
77
config: Config,

rustfmt-core/rustfmt-emitter/src/rustfmt_diff.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,5 +424,4 @@ mod test {
424424
let diff = make_diff("a\nb\nc\nd", "a\nb\nc\nd", 3);
425425
assert_eq!(diff, vec![]);
426426
}
427-
428427
}

rustfmt-core/rustfmt-lib/src/closures.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -415,8 +415,8 @@ fn is_block_closure_forced(
415415
false
416416
} else {
417417
if let ast::ExprKind::Match(..) = expr.kind {
418-
let is_move_closure_without_brace =
419-
capture == ast::CaptureBy::Value && !context.snippet(expr.span).trim().starts_with("{");
418+
let is_move_closure_without_brace = capture == ast::CaptureBy::Value
419+
&& !context.snippet(expr.span).trim().starts_with("{");
420420

421421
is_block_closure_forced_inner(expr) || is_move_closure_without_brace
422422
} else {

rustfmt-core/rustfmt-lib/src/stmt.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,5 @@ fn format_stmt(
107107
}
108108
ast::StmtKind::Mac(..) | ast::StmtKind::Item(..) => None,
109109
};
110-
result.and_then(|res| {
111-
recover_comment_removed(res, stmt.span(), context)
112-
})
110+
result.and_then(|res| recover_comment_removed(res, stmt.span(), context))
113111
}

rustfmt-core/rustfmt-lib/src/test/configuration_snippet.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ use std::path::{Path, PathBuf};
66

77
use rustfmt_emitter::rustfmt_diff::{make_diff, Mismatch};
88

9+
use super::{print_mismatches, write_message, DIFF_CONTEXT_SIZE};
910
use crate::config::{Config, EmitMode, Verbosity};
1011
use crate::{Input, Session};
11-
use super::{print_mismatches, write_message, DIFF_CONTEXT_SIZE};
1212

1313
const CONFIGURATIONS_FILE_NAME: &str = "../../Configurations.md";
1414

rustfmt-core/rustfmt-lib/src/test/mod.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ use std::path::{Path, PathBuf};
77
use std::str::Chars;
88
use std::thread;
99

10-
use rustfmt_emitter::rustfmt_diff::{
11-
make_diff, print_diff, Mismatch, ModifiedChunk, OutputWriter,
12-
};
10+
use rustfmt_emitter::rustfmt_diff::{make_diff, print_diff, Mismatch, ModifiedChunk, OutputWriter};
1311

1412
use crate::config::{Color, Config, EmitMode, FileName, NewlineStyle, ReportTactic};
1513
use crate::formatting::{ReportedErrors, SourceFile};
@@ -87,16 +85,16 @@ where
8785
.any(|c| c.zip(subpath.as_ref().components()).all(|(a, b)| a == b))
8886
}
8987

90-
fn is_file_skip(path: &Path) -> bool {
91-
SKIP_FILE_WHITE_LIST
88+
fn is_file_skip(skip_file_white_list: &[&str], path: &Path) -> bool {
89+
skip_file_white_list
9290
.iter()
9391
.any(|file_path| is_subpath(path, file_path))
9492
}
9593

9694
// Returns a `Vec` containing `PathBuf`s of files with an `rs` extension in the
9795
// given path. The `recursive` argument controls if files from subdirectories
9896
// are also returned.
99-
fn get_test_files(path: &Path, recursive: bool) -> Vec<PathBuf> {
97+
fn get_test_files(path: &Path, recursive: bool, skip_file_white_list: &[&str]) -> Vec<PathBuf> {
10098
let mut files = vec![];
10199
if path.is_dir() {
102100
for entry in fs::read_dir(path)
@@ -105,8 +103,10 @@ fn get_test_files(path: &Path, recursive: bool) -> Vec<PathBuf> {
105103
let entry = entry.expect("couldn't get `DirEntry`");
106104
let path = entry.path();
107105
if path.is_dir() && recursive {
108-
files.append(&mut get_test_files(&path, recursive));
109-
} else if path.extension().map_or(false, |f| f == "rs") && !is_file_skip(&path) {
106+
files.append(&mut get_test_files(&path, recursive, skip_file_white_list));
107+
} else if path.extension().map_or(false, |f| f == "rs")
108+
&& !is_file_skip(skip_file_white_list, &path)
109+
{
110110
files.push(path);
111111
}
112112
}
@@ -179,7 +179,7 @@ fn system_tests() {
179179
init_log();
180180
run_test_with(&TestSetting::default(), || {
181181
// Get all files in the tests/source directory.
182-
let files = get_test_files(Path::new("tests/source"), true);
182+
let files = get_test_files(Path::new("tests/source"), true, SKIP_FILE_WHITE_LIST);
183183
let (_reports, count, fails) = check_files(files, &None);
184184

185185
// Display results.
@@ -198,7 +198,11 @@ fn system_tests() {
198198
#[test]
199199
fn coverage_tests() {
200200
init_log();
201-
let files = get_test_files(Path::new("tests/coverage/source"), true);
201+
let files = get_test_files(
202+
Path::new("tests/coverage/source"),
203+
true,
204+
SKIP_FILE_WHITE_LIST,
205+
);
202206
let (_reports, count, fails) = check_files(files, &None);
203207

204208
println!("Ran {} tests in coverage mode.", count);
@@ -362,7 +366,7 @@ fn idempotence_tests() {
362366
return;
363367
}
364368
// Get all files in the tests/target directory.
365-
let files = get_test_files(Path::new("tests/target"), true);
369+
let files = get_test_files(Path::new("tests/target"), true, SKIP_FILE_WHITE_LIST);
366370
let (_reports, count, fails) = check_files(files, &None);
367371

368372
// Display results.
@@ -385,8 +389,8 @@ fn self_tests() {
385389
if !is_nightly_channel!() {
386390
return;
387391
}
388-
let mut files = get_test_files(Path::new("tests"), false);
389-
files.push(PathBuf::from("src/lib.rs"));
392+
let skip_file_white_list = ["target", "tests"];
393+
let files = get_test_files(Path::new("../../rustfmt-core"), true, &skip_file_white_list);
390394

391395
let (reports, count, fails) = check_files(files, &Some(PathBuf::from("../../rustfmt.toml")));
392396
let mut warnings = 0;
@@ -809,4 +813,3 @@ fn string_eq_ignore_newline_repr_test() {
809813
assert!(string_eq_ignore_newline_repr("a\r\n\r\n\r\nb", "a\n\n\nb"));
810814
assert!(!string_eq_ignore_newline_repr("a\r\nbcd", "a\nbcdefghijk"));
811815
}
812-

0 commit comments

Comments
 (0)