]> git.lizzy.rs Git - rust.git/blob - tests/missing-test-files.rs
Auto merge of #9927 - xFrednet:0000-rustc-tool-macro-update, r=matthiaskrgr
[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 #![feature(path_file_prefix)]
5
6 use std::cmp::Ordering;
7 use std::ffi::OsStr;
8 use std::fs::{self, DirEntry};
9 use std::path::Path;
10
11 #[test]
12 fn test_missing_tests() {
13     let missing_files = explore_directory(Path::new("./tests"));
14     if !missing_files.is_empty() {
15         assert!(
16             false,
17             "Didn't see a test file for the following files:\n\n{}\n",
18             missing_files
19                 .iter()
20                 .map(|s| format!("\t{s}"))
21                 .collect::<Vec<_>>()
22                 .join("\n")
23         );
24     }
25 }
26
27 // Test for missing files.
28 fn explore_directory(dir: &Path) -> Vec<String> {
29     let mut missing_files: Vec<String> = Vec::new();
30     let mut current_file = String::new();
31     let mut files: Vec<DirEntry> = fs::read_dir(dir).unwrap().filter_map(Result::ok).collect();
32     files.sort_by(|x, y| {
33         match x.path().file_prefix().cmp(&y.path().file_prefix()) {
34             Ordering::Equal => (),
35             ord => return ord,
36         }
37         // Sort rs files before the others if they share the same prefix. So when we see
38         // the file prefix for the first time and it's not a rust file, it means the rust
39         // file has to be missing.
40         match (
41             x.path().extension().and_then(OsStr::to_str),
42             y.path().extension().and_then(OsStr::to_str),
43         ) {
44             (Some("rs"), _) => Ordering::Less,
45             (_, Some("rs")) => Ordering::Greater,
46             _ => Ordering::Equal,
47         }
48     });
49     for entry in &files {
50         let path = entry.path();
51         if path.is_dir() {
52             missing_files.extend(explore_directory(&path));
53         } else {
54             let file_prefix = path.file_prefix().unwrap().to_str().unwrap().to_string();
55             if let Some(ext) = path.extension() {
56                 match ext.to_str().unwrap() {
57                     "rs" => current_file = file_prefix.clone(),
58                     "stderr" | "stdout" => {
59                         if file_prefix != current_file {
60                             missing_files.push(path.to_str().unwrap().to_string());
61                         }
62                     },
63                     _ => continue,
64                 };
65             }
66         }
67     }
68     missing_files
69 }