Auto merge of #124753 - GuillaumeGomez:migrate-rustdoc-determinism, r=jieyouxu

Migrate `run-make/rustdoc-error-lines` to new `rmake.rs`

Part of https://github.com/rust-lang/rust/issues/121876.

There was a weird naming inconsistency with `input`/`output`. A few tests write `.arg("-o").arg(path)` and the `output` method was actually the command output. So instead, I renamed the original `output` into `command_output` so that I could create the `output` method with the expected effect (and updated the tests to use it too).

EDIT: The first two commits come from https://github.com/rust-lang/rust/pull/124711. Some weird things happened recently pparently. ^^'

r? `@jieyouxu`
diff --git a/src/tools/run-make-support/src/cc.rs b/src/tools/run-make-support/src/cc.rs
index 4082639..a67f5c8 100644
--- a/src/tools/run-make-support/src/cc.rs
+++ b/src/tools/run-make-support/src/cc.rs
@@ -73,7 +73,7 @@
     }
 
     /// Get the [`Output`][::std::process::Output] of the finished process.
-    pub fn output(&mut self) -> ::std::process::Output {
+    pub fn command_output(&mut self) -> ::std::process::Output {
         self.cmd.output().expect("failed to get output of finished process")
     }
 }
diff --git a/src/tools/run-make-support/src/clang.rs b/src/tools/run-make-support/src/clang.rs
index c30ba29..6ccce67 100644
--- a/src/tools/run-make-support/src/clang.rs
+++ b/src/tools/run-make-support/src/clang.rs
@@ -72,7 +72,7 @@
     }
 
     /// Get the [`Output`][::std::process::Output] of the finished process.
-    pub fn output(&mut self) -> ::std::process::Output {
+    pub fn command_output(&mut self) -> ::std::process::Output {
         self.cmd.output().expect("failed to get output of finished process")
     }
 }
diff --git a/src/tools/run-make-support/src/lib.rs b/src/tools/run-make-support/src/lib.rs
index d040b05..9888ca2 100644
--- a/src/tools/run-make-support/src/lib.rs
+++ b/src/tools/run-make-support/src/lib.rs
@@ -164,7 +164,7 @@
 ///
 /// impl CommandWrapper {
 ///     /// Get the [`Output`][::std::process::Output] of the finished process.
-///     pub fn output(&mut self) -> Output { /* ... */ } // <- required `output()` method
+///     pub fn command_output(&mut self) -> Output { /* ... */ } // <- required `command_output()` method
 /// }
 ///
 /// crate::impl_common_helpers!(CommandWrapper);
@@ -242,7 +242,7 @@
                 let caller_location = ::std::panic::Location::caller();
                 let caller_line_number = caller_location.line();
 
-                let output = self.output();
+                let output = self.command_output();
                 if !output.status.success() {
                     handle_failed_output(&self.cmd, output, caller_line_number);
                 }
@@ -255,7 +255,7 @@
                 let caller_location = ::std::panic::Location::caller();
                 let caller_line_number = caller_location.line();
 
-                let output = self.output();
+                let output = self.command_output();
                 if output.status.success() {
                     handle_failed_output(&self.cmd, output, caller_line_number);
                 }
diff --git a/src/tools/run-make-support/src/llvm_readobj.rs b/src/tools/run-make-support/src/llvm_readobj.rs
index 4e1f2b0..f114aac 100644
--- a/src/tools/run-make-support/src/llvm_readobj.rs
+++ b/src/tools/run-make-support/src/llvm_readobj.rs
@@ -44,7 +44,7 @@
 
     /// Get the [`Output`][::std::process::Output] of the finished process.
     #[track_caller]
-    pub fn output(&mut self) -> ::std::process::Output {
+    pub fn command_output(&mut self) -> ::std::process::Output {
         self.cmd.output().expect("failed to get output of finished process")
     }
 }
diff --git a/src/tools/run-make-support/src/rustc.rs b/src/tools/run-make-support/src/rustc.rs
index 3753952..de773d6 100644
--- a/src/tools/run-make-support/src/rustc.rs
+++ b/src/tools/run-make-support/src/rustc.rs
@@ -91,6 +91,13 @@
         self
     }
 
