Skip to content

Commit c0ead61

Browse files
authored
Give a better error message if llvm-profdata isn't installed (#55)
Previously, the test failed to check if the command actually succeeded, and so would run assertions on the command's output even if it failed. Before: ``` $ cargo test --test profdata -- profraws LLVM tools failed: error: no such command: `profdata` help: view all installed commands with `cargo --list` help: find a package to install `profdata` with `cargo search cargo-profdata` "c-general.profraw" LLVM tools failed: error: no such command: `profdata` help: view all installed commands with `cargo --list` help: find a package to install `profdata` with `cargo search cargo-profdata` "compressed.profraw" <... repeated for each failing test ...> ``` After: ``` $ cargo test --test profdata -- profdatas ---- show_profdatas stdout ---- thread 'show_profraws' panicked at /rustc/ec7c02612527d185c379900b613311bc1dcbf7dc/library/core/src/ops/function.rs:253:5: Unexpected failure. code=101 stderr=```"error: No such file or directory (os error 2)\n"``` command=`"cargo" "profdata" "--" "--version"` help=`run 'rustup component add llvm-tools-preview'` code=101 stdout="" stderr="error: No such file or directory (os error 2)\n" note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace ```
1 parent 2315584 commit c0ead61

1 file changed

Lines changed: 31 additions & 3 deletions

File tree

tests/profdata.rs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::ffi::OsStr;
55
use std::fs::read_dir;
66
use std::path::PathBuf;
77
use std::process::Command;
8+
use std::sync::LazyLock;
89

910
/*
1011
Counters:
@@ -126,7 +127,28 @@ fn check_merge_command(files: &[PathBuf], id: &str) {
126127
}
127128
}
128129

130+
// Check that we have the exes we need to run these test at all.
131+
// Otherwise, we give very poor error messages running the same commands in a loop over and over.
132+
static ASSERT_CMDS_EXIST: LazyLock<()> = LazyLock::new(|| {
133+
assert_cmd::Command::new("cargo")
134+
.args(&["profdata", "--version"])
135+
.assert()
136+
.append_context(
137+
"help",
138+
"run 'cargo install cargo-binutils && rustup component add llvm-tools-preview'",
139+
)
140+
.success();
141+
// this is the version of llvm-profdata itself
142+
assert_cmd::Command::new("cargo")
143+
.args(&["profdata", "--", "--version"])
144+
.assert()
145+
.append_context("help", "run 'rustup component add llvm-tools-preview'")
146+
.success();
147+
});
148+
129149
fn check_command(ext: &OsStr) {
150+
LazyLock::force(&ASSERT_CMDS_EXIST);
151+
130152
// TODO we should consider doing different permutations of args. Some things which rely on
131153
// the ordering of elements in a priority_queue etc will display differently though...
132154
let data = get_data_dir();
@@ -145,7 +167,7 @@ fn check_command(ext: &OsStr) {
145167
.args(&["profdata", "--", "show", "--all-functions", "--counts"])
146168
.arg(raw_file.file_name())
147169
.output()
148-
.expect("cargo binutils or llvm-profdata is not installed");
170+
.expect("cargo not installed???");
149171

150172
let llvm_struct: Output = serde_yaml::from_slice(&llvm.stdout).unwrap();
151173

@@ -177,6 +199,8 @@ fn check_command(ext: &OsStr) {
177199
}
178200

179201
fn check_against_text(ext: &OsStr) {
202+
LazyLock::force(&ASSERT_CMDS_EXIST);
203+
180204
let data = get_data_dir();
181205
let mut count = 0;
182206
for raw_file in read_dir(&data)
@@ -197,7 +221,7 @@ fn check_against_text(ext: &OsStr) {
197221
])
198222
.arg(raw_file.file_name())
199223
.output()
200-
.expect("cargo binutils or llvm-profdata is not installed");
224+
.expect("failed to spawn cargo?");
201225

202226
if llvm.status.success() {
203227
count += 1;
@@ -224,7 +248,11 @@ fn check_against_text(ext: &OsStr) {
224248
let parse_records = parsed_prof.records().iter().collect::<HashSet<_>>();
225249
assert_eq!(text_records, parse_records);
226250
} else {
227-
println!("{} failed", raw_file.path().display());
251+
println!(
252+
"{} failed: {}",
253+
raw_file.path().display(),
254+
String::from_utf8_lossy(&llvm.stderr),
255+
);
228256
}
229257
}
230258
if count == 0 {

0 commit comments

Comments
 (0)