Skip to content

Commit ac1d44a

Browse files
authored
Rollup merge of #59398 - phansch:rustfix_coverage, r=oli-obk
Add a way to track Rustfix UI test coverage This came out of the first Rustfix WG meeting. One of the goals is to enable Rustfix tests for all UI tests that trigger lints with `MachineApplicable` suggestions. In order to do that we first want to create a tracking issue that lists all files with missing `// run-rustfix` headers. This PR adds a `--rustfix-coverage` flag to `./x.py` and compiletest to list the files with the missing headers in `/tmp/rustfix_missing_coverage.txt`. From that file we can create the tracking issue and at some point also enforce the `// run-rustfix` flag on UI tests with `MachineApplicable` lints.
2 parents 322aae1 + a99de47 commit ac1d44a

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

src/common.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,11 @@ pub struct Config {
246246
/// mode describing what file the actual ui output will be compared to
247247
pub compare_mode: Option<CompareMode>,
248248

249+
/// If true, this will generate a coverage file with UI test files that run `MachineApplicable`
250+
/// diagnostics but are missing `run-rustfix` annotations. The generated coverage file is
251+
/// created in `/<build_base>/rustfix_missing_coverage.txt`
252+
pub rustfix_coverage: bool,
253+
249254
// Configuration for various run-make tests frobbing things like C compilers
250255
// or querying about various LLVM component information.
251256
pub cc: String,

src/main.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,12 @@ pub fn parse_config(args: Vec<String>) -> Config {
233233
"mode describing what file the actual ui output will be compared to",
234234
"COMPARE MODE",
235235
)
236+
.optflag(
237+
"",
238+
"rustfix-coverage",
239+
"enable this to generate a Rustfix coverage file, which is saved in \
240+
`./<build_base>/rustfix_missing_coverage.txt`",
241+
)
236242
.optflag("h", "help", "show this message");
237243

238244
let (argv0, args_) = args.split_first().unwrap();
@@ -336,6 +342,7 @@ pub fn parse_config(args: Vec<String>) -> Config {
336342
color,
337343
remote_test_client: matches.opt_str("remote-test-client").map(PathBuf::from),
338344
compare_mode: matches.opt_str("compare-mode").map(CompareMode::parse),
345+
rustfix_coverage: matches.opt_present("rustfix-coverage"),
339346

340347
cc: matches.opt_str("cc").unwrap(),
341348
cxx: matches.opt_str("cxx").unwrap(),
@@ -475,6 +482,19 @@ pub fn run_tests(config: &Config) {
475482
let _ = fs::remove_dir_all("tmp/partitioning-tests");
476483
}
477484

485+
// If we want to collect rustfix coverage information,
486+
// we first make sure that the coverage file does not exist.
487+
// It will be created later on.
488+
if config.rustfix_coverage {
489+
let mut coverage_file_path = config.build_base.clone();
490+
coverage_file_path.push("rustfix_missing_coverage.txt");
491+
if coverage_file_path.exists() {
492+
if let Err(e) = fs::remove_file(&coverage_file_path) {
493+
panic!("Could not delete {} due to {}", coverage_file_path.display(), e)
494+
}
495+
}
496+
}
497+
478498
let opts = test_opts(config);
479499
let tests = make_tests(config);
480500
// sadly osx needs some file descriptor limits raised for running tests in

src/runtest.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use std::collections::{HashMap, HashSet, VecDeque};
1919
use std::env;
2020
use std::ffi::{OsStr, OsString};
2121
use std::fmt;
22-
use std::fs::{self, create_dir_all, File};
22+
use std::fs::{self, create_dir_all, File, OpenOptions};
2323
use std::hash::{Hash, Hasher};
2424
use std::io::prelude::*;
2525
use std::io::{self, BufReader};
@@ -2818,6 +2818,31 @@ impl<'test> TestCx<'test> {
28182818

28192819
if self.config.compare_mode.is_some() {
28202820
// don't test rustfix with nll right now
2821+
} else if self.config.rustfix_coverage {
2822+
// Find out which tests have `MachineApplicable` suggestions but are missing
2823+
// `run-rustfix` or `run-rustfix-only-machine-applicable` headers
2824+
let suggestions = get_suggestions_from_json(
2825+
&proc_res.stderr,
2826+
&HashSet::new(),
2827+
Filter::MachineApplicableOnly
2828+
).unwrap();
2829+
if suggestions.len() > 0
2830+
&& !self.props.run_rustfix
2831+
&& !self.props.rustfix_only_machine_applicable {
2832+
let mut coverage_file_path = self.config.build_base.clone();
2833+
coverage_file_path.push("rustfix_missing_coverage.txt");
2834+
debug!("coverage_file_path: {}", coverage_file_path.display());
2835+
2836+
let mut file = OpenOptions::new()
2837+
.create(true)
2838+
.append(true)
2839+
.open(coverage_file_path.as_path())
2840+
.expect("could not create or open file");
2841+
2842+
if let Err(_) = writeln!(file, "{}", self.testpaths.file.display()) {
2843+
panic!("couldn't write to {}", coverage_file_path.display());
2844+
}
2845+
}
28212846
} else if self.props.run_rustfix {
28222847
// Apply suggestions from rustc to the code itself
28232848
let unfixed_code = self

0 commit comments

Comments
 (0)