Skip to content

Commit a70955a

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

File tree

3 files changed

+25
-17
lines changed

3 files changed

+25
-17
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

+5-3
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
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() {
12-
rustc().arg("-g").profile_generate(cwd()).run();
12+
rustc().arg("-g").profile_generate(cwd()).input("test.rs").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(path, "default") && has_extension(path, "profraw") })
16+
.len()
17+
> 0,
1618
"no .profraw file generated"
1719
);
1820
}

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

+6-3
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,10 @@ 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(path, "default") && has_extension(path, "profraw"))
36+
[0],
37+
)
3538
.run();
3639
// Compile the test program again, making use of the profiling data
3740
rustc()
@@ -48,7 +51,7 @@ fn main() {
4851
// line with the function name before the line with the function attributes.
4952
// FileCheck only supports checking that something matches on the next line,
5053
// but not if something matches on the previous line.
51-
let mut bytes = fs_wrapper::read("interesting.ll");
54+
let mut bytes = fs_wrapper::read("main.ll");
5255
bytes.reverse();
5356
llvm_filecheck().patterns("filecheck-patterns.txt").stdin(bytes).run();
5457
}

0 commit comments

Comments
 (0)