Skip to content

Commit 13acecc

Browse files
committed
Add support for rustfix coverage tracking
Pretty much a copy of rust-lang/rust#59398 minus the rustc bootstrap things. This will allow rustfix coverage tracking in Clippy and other libraries that emit `MachineApplicable` diagnostics.
1 parent e2056b1 commit 13acecc

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

src/common.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,11 @@ pub struct Config {
215215
/// where to find the remote test client process, if we're using it
216216
pub remote_test_client: Option<PathBuf>,
217217

218+
/// If true, this will generate a coverage file with UI test files that run `MachineApplicable`
219+
/// diagnostics but are missing `run-rustfix` annotations. The generated coverage file is
220+
/// created in `/<build_base>/rustfix_missing_coverage.txt`
221+
pub rustfix_coverage: bool,
222+
218223
// Configuration for various run-make tests frobbing things like C compilers
219224
// or querying about various LLVM component information.
220225
pub cc: String,
@@ -359,6 +364,7 @@ impl Default for Config {
359364
host: platform.clone(),
360365
#[cfg(feature = "norustc")]
361366
host: env!("HOST").to_string(),
367+
rustfix_coverage: false,
362368
gdb: None,
363369
gdb_version: None,
364370
gdb_native_rust: false,

src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,18 @@ pub fn run_tests(config: &Config) {
7575
env::set_var("RUST_TEST_TASKS", "1");
7676
}
7777

78+
// If we want to collect rustfix coverage information,
79+
// we first make sure that the coverage file does not exist.
80+
// It will be created later on.
81+
if config.rustfix_coverage {
82+
let mut coverage_file_path = config.build_base.clone();
83+
coverage_file_path.push("rustfix_missing_coverage.txt");
84+
if coverage_file_path.exists() {
85+
if let Err(e) = fs::remove_file(&coverage_file_path) {
86+
panic!("Could not delete {} due to {}", coverage_file_path.display(), e)
87+
}
88+
}
89+
}
7890
let opts = test_opts(config);
7991
let tests = make_tests(config);
8092
// sadly osx needs some file descriptor limits raised for running tests in

src/runtest.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use std::collections::HashMap;
2626
use std::collections::HashSet;
2727
use std::env;
2828
use std::ffi::OsString;
29-
use std::fs::{self, File, create_dir_all};
29+
use std::fs::{self, File, create_dir_all, OpenOptions};
3030
use std::fmt;
3131
use std::io::prelude::*;
3232
use std::io::{self, BufReader};
@@ -2268,6 +2268,33 @@ actual:\n\
22682268
errors += self.compare_output(UI_STDERR, &normalized_stderr, &expected_stderr);
22692269

22702270

2271+
if self.config.rustfix_coverage {
2272+
// Find out which tests have `MachineApplicable` suggestions but are missing
2273+
// `run-rustfix` or `run-rustfix-only-machine-applicable` headers
2274+
let suggestions = get_suggestions_from_json(
2275+
&proc_res.stderr,
2276+
&HashSet::new(),
2277+
Filter::MachineApplicableOnly
2278+
).unwrap();
2279+
if suggestions.len() > 0
2280+
&& !self.props.run_rustfix
2281+
&& !self.props.rustfix_only_machine_applicable {
2282+
let mut coverage_file_path = self.config.build_base.clone();
2283+
coverage_file_path.push("rustfix_missing_coverage.txt");
2284+
debug!("coverage_file_path: {}", coverage_file_path.display());
2285+
2286+
let mut file = OpenOptions::new()
2287+
.create(true)
2288+
.append(true)
2289+
.open(coverage_file_path.as_path())
2290+
.expect("could not create or open file");
2291+
2292+
if let Err(_) = writeln!(file, "{}", self.testpaths.file.display()) {
2293+
panic!("couldn't write to {}", coverage_file_path.display());
2294+
}
2295+
}
2296+
}
2297+
22712298
if self.props.run_rustfix {
22722299
// Apply suggestions from lints to the code itself
22732300
let unfixed_code = self

0 commit comments

Comments
 (0)