summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/tests/missing-test-files.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
commit698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch)
tree173a775858bd501c378080a10dca74132f05bc50 /src/tools/clippy/tests/missing-test-files.rs
parentInitial commit. (diff)
downloadrustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz
rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/tools/clippy/tests/missing-test-files.rs')
-rw-r--r--src/tools/clippy/tests/missing-test-files.rs69
1 files changed, 69 insertions, 0 deletions
diff --git a/src/tools/clippy/tests/missing-test-files.rs b/src/tools/clippy/tests/missing-test-files.rs
new file mode 100644
index 000000000..7d6edc2b1
--- /dev/null
+++ b/src/tools/clippy/tests/missing-test-files.rs
@@ -0,0 +1,69 @@
+#![cfg_attr(feature = "deny-warnings", deny(warnings))]
+#![warn(rust_2018_idioms, unused_lifetimes)]
+#![allow(clippy::assertions_on_constants)]
+#![feature(path_file_prefix)]
+
+use std::cmp::Ordering;
+use std::ffi::OsStr;
+use std::fs::{self, DirEntry};
+use std::path::Path;
+
+#[test]
+fn test_missing_tests() {
+ 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")
+ );
+ }
+}
+
+// Test for missing files.
+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).unwrap().filter_map(Result::ok).collect();
+ files.sort_by(|x, y| {
+ match x.path().file_prefix().cmp(&y.path().file_prefix()) {
+ Ordering::Equal => (),
+ ord => return ord,
+ }
+ // Sort rs files before the others if they share the same prefix. So when we see
+ // the file prefix for the first time and it's not a rust file, it means the rust
+ // file has to be missing.
+ match (
+ x.path().extension().and_then(OsStr::to_str),
+ y.path().extension().and_then(OsStr::to_str),
+ ) {
+ (Some("rs"), _) => Ordering::Less,
+ (_, Some("rs")) => Ordering::Greater,
+ _ => Ordering::Equal,
+ }
+ });
+ for entry in &files {
+ let path = entry.path();
+ if path.is_dir() {
+ missing_files.extend(explore_directory(&path));
+ } else {
+ let file_prefix = path.file_prefix().unwrap().to_str().unwrap().to_string();
+ if let Some(ext) = path.extension() {
+ match ext.to_str().unwrap() {
+ "rs" => current_file = file_prefix.clone(),
+ "stderr" | "stdout" => {
+ if file_prefix != current_file {
+ missing_files.push(path.to_str().unwrap().to_string());
+ }
+ },
+ _ => continue,
+ };
+ }
+ }
+ }
+ missing_files
+}