Skip to content

Commit dd52066

Browse files
committed
Auto merge of #6547 - camsteffen:curse-outdated, r=phansch
Curse outdated test output changelog: internal Change `cargo dev bless` to only include test output that was generated since the last build of clippy. This is especially useful when running tests with `TESTNAME=...`. The feature may be disabled by `cargo dev bless --ignore-timestamp`.
2 parents 976850b + cbbb188 commit dd52066

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

clippy_dev/src/bless.rs

+29-7
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::env;
55
use std::ffi::OsStr;
66
use std::fs;
77
use std::lazy::SyncLazy;
8-
use std::path::PathBuf;
8+
use std::path::{Path, PathBuf};
99
use walkdir::WalkDir;
1010

1111
use crate::clippy_project_root;
@@ -16,7 +16,15 @@ pub static CARGO_TARGET_DIR: SyncLazy<PathBuf> = SyncLazy::new(|| match env::var
1616
None => env::current_dir().unwrap().join("target"),
1717
});
1818

19-
pub fn bless() {
19+
static CLIPPY_BUILD_TIME: SyncLazy<Option<std::time::SystemTime>> = SyncLazy::new(|| {
20+
let profile = env::var("PROFILE").unwrap_or_else(|_| "debug".to_string());
21+
let mut path = PathBuf::from(&**CARGO_TARGET_DIR);
22+
path.push(profile);
23+
path.push("cargo-clippy");
24+
fs::metadata(path).ok()?.modified().ok()
25+
});
26+
27+
pub fn bless(ignore_timestamp: bool) {
2028
let test_suite_dirs = [
2129
clippy_project_root().join("tests").join("ui"),
2230
clippy_project_root().join("tests").join("ui-internal"),
@@ -30,15 +38,18 @@ pub fn bless() {
3038
.filter(|f| f.path().extension() == Some(OsStr::new("rs")))
3139
.for_each(|f| {
3240
let test_name = f.path().strip_prefix(test_suite_dir).unwrap();
33-
34-
update_reference_file(f.path().with_extension("stdout"), test_name.with_extension("stdout"));
35-
update_reference_file(f.path().with_extension("stderr"), test_name.with_extension("stderr"));
36-
update_reference_file(f.path().with_extension("fixed"), test_name.with_extension("fixed"));
41+
for &ext in &["stdout", "stderr", "fixed"] {
42+
update_reference_file(
43+
f.path().with_extension(ext),
44+
test_name.with_extension(ext),
45+
ignore_timestamp,
46+
);
47+
}
3748
});
3849
}
3950
}
4051

41-
fn update_reference_file(reference_file_path: PathBuf, test_name: PathBuf) {
52+
fn update_reference_file(reference_file_path: PathBuf, test_name: PathBuf, ignore_timestamp: bool) {
4253
let test_output_path = build_dir().join(test_name);
4354
let relative_reference_file_path = reference_file_path.strip_prefix(clippy_project_root()).unwrap();
4455

@@ -48,6 +59,11 @@ fn update_reference_file(reference_file_path: PathBuf, test_name: PathBuf) {
4859
return;
4960
}
5061

62+
// If the test output was not updated since the last clippy build, it may be outdated
63+
if !ignore_timestamp && !updated_since_clippy_build(&test_output_path).unwrap_or(true) {
64+
return;
65+
}
66+
5167
let test_output_file = fs::read(&test_output_path).expect("Unable to read test output file");
5268
let reference_file = fs::read(&reference_file_path).unwrap_or_default();
5369

@@ -67,6 +83,12 @@ fn update_reference_file(reference_file_path: PathBuf, test_name: PathBuf) {
6783
}
6884
}
6985

86+
fn updated_since_clippy_build(path: &Path) -> Option<bool> {
87+
let clippy_build_time = (*CLIPPY_BUILD_TIME)?;
88+
let modified = fs::metadata(path).ok()?.modified().ok()?;
89+
Some(modified >= clippy_build_time)
90+
}
91+
7092
fn build_dir() -> PathBuf {
7193
let profile = env::var("PROFILE").unwrap_or_else(|_| "debug".to_string());
7294
let mut path = PathBuf::new();

clippy_dev/src/main.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ fn main() {
77
let matches = get_clap_config();
88

99
match matches.subcommand() {
10-
("bless", Some(_)) => {
11-
bless::bless();
10+
("bless", Some(matches)) => {
11+
bless::bless(matches.is_present("ignore-timestamp"));
1212
},
1313
("fmt", Some(matches)) => {
1414
fmt::run(matches.is_present("check"), matches.is_present("verbose"));
@@ -47,7 +47,15 @@ fn main() {
4747

4848
fn get_clap_config<'a>() -> ArgMatches<'a> {
4949
App::new("Clippy developer tooling")
50-
.subcommand(SubCommand::with_name("bless").about("bless the test output changes"))
50+
.subcommand(
51+
SubCommand::with_name("bless")
52+
.about("bless the test output changes")
53+
.arg(
54+
Arg::with_name("ignore-timestamp")
55+
.long("ignore-timestamp")
56+
.help("Include files updated before clippy was built"),
57+
),
58+
)
5159
.subcommand(
5260
SubCommand::with_name("fmt")
5361
.about("Run rustfmt on all projects and tests")

0 commit comments

Comments
 (0)