]> git.lizzy.rs Git - rust.git/blobdiff - tests/missing-test-files.rs
Auto merge of #7036 - horacimacias:master, r=giraffate
[rust.git] / tests / missing-test-files.rs
index 31d2dccff711af06f8076ef26b3d2ef5a9d9bb43..9cef7438d225cbc51cdedd1b471ece46bbb89e78 100644 (file)
@@ -1,10 +1,22 @@
+#![allow(clippy::assertions_on_constants)]
+
 use std::fs::{self, DirEntry};
-use std::io;
 use std::path::Path;
 
 #[test]
 fn test_missing_tests() {
-    explore_directory(Path::new("./tests")).unwrap();
+    let missing_files = explore_directory(Path::new("./tests"));
+    if !missing_files.is_empty() {
+        assert!(
+            false,
+            "Didn't see a test file for the following files:\n\n{}\n",
+            missing_files
+                .iter()
+                .map(|s| format!("\t{}", s))
+                .collect::<Vec<_>>()
+                .join("\n")
+        );
+    }
 }
 
 /*
@@ -14,34 +26,29 @@ fn test_missing_tests() {
 and iter in that order. If we've seen the file stem for the first time and it's not
 a rust file, it means the rust file has to be missing.
 */
-fn explore_directory(dir: &Path) -> io::Result<()> {
+fn explore_directory(dir: &Path) -> Vec<String> {
+    let mut missing_files: Vec<String> = Vec::new();
     let mut current_file = String::new();
-    let mut files: Vec<DirEntry> = fs::read_dir(dir)?.filter_map(Result::ok).collect();
-    files.sort_by_key(|e| e.path());
-    for entry in files.iter() {
+    let mut files: Vec<DirEntry> = fs::read_dir(dir).unwrap().filter_map(Result::ok).collect();
+    files.sort_by_key(std::fs::DirEntry::path);
+    for entry in &files {
         let path = entry.path();
         if path.is_dir() {
-            explore_directory(&path)?;
+            missing_files.extend(explore_directory(&path));
         } else {
             let file_stem = path.file_stem().unwrap().to_str().unwrap().to_string();
-            match path.extension() {
-                Some(ext) => {
-                    match ext.to_str().unwrap() {
-                        "rs" => current_file = file_stem.clone(),
-                        "stderr" | "stdout" => {
-                            assert_eq!(
-                                file_stem,
-                                current_file,
-                                "{}",
-                                format!("Didn't see a test file for {:}", path.to_str().unwrap())
-                            );
-                        },
-                        _ => continue,
-                    };
-                },
-                None => {},
+            if let Some(ext) = path.extension() {
+                match ext.to_str().unwrap() {
+                    "rs" => current_file = file_stem.clone(),
+                    "stderr" | "stdout" => {
+                        if file_stem != current_file {
+                            missing_files.push(path.to_str().unwrap().to_string());
+                        }
+                    },
+                    _ => continue,
+                };
             }
         }
     }
-    Ok(())
+    missing_files
 }