]> git.lizzy.rs Git - rust.git/blob - tests/missing-test-files.rs
Rollup merge of #89786 - jkugelman:must-use-len-and-is_empty, r=joshtriplett
[rust.git] / tests / missing-test-files.rs
1 #![cfg_attr(feature = "deny-warnings", deny(warnings))]
2 #![warn(rust_2018_idioms, unused_lifetimes)]
3 #![allow(clippy::assertions_on_constants)]
4
5 use std::fs::{self, DirEntry};
6 use std::path::Path;
7
8 #[test]
9 fn test_missing_tests() {
10     let missing_files = explore_directory(Path::new("./tests"));
11     if !missing_files.is_empty() {
12         assert!(
13             false,
14             "Didn't see a test file for the following files:\n\n{}\n",
15             missing_files
16                 .iter()
17                 .map(|s| format!("\t{}", s))
18                 .collect::<Vec<_>>()
19                 .join("\n")
20         );
21     }
22 }
23
24 /*
25 Test for missing files.
26
27 Since rs files are alphabetically before stderr/stdout, we can sort by the full name
28 and iter in that order. If we've seen the file stem for the first time and it's not
29 a rust file, it means the rust file has to be missing.
30 */
31 fn explore_directory(dir: &Path) -> Vec<String> {
32     let mut missing_files: Vec<String> = Vec::new();
33     let mut current_file = String::new();
34     let mut files: Vec<DirEntry> = fs::read_dir(dir).unwrap().filter_map(Result::ok).collect();
35     files.sort_by_key(std::fs::DirEntry::path);
36     for entry in &files {
37         let path = entry.path();
38         if path.is_dir() {
39             missing_files.extend(explore_directory(&path));
40         } else {
41             let file_stem = path.file_stem().unwrap().to_str().unwrap().to_string();
42             if let Some(ext) = path.extension() {
43                 match ext.to_str().unwrap() {
44                     "rs" => current_file = file_stem.clone(),
45                     "stderr" | "stdout" => {
46                         if file_stem != current_file {
47                             missing_files.push(path.to_str().unwrap().to_string());
48                         }
49                     },
50                     _ => continue,
51                 };
52             }
53         }
54     }
55     missing_files
56 }