Skip to content

Commit 14e0390

Browse files
committed
Account for revisions in missing-test-files
1 parent 7e5f99a commit 14e0390

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

tests/missing-test-files.rs

+24-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
#![cfg_attr(feature = "deny-warnings", deny(warnings))]
22
#![warn(rust_2018_idioms, unused_lifetimes)]
33
#![allow(clippy::assertions_on_constants)]
4+
#![feature(path_file_prefix)]
45

6+
use std::cmp::Ordering;
7+
use std::ffi::OsStr;
58
use std::fs::{self, DirEntry};
69
use std::path::Path;
710

@@ -21,29 +24,39 @@ fn test_missing_tests() {
2124
}
2225
}
2326

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-
*/
27+
// Test for missing files.
3128
fn explore_directory(dir: &Path) -> Vec<String> {
3229
let mut missing_files: Vec<String> = Vec::new();
3330
let mut current_file = String::new();
3431
let mut files: Vec<DirEntry> = fs::read_dir(dir).unwrap().filter_map(Result::ok).collect();
35-
files.sort_by_key(std::fs::DirEntry::path);
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+
});
3649
for entry in &files {
3750
let path = entry.path();
3851
if path.is_dir() {
3952
missing_files.extend(explore_directory(&path));
4053
} else {
41-
let file_stem = path.file_stem().unwrap().to_str().unwrap().to_string();
54+
let file_prefix = path.file_prefix().unwrap().to_str().unwrap().to_string();
4255
if let Some(ext) = path.extension() {
4356
match ext.to_str().unwrap() {
44-
"rs" => current_file = file_stem.clone(),
57+
"rs" => current_file = file_prefix.clone(),
4558
"stderr" | "stdout" => {
46-
if file_stem != current_file {
59+
if file_prefix != current_file {
4760
missing_files.push(path.to_str().unwrap().to_string());
4861
}
4962
},

0 commit comments

Comments
 (0)