use std::str::Chars;
use std::thread;
-use crate::config::{Color, Config, EmitMode, FileName, NewlineStyle, ReportTactic};
+use crate::config::{Color, Config, EmitMode, FileName, NewlineStyle, ReportTactic, Verbosity};
use crate::formatting::{ReportedErrors, SourceFile};
+use crate::is_nightly_channel;
use crate::rustfmt_diff::{make_diff, print_diff, DiffLine, Mismatch, ModifiedChunk, OutputWriter};
use crate::source_file;
-use crate::{FormatReport, Input, Session};
+use crate::{FormatReport, FormatReportFormatterBuilder, Input, Session};
const DIFF_CONTEXT_SIZE: usize = 3;
const CONFIGURATIONS_FILE_NAME: &str = "Configurations.md";
// so we do not want to test this file directly.
"configs/skip_children/foo/mod.rs",
"issue-3434/no_entry.rs",
+ // These files and directory are a part of modules defined inside `cfg_if!`.
+ "cfg_if/mod.rs",
+ "cfg_if/detect",
+ // These files and directory are a part of modules defined inside `cfg_attr(..)`.
+ "cfg_mod/dir",
+ "cfg_mod/bar.rs",
+ "cfg_mod/foo.rs",
+ "cfg_mod/wasm32.rs",
];
struct TestSetting {
impl Default for TestSetting {
fn default() -> Self {
TestSetting {
- stack_size: 8388608, // 8MB
+ stack_size: 8_388_608, // 8MB
}
}
}
.expect("Failed to join a test thread")
}
+fn is_subpath<P>(path: &Path, subpath: &P) -> bool
+where
+ P: AsRef<Path>,
+{
+ (0..path.components().count())
+ .map(|i| {
+ path.components()
+ .skip(i)
+ .take(subpath.as_ref().components().count())
+ })
+ .any(|c| c.zip(subpath.as_ref().components()).all(|(a, b)| a == b))
+}
+
fn is_file_skip(path: &Path) -> bool {
SKIP_FILE_WHITE_LIST
.iter()
- .any(|file_path| path.ends_with(file_path))
+ .any(|file_path| is_subpath(path, file_path))
}
// Returns a `Vec` containing `PathBuf`s of files with an `rs` extension in the
if path.extension().map_or(false, |f| f == "rs") {
// check if "// rustfmt-<config_name>:" appears in the file.
let filebuf = BufReader::new(
- fs::File::open(&path).expect(&format!("couldn't read file {}", path.display())),
+ fs::File::open(&path)
+ .unwrap_or_else(|_| panic!("couldn't read file {}", path.display())),
);
assert!(
filebuf
.lines()
- .map(|l| l.unwrap())
+ .map(Result::unwrap)
.take_while(|l| l.starts_with("//"))
.any(|l| l.starts_with(&format!("// rustfmt-{}", config_name))),
format!(
let mut failures = HashMap::new();
failures.insert(source.to_owned(), compare);
print_mismatches_default_message(failures);
- assert!(false, "Text does not match expected output");
+ panic!("Text does not match expected output");
}
}
#[test]
fn idempotence_tests() {
run_test_with(&TestSetting::default(), || {
- match option_env!("CFG_RELEASE_CHANNEL") {
- None | Some("nightly") => {}
- _ => return, // these tests require nightly
+ // these tests require nightly
+ if !is_nightly_channel!() {
+ return;
}
// Get all files in the tests/target directory.
let files = get_test_files(Path::new("tests/target"), true);
// no warnings are emitted.
#[test]
fn self_tests() {
- match option_env!("CFG_RELEASE_CHANNEL") {
- None | Some("nightly") => {}
- _ => return, // Issue-3443: these tests require nightly
+ // Issue-3443: these tests require nightly
+ if !is_nightly_channel!() {
+ return;
}
let mut files = get_test_files(Path::new("tests"), false);
let bin_directories = vec!["cargo-fmt", "git-rustfmt", "bin", "format-diff"];
assert_eq!(fails, 0, "{} self tests failed", fails);
for format_report in reports {
- println!("{}", format_report);
+ println!(
+ "{}",
+ FormatReportFormatterBuilder::new(&format_report).build()
+ );
warnings += format_report.warning_count();
}
}
#[cfg(not(windows))]
- assert_eq!(buf, "fn main() {}\n".as_bytes());
+ assert_eq!(buf, "stdin:\n\nfn main() {}\n".as_bytes());
#[cfg(windows)]
- assert_eq!(buf, "fn main() {}\r\n".as_bytes());
+ assert_eq!(buf, "stdin:\n\nfn main() {}\r\n".as_bytes());
}
#[test]
let mut reports = vec![];
for file_name in files {
+ let sig_comments = read_significant_comments(&file_name);
+ if sig_comments.contains_key("unstable") && !is_nightly_channel!() {
+ debug!(
+ "Skipping '{}' because it requires unstable \
+ features which are only available on nightly...",
+ file_name.display()
+ );
+ continue;
+ }
+
debug!("Testing '{}'...", file_name.display());
match idempotent_check(&file_name, &opt_config) {
Ok(ref report) if report.has_warnings() => {
- print!("{}", report);
+ print!("{}", FormatReportFormatterBuilder::new(&report).build());
fails += 1;
}
Ok(report) => reports.push(report),
};
for (key, val) in &sig_comments {
- if key != "target" && key != "config" {
+ if key != "target" && key != "config" && key != "unstable" {
config.override_value(key, val);
if config.is_default(key) {
warn!("Default value {} used explicitly for {}", val, key);
// Reads significant comments of the form: `// rustfmt-key: value` into a hash map.
fn read_significant_comments(file_name: &Path) -> HashMap<String, String> {
- let file =
- fs::File::open(file_name).expect(&format!("couldn't read file {}", file_name.display()));
+ let file = fs::File::open(file_name)
+ .unwrap_or_else(|_| panic!("couldn't read file {}", file_name.display()));
let reader = BufReader::new(file);
let pattern = r"^\s*//\s*rustfmt-([^:]+):\s*(\S+)";
let regex = regex::Regex::new(pattern).expect("failed creating pattern 1");
fn get_block_config(&self) -> Config {
let mut config = Config::default();
- if self.config_value.is_some() && self.config_value.is_some() {
+ config.set().verbose(Verbosity::Quiet);
+ if self.config_name.is_some() && self.config_value.is_some() {
config.override_value(
self.config_name.as_ref().unwrap(),
self.config_value.as_ref().unwrap(),
fn get_code_blocks() -> Vec<ConfigCodeBlock> {
let mut file_iter = BufReader::new(
fs::File::open(Path::new(CONFIGURATIONS_FILE_NAME))
- .expect(&format!("couldn't read file {}", CONFIGURATIONS_FILE_NAME)),
+ .unwrap_or_else(|_| panic!("couldn't read file {}", CONFIGURATIONS_FILE_NAME)),
)
.lines()
- .map(|l| l.unwrap())
+ .map(Result::unwrap)
.enumerate();
let mut code_blocks: Vec<ConfigCodeBlock> = Vec::new();
let mut hash_set = Config::hash_set();
let blocks = get_code_blocks();
let failures = blocks
.iter()
- .map(|b| b.formatted_is_idempotent())
+ .map(ConfigCodeBlock::formatted_is_idempotent)
.fold(0, |acc, r| acc + (!r as u32));
// Display results.