Skip to content

Commit 42f70c2

Browse files
authored
Rollup merge of #129040 - Zalathar:bless-rmake, r=jieyouxu
Fix blessing of rmake tests Fixes #129038. When running in `--bless` mode, we now set the value of `RUSTC_BLESS_TEST` to the current test's source directory. This allows the diff helper in `run_make_support` to find the original snapshot file in the source directory and bless that, instead of unhelpfully blessing the temporary copy in `build`. r? `@jieyouxu`
2 parents 41aa963 + cc58cf6 commit 42f70c2

File tree

2 files changed

+31
-24
lines changed

2 files changed

+31
-24
lines changed

src/tools/compiletest/src/runtest.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -3735,15 +3735,14 @@ impl<'test> TestCx<'test> {
37353735
}
37363736

37373737
if self.config.bless {
3738-
cmd.env("RUSTC_BLESS_TEST", "--bless");
3739-
// Assume this option is active if the environment variable is "defined", with _any_ value.
3740-
// As an example, a `Makefile` can use this option by:
3738+
// If we're running in `--bless` mode, set an environment variable to tell
3739+
// `run_make_support` to bless snapshot files instead of checking them.
37413740
//
3742-
// ifdef RUSTC_BLESS_TEST
3743-
// cp "$(TMPDIR)"/actual_something.ext expected_something.ext
3744-
// else
3745-
// $(DIFF) expected_something.ext "$(TMPDIR)"/actual_something.ext
3746-
// endif
3741+
// The value is this test's source directory, because the support code
3742+
// will need that path in order to bless the _original_ snapshot files,
3743+
// not the copies in `rmake_out`.
3744+
// (See <https://github.com/rust-lang/rust/issues/129038>.)
3745+
cmd.env("RUSTC_BLESS_TEST", &self.testpaths.file);
37473746
}
37483747

37493748
if self.config.target.contains("msvc") && !self.config.cc.is_empty() {

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

+24-16
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,8 @@ impl Diff {
112112
let (expected_name, actual_name, output, actual) = self.run_common();
113113

114114
if !output.is_empty() {
115-
// If we can bless (meaning we have a file to write into and the `RUSTC_BLESS_TEST`
116-
// environment variable set), then we write into the file and return.
117-
if let Some(ref expected_file) = self.expected_file {
118-
if std::env::var("RUSTC_BLESS_TEST").is_ok() {
119-
println!("Blessing `{}`", expected_file.display());
120-
fs::write(expected_file, actual);
121-
return;
122-
}
115+
if self.maybe_bless_expected_file(&actual) {
116+
return;
123117
}
124118
panic!(
125119
"test failed: `{}` is different from `{}`\n\n{}",
@@ -134,19 +128,33 @@ impl Diff {
134128
let (expected_name, actual_name, output, actual) = self.run_common();
135129

136130
if output.is_empty() {
137-
// If we can bless (meaning we have a file to write into and the `RUSTC_BLESS_TEST`
138-
// environment variable set), then we write into the file and return.
139-
if let Some(ref expected_file) = self.expected_file {
140-
if std::env::var("RUSTC_BLESS_TEST").is_ok() {
141-
println!("Blessing `{}`", expected_file.display());
142-
fs::write(expected_file, actual);
143-
return;
144-
}
131+
if self.maybe_bless_expected_file(&actual) {
132+
return;
145133
}
146134
panic!(
147135
"test failed: `{}` is not different from `{}`\n\n{}",
148136
expected_name, actual_name, output
149137
)
150138
}
151139
}
140+
141+
/// If we have an expected file to write into, and `RUSTC_BLESS_TEST` is
142+
/// set, then write the actual output into the file and return `true`.
143+
///
144+
/// We assume that `RUSTC_BLESS_TEST` contains the path to the original test's
145+
/// source directory. That lets us bless the original snapshot file in the
146+
/// source tree, not the copy in `rmake_out` that we would normally use.
147+
fn maybe_bless_expected_file(&self, actual: &str) -> bool {
148+
let Some(ref expected_file) = self.expected_file else {
149+
return false;
150+
};
151+
let Ok(bless_dir) = std::env::var("RUSTC_BLESS_TEST") else {
152+
return false;
153+
};
154+
155+
let bless_file = Path::new(&bless_dir).join(expected_file);
156+
println!("Blessing `{}`", bless_file.display());
157+
fs::write(bless_file, actual);
158+
true
159+
}
152160
}

0 commit comments

Comments
 (0)