in src/llvm_tools.rs [444:523]
fn test_llvm_aggregate_profraws() {
if !check_nightly_rust() {
return;
}
let tmp_dir = copy_fixture("hello_name");
let tmp_path = tmp_dir.path();
let bin_path = get_binary_path("hello_name");
let path_without = tmp_path.join("without-arg.profraw");
let path_with = tmp_path.join("with-arg.profraw");
// Run the program twice:
// - Once without arguments,
// - Once with a simple argument to enter the other side of the if.
let status = Command::new("cargo")
.arg("run")
.env("RUSTFLAGS", "-Cinstrument-coverage")
.env("LLVM_PROFILE_FILE", &path_without)
.current_dir(tmp_path)
.status()
.expect("Failed to build");
assert!(status.success(), "Error when running `cargo run`");
let status = Command::new("cargo")
.arg("run")
.arg("--")
.arg("John")
.env("RUSTFLAGS", "-Cinstrument-coverage")
.env("LLVM_PROFILE_FILE", &path_with)
.current_dir(tmp_path)
.status()
.expect("Failed to build");
assert!(status.success(), "Error when running `cargo run`");
let lcovs = llvm_profiles_to_lcov(
&[path_with, path_without],
&tmp_path.join(bin_path),
tmp_path,
);
assert!(lcovs.is_ok(), "Error: {}", lcovs.unwrap_err());
let lcovs = lcovs.unwrap();
assert_eq!(lcovs.len(), 1);
let output_lcov = String::from_utf8_lossy(&lcovs[0]);
println!("{}", output_lcov);
let lcov = String::from_utf8_lossy(&lcovs[0]);
let lcov_entries = [
"FNF:1", // # of function found
"FNH:1", // # of function hit
"DA:1,2", // Line 1 hit 2 times
"DA:2,2", // Line 2 hit 2 times
"DA:3,1", // Line 3 hit 1 time
"DA:4,1", // Line 4 hit 1 time
"DA:5,1", // Line 5 hit 1 time
"DA:6,1", // Line 6 hit 1 time
"DA:7,2", // Line 7 hit 2 time
"BRF:0", // # of branch found
"BRH:0", // # of branch hit
"LF:7", // # of line found
"LH:7", // # of line hit
];
for entry in lcov_entries {
assert!(lcov.contains(&format!("{entry}\n")));
}
let main_path = tmp_path
.join("src")
.join("main.rs")
.to_string_lossy()
.into_owned();
assert!(
lcov.lines()
.any(|line| line.contains("SF:") && line.contains(&main_path)),
"Missing source file declaration (SF) in lcov report",
);
}