about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
author许杰友 Jieyou Xu (Joe) <39484203+jieyouxu@users.noreply.github.com>2025-01-16 03:14:34 +0800
committer许杰友 Jieyou Xu (Joe) <39484203+jieyouxu@users.noreply.github.com>2025-02-01 16:14:36 +0800
commit68b6f92a56f36cb891b84c918eb70d3acbffd10f (patch)
tree4625424bee0fb9e4984e0639d12678fd83bc408c /src
parent223e0f1ce1dc7e06041f7226a661a653c0ddb3df (diff)
run-make-support: add `shallow_find_directories` helper
Diffstat (limited to 'src')
-rw-r--r--src/tools/run-make-support/src/fs.rs4
-rw-r--r--src/tools/run-make-support/src/lib.rs4
-rw-r--r--src/tools/run-make-support/src/path_helpers.rs19
3 files changed, 22 insertions, 5 deletions
diff --git a/src/tools/run-make-support/src/fs.rs b/src/tools/run-make-support/src/fs.rs
index 7ebe4a9ca13..ceb4ebbaca5 100644
--- a/src/tools/run-make-support/src/fs.rs
+++ b/src/tools/run-make-support/src/fs.rs
@@ -238,9 +238,7 @@ pub fn set_permissions<P: AsRef<Path>>(path: P, perm: std::fs::Permissions) {
     ));
 }
 
-/// A function which prints all file names in the directory `dir` similarly to Unix's `ls`.
-/// Useful for debugging.
-/// Usage: `eprintln!("{:#?}", shallow_find_dir_entries(some_dir));`
+/// List directory entries immediately under the given `dir`.
 #[track_caller]
 pub fn shallow_find_dir_entries<P: AsRef<Path>>(dir: P) -> Vec<PathBuf> {
     let paths = read_dir(dir);
diff --git a/src/tools/run-make-support/src/lib.rs b/src/tools/run-make-support/src/lib.rs
index a8c9bec57fd..7e63ab3159a 100644
--- a/src/tools/run-make-support/src/lib.rs
+++ b/src/tools/run-make-support/src/lib.rs
@@ -94,8 +94,8 @@ pub use artifact_names::{
 
 /// Path-related helpers.
 pub use path_helpers::{
-    cwd, filename_contains, filename_not_in_denylist, has_extension, has_prefix, has_suffix,
-    not_contains, path, shallow_find_files, build_root, source_root,
+    build_root, cwd, filename_contains, filename_not_in_denylist, has_extension, has_prefix,
+    has_suffix, not_contains, path, shallow_find_directories, shallow_find_files, source_root,
 };
 
 /// Helpers for scoped test execution where certain properties are attempted to be maintained.
diff --git a/src/tools/run-make-support/src/path_helpers.rs b/src/tools/run-make-support/src/path_helpers.rs
index 1c59f2feea4..b766e50e523 100644
--- a/src/tools/run-make-support/src/path_helpers.rs
+++ b/src/tools/run-make-support/src/path_helpers.rs
@@ -59,6 +59,25 @@ pub fn shallow_find_files<P: AsRef<Path>, F: Fn(&PathBuf) -> bool>(
     matching_files
 }
 
+/// Browse the directory `path` non-recursively and return all directories which respect the
+/// parameters outlined by `closure`.
+#[track_caller]
+pub fn shallow_find_directories<P: AsRef<Path>, F: Fn(&PathBuf) -> bool>(
+    path: P,
+    filter: F,
+) -> Vec<PathBuf> {
+    let mut matching_files = Vec::new();
+    for entry in rfs::read_dir(path) {
+        let entry = entry.expect("failed to read directory entry.");
+        let path = entry.path();
+
+        if path.is_dir() && filter(&path) {
+            matching_files.push(path);
+        }
+    }
+    matching_files
+}
+
 /// Returns true if the filename at `path` does not contain `expected`.
 pub fn not_contains<P: AsRef<Path>>(path: P, expected: &str) -> bool {
     !path.as_ref().file_name().is_some_and(|name| name.to_str().unwrap().contains(expected))