From 9439e02fb2332d6ffed6a61f95923204faa82655 Mon Sep 17 00:00:00 2001
From: Trevor Gross <tmgross@umich.edu>
Date: Mon, 3 Jul 2023 13:40:14 -0400
Subject: [PATCH] Unite bless environment variables under `RUSTC_BLESS`

Currently, Clippy, Miri, Rustfmt, and rustc all use an environment variable to
indicate that output should be blessed, but they use different variable names.
In order to improve consistency, this patch applies the following changes:

- Emit `RUSTC_BLESS` within `prepare_cargo_test` so it is always
  available
- Change usage of `MIRI_BLESS` in the Miri subtree to use `RUSTC_BLESS`
- Change usage of `BLESS` in the Clippy subtree to `RUSTC_BLESS`
- Change usage of `BLESS` in the Rustfmt subtree to `RUSTC_BLESS`
- Adjust the blessable test in `rustc_errors` to use this same
  convention
- Update documentation where applicable

Any tools that uses `RUSTC_BLESS` should check that it is set to any value
other than `"0"`.
---
 .../rustc_errors/src/markdown/tests/term.rs   |  2 +-
 src/bootstrap/test.rs                         | 25 ++++++-------------
 src/tools/clippy/tests/compile-test.rs        |  4 ++-
 src/tools/miri/README.md                      |  2 +-
 src/tools/miri/miri                           |  2 +-
 src/tools/miri/test-cargo-miri/run-test.py    |  5 ++--
 src/tools/miri/tests/compiletest.rs           |  5 ++--
 src/tools/rustfmt/src/test/mod.rs             |  8 +++---
 8 files changed, 22 insertions(+), 31 deletions(-)

diff --git a/compiler/rustc_errors/src/markdown/tests/term.rs b/compiler/rustc_errors/src/markdown/tests/term.rs
index 3b31c6d6295..6f68fb25a58 100644
--- a/compiler/rustc_errors/src/markdown/tests/term.rs
+++ b/compiler/rustc_errors/src/markdown/tests/term.rs
@@ -63,7 +63,7 @@ fn test_wrapping_write() {
 #[test]
 fn test_output() {
     // Capture `--bless` when run via ./x
-    let bless = std::env::var("RUSTC_BLESS").unwrap_or_default() == "1";
+    let bless = std::env::var_os("RUSTC_BLESS").is_some_and(|v| v != "0");
     let ast = MdStream::parse_str(INPUT);
     let bufwtr = BufferWriter::stderr(ColorChoice::Always);
     let mut buffer = bufwtr.buffer();
diff --git a/src/bootstrap/test.rs b/src/bootstrap/test.rs
index 1fe92098fd6..820fe5e5294 100644
--- a/src/bootstrap/test.rs
+++ b/src/bootstrap/test.rs
@@ -430,10 +430,6 @@ impl Step for Rustfmt {
             &[],
         );
 
-        if builder.config.cmd.bless() {
-            cargo.env("BLESS", "1");
-        }
-
         let dir = testdir(builder, compiler.host);
         t!(fs::create_dir_all(&dir));
         cargo.env("RUSTFMT_TEST_DIR", dir);
@@ -633,10 +629,6 @@ impl Step for Miri {
         cargo.env("MIRI_SYSROOT", &miri_sysroot);
         cargo.env("MIRI_HOST_SYSROOT", sysroot);
         cargo.env("MIRI", &miri);
-        // propagate --bless
-        if builder.config.cmd.bless() {
-            cargo.env("MIRI_BLESS", "Gesundheit");
-        }
 
         // Set the target.
         cargo.env("MIRI_TEST_TARGET", target.rustc_target_arg());
@@ -654,8 +646,8 @@ impl Step for Miri {
             cargo.env("MIRIFLAGS", "-O -Zmir-opt-level=4 -Cdebug-assertions=yes");
             // Optimizations can change backtraces
             cargo.env("MIRI_SKIP_UI_CHECKS", "1");
-            // `MIRI_SKIP_UI_CHECKS` and `MIRI_BLESS` are incompatible
-            cargo.env_remove("MIRI_BLESS");
+            // `MIRI_SKIP_UI_CHECKS` and `RUSTC_BLESS` are incompatible
+            cargo.env_remove("RUSTC_BLESS");
             // Optimizations can change error locations and remove UB so don't run `fail` tests.
             cargo.args(&["tests/pass", "tests/panic"]);
 
@@ -799,11 +791,6 @@ impl Step for Clippy {
         cargo.add_rustc_lib_path(builder, compiler);
         let mut cargo = prepare_cargo_test(cargo, &[], &[], "clippy", compiler, host, builder);
 
-        // propagate --bless
-        if builder.config.cmd.bless() {
-            cargo.env("BLESS", "Gesundheit");
-        }
-
         let _guard = builder.msg_sysroot_tool(Kind::Test, compiler.stage, "clippy", host, host);
 
         #[allow(deprecated)] // Clippy reports errors if it blessed the outputs
@@ -2245,9 +2232,11 @@ fn prepare_cargo_test(
 ) -> Command {
     let mut cargo = cargo.into();
 
-    // If bless is passed, give downstream crates a way to use it
-    if builder.config.cmd.bless() {
-        cargo.env("RUSTC_BLESS", "1");
+    // Propegate `--bless` if it has not already been set/unset
+    // Any tools that want to use this should bless if `RUSTC_BLESS` is set to
+    // anything other than `0`.
+    if builder.config.cmd.bless() && !cargo.get_envs().any(|v| v.0 == "RUSTC_BLESS") {
+        cargo.env("RUSTC_BLESS", "Gesundheit");
     }
 
     // Pass in some standard flags then iterate over the graph we've discovered
diff --git a/src/tools/clippy/tests/compile-test.rs b/src/tools/clippy/tests/compile-test.rs
index d70c4ea34cb..f714b296233 100644
--- a/src/tools/clippy/tests/compile-test.rs
+++ b/src/tools/clippy/tests/compile-test.rs
@@ -115,7 +115,9 @@ fn base_config(test_dir: &str) -> compiletest::Config {
         mode: TestMode::Yolo,
         stderr_filters: vec![],
         stdout_filters: vec![],
-        output_conflict_handling: if var_os("BLESS").is_some() || env::args().any(|arg| arg == "--bless") {
+        // FIXME(tgross35): deduplicate bless env once clippy can update
+        output_conflict_handling: if var_os("RUSTC_BLESS").is_some_and(|v| v != "0")
+        || env::args().any(|arg| arg == "--bless") {
             compiletest::OutputConflictHandling::Bless
         } else {
             compiletest::OutputConflictHandling::Error("cargo test -- -- --bless".into())
diff --git a/src/tools/miri/README.md b/src/tools/miri/README.md
index eaf58340d7b..89d4b29ebb8 100644
--- a/src/tools/miri/README.md
+++ b/src/tools/miri/README.md
@@ -482,7 +482,7 @@ Moreover, Miri recognizes some environment variables:
   purpose.
 * `MIRI_NO_STD` (recognized by `cargo miri` and the test suite) makes sure that the target's
   sysroot is built without libstd. This allows testing and running no_std programs.
-* `MIRI_BLESS` (recognized by the test suite and `cargo-miri-test/run-test.py`): overwrite all
+* `RUSTC_BLESS` (recognized by the test suite and `cargo-miri-test/run-test.py`): overwrite all
   `stderr` and `stdout` files instead of checking whether the output matches.
 * `MIRI_SKIP_UI_CHECKS` (recognized by the test suite): don't check whether the
   `stderr` or `stdout` files match the actual output.
diff --git a/src/tools/miri/miri b/src/tools/miri/miri
index 1bc4e254ad4..bccf6d835ff 100755
--- a/src/tools/miri/miri
+++ b/src/tools/miri/miri
@@ -303,7 +303,7 @@ test|bless)
     $CARGO build $CARGO_EXTRA_FLAGS --manifest-path "$MIRIDIR"/Cargo.toml
     find_sysroot
     if [ "$COMMAND" = "bless" ]; then
-        export MIRI_BLESS="Gesundheit"
+        export RUSTC_BLESS="Gesundheit"
     fi
     # Then test, and let caller control flags.
     # Only in root project as `cargo-miri` has no tests.
diff --git a/src/tools/miri/test-cargo-miri/run-test.py b/src/tools/miri/test-cargo-miri/run-test.py
index f022c51e59f..ca2f69fc8cf 100755
--- a/src/tools/miri/test-cargo-miri/run-test.py
+++ b/src/tools/miri/test-cargo-miri/run-test.py
@@ -8,8 +8,8 @@ and the working directory to contain the cargo-miri-test project.
 import difflib
 import os
 import re
-import sys
 import subprocess
+import sys
 
 CGREEN  = '\33[32m'
 CBOLD   = '\33[1m'
@@ -37,7 +37,8 @@ def normalize_stderr(str):
     return str
 
 def check_output(actual, path, name):
-    if 'MIRI_BLESS' in os.environ:
+    if os.environ.get("RUSTC_BLESS", "0") != "0":
+        # Write the output only if bless is set
         open(path, mode='w').write(actual)
         return True
     expected = open(path).read()
diff --git a/src/tools/miri/tests/compiletest.rs b/src/tools/miri/tests/compiletest.rs
index 59143550253..c26954da272 100644
--- a/src/tools/miri/tests/compiletest.rs
+++ b/src/tools/miri/tests/compiletest.rs
@@ -72,13 +72,14 @@ fn test_config(target: &str, path: &str, mode: Mode, with_dependencies: bool) ->
         program.args.push(flag);
     }
 
+    let bless = env::var_os("RUSTC_BLESS").is_some_and(|v| v !="0");
     let skip_ui_checks = env::var_os("MIRI_SKIP_UI_CHECKS").is_some();
 
-    let output_conflict_handling = match (env::var_os("MIRI_BLESS").is_some(), skip_ui_checks) {
+    let output_conflict_handling = match (bless, skip_ui_checks) {
         (false, false) => OutputConflictHandling::Error("./miri bless".into()),
         (true, false) => OutputConflictHandling::Bless,
         (false, true) => OutputConflictHandling::Ignore,
-        (true, true) => panic!("cannot use MIRI_BLESS and MIRI_SKIP_UI_CHECKS at the same time"),
+        (true, true) => panic!("cannot use RUSTC_BLESS and MIRI_SKIP_UI_CHECKS at the same time"),
     };
 
     let mut config = Config {
diff --git a/src/tools/rustfmt/src/test/mod.rs b/src/tools/rustfmt/src/test/mod.rs
index 364aa225f68..37854ead28b 100644
--- a/src/tools/rustfmt/src/test/mod.rs
+++ b/src/tools/rustfmt/src/test/mod.rs
@@ -838,11 +838,9 @@ fn handle_result(
 
         // Ignore LF and CRLF difference for Windows.
         if !string_eq_ignore_newline_repr(&fmt_text, &text) {
-            if let Some(bless) = std::env::var_os("BLESS") {
-                if bless != "0" {
-                    std::fs::write(file_name, fmt_text).unwrap();
-                    continue;
-                }
+            if std::env::var_os("RUSTC_BLESS").is_some_and(|v| v != "0") {
+                std::fs::write(file_name, fmt_text).unwrap();
+                continue;
             }
             let diff = make_diff(&text, &fmt_text, DIFF_CONTEXT_SIZE);
             assert!(