]> git.lizzy.rs Git - rust.git/blobdiff - src/test/mod.rs
Fix is_subpath
[rust.git] / src / test / mod.rs
index 23f6a69b8d187a716dd92728f3cc9dd59dbcaf25..5ac0a1d445099a27311b66fd3c19fa80292446b6 100644 (file)
@@ -9,8 +9,9 @@
 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, FormatReportFormatterBuilder, Input, Session};
     // 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 {
@@ -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
@@ -259,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);
@@ -277,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"];
@@ -326,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]
@@ -426,6 +448,16 @@ 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) {
@@ -485,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);
@@ -810,6 +842,7 @@ fn set_code_block(&mut self, code_block: String, code_block_start: u32) {
 
     fn get_block_config(&self) -> Config {
         let mut config = Config::default();
+        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(),