Skip to content

Commit 53688ef

Browse files
committed
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.
1 parent 0f35336 commit 53688ef

File tree

1 file changed

+8
-1
lines changed
  • src/tools/run-make-support/src/diff

1 file changed

+8
-1
lines changed

src/tools/run-make-support/src/diff/mod.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub struct Diff {
2323
actual: Option<String>,
2424
actual_name: Option<String>,
2525
normalizers: Vec<(String, String)>,
26+
bless_dir: Option<String>,
2627
drop_bomb: DropBomb,
2728
}
2829

@@ -37,13 +38,19 @@ impl Diff {
3738
actual: None,
3839
actual_name: None,
3940
normalizers: Vec::new(),
41+
bless_dir: std::env::var("RUSTC_BLESS_TEST").ok(),
4042
drop_bomb: DropBomb::arm("diff"),
4143
}
4244
}
4345

4446
/// Specify the expected output for the diff from a file.
4547
pub fn expected_file<P: AsRef<Path>>(&mut self, path: P) -> &mut Self {
4648
let path = path.as_ref();
49+
// In `--bless` mode, create the snapshot file if it doesn't already exist.
50+
// The empty file will be overwritten with the actual text.
51+
if self.bless_dir.is_some() && let Ok(false) = std::fs::exists(path) {
52+
fs::write(path, "");
53+
}
4754
let content = fs::read_to_string(path);
4855
let name = path.to_string_lossy().to_string();
4956

@@ -148,7 +155,7 @@ impl Diff {
148155
let Some(ref expected_file) = self.expected_file else {
149156
return false;
150157
};
151-
let Ok(bless_dir) = std::env::var("RUSTC_BLESS_TEST") else {
158+
let Some(ref bless_dir) = self.bless_dir else {
152159
return false;
153160
};
154161

0 commit comments

Comments
 (0)