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,23 +3,29 @@
//! - 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();
let parent = entry.path().parent().unwrap().to_path_buf();
*directories.entry(parent).or_default() += 1;
}
}
for (dir_path, count) in directories {
// 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 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 {
@ -28,14 +34,6 @@ fn check_entries(path: &Path, bad: &mut bool) {
ENTRY_LIMIT
};
let count = WalkBuilder::new(&dir_path)
.max_depth(Some(1))
.build()
.into_iter()
.collect::<Vec<_>>()
.len()
- 1; // remove the dir itself
if count > limit {
tidy_error!(
bad,
@ -48,8 +46,6 @@ fn check_entries(path: &Path, bad: &mut bool) {
}
}
}
}
}
pub fn check(path: &Path, bad: &mut bool) {
check_entries(&path, bad);