]> git.lizzy.rs Git - rust.git/blobdiff - clippy_dev/src/bless.rs
s/test_dir/test_suite_dir
[rust.git] / clippy_dev / src / bless.rs
index 45e403fa74d38796f494c45f13b90472deeb65d5..5f66ff4f30eab432c6862edf108d5b5fb44ee8fe 100644 (file)
@@ -1,4 +1,4 @@
-//! `bless` updates the 'expected output' files in the repo with changed output files
+//! `bless` updates the reference files in the repo with changed output files
 //! from the last test run.
 
 use std::env;
 });
 
 pub fn bless() {
-    let test_dirs = [
+    let test_suite_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)
+    for test_suite_dir in &test_suite_dirs {
+        WalkDir::new(test_suite_dir)
             .into_iter()
             .filter_map(Result::ok)
             .filter(|f| f.path().extension() == Some(OsStr::new("rs")))
             .for_each(|f| {
-                update_test_file(f.path().with_extension("stdout"));
-                update_test_file(f.path().with_extension("stderr"));
-                update_test_file(f.path().with_extension("fixed"));
+                let test_name = f.path().strip_prefix(test_suite_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_test_file(test_file_path: PathBuf) {
-    let build_output_path = build_dir().join(PathBuf::from(test_file_path.file_name().unwrap()));
-    let relative_test_file_path = test_file_path.strip_prefix(clippy_project_root()).unwrap();
+fn update_reference_file(reference_file_path: PathBuf, test_name: PathBuf) {
+    let test_output_path = build_dir().join(test_name);
+    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
-    if !build_output_path.exists() {
+    if !test_output_path.exists() {
         return;
     }
 
-    let build_output = fs::read(&build_output_path).expect("Unable to read build output file");
-    let test_file = fs::read(&test_file_path).expect("Unable to read test file");
+    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();
+
+    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");
 
-    if build_output != test_file {
-        // If a test run caused an output file to change, update the test file
-        println!("updating {}", &relative_test_file_path.display());
-        fs::copy(build_output_path, &test_file_path).expect("Could not update test 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();
 
-        if test_file.is_empty() {
-            // If we copied over an empty output file, we remove it
-            println!("removing {}", &relative_test_file_path.display());
-            fs::remove_file(test_file_path).expect("Could not remove test file");
+        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 {
-    let profile = format!("{}", env::var("PROFILE").unwrap_or("debug".to_string()));
+    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);