forked from bazelbuild/rules_rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollect_coverage.rs
181 lines (157 loc) · 5.78 KB
/
collect_coverage.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//! This script collects code coverage data for Rust sources, after the tests
//! were executed.
//!
//! By taking advantage of Bazel C++ code coverage collection, this script is
//! able to be executed by the existing coverage collection mechanics.
//!
//! Bazel uses the lcov tool for gathering coverage data. There is also
//! an experimental support for clang llvm coverage, which uses the .profraw
//! data files to compute the coverage report.
//!
//! This script assumes the following environment variables are set:
//! - `COVERAGE_DIR``: Directory containing metadata files needed for coverage collection (e.g. gcda files, profraw).
//! - `COVERAGE_OUTPUT_FILE`: The coverage action output path.
//! - `ROOT`: Location from where the code coverage collection was invoked.
//! - `RUNFILES_DIR`: Location of the test's runfiles.
//! - `VERBOSE_COVERAGE`: Print debug info from the coverage scripts
//!
//! The script looks in $COVERAGE_DIR for the Rust metadata coverage files
//! (profraw) and uses lcov to get the coverage data. The coverage data
//! is placed in $COVERAGE_DIR as a `coverage.dat` file.
use std::env;
use std::fs;
use std::path::Path;
use std::path::PathBuf;
use std::process;
macro_rules! debug_log {
($($arg:tt)*) => {
if env::var("VERBOSE_COVERAGE").is_ok() {
eprintln!($($arg)*);
}
};
}
fn find_metadata_file(execroot: &Path, runfiles_dir: &Path, path: &str) -> PathBuf {
if execroot.join(path).exists() {
return execroot.join(path);
}
debug_log!(
"File does not exist in execroot, falling back to runfiles: {}",
path
);
runfiles_dir.join(path)
}
fn find_test_binary(execroot: &Path, runfiles_dir: &Path) -> PathBuf {
let test_binary = runfiles_dir
.join(env::var("TEST_WORKSPACE").unwrap())
.join(env::var("TEST_BINARY").unwrap());
if !test_binary.exists() {
let configuration = runfiles_dir
.strip_prefix(execroot)
.expect("RUNFILES_DIR should be relative to ROOT")
.components()
.enumerate()
.filter_map(|(i, part)| {
// Keep only `bazel-out/<configuration>/bin`
if i < 3 {
Some(PathBuf::from(part.as_os_str()))
} else {
None
}
})
.fold(PathBuf::new(), |mut path, part| {
path.push(part);
path
});
let test_binary = execroot
.join(configuration)
.join(env::var("TEST_BINARY").unwrap());
debug_log!(
"TEST_BINARY is not found in runfiles. Falling back to: {}",
test_binary.display()
);
test_binary
} else {
test_binary
}
}
fn main() {
let coverage_dir = PathBuf::from(env::var("COVERAGE_DIR").unwrap());
let execroot = PathBuf::from(env::var("ROOT").unwrap());
let mut runfiles_dir = PathBuf::from(env::var("RUNFILES_DIR").unwrap());
if !runfiles_dir.is_absolute() {
runfiles_dir = execroot.join(runfiles_dir);
}
debug_log!("ROOT: {}", execroot.display());
debug_log!("RUNFILES_DIR: {}", runfiles_dir.display());
let coverage_output_file = coverage_dir.join("coverage.dat");
let profdata_file = coverage_dir.join("coverage.profdata");
let llvm_cov = find_metadata_file(
&execroot,
&runfiles_dir,
&env::var("RUST_LLVM_COV").unwrap(),
);
let llvm_profdata = find_metadata_file(
&execroot,
&runfiles_dir,
&env::var("RUST_LLVM_PROFDATA").unwrap(),
);
let test_binary = find_test_binary(&execroot, &runfiles_dir);
let profraw_files: Vec<PathBuf> = fs::read_dir(coverage_dir)
.unwrap()
.flatten()
.filter_map(|entry| {
let path = entry.path();
if let Some(ext) = path.extension() {
if ext == "profraw" {
return Some(path);
}
}
None
})
.collect();
let mut llvm_profdata_cmd = process::Command::new(llvm_profdata);
llvm_profdata_cmd
.arg("merge")
.arg("--sparse")
.args(profraw_files)
.arg("--output")
.arg(&profdata_file);
debug_log!("Spawning {:#?}", llvm_profdata_cmd);
let status = llvm_profdata_cmd
.status()
.expect("Failed to spawn llvm-profdata process");
if !status.success() {
process::exit(status.code().unwrap_or(1));
}
let mut llvm_cov_cmd = process::Command::new(llvm_cov);
llvm_cov_cmd
.arg("export")
.arg("-format=lcov")
.arg("-instr-profile")
.arg(&profdata_file)
.arg("-ignore-filename-regex='.*external/.+'")
.arg("-ignore-filename-regex='/tmp/.+'")
.arg(format!("-path-equivalence=.,'{}'", execroot.display()))
.arg(test_binary)
.stdout(process::Stdio::piped());
debug_log!("Spawning {:#?}", llvm_cov_cmd);
let child = llvm_cov_cmd
.spawn()
.expect("Failed to spawn llvm-cov process");
let output = child.wait_with_output().expect("llvm-cov process failed");
// Parse the child process's stdout to a string now that it's complete.
debug_log!("Parsing llvm-cov output");
let report_str = std::str::from_utf8(&output.stdout).expect("Failed to parse llvm-cov output");
debug_log!("Writing output to {}", coverage_output_file.display());
fs::write(
coverage_output_file,
report_str
.replace("#/proc/self/cwd/", "")
.replace(&execroot.display().to_string(), ""),
)
.unwrap();
// Destroy the intermediate binary file so lcov_merger doesn't parse it twice.
debug_log!("Cleaning up {}", profdata_file.display());
fs::remove_file(profdata_file).unwrap();
debug_log!("Success!");
}