Skip to content

Commit 769d988

Browse files
committed
Add find_files helper function to run-make-support
1 parent a024826 commit 769d988

File tree

3 files changed

+22
-15
lines changed

3 files changed

+22
-15
lines changed

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

+14-11
Original file line numberDiff line numberDiff line change
@@ -247,23 +247,16 @@ pub fn test_while_readonly<P: AsRef<Path>, F: FnOnce() + std::panic::UnwindSafe>
247247
success.unwrap();
248248
}
249249

250-
/// Browse the directory `path` non-recursively and return the first file path which starts with `prefix` and has the
251-
/// file extension `extension`.
250+
/// Browse the directory `path` non-recursively and return all files which respect the parameters
251+
/// outlined by `closure`.
252252
#[track_caller]
253-
pub fn find_files_by_prefix_and_extension<P: AsRef<Path>>(
254-
path: P,
255-
prefix: &str,
256-
extension: &str,
257-
) -> Vec<PathBuf> {
253+
pub fn find_files<P: AsRef<Path>, F: Fn(&PathBuf) -> bool>(path: P, closure: F) -> Vec<PathBuf> {
258254
let mut matching_files = Vec::new();
259255
for entry in fs_wrapper::read_dir(path) {
260256
let entry = entry.expect("failed to read directory entry.");
261257
let path = entry.path();
262258

263-
if path.is_file()
264-
&& path.file_name().unwrap().to_str().unwrap().starts_with(prefix)
265-
&& path.extension().unwrap().to_str().unwrap() == extension
266-
{
259+
if path.is_file() && closure(&path) {
267260
matching_files.push(path);
268261
}
269262
}
@@ -275,6 +268,16 @@ pub fn find_files_by_prefix_and_extension<P: AsRef<Path>>(
275268
}
276269
}
277270

271+
/// Returns true if the filename at `path` starts with `prefix`.
272+
pub fn has_prefix<P: AsRef<Path>>(path: P, prefix: &str) -> bool {
273+
path.as_ref().file_name().unwrap().to_str().unwrap().starts_with(prefix)
274+
}
275+
276+
/// Returns true if the filename at `path` has the extension `extension`.
277+
pub fn has_extension<P: AsRef<Path>>(path: P, extension: &str) -> bool {
278+
path.as_ref().extension().unwrap().to_str().unwrap() == extension
279+
}
280+
278281
/// Use `cygpath -w` on a path to get a Windows path string back. This assumes that `cygpath` is
279282
/// available on the platform!
280283
#[track_caller]

tests/run-make/pgo-gen/rmake.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
//@ needs-profiler-support
77
//@ ignore-cross-compile
88

9-
use run_make_support::{cwd, find_files_by_prefix_and_extension, run, rustc};
9+
use run_make_support::{cwd, find_files, has_extension, has_prefix, run, rustc};
1010

1111
fn main() {
1212
rustc().arg("-g").profile_generate(cwd()).run();
1313
run("test");
1414
assert!(
15-
find_files_by_prefix_and_extension(cwd(), "default", "profraw").len() > 0,
15+
find_files(cwd(), |path| { has_prefix("default") && has_extension("profraw") }).len() > 0,
1616
"no .profraw file generated"
1717
);
1818
}

tests/run-make/pgo-use/rmake.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
//@ ignore-cross-compile
1010

1111
use run_make_support::{
12-
cwd, find_files_by_prefix_and_extension, fs_wrapper, llvm_filecheck, llvm_profdata,
12+
cwd, find_files, fs_wrapper, has_extension, has_prefix, llvm_filecheck, llvm_profdata,
1313
run_with_args, rustc,
1414
};
1515

@@ -31,7 +31,11 @@ fn main() {
3131
llvm_profdata()
3232
.merge()
3333
.output("merged.profdata")
34-
.input(find_files_by_prefix_and_extension(cwd(), "default", "profraw").get(0).unwrap())
34+
.input(
35+
find_files(cwd(), |path| has_prefix("default") && has_extension("profraw"))
36+
.get(0)
37+
.unwrap(),
38+
)
3539
.run();
3640
// Compile the test program again, making use of the profiling data
3741
rustc()

0 commit comments

Comments
 (0)