add shallow_find_files helper function to run-make-support

This commit is contained in:
Oneirical 2024-06-20 16:02:22 -04:00
parent b167a15d64
commit 3ea36f5bb6
3 changed files with 69 additions and 10 deletions

View File

@ -261,6 +261,40 @@ pub fn test_while_readonly<P: AsRef<Path>, F: FnOnce() + std::panic::UnwindSafe>
success.unwrap(); success.unwrap();
} }
/// Browse the directory `path` non-recursively and return all files which respect the parameters
/// outlined by `closure`.
#[track_caller]
pub fn shallow_find_files<P: AsRef<Path>, F: Fn(&PathBuf) -> bool>(
path: P,
closure: F,
) -> Vec<PathBuf> {
let mut matching_files = Vec::new();
for entry in fs_wrapper::read_dir(path) {
let entry = entry.expect("failed to read directory entry.");
let path = entry.path();
if path.is_file() && closure(&path) {
matching_files.push(path);
}
}
matching_files
}
/// Returns true if the filename at `path` starts with `prefix`.
pub fn has_prefix<P: AsRef<Path>>(path: P, prefix: &str) -> bool {
path.as_ref().file_name().is_some_and(|name| name.to_str().unwrap().starts_with(prefix))
}
/// Returns true if the filename at `path` has the extension `extension`.
pub fn has_extension<P: AsRef<Path>>(path: P, extension: &str) -> bool {
path.as_ref().extension().is_some_and(|ext| ext == extension)
}
/// 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))
}
/// Use `cygpath -w` on a path to get a Windows path string back. This assumes that `cygpath` is /// Use `cygpath -w` on a path to get a Windows path string back. This assumes that `cygpath` is
/// available on the platform! /// available on the platform!
#[track_caller] #[track_caller]

View File

@ -7,16 +7,20 @@
//@ needs-profiler-support //@ needs-profiler-support
//@ ignore-cross-compile //@ ignore-cross-compile
use run_make_support::{invalid_utf8_contains, llvm_profdata, run, rustc}; use run_make_support::{
has_extension, has_prefix, invalid_utf8_contains, llvm_profdata, run, rustc, shallow_find_files,
};
fn main() { fn main() {
rustc().profile_generate("profdata").opt().input("foo.rs").output("foo").run(); rustc().profile_generate("profdata").opt().input("foo.rs").output("foo").run();
run("foo"); run("foo");
llvm_profdata() // The profdata filename is a long sequence of numbers, fetch it by prefix and extension
.merge() // to keep the test working even if the filename changes.
.output("merged.profdata") let profdata_files = shallow_find_files("profdata", |path| {
.input("profdata/default_15907418011457399462_0.profraw") has_prefix(path, "default") && has_extension(path, "profraw")
.run(); });
let profdata_file = profdata_files.get(0).unwrap();
llvm_profdata().merge().output("merged.profdata").input(profdata_file).run();
rustc() rustc()
.profile_use("merged.profdata") .profile_use("merged.profdata")
.opt() .opt()
@ -25,5 +29,13 @@ fn main() {
.arg("-Zremark-dir=profiles") .arg("-Zremark-dir=profiles")
.run(); .run();
// Check that PGO hotness is included in the remark files // Check that PGO hotness is included in the remark files
invalid_utf8_contains("profiles/foo.cba44757bc0621b9-cgu.0.opt.opt.yaml", "Hotness"); let remark_files = shallow_find_files("profiles", |path| {
has_prefix(path, "foo") && has_extension(path, "yaml")
});
assert!(!remark_files.is_empty());
for file in remark_files {
if !file.to_str().unwrap().contains("codegen") {
invalid_utf8_contains(file, "Hotness")
};
}
} }

View File

@ -4,7 +4,10 @@
// (should not have the `inline` mention). // (should not have the `inline` mention).
// See https://github.com/rust-lang/rust/pull/113040 // See https://github.com/rust-lang/rust/pull/113040
use run_make_support::{invalid_utf8_contains, invalid_utf8_not_contains, rustc}; use run_make_support::{
has_extension, has_prefix, invalid_utf8_contains, invalid_utf8_not_contains, not_contains,
rustc, shallow_find_files,
};
fn main() { fn main() {
rustc() rustc()
@ -14,7 +17,12 @@ fn main() {
.arg("-Cremark=all") .arg("-Cremark=all")
.arg("-Zremark-dir=profiles_all") .arg("-Zremark-dir=profiles_all")
.run(); .run();
invalid_utf8_contains("profiles_all/foo.5be5606e1f6aa79b-cgu.0.opt.opt.yaml", "inline"); let all_remark_files = shallow_find_files("profiles_all", |path| {
has_prefix(path, "foo") && has_extension(path, "yaml") && not_contains(path, "codegen")
});
for file in all_remark_files {
invalid_utf8_contains(file, "inline")
}
rustc() rustc()
.opt() .opt()
.input("foo.rs") .input("foo.rs")
@ -22,5 +30,10 @@ fn main() {
.arg("-Cremark=foo") .arg("-Cremark=foo")
.arg("-Zremark-dir=profiles_foo") .arg("-Zremark-dir=profiles_foo")
.run(); .run();
invalid_utf8_not_contains("profiles_foo/foo.5be5606e1f6aa79b-cgu.0.opt.opt.yaml", "inline"); let foo_remark_files = shallow_find_files("profiles_foo", |path| {
has_prefix(path, "foo") && has_extension(path, "yaml")
});
for file in foo_remark_files {
invalid_utf8_not_contains(file, "inline")
}
} }