Skip to content

Commit 9af03e1

Browse files
Add fs_wrapper::has_extension and get_files_extensions helper functions in run-make-support
1 parent 7d97c59 commit 9af03e1

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::ffi::OsStr;
12
use std::fs;
23
use std::path::Path;
34

@@ -111,3 +112,9 @@ pub fn set_permissions<P: AsRef<Path>>(path: P, perm: fs::Permissions) {
111112
path.as_ref().display()
112113
));
113114
}
115+
116+
/// Checks if the given file path has the expected extension.
117+
#[track_caller]
118+
pub fn has_extension<P: AsRef<Path>, S: AsRef<OsStr>>(path: P, extension: S) -> bool {
119+
path.as_ref().extension().is_some_and(|ext| ext == extension.as_ref())
120+
}

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub mod rustc;
1515
pub mod rustdoc;
1616

1717
use std::env;
18-
use std::ffi::OsString;
18+
use std::ffi::{OsStr, OsString};
1919
use std::fs;
2020
use std::io;
2121
use std::panic;
@@ -401,12 +401,26 @@ pub fn recursive_diff(dir1: impl AsRef<Path>, dir2: impl AsRef<Path>) {
401401
});
402402
}
403403

404-
pub fn read_dir<F: FnMut(&Path)>(dir: impl AsRef<Path>, mut callback: F) {
404+
pub fn read_dir<F: FnMut(&Path), P: AsRef<Path>>(dir: P, mut callback: F) {
405405
for entry in fs_wrapper::read_dir(dir) {
406406
callback(&entry.unwrap().path());
407407
}
408408
}
409409

410+
pub fn get_files_with_extension<P: AsRef<Path>, S: AsRef<OsStr>>(
411+
dir: P,
412+
extension: S,
413+
) -> Vec<PathBuf> {
414+
let mut files = Vec::new();
415+
let extension = extension.as_ref();
416+
read_dir(dir, |path| {
417+
if path.is_file() && fs_wrapper::has_extension(path, extension) {
418+
files.push(path.to_path_buf());
419+
}
420+
});
421+
files
422+
}
423+
410424
/// Check that `actual` is equal to `expected`. Panic otherwise.
411425
#[track_caller]
412426
pub fn assert_equals<S1: AsRef<str>, S2: AsRef<str>>(actual: S1, expected: S2) {

0 commit comments

Comments
 (0)