rust/clippy_dev/src/bless.rs

77 lines
3.0 KiB
Rust
Raw Normal View History

2020-12-10 11:34:22 +01:00
//! `bless` updates the reference files in the repo with changed output files
//! from the last test run.
use std::env;
use std::ffi::OsStr;
use std::fs;
use std::lazy::SyncLazy;
use std::path::PathBuf;
use walkdir::WalkDir;
use crate::clippy_project_root;
// NOTE: this is duplicated with tests/cargo/mod.rs What to do?
pub static CARGO_TARGET_DIR: SyncLazy<PathBuf> = SyncLazy::new(|| match env::var_os("CARGO_TARGET_DIR") {
Some(v) => v.into(),
None => env::current_dir().unwrap().join("target"),
});
pub fn bless() {
let test_dirs = [
clippy_project_root().join("tests").join("ui"),
clippy_project_root().join("tests").join("ui-toml"),
clippy_project_root().join("tests").join("ui-cargo"),
];
for test_dir in &test_dirs {
WalkDir::new(test_dir)
.into_iter()
.filter_map(Result::ok)
.filter(|f| f.path().extension() == Some(OsStr::new("rs")))
.for_each(|f| {
let test_name = f.path().strip_prefix(test_dir).unwrap();
update_reference_file(f.path().with_extension("stdout"), test_name.with_extension("stdout"));
update_reference_file(f.path().with_extension("stderr"), test_name.with_extension("stderr"));
update_reference_file(f.path().with_extension("fixed"), test_name.with_extension("fixed"));
});
}
}
fn update_reference_file(reference_file_path: PathBuf, test_name: PathBuf) {
let test_output_path = build_dir().join(test_name);
2020-12-10 11:34:22 +01:00
let relative_reference_file_path = reference_file_path.strip_prefix(clippy_project_root()).unwrap();
// If compiletest did not write any changes during the test run,
// we don't have to update anything
2020-12-10 11:34:22 +01:00
if !test_output_path.exists() {
return;
}
2020-12-10 11:34:22 +01:00
let test_output_file = fs::read(&test_output_path).expect("Unable to read test output file");
let reference_file = fs::read(&reference_file_path).unwrap_or_default();
2020-12-10 11:34:22 +01:00
if test_output_file != reference_file {
// If a test run caused an output file to change, update the reference file
println!("updating {}", &relative_reference_file_path.display());
fs::copy(test_output_path, &reference_file_path).expect("Could not update reference file");
// We need to re-read the file now because it was potentially updated from copying
let reference_file = fs::read(&reference_file_path).unwrap_or_default();
2020-12-10 11:34:22 +01:00
if reference_file.is_empty() {
// If we copied over an empty output file, we remove the now empty reference file
println!("removing {}", &relative_reference_file_path.display());
fs::remove_file(reference_file_path).expect("Could not remove reference file");
}
}
}
fn build_dir() -> PathBuf {
2020-12-12 15:14:54 +01:00
let profile = env::var("PROFILE").unwrap_or_else(|_| "debug".to_string());
let mut path = PathBuf::new();
path.push(CARGO_TARGET_DIR.clone());
path.push(profile);
path.push("test_build_base");
path
}