From 553204d26d800084431f328612da01c3d15ea01f Mon Sep 17 00:00:00 2001 From: Oneirical Date: Fri, 24 May 2024 10:02:53 -0400 Subject: [PATCH 1/3] Rewrite `link-arg` in rmake.rs --- .../tidy/src/allowed_run_make_makefiles.txt | 1 - tests/run-make/link-arg/Makefile | 5 ---- tests/run-make/link-arg/rmake.rs | 24 +++++++++++++++++++ 3 files changed, 24 insertions(+), 6 deletions(-) delete mode 100644 tests/run-make/link-arg/Makefile create mode 100644 tests/run-make/link-arg/rmake.rs diff --git a/src/tools/tidy/src/allowed_run_make_makefiles.txt b/src/tools/tidy/src/allowed_run_make_makefiles.txt index fdd0be79a4d..4ef23a19e79 100644 --- a/src/tools/tidy/src/allowed_run_make_makefiles.txt +++ b/src/tools/tidy/src/allowed_run_make_makefiles.txt @@ -109,7 +109,6 @@ run-make/libtest-json/Makefile run-make/libtest-junit/Makefile run-make/libtest-padding/Makefile run-make/libtest-thread-limit/Makefile -run-make/link-arg/Makefile run-make/link-args-order/Makefile run-make/link-cfg/Makefile run-make/link-dedup/Makefile diff --git a/tests/run-make/link-arg/Makefile b/tests/run-make/link-arg/Makefile deleted file mode 100644 index 103527c3e14..00000000000 --- a/tests/run-make/link-arg/Makefile +++ /dev/null @@ -1,5 +0,0 @@ -include ../tools.mk -RUSTC_FLAGS = -C link-arg="-lfoo" -C link-arg="-lbar" --print link-args - -all: - $(RUSTC) $(RUSTC_FLAGS) empty.rs | $(CGREP) lfoo lbar diff --git a/tests/run-make/link-arg/rmake.rs b/tests/run-make/link-arg/rmake.rs new file mode 100644 index 00000000000..dd496101f6a --- /dev/null +++ b/tests/run-make/link-arg/rmake.rs @@ -0,0 +1,24 @@ +// In 2016, the rustc flag "-C link-arg" was introduced - it can be repeatedly used +// to add single arguments to the linker. This test passes 2 arguments to the linker using it, +// then checks that the compiler's output contains the arguments passed to it. +// This ensures that the compiler successfully parses this flag. +// See https://github.com/rust-lang/rust/pull/36574 + +use run_make_support::rustc; + +fn main() { + let output = String::from_utf8( + rustc() + .input("empty.rs") + .link_arg("-lfoo") + .link_arg("-lbar") + .print("link-args") + .command_output() + .stdout, + ) + .unwrap(); + assert!( + output.contains("lfoo") || output.contains("lbar"), + "The output did not contain the expected \"lfoo\" or \"lbar\" strings." + ); +} From 5f44f9511d95c8efbae0e1c312e130d91acd1134 Mon Sep 17 00:00:00 2001 From: Oneirical Date: Fri, 24 May 2024 11:22:04 -0400 Subject: [PATCH 2/3] rewrite `link-dedup` to rmake --- .../tidy/src/allowed_run_make_makefiles.txt | 1 - tests/run-make/link-dedup/rmake.rs | 32 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 tests/run-make/link-dedup/rmake.rs diff --git a/src/tools/tidy/src/allowed_run_make_makefiles.txt b/src/tools/tidy/src/allowed_run_make_makefiles.txt index 4ef23a19e79..6a1809951fd 100644 --- a/src/tools/tidy/src/allowed_run_make_makefiles.txt +++ b/src/tools/tidy/src/allowed_run_make_makefiles.txt @@ -111,7 +111,6 @@ run-make/libtest-padding/Makefile run-make/libtest-thread-limit/Makefile run-make/link-args-order/Makefile run-make/link-cfg/Makefile -run-make/link-dedup/Makefile run-make/link-framework/Makefile run-make/link-path-order/Makefile run-make/linkage-attr-on-static/Makefile diff --git a/tests/run-make/link-dedup/rmake.rs b/tests/run-make/link-dedup/rmake.rs new file mode 100644 index 00000000000..a0d3ac86542 --- /dev/null +++ b/tests/run-make/link-dedup/rmake.rs @@ -0,0 +1,32 @@ +// When native libraries are passed to the linker, there used to be an annoyance +// where multiple instances of the same library in a row would cause duplication in +// outputs. This has been fixed, and this test checks that it stays fixed. +// With the --cfg flag, -ltestb gets added to the output, breaking up the chain of -ltesta. +// Without the --cfg flag, there should be a single -ltesta, no more, no less. +// See https://github.com/rust-lang/rust/pull/84794 + +//@ ignore-msvc + +fn main() { + rustc().input("depa.rs").run(); + rustc().input("depb.rs").run(); + rustc().input("depc.rs").run(); + let output = + String::from_utf8(rustc().input("empty.rs").cfg("bar").command_output().stderr).unwrap(); + let pos_a1 = + output.find("-ltesta").expect("empty.rs, compiled with --cfg, should contain -ltesta"); + let pos_b = output[pos_a1..] + .find("-ltestb") + .map(|pos| pos + pos_a1) + .expect("empty.rs, compiled with --cfg, should contain -ltestb"); + let _ = output[pos_b..] + .find("-ltesta") + .map(|pos| pos + pos_b) + .expect("empty.rs, compiled with --cfg, should contain a second -ltesta"); + let output = String::from_utf8(rustc().input("empty.rs").command_output().stderr).unwrap(); + assert!(output.contains("-ltesta")); + let output = String::from_utf8(rustc().input("empty.rs").command_output().stderr).unwrap(); + assert!(!output.contains("-ltestb")); + let output = String::from_utf8(rustc().input("empty.rs").command_output().stderr).unwrap(); + assert_eq!(output.matches("-ltesta").count, 1); +} From 6228b3e40e78ca0a29fd4599c9b7a3e340bb1572 Mon Sep 17 00:00:00 2001 From: Oneirical Date: Fri, 24 May 2024 11:50:35 -0400 Subject: [PATCH 3/3] Rewrite and rename `issue-26092` to rmake --- src/tools/run-make-support/src/command.rs | 44 ++++++++++++------- src/tools/run-make-support/src/lib.rs | 21 +++++++++ .../tidy/src/allowed_run_make_makefiles.txt | 1 - tests/run-make/CURRENT_RUSTC_VERSION/rmake.rs | 1 - .../blank.rs | 0 .../clear-error-blank-output/rmake.rs | 13 ++++++ tests/run-make/issue-26092/Makefile | 6 --- tests/run-make/link-arg/rmake.rs | 24 +++++----- tests/run-make/link-dedup/Makefile | 12 ----- tests/run-make/link-dedup/rmake.rs | 28 +++++------- 10 files changed, 83 insertions(+), 67 deletions(-) rename tests/run-make/{issue-26092 => clear-error-blank-output}/blank.rs (100%) create mode 100644 tests/run-make/clear-error-blank-output/rmake.rs delete mode 100644 tests/run-make/issue-26092/Makefile delete mode 100644 tests/run-make/link-dedup/Makefile diff --git a/src/tools/run-make-support/src/command.rs b/src/tools/run-make-support/src/command.rs index dab18dca2ff..f39bcfd60df 100644 --- a/src/tools/run-make-support/src/command.rs +++ b/src/tools/run-make-support/src/command.rs @@ -6,7 +6,7 @@ use std::process::{Command as StdCommand, ExitStatus, Output, Stdio}; use crate::drop_bomb::DropBomb; -use crate::{assert_contains, assert_not_contains, handle_failed_output}; +use crate::{assert_contains, assert_equals, assert_not_contains, handle_failed_output}; /// This is a custom command wrapper that simplifies working with commands and makes it easier to /// ensure that we check the exit status of executed processes. @@ -21,6 +21,7 @@ /// /// [`run`]: Self::run /// [`run_fail`]: Self::run_fail +/// [`run_unchecked`]: Self::run_unchecked #[derive(Debug)] pub struct Command { cmd: StdCommand, @@ -116,6 +117,15 @@ pub fn run_fail(&mut self) -> CompletedProcess { output } + /// Run the command but do not check its exit status. + /// Only use if you explicitly don't care about the exit status. + /// Prefer to use [`Self::run`] and [`Self::run_fail`] + /// whenever possible. + #[track_caller] + pub fn run_unchecked(&mut self) -> CompletedProcess { + self.command_output() + } + #[track_caller] fn command_output(&mut self) -> CompletedProcess { self.drop_bomb.defuse(); @@ -163,41 +173,45 @@ pub fn status(&self) -> ExitStatus { self.output.status } - /// Checks that trimmed `stdout` matches trimmed `content`. + /// Checks that trimmed `stdout` matches trimmed `expected`. #[track_caller] - pub fn assert_stdout_equals>(&self, content: S) -> &Self { - assert_eq!(self.stdout_utf8().trim(), content.as_ref().trim()); + pub fn assert_stdout_equals>(&self, expected: S) -> &Self { + assert_equals(self.stdout_utf8().trim(), expected.as_ref().trim()); self } + /// Checks that `stdout` does not contain `unexpected`. #[track_caller] - pub fn assert_stdout_contains>(self, needle: S) -> Self { - assert_contains(&self.stdout_utf8(), needle.as_ref()); + pub fn assert_stdout_not_contains>(&self, unexpected: S) -> &Self { + assert_not_contains(&self.stdout_utf8(), unexpected.as_ref()); self } + /// Checks that `stdout` contains `expected`. #[track_caller] - pub fn assert_stdout_not_contains>(&self, needle: S) -> &Self { - assert_not_contains(&self.stdout_utf8(), needle.as_ref()); + pub fn assert_stdout_contains>(&self, expected: S) -> &Self { + assert_contains(&self.stdout_utf8(), expected.as_ref()); self } - /// Checks that trimmed `stderr` matches trimmed `content`. + /// Checks that trimmed `stderr` matches trimmed `expected`. #[track_caller] - pub fn assert_stderr_equals>(&self, content: S) -> &Self { - assert_eq!(self.stderr_utf8().trim(), content.as_ref().trim()); + pub fn assert_stderr_equals>(&self, expected: S) -> &Self { + assert_equals(self.stderr_utf8().trim(), expected.as_ref().trim()); self } + /// Checks that `stderr` contains `expected`. #[track_caller] - pub fn assert_stderr_contains>(&self, needle: S) -> &Self { - assert_contains(&self.stderr_utf8(), needle.as_ref()); + pub fn assert_stderr_contains>(&self, expected: S) -> &Self { + assert_contains(&self.stderr_utf8(), expected.as_ref()); self } + /// Checks that `stderr` does not contain `unexpected`. #[track_caller] - pub fn assert_stderr_not_contains>(&self, needle: S) -> &Self { - assert_not_contains(&self.stdout_utf8(), needle.as_ref()); + pub fn assert_stderr_not_contains>(&self, unexpected: S) -> &Self { + assert_not_contains(&self.stdout_utf8(), unexpected.as_ref()); self } diff --git a/src/tools/run-make-support/src/lib.rs b/src/tools/run-make-support/src/lib.rs index ba4524c150c..84b0a4f61ee 100644 --- a/src/tools/run-make-support/src/lib.rs +++ b/src/tools/run-make-support/src/lib.rs @@ -332,6 +332,18 @@ pub fn read_dir(dir: impl AsRef, callback: F) { } } +/// Check that `actual` is equal to `expected`. Panic otherwise. +#[track_caller] +pub fn assert_equals(actual: &str, expected: &str) { + if actual != expected { + eprintln!("=== ACTUAL TEXT ==="); + eprintln!("{}", actual); + eprintln!("=== EXPECTED ==="); + eprintln!("{}", expected); + panic!("expected text was not found in actual text"); + } +} + /// Check that `haystack` contains `needle`. Panic otherwise. #[track_caller] pub fn assert_contains(haystack: &str, needle: &str) { @@ -468,6 +480,15 @@ pub fn run_fail(&mut self) -> crate::command::CompletedProcess { self.cmd.run_fail() } + /// Run the command but do not check its exit status. + /// Only use if you explicitly don't care about the exit status. + /// Prefer to use [`Self::run`] and [`Self::run_fail`] + /// whenever possible. + #[track_caller] + pub fn run_unchecked(&mut self) -> crate::command::CompletedProcess { + self.cmd.run_unchecked() + } + /// Set the path where the command will be run. pub fn current_dir>(&mut self, path: P) -> &mut Self { self.cmd.current_dir(path); diff --git a/src/tools/tidy/src/allowed_run_make_makefiles.txt b/src/tools/tidy/src/allowed_run_make_makefiles.txt index 6a1809951fd..69a8aa05360 100644 --- a/src/tools/tidy/src/allowed_run_make_makefiles.txt +++ b/src/tools/tidy/src/allowed_run_make_makefiles.txt @@ -83,7 +83,6 @@ run-make/issue-20626/Makefile run-make/issue-22131/Makefile run-make/issue-25581/Makefile run-make/issue-26006/Makefile -run-make/issue-26092/Makefile run-make/issue-28595/Makefile run-make/issue-33329/Makefile run-make/issue-35164/Makefile diff --git a/tests/run-make/CURRENT_RUSTC_VERSION/rmake.rs b/tests/run-make/CURRENT_RUSTC_VERSION/rmake.rs index b3227b79559..d937514c2ca 100644 --- a/tests/run-make/CURRENT_RUSTC_VERSION/rmake.rs +++ b/tests/run-make/CURRENT_RUSTC_VERSION/rmake.rs @@ -12,7 +12,6 @@ fn main() { let output = rustc().input("main.rs").emit("metadata").extern_("stable", "libstable.rmeta").run(); - let version = fs_wrapper::read_to_string(source_root().join("src/version")); let expected_string = format!("stable since {}", version.trim()); output.assert_stderr_contains(expected_string); diff --git a/tests/run-make/issue-26092/blank.rs b/tests/run-make/clear-error-blank-output/blank.rs similarity index 100% rename from tests/run-make/issue-26092/blank.rs rename to tests/run-make/clear-error-blank-output/blank.rs diff --git a/tests/run-make/clear-error-blank-output/rmake.rs b/tests/run-make/clear-error-blank-output/rmake.rs new file mode 100644 index 00000000000..e0f042cf805 --- /dev/null +++ b/tests/run-make/clear-error-blank-output/rmake.rs @@ -0,0 +1,13 @@ +// When an empty output file is passed to rustc, the ensuing error message +// should be clear. However, calling file_stem on an empty path returns None, +// which, when unwrapped, causes a panic, stopping execution of rustc +// and printing an obscure message instead of reaching the helpful +// error message. This test checks that the panic does not occur. +// See https://github.com/rust-lang/rust/pull/26199 + +use run_make_support::rustc; + +fn main() { + let output = rustc().output("").stdin(b"fn main() {}").run_fail(); + output.assert_stderr_not_contains("panic"); +} diff --git a/tests/run-make/issue-26092/Makefile b/tests/run-make/issue-26092/Makefile deleted file mode 100644 index 96822e7690b..00000000000 --- a/tests/run-make/issue-26092/Makefile +++ /dev/null @@ -1,6 +0,0 @@ -include ../tools.mk - -# This test ensures that rustc does not panic with `-o ""` option. - -all: - $(RUSTC) -o "" blank.rs 2>&1 | $(CGREP) -i 'panic' && exit 1 || exit 0 diff --git a/tests/run-make/link-arg/rmake.rs b/tests/run-make/link-arg/rmake.rs index dd496101f6a..a6d68800792 100644 --- a/tests/run-make/link-arg/rmake.rs +++ b/tests/run-make/link-arg/rmake.rs @@ -7,18 +7,14 @@ use run_make_support::rustc; fn main() { - let output = String::from_utf8( - rustc() - .input("empty.rs") - .link_arg("-lfoo") - .link_arg("-lbar") - .print("link-args") - .command_output() - .stdout, - ) - .unwrap(); - assert!( - output.contains("lfoo") || output.contains("lbar"), - "The output did not contain the expected \"lfoo\" or \"lbar\" strings." - ); + // We are only checking for the output of --print=link-args, + // rustc failing or succeeding does not matter. + let out = rustc() + .input("empty.rs") + .link_arg("-lfoo") + .link_arg("-lbar") + .print("link-args") + .run_unchecked(); + out.assert_stdout_contains("lfoo"); + out.assert_stdout_contains("lbar"); } diff --git a/tests/run-make/link-dedup/Makefile b/tests/run-make/link-dedup/Makefile deleted file mode 100644 index eff18ab48ab..00000000000 --- a/tests/run-make/link-dedup/Makefile +++ /dev/null @@ -1,12 +0,0 @@ -# ignore-msvc - -include ../tools.mk - -all: - $(RUSTC) depa.rs - $(RUSTC) depb.rs - $(RUSTC) depc.rs - $(RUSTC) empty.rs --cfg bar 2>&1 | $(CGREP) '"-ltesta" "-ltestb" "-ltesta"' - $(RUSTC) empty.rs 2>&1 | $(CGREP) '"-ltesta"' - $(RUSTC) empty.rs 2>&1 | $(CGREP) -v '"-ltestb"' - $(RUSTC) empty.rs 2>&1 | $(CGREP) -v '"-ltesta" "-ltesta" "-ltesta"' diff --git a/tests/run-make/link-dedup/rmake.rs b/tests/run-make/link-dedup/rmake.rs index a0d3ac86542..9bff3a4b44c 100644 --- a/tests/run-make/link-dedup/rmake.rs +++ b/tests/run-make/link-dedup/rmake.rs @@ -7,26 +7,18 @@ //@ ignore-msvc +use run_make_support::rustc; + fn main() { rustc().input("depa.rs").run(); rustc().input("depb.rs").run(); rustc().input("depc.rs").run(); - let output = - String::from_utf8(rustc().input("empty.rs").cfg("bar").command_output().stderr).unwrap(); - let pos_a1 = - output.find("-ltesta").expect("empty.rs, compiled with --cfg, should contain -ltesta"); - let pos_b = output[pos_a1..] - .find("-ltestb") - .map(|pos| pos + pos_a1) - .expect("empty.rs, compiled with --cfg, should contain -ltestb"); - let _ = output[pos_b..] - .find("-ltesta") - .map(|pos| pos + pos_b) - .expect("empty.rs, compiled with --cfg, should contain a second -ltesta"); - let output = String::from_utf8(rustc().input("empty.rs").command_output().stderr).unwrap(); - assert!(output.contains("-ltesta")); - let output = String::from_utf8(rustc().input("empty.rs").command_output().stderr).unwrap(); - assert!(!output.contains("-ltestb")); - let output = String::from_utf8(rustc().input("empty.rs").command_output().stderr).unwrap(); - assert_eq!(output.matches("-ltesta").count, 1); + let output = rustc().input("empty.rs").cfg("bar").run_fail(); + output.assert_stderr_contains(r#""-ltesta" "-ltestb" "-ltesta""#); + let output = rustc().input("empty.rs").run_fail(); + output.assert_stderr_contains(r#""-ltesta""#); + let output = rustc().input("empty.rs").run_fail(); + output.assert_stderr_not_contains(r#""-ltestb""#); + let output = rustc().input("empty.rs").run_fail(); + output.assert_stderr_not_contains(r#""-ltesta" "-ltesta" "-ltesta""#); }