Skip to content

Commit 02b8533

Browse files
committed
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.
1 parent fb5ed48 commit 02b8533

File tree

5 files changed

+66
-1
lines changed

5 files changed

+66
-1
lines changed

src/bootstrap/flags.rs

+15
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ pub enum Subcommand {
5656
rustc_args: Vec<String>,
5757
fail_fast: bool,
5858
doc_tests: DocTests,
59+
rustfix_coverage: bool,
5960
},
6061
Bench {
6162
paths: Vec<PathBuf>,
@@ -188,6 +189,12 @@ To learn more about a subcommand, run `./x.py <subcommand> -h`"
188189
"mode describing what file the actual ui output will be compared to",
189190
"COMPARE MODE",
190191
);
192+
opts.optflag(
193+
"",
194+
"rustfix-coverage",
195+
"enable this to generate a Rustfix coverage file, which is saved in \
196+
`/tmp/rustfix_missing_coverage.txt`",
197+
);
191198
}
192199
"bench" => {
193200
opts.optmulti("", "test-args", "extra arguments", "ARGS");
@@ -363,6 +370,7 @@ Arguments:
363370
test_args: matches.opt_strs("test-args"),
364371
rustc_args: matches.opt_strs("rustc-args"),
365372
fail_fast: !matches.opt_present("no-fail-fast"),
373+
rustfix_coverage: matches.opt_present("rustfix-coverage"),
366374
doc_tests: if matches.opt_present("doc") {
367375
DocTests::Only
368376
} else if matches.opt_present("no-doc") {
@@ -467,6 +475,13 @@ impl Subcommand {
467475
}
468476
}
469477

478+
pub fn rustfix_coverage(&self) -> bool {
479+
match *self {
480+
Subcommand::Test { rustfix_coverage, .. } => rustfix_coverage,
481+
_ => false,
482+
}
483+
}
484+
470485
pub fn compare_mode(&self) -> Option<&str> {
471486
match *self {
472487
Subcommand::Test {

src/bootstrap/test.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1284,6 +1284,10 @@ impl Step for Compiletest {
12841284
cmd.arg("--android-cross-path").arg("");
12851285
}
12861286

1287+
if builder.config.cmd.rustfix_coverage() {
1288+
cmd.arg("--rustfix-coverage");
1289+
}
1290+
12871291
builder.ci_env.force_coloring_in_ci(&mut cmd);
12881292

12891293
let _folder = builder.fold_output(|| format!("test_{}", suite));

src/tools/compiletest/src/common.rs

+5
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,11 @@ pub struct Config {
245245
/// mode describing what file the actual ui output will be compared to
246246
pub compare_mode: Option<CompareMode>,
247247

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

src/tools/compiletest/src/main.rs

+19
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+
`/tmp/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,18 @@ 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 coverage_file_path = Path::new("/tmp/rustfix_missing_coverage.txt");
490+
if coverage_file_path.exists() {
491+
if let Err(e) = fs::remove_file(coverage_file_path) {
492+
panic!("Could not delete {} due to {}", coverage_file_path.display(), e)
493+
}
494+
}
495+
}
496+
478497
let opts = test_opts(config);
479498
let tests = make_tests(config);
480499
// sadly osx needs some file descriptor limits raised for running tests in

src/tools/compiletest/src/runtest.rs

+23-1
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,28 @@ 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 coverage_file_path = Path::new("/tmp/rustfix_missing_coverage.txt");
2833+
let mut file = OpenOptions::new()
2834+
.create(true)
2835+
.append(true)
2836+
.open(coverage_file_path)
2837+
.expect("could not create or open file");
2838+
2839+
if let Err(_) = writeln!(file, "{}", self.testpaths.file.display()) {
2840+
panic!("couldn't write to {}", coverage_file_path.display());
2841+
}
2842+
}
28212843
} else if self.props.run_rustfix {
28222844
// Apply suggestions from rustc to the code itself
28232845
let unfixed_code = self

0 commit comments

Comments
 (0)