]> git.lizzy.rs Git - rust.git/blobdiff - src/test/mod.rs
Fix is_subpath
[rust.git] / src / test / mod.rs
index dc297a52dfb967f852ecbe3bb822549ff4bdeecc..5ac0a1d445099a27311b66fd3c19fa80292446b6 100644 (file)
@@ -9,11 +9,12 @@
 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 {
@@ -34,7 +43,7 @@ struct TestSetting {
 impl Default for TestSetting {
     fn default() -> Self {
         TestSetting {
-            stack_size: 8388608, // 8MB
+            stack_size: 8_388_608, // 8MB
         }
     }
 }
@@ -52,10 +61,23 @@ fn run_test_with<F>(test_setting: &TestSetting, f: F)
         .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
@@ -90,12 +112,13 @@ fn verify_config_used(path: &Path, config_name: &str) {
         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!(
@@ -249,7 +272,7 @@ fn assert_output(source: &Path, expected_filename: &Path) {
         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");
     }
 }
 
@@ -258,9 +281,9 @@ fn assert_output(source: &Path, expected_filename: &Path) {
 #[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);
@@ -276,9 +299,9 @@ fn idempotence_tests() {
 // 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"];
@@ -298,7 +321,10 @@ fn self_tests() {
     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();
     }
 
@@ -322,9 +348,9 @@ fn stdin_formatting_smoke_test() {
     }
 
     #[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]
@@ -422,11 +448,21 @@ fn check_files(files: Vec<PathBuf>, opt_config: &Option<PathBuf>) -> (Vec<Format
     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),
@@ -481,7 +517,7 @@ fn read_config(filename: &Path) -> Config {
     };
 
     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);
@@ -565,8 +601,8 @@ fn get_config(config_file: Option<&Path>) -> Config {
 
 // 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");
@@ -806,7 +842,8 @@ fn set_code_block(&mut self, code_block: String, code_block_start: u32) {
 
     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(),
@@ -962,10 +999,10 @@ fn configuration_snippet_tests() {
     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();
@@ -988,7 +1025,7 @@ fn get_code_blocks() -> Vec<ConfigCodeBlock> {
     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.