Make ui_tests non-quadratic

Previously, it would walk each directory twice: once in the main `Walk`
iterator, and once to count the number of entries in the directory. Now
it only walks each directory once.
This commit is contained in:
Joshua Nelson 2023-03-05 07:37:44 -06:00
parent 9b606a3203
commit d26a15563d

View File

@ -3,50 +3,46 @@
//! - there are no stray `.stderr` files
use ignore::Walk;
use ignore::WalkBuilder;
use std::collections::HashMap;
use std::fs;
use std::path::Path;
use std::path::{Path, PathBuf};
const ENTRY_LIMIT: usize = 1000;
// FIXME: The following limits should be reduced eventually.
const ROOT_ENTRY_LIMIT: usize = 940;
const ISSUES_ENTRY_LIMIT: usize = 1978;
fn check_entries(path: &Path, bad: &mut bool) {
for dir in Walk::new(&path.join("ui")) {
fn check_entries(tests_path: &Path, bad: &mut bool) {
let mut directories: HashMap<PathBuf, usize> = HashMap::new();
for dir in Walk::new(&tests_path.join("ui")) {
if let Ok(entry) = dir {
if entry.file_type().map(|ft| ft.is_dir()).unwrap_or(false) {
let dir_path = entry.path();
// Use special values for these dirs.
let is_root = path.join("ui") == dir_path;
let is_issues_dir = path.join("ui/issues") == dir_path;
let limit = if is_root {
ROOT_ENTRY_LIMIT
} else if is_issues_dir {
ISSUES_ENTRY_LIMIT
} else {
ENTRY_LIMIT
};
let parent = entry.path().parent().unwrap().to_path_buf();
*directories.entry(parent).or_default() += 1;
}
}
let count = WalkBuilder::new(&dir_path)
.max_depth(Some(1))
.build()
.into_iter()
.collect::<Vec<_>>()
.len()
- 1; // remove the dir itself
for (dir_path, count) in directories {
// Use special values for these dirs.
let is_root = tests_path.join("ui") == dir_path;
let is_issues_dir = tests_path.join("ui/issues") == dir_path;
let limit = if is_root {
ROOT_ENTRY_LIMIT
} else if is_issues_dir {
ISSUES_ENTRY_LIMIT
} else {
ENTRY_LIMIT
};
if count > limit {
tidy_error!(
bad,
"following path contains more than {} entries, \
you should move the test to some relevant subdirectory (current: {}): {}",
limit,
count,
dir_path.display()
);
}
}
if count > limit {
tidy_error!(
bad,
"following path contains more than {} entries, \
you should move the test to some relevant subdirectory (current: {}): {}",
limit,
count,
dir_path.display()
);
}
}
}