Skip to content

Commit 2496742

Browse files
committed
add shallow_find_files helper function to run-make-support
1 parent 9619479 commit 2496742

File tree

3 files changed

+69
-10
lines changed
  • src/tools/run-make-support/src
  • tests/run-make

3 files changed

+69
-10
lines changed

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

+34
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,40 @@ 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 all files which respect the parameters
251+
/// outlined by `closure`.
252+
#[track_caller]
253+
pub fn shallow_find_files<P: AsRef<Path>, F: Fn(&PathBuf) -> bool>(
254+
path: P,
255+
closure: F,
256+
) -> Vec<PathBuf> {
257+
let mut matching_files = Vec::new();
258+
for entry in fs_wrapper::read_dir(path) {
259+
let entry = entry.expect("failed to read directory entry.");
260+
let path = entry.path();
261+
262+
if path.is_file() && closure(&path) {
263+
matching_files.push(path);
264+
}
265+
}
266+
matching_files
267+
}
268+
269+
/// Returns true if the filename at `path` starts with `prefix`.
270+
pub fn has_prefix<P: AsRef<Path>>(path: P, prefix: &str) -> bool {
271+
path.as_ref().file_name().is_some_and(|name| name.to_str().unwrap().starts_with(prefix))
272+
}
273+
274+
/// Returns true if the filename at `path` has the extension `extension`.
275+
pub fn has_extension<P: AsRef<Path>>(path: P, extension: &str) -> bool {
276+
path.as_ref().extension().is_some_and(|ext| ext == extension)
277+
}
278+
279+
/// Returns true if the filename at `path` does not contain `expected`.
280+
pub fn not_contains<P: AsRef<Path>>(path: P, expected: &str) -> bool {
281+
!path.as_ref().file_name().is_some_and(|name| name.to_str().unwrap().contains(expected))
282+
}
283+
250284
/// Use `cygpath -w` on a path to get a Windows path string back. This assumes that `cygpath` is
251285
/// available on the platform!
252286
#[track_caller]

tests/run-make/optimization-remarks-dir-pgo/rmake.rs

+19-7
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,20 @@
77
//@ needs-profiler-support
88
//@ ignore-cross-compile
99

10-
use run_make_support::{invalid_utf8_contains, llvm_profdata, run, rustc};
10+
use run_make_support::{
11+
has_extension, has_prefix, invalid_utf8_contains, llvm_profdata, run, rustc, shallow_find_files,
12+
};
1113

1214
fn main() {
1315
rustc().profile_generate("profdata").opt().input("foo.rs").output("foo").run();
1416
run("foo");
15-
llvm_profdata()
16-
.merge()
17-
.output("merged.profdata")
18-
.input("profdata/default_15907418011457399462_0.profraw")
19-
.run();
17+
// The profdata filename is a long sequence of numbers, fetch it by prefix and extension
18+
// to keep the test working even if the filename changes.
19+
let profdata_files = shallow_find_files("profdata", |path| {
20+
has_prefix(path, "default") && has_extension(path, "profraw")
21+
});
22+
let profdata_file = profdata_files.get(0).unwrap();
23+
llvm_profdata().merge().output("merged.profdata").input(profdata_file).run();
2024
rustc()
2125
.profile_use("merged.profdata")
2226
.opt()
@@ -25,5 +29,13 @@ fn main() {
2529
.arg("-Zremark-dir=profiles")
2630
.run();
2731
// Check that PGO hotness is included in the remark files
28-
invalid_utf8_contains("profiles/foo.cba44757bc0621b9-cgu.0.opt.opt.yaml", "Hotness");
32+
let remark_files = shallow_find_files("profiles", |path| {
33+
has_prefix(path, "foo") && has_extension(path, "yaml")
34+
});
35+
assert!(!remark_files.is_empty());
36+
for file in remark_files {
37+
if !file.to_str().unwrap().contains("codegen") {
38+
invalid_utf8_contains(file, "Hotness")
39+
};
40+
}
2941
}

tests/run-make/optimization-remarks-dir/rmake.rs

+16-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
// (should not have the `inline` mention).
55
// See https://github.com/rust-lang/rust/pull/113040
66

7-
use run_make_support::{invalid_utf8_contains, invalid_utf8_not_contains, rustc};
7+
use run_make_support::{
8+
has_extension, has_prefix, invalid_utf8_contains, invalid_utf8_not_contains, not_contains,
9+
rustc, shallow_find_files,
10+
};
811

912
fn main() {
1013
rustc()
@@ -14,13 +17,23 @@ fn main() {
1417
.arg("-Cremark=all")
1518
.arg("-Zremark-dir=profiles_all")
1619
.run();
17-
invalid_utf8_contains("profiles_all/foo.5be5606e1f6aa79b-cgu.0.opt.opt.yaml", "inline");
20+
let all_remark_files = shallow_find_files("profiles_all", |path| {
21+
has_prefix(path, "foo") && has_extension(path, "yaml") && not_contains(path, "codegen")
22+
});
23+
for file in all_remark_files {
24+
invalid_utf8_contains(file, "inline")
25+
}
1826
rustc()
1927
.opt()
2028
.input("foo.rs")
2129
.crate_type("lib")
2230
.arg("-Cremark=foo")
2331
.arg("-Zremark-dir=profiles_foo")
2432
.run();
25-
invalid_utf8_not_contains("profiles_foo/foo.5be5606e1f6aa79b-cgu.0.opt.opt.yaml", "inline");
33+
let foo_remark_files = shallow_find_files("profiles_foo", |path| {
34+
has_prefix(path, "foo") && has_extension(path, "yaml")
35+
});
36+
for file in foo_remark_files {
37+
invalid_utf8_not_contains(file, "inline")
38+
}
2639
}

0 commit comments

Comments
 (0)