Skip to content

Commit

Permalink
add shallow_find_files helper function to run-make-support
Browse files Browse the repository at this point in the history
  • Loading branch information
Oneirical committed Jul 3, 2024
1 parent 9619479 commit 2496742
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 10 deletions.
34 changes: 34 additions & 0 deletions src/tools/run-make-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,40 @@ pub fn test_while_readonly<P: AsRef<Path>, 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<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
/// available on the platform!
#[track_caller]
Expand Down
26 changes: 19 additions & 7 deletions tests/run-make/optimization-remarks-dir-pgo/rmake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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")
};
}
}
19 changes: 16 additions & 3 deletions tests/run-make/optimization-remarks-dir/rmake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
// (should not have the `inline` mention).
// See https:/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()
Expand All @@ -14,13 +17,23 @@ 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")
.crate_type("lib")
.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")
}
}

0 comments on commit 2496742

Please sign in to comment.