Skip to content

Commit 68b6f92

Browse files
committed
run-make-support: add shallow_find_directories helper
1 parent 223e0f1 commit 68b6f92

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

src/tools/run-make-support/src/fs.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,7 @@ pub fn set_permissions<P: AsRef<Path>>(path: P, perm: std::fs::Permissions) {
238238
));
239239
}
240240

241-
/// A function which prints all file names in the directory `dir` similarly to Unix's `ls`.
242-
/// Useful for debugging.
243-
/// Usage: `eprintln!("{:#?}", shallow_find_dir_entries(some_dir));`
241+
/// List directory entries immediately under the given `dir`.
244242
#[track_caller]
245243
pub fn shallow_find_dir_entries<P: AsRef<Path>>(dir: P) -> Vec<PathBuf> {
246244
let paths = read_dir(dir);

src/tools/run-make-support/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ pub use artifact_names::{
9494

9595
/// Path-related helpers.
9696
pub use path_helpers::{
97-
cwd, filename_contains, filename_not_in_denylist, has_extension, has_prefix, has_suffix,
98-
not_contains, path, shallow_find_files, build_root, source_root,
97+
build_root, cwd, filename_contains, filename_not_in_denylist, has_extension, has_prefix,
98+
has_suffix, not_contains, path, shallow_find_directories, shallow_find_files, source_root,
9999
};
100100

101101
/// Helpers for scoped test execution where certain properties are attempted to be maintained.

src/tools/run-make-support/src/path_helpers.rs

+19
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,25 @@ pub fn shallow_find_files<P: AsRef<Path>, F: Fn(&PathBuf) -> bool>(
5959
matching_files
6060
}
6161

62+
/// Browse the directory `path` non-recursively and return all directories which respect the
63+
/// parameters outlined by `closure`.
64+
#[track_caller]
65+
pub fn shallow_find_directories<P: AsRef<Path>, F: Fn(&PathBuf) -> bool>(
66+
path: P,
67+
filter: F,
68+
) -> Vec<PathBuf> {
69+
let mut matching_files = Vec::new();
70+
for entry in rfs::read_dir(path) {
71+
let entry = entry.expect("failed to read directory entry.");
72+
let path = entry.path();
73+
74+
if path.is_dir() && filter(&path) {
75+
matching_files.push(path);
76+
}
77+
}
78+
matching_files
79+
}
80+
6281
/// Returns true if the filename at `path` does not contain `expected`.
6382
pub fn not_contains<P: AsRef<Path>>(path: P, expected: &str) -> bool {
6483
!path.as_ref().file_name().is_some_and(|name| name.to_str().unwrap().contains(expected))

0 commit comments

Comments
 (0)