about summary refs log tree commit diff
path: root/src/tools
diff options
context:
space:
mode:
authorZalathar <Zalathar@users.noreply.github.com>2025-08-05 22:30:43 +1000
committerZalathar <Zalathar@users.noreply.github.com>2025-08-06 11:17:56 +1000
commitf96fbb214b64a6d373b239deeb8a65b8059ffda7 (patch)
tree53a0feb606f95ca33a929b7e85ab4e77eb876f1f /src/tools
parent0f353363965ebf05e0757f7679c800b39c51a07e (diff)
downloadrust-f96fbb214b64a6d373b239deeb8a65b8059ffda7.tar.gz
rust-f96fbb214b64a6d373b239deeb8a65b8059ffda7.zip
run-make: Allow blessing snapshot files that don't exist yet
This makes it possible to bless the snapshot files used by `diff()` in
newly-created run-make tests, without having to create the files manually
beforehand.
Diffstat (limited to 'src/tools')
-rw-r--r--src/tools/run-make-support/src/diff/mod.rs11
1 files changed, 10 insertions, 1 deletions
diff --git a/src/tools/run-make-support/src/diff/mod.rs b/src/tools/run-make-support/src/diff/mod.rs
index ee48e373366..739eff72f05 100644
--- a/src/tools/run-make-support/src/diff/mod.rs
+++ b/src/tools/run-make-support/src/diff/mod.rs
@@ -23,6 +23,7 @@ pub struct Diff {
     actual: Option<String>,
     actual_name: Option<String>,
     normalizers: Vec<(String, String)>,
+    bless_dir: Option<String>,
     drop_bomb: DropBomb,
 }
 
@@ -37,6 +38,7 @@ impl Diff {
             actual: None,
             actual_name: None,
             normalizers: Vec::new(),
+            bless_dir: std::env::var("RUSTC_BLESS_TEST").ok(),
             drop_bomb: DropBomb::arm("diff"),
         }
     }
@@ -44,6 +46,13 @@ impl Diff {
     /// Specify the expected output for the diff from a file.
     pub fn expected_file<P: AsRef<Path>>(&mut self, path: P) -> &mut Self {
         let path = path.as_ref();
+        // In `--bless` mode, create the snapshot file if it doesn't already exist.
+        // The empty file will be overwritten with the actual text.
+        if self.bless_dir.is_some()
+            && let Ok(false) = std::fs::exists(path)
+        {
+            fs::write(path, "");
+        }
         let content = fs::read_to_string(path);
         let name = path.to_string_lossy().to_string();
 
@@ -148,7 +157,7 @@ impl Diff {
         let Some(ref expected_file) = self.expected_file else {
             return false;
         };
-        let Ok(bless_dir) = std::env::var("RUSTC_BLESS_TEST") else {
+        let Some(ref bless_dir) = self.bless_dir else {
             return false;
         };