Skip to content

Commit b75b165

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 a92d689 + d808bd4 commit b75b165

File tree

6 files changed

+72
-1
lines changed

6 files changed

+72
-1
lines changed

src/bootstrap/builder.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1856,6 +1856,7 @@ mod __test {
18561856
doc_tests: DocTests::No,
18571857
bless: false,
18581858
compare_mode: None,
1859+
rustfix_coverage: false,
18591860
};
18601861

18611862
let build = Build::new(config);
@@ -1897,6 +1898,7 @@ mod __test {
18971898
doc_tests: DocTests::No,
18981899
bless: false,
18991900
compare_mode: None,
1901+
rustfix_coverage: false,
19001902
};
19011903

19021904
let build = Build::new(config);

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+
`/<build_base>/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
@@ -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/tools/compiletest/src/main.rs

+20
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/tools/compiletest/src/runtest.rs

+26-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,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)