Skip to content

Commit 3e554c6

Browse files
committed
run-make-support: add shallow_find_directories helper
1 parent 612aa93 commit 3e554c6

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ pub use artifact_names::{
9090
/// Path-related helpers.
9191
pub use path_helpers::{
9292
cwd, filename_contains, filename_not_in_denylist, has_extension, has_prefix, has_suffix,
93-
not_contains, path, shallow_find_files, source_root,
93+
not_contains, path, shallow_find_directories, shallow_find_files, source_root,
9494
};
9595

9696
/// 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
@@ -53,6 +53,25 @@ pub fn shallow_find_files<P: AsRef<Path>, F: Fn(&PathBuf) -> bool>(
5353
matching_files
5454
}
5555

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

0 commit comments

Comments
 (0)