Skip to content

Commit d26a155

Browse files
committed
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.
1 parent 9b606a3 commit d26a155

File tree

1 file changed

+30
-34
lines changed

1 file changed

+30
-34
lines changed

src/tools/tidy/src/ui_tests.rs

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,50 +3,46 @@
33
//! - there are no stray `.stderr` files
44
55
use ignore::Walk;
6-
use ignore::WalkBuilder;
6+
use std::collections::HashMap;
77
use std::fs;
8-
use std::path::Path;
8+
use std::path::{Path, PathBuf};
99

1010
const ENTRY_LIMIT: usize = 1000;
1111
// FIXME: The following limits should be reduced eventually.
1212
const ROOT_ENTRY_LIMIT: usize = 940;
1313
const ISSUES_ENTRY_LIMIT: usize = 1978;
1414

15-
fn check_entries(path: &Path, bad: &mut bool) {
16-
for dir in Walk::new(&path.join("ui")) {
15+
fn check_entries(tests_path: &Path, bad: &mut bool) {
16+
let mut directories: HashMap<PathBuf, usize> = HashMap::new();
17+
18+
for dir in Walk::new(&tests_path.join("ui")) {
1719
if let Ok(entry) = dir {
18-
if entry.file_type().map(|ft| ft.is_dir()).unwrap_or(false) {
19-
let dir_path = entry.path();
20-
// Use special values for these dirs.
21-
let is_root = path.join("ui") == dir_path;
22-
let is_issues_dir = path.join("ui/issues") == dir_path;
23-
let limit = if is_root {
24-
ROOT_ENTRY_LIMIT
25-
} else if is_issues_dir {
26-
ISSUES_ENTRY_LIMIT
27-
} else {
28-
ENTRY_LIMIT
29-
};
20+
let parent = entry.path().parent().unwrap().to_path_buf();
21+
*directories.entry(parent).or_default() += 1;
22+
}
23+
}
3024

31-
let count = WalkBuilder::new(&dir_path)
32-
.max_depth(Some(1))
33-
.build()
34-
.into_iter()
35-
.collect::<Vec<_>>()
36-
.len()
37-
- 1; // remove the dir itself
25+
for (dir_path, count) in directories {
26+
// Use special values for these dirs.
27+
let is_root = tests_path.join("ui") == dir_path;
28+
let is_issues_dir = tests_path.join("ui/issues") == dir_path;
29+
let limit = if is_root {
30+
ROOT_ENTRY_LIMIT
31+
} else if is_issues_dir {
32+
ISSUES_ENTRY_LIMIT
33+
} else {
34+
ENTRY_LIMIT
35+
};
3836

39-
if count > limit {
40-
tidy_error!(
41-
bad,
42-
"following path contains more than {} entries, \
43-
you should move the test to some relevant subdirectory (current: {}): {}",
44-
limit,
45-
count,
46-
dir_path.display()
47-
);
48-
}
49-
}
37+
if count > limit {
38+
tidy_error!(
39+
bad,
40+
"following path contains more than {} entries, \
41+
you should move the test to some relevant subdirectory (current: {}): {}",
42+
limit,
43+
count,
44+
dir_path.display()
45+
);
5046
}
5147
}
5248
}

0 commit comments

Comments
 (0)