diff --git a/src/tools/run-make-support/src/lib.rs b/src/tools/run-make-support/src/lib.rs index 9a180fe4ad1..af5ae6a8e60 100644 --- a/src/tools/run-make-support/src/lib.rs +++ b/src/tools/run-make-support/src/lib.rs @@ -261,6 +261,40 @@ pub fn test_while_readonly, F: FnOnce() + std::panic::UnwindSafe> 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, F: Fn(&PathBuf) -> bool>( + path: P, + closure: F, +) -> Vec { + 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>(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>(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>(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 /// available on the platform! #[track_caller] diff --git a/tests/run-make/optimization-remarks-dir-pgo/rmake.rs b/tests/run-make/optimization-remarks-dir-pgo/rmake.rs index e527dcb0bef..228c43cc5f1 100644 --- a/tests/run-make/optimization-remarks-dir-pgo/rmake.rs +++ b/tests/run-make/optimization-remarks-dir-pgo/rmake.rs @@ -7,16 +7,20 @@ //@ needs-profiler-support //@ 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() { rustc().profile_generate("profdata").opt().input("foo.rs").output("foo").run(); run("foo"); - llvm_profdata() - .merge() - .output("merged.profdata") - .input("profdata/default_15907418011457399462_0.profraw") - .run(); + // The profdata filename is a long sequence of numbers, fetch it by prefix and extension + // to keep the test working even if the filename changes. + let profdata_files = shallow_find_files("profdata", |path| { + has_prefix(path, "default") && has_extension(path, "profraw") + }); + let profdata_file = profdata_files.get(0).unwrap(); + llvm_profdata().merge().output("merged.profdata").input(profdata_file).run(); rustc() .profile_use("merged.profdata") .opt() @@ -25,5 +29,13 @@ fn main() { .arg("-Zremark-dir=profiles") .run(); // 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") + }; + } } diff --git a/tests/run-make/optimization-remarks-dir/rmake.rs b/tests/run-make/optimization-remarks-dir/rmake.rs index d0c4ccd72d1..afcb8c3e3eb 100644 --- a/tests/run-make/optimization-remarks-dir/rmake.rs +++ b/tests/run-make/optimization-remarks-dir/rmake.rs @@ -4,7 +4,10 @@ // (should not have the `inline` mention). // 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() { rustc() @@ -14,7 +17,12 @@ fn main() { .arg("-Cremark=all") .arg("-Zremark-dir=profiles_all") .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() .opt() .input("foo.rs") @@ -22,5 +30,10 @@ fn main() { .arg("-Cremark=foo") .arg("-Zremark-dir=profiles_foo") .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") + } }