+    /// Specify path to the output file.
+    pub fn output<P: AsRef<Path>>(&mut self, path: P) -> &mut Self {
+        self.cmd.arg("-o");
+        self.cmd.arg(path.as_ref());
+        self
+    }
+
     /// This flag defers LTO optimizations to the linker.
     pub fn linker_plugin_lto(&mut self, option: &str) -> &mut Self {
         self.cmd.arg(format!("-Clinker-plugin-lto={option}"));
@@ -171,7 +178,7 @@
 
     /// Get the [`Output`][::std::process::Output] of the finished process.
     #[track_caller]
-    pub fn output(&mut self) -> ::std::process::Output {
+    pub fn command_output(&mut self) -> ::std::process::Output {
         // let's make sure we piped all the input and outputs
         self.cmd.stdin(Stdio::piped());
         self.cmd.stdout(Stdio::piped());
@@ -196,7 +203,7 @@
         let caller_location = std::panic::Location::caller();
         let caller_line_number = caller_location.line();
 
-        let output = self.output();
+        let output = self.command_output();
         if output.status.code().unwrap() != code {
             handle_failed_output(&self.cmd, output, caller_line_number);
         }
diff --git a/src/tools/run-make-support/src/rustdoc.rs b/src/tools/run-make-support/src/rustdoc.rs
index c2c4f2e..aa3c7dc 100644
--- a/src/tools/run-make-support/src/rustdoc.rs
+++ b/src/tools/run-make-support/src/rustdoc.rs
@@ -51,6 +51,13 @@
         self
     }
 
+    /// Specify path to the output folder.
+    pub fn output<P: AsRef<Path>>(&mut self, path: P) -> &mut Self {
+        self.cmd.arg("-o");
+        self.cmd.arg(path.as_ref());
+        self
+    }
+
     /// Specify output directory.
     pub fn out_dir<P: AsRef<Path>>(&mut self, path: P) -> &mut Self {
         self.cmd.arg("--out-dir").arg(path.as_ref());
@@ -73,7 +80,7 @@
 
     /// Get the [`Output`][::std::process::Output] of the finished process.
     #[track_caller]
-    pub fn output(&mut self) -> ::std::process::Output {
+    pub fn command_output(&mut self) -> ::std::process::Output {
         // let's make sure we piped all the input and outputs
         self.cmd.stdin(Stdio::piped());
         self.cmd.stdout(Stdio::piped());
@@ -93,12 +100,19 @@
         }
     }
 
+    /// Specify the edition year.
+    pub fn edition(&mut self, edition: &str) -> &mut Self {
+        self.cmd.arg("--edition");
+        self.cmd.arg(edition);
+        self
+    }
+
     #[track_caller]
     pub fn run_fail_assert_exit_code(&mut self, code: i32) -> Output {
         let caller_location = std::panic::Location::caller();
         let caller_line_number = caller_location.line();
 
-        let output = self.output();
+        let output = self.command_output();
         if output.status.code().unwrap() != code {
             handle_failed_output(&self.cmd, output, caller_line_number);
         }
diff --git a/src/tools/tidy/src/allowed_run_make_makefiles.txt b/src/tools/tidy/src/allowed_run_make_makefiles.txt
index d4d6c14..f4ae7b0 100644
--- a/src/tools/tidy/src/allowed_run_make_makefiles.txt
+++ b/src/tools/tidy/src/allowed_run_make_makefiles.txt
@@ -44,7 +44,6 @@
 run-make/dep-info-doesnt-run-much/Makefile
 run-make/dep-info-spaces/Makefile
 run-make/dep-info/Makefile
-run-make/doctests-runtool/Makefile
 run-make/dump-ice-to-disk/Makefile
 run-make/dump-mono-stats/Makefile
 run-make/duplicate-output-flavors/Makefile
@@ -245,7 +244,6 @@
 run-make/rlib-format-packed-bundled-libs/Makefile
 run-make/rmeta-preferred/Makefile
 run-make/rustc-macro-dep-files/Makefile
-run-make/rustdoc-error-lines/Makefile
 run-make/rustdoc-io-error/Makefile
 run-make/rustdoc-map-file/Makefile
 run-make/rustdoc-output-path/Makefile
diff --git a/tests/run-make/CURRENT_RUSTC_VERSION/rmake.rs b/tests/run-make/CURRENT_RUSTC_VERSION/rmake.rs
index 4b7ce4e..1bdb634 100644
--- a/tests/run-make/CURRENT_RUSTC_VERSION/rmake.rs
+++ b/tests/run-make/CURRENT_RUSTC_VERSION/rmake.rs
@@ -13,7 +13,8 @@
     let mut stable_path = PathBuf::from(env!("TMPDIR"));
     stable_path.push("libstable.rmeta");
 
-    let output = rustc().input("main.rs").emit("metadata").extern_("stable", &stable_path).output();
+    let output =
+        rustc().input("main.rs").emit("metadata").extern_("stable", &stable_path).command_output();
 
     let stderr = String::from_utf8_lossy(&output.stderr);
     let version = include_str!(concat!(env!("S"), "/src/version"));
diff --git a/tests/run-make/doctests-runtool/Makefile b/tests/run-make/doctests-runtool/Makefile
deleted file mode 100644
index 7d5df1e..0000000
--- a/tests/run-make/doctests-runtool/Makefile
+++ /dev/null
@@ -1,20 +0,0 @@
-# ignore-cross-compile
-include ../tools.mk
-
-# Tests behavior of rustdoc --runtool
-
-MY_SRC_DIR := ${CURDIR}
-
-all: with_test_run_directory
-
-# Behavior with --runtool with relative paths and --test-run-directory.
-with_test_run_directory:
-	mkdir -p $(TMPDIR)/rundir
-	mkdir -p $(TMPDIR)/runtool
-	$(RUSTC) --crate-type rlib t.rs
-	$(RUSTC) runtool.rs -o $(TMPDIR)/runtool/runtool
-	( cd $(TMPDIR); \
-		$(RUSTDOC) -Zunstable-options --test --test-run-directory rundir \
-			--runtool runtool/runtool --extern t=libt.rlib $(MY_SRC_DIR)/t.rs \
-	)
-	rm -rf $(TMPDIR)/rundir $(TMPDIR)/runtool
diff --git a/tests/run-make/doctests-runtool/rmake.rs b/tests/run-make/doctests-runtool/rmake.rs
new file mode 100644
index 0000000..6f89bf2
--- /dev/null
+++ b/tests/run-make/doctests-runtool/rmake.rs
@@ -0,0 +1,39 @@
+// Tests behavior of rustdoc `--runtool`.
+
+use run_make_support::{rustc, rustdoc, tmp_dir};
+use std::env::current_dir;
+use std::fs::{create_dir, remove_dir_all};
+use std::path::PathBuf;
+
+fn mkdir(name: &str) -> PathBuf {
+    let dir = tmp_dir().join(name);
+    create_dir(&dir).expect("failed to create doctests folder");
+    dir
+}
+
+// Behavior with --runtool with relative paths and --test-run-directory.
+fn main() {
+    let run_dir_name = "rundir";
+    let run_dir = mkdir(run_dir_name);
+    let run_tool = mkdir("runtool");
+    let run_tool_binary = run_tool.join("runtool");
+
+    rustc().input("t.rs").crate_type("rlib").run();
+    rustc().input("runtool.rs").output(&run_tool_binary).run();
+
+    rustdoc()
+        .input(current_dir().unwrap().join("t.rs"))
+        .arg("-Zunstable-options")
+        .arg("--test")
+        .arg("--test-run-directory")
+        .arg(run_dir_name)
+        .arg("--runtool")
+        .arg(&run_tool_binary)
+        .arg("--extern")
+        .arg("t=libt.rlib")
+        .current_dir(tmp_dir())
+        .run();
+
+    remove_dir_all(run_dir);
+    remove_dir_all(run_tool);
+}
diff --git a/tests/run-make/exit-code/rmake.rs b/tests/run-make/exit-code/rmake.rs
index b114315..76d7777 100644
--- a/tests/run-make/exit-code/rmake.rs
+++ b/tests/run-make/exit-code/rmake.rs
@@ -15,7 +15,7 @@
         .arg("compile-error.rs")
         .run_fail_assert_exit_code(101);
 
-    rustdoc().arg("success.rs").arg("-o").arg(tmp_dir().join("exit-code")).run();
+    rustdoc().arg("success.rs").output(tmp_dir().join("exit-code")).run();
 
     rustdoc().arg("--invalid-arg-foo").run_fail_assert_exit_code(1);
 
diff --git a/tests/run-make/repr128-dwarf/rmake.rs b/tests/run-make/repr128-dwarf/rmake.rs
index d734b2a..fd5dd81 100644
--- a/tests/run-make/repr128-dwarf/rmake.rs
+++ b/tests/run-make/repr128-dwarf/rmake.rs
@@ -10,7 +10,7 @@
 
 fn main() {
     let output = tmp_dir().join("repr128");
-    rustc().input("main.rs").arg("-o").arg(&output).arg("-Cdebuginfo=2").run();
+    rustc().input("main.rs").output(&output).arg("-Cdebuginfo=2").run();
     // Mach-O uses packed debug info
     let dsym_location = output
         .with_extension("dSYM")
diff --git a/tests/run-make/rustdoc-determinism/rmake.rs b/tests/run-make/rustdoc-determinism/rmake.rs
index 38ae751..09097d4 100644
--- a/tests/run-make/rustdoc-determinism/rmake.rs
+++ b/tests/run-make/rustdoc-determinism/rmake.rs
@@ -1,18 +1,19 @@
-use run_make_support::{diff, rustc, rustdoc, tmp_dir};
+// Assert that the search index is generated deterministically, regardless of the
+// order that crates are documented in.
 
-/// Assert that the search index is generated deterministically, regardless of the
-/// order that crates are documented in.
+use run_make_support::{diff, rustdoc, tmp_dir};
+
 fn main() {
-    let dir_first = tmp_dir().join("first");
-    rustdoc().out_dir(&dir_first).input("foo.rs").run();
-    rustdoc().out_dir(&dir_first).input("bar.rs").run();
+    let foo_first = tmp_dir().join("foo_first");
+    rustdoc().input("foo.rs").output(&foo_first).run();
+    rustdoc().input("bar.rs").output(&foo_first).run();
 
-    let dir_second = tmp_dir().join("second");
-    rustdoc().out_dir(&dir_second).input("bar.rs").run();
-    rustdoc().out_dir(&dir_second).input("foo.rs").run();
+    let bar_first = tmp_dir().join("bar_first");
+    rustdoc().input("bar.rs").output(&bar_first).run();
+    rustdoc().input("foo.rs").output(&bar_first).run();
 
     diff()
-        .expected_file(dir_first.join("search-index.js"))
-        .actual_file(dir_second.join("search-index.js"))
+        .expected_file(foo_first.join("search-index.js"))
+        .actual_file(bar_first.join("search-index.js"))
         .run();
 }
diff --git a/tests/run-make/rustdoc-error-lines/Makefile b/tests/run-make/rustdoc-error-lines/Makefile
deleted file mode 100644
index 2dc30f5..0000000
--- a/tests/run-make/rustdoc-error-lines/Makefile
+++ /dev/null
@@ -1,13 +0,0 @@
-include ../tools.mk
-
-# Test that hir-tree output doesn't crash and includes
-# the string constant we would expect to see.
-
-all:
-	$(RUSTDOC) --test input.rs > $(TMPDIR)/output || true
-	$(CGREP) 'input.rs - foo (line 5)' < $(TMPDIR)/output
-	$(CGREP) 'input.rs:7:15' < $(TMPDIR)/output
-	$(CGREP) 'input.rs - bar (line 15)' < $(TMPDIR)/output
-	$(CGREP) 'input.rs:17:15' < $(TMPDIR)/output
-	$(CGREP) 'input.rs - bar (line 24)' < $(TMPDIR)/output
-	$(CGREP) 'input.rs:26:15' < $(TMPDIR)/output
diff --git a/tests/run-make/rustdoc-error-lines/rmake.rs b/tests/run-make/rustdoc-error-lines/rmake.rs
new file mode 100644
index 0000000..31536c7
--- /dev/null
+++ b/tests/run-make/rustdoc-error-lines/rmake.rs
@@ -0,0 +1,22 @@
+// Assert that the search index is generated deterministically, regardless of the
+// order that crates are documented in.
+
+use run_make_support::rustdoc;
+
+fn main() {
+    let output =
+        String::from_utf8(rustdoc().input("input.rs").arg("--test").command_output().stdout)
+            .unwrap();
+
+    let should_contain = &[
+        "input.rs - foo (line 5)",
+        "input.rs:7:15",
+        "input.rs - bar (line 15)",
+        "input.rs:17:15",
+        "input.rs - bar (line 24)",
+        "input.rs:26:15",
+    ];
+    for text in should_contain {
+        assert!(output.contains(text), "output doesn't contains {:?}", text);
+    }
+}