From 3f6e313f5ea01f109b82b70cfeaf596d52a2f25f Mon Sep 17 00:00:00 2001 From: Jerry Wang Date: Wed, 17 Jul 2024 19:53:12 -0400 Subject: [PATCH 1/2] run_make_support: add `CompletedProcess` checks for `Command` --- src/tools/run-make-support/src/command.rs | 46 +++++++++++++++++++++-- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/src/tools/run-make-support/src/command.rs b/src/tools/run-make-support/src/command.rs index 47376c401bb67..e9cd7407e5ff0 100644 --- a/src/tools/run-make-support/src/command.rs +++ b/src/tools/run-make-support/src/command.rs @@ -24,18 +24,50 @@ use build_helper::drop_bomb::DropBomb; /// [`run`]: Self::run /// [`run_fail`]: Self::run_fail /// [`run_unchecked`]: Self::run_unchecked -#[derive(Debug)] pub struct Command { cmd: StdCommand, stdin: Option>, + completed_process_checks: Vec<(String, Box)>, drop_bomb: DropBomb, } +impl std::fmt::Debug for Command { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Command") + .field("cmd", &self.cmd) + .field("stdin", &self.stdin) + .field( + "completed_process_checks", + &self + .completed_process_checks + .iter() + .map(|(name, _check)| name) + .collect::>(), + ) + .field("drop_bomb", &self.drop_bomb) + .finish() + } +} + impl Command { #[track_caller] pub fn new>(program: P) -> Self { let program = program.as_ref(); - Self { cmd: StdCommand::new(program), stdin: None, drop_bomb: DropBomb::arm(program) } + Self { + cmd: StdCommand::new(program), + stdin: None, + completed_process_checks: vec![], + drop_bomb: DropBomb::arm(program), + } + } + + /// Adds a `check` to be completed once the `Command` is run. + pub(crate) fn add_completed_process_check(&mut self, name: &str, check: F) -> &mut Self + where + F: Fn(&CompletedProcess) + 'static, + { + self.completed_process_checks.push((name.to_string(), Box::new(check))); + self } /// Specify a stdin input @@ -151,7 +183,15 @@ impl Command { } else { self.cmd.output().expect("failed to get output of finished process") }; - output.into() + + let completed_process = CompletedProcess::from(output); + + for (name, check) in self.completed_process_checks.iter() { + eprintln!("checking: {name}"); + check(&completed_process); + } + + completed_process } } From ad624c8e84e8cd6b9e3356876de497189dd17d1a Mon Sep 17 00:00:00 2001 From: Jerry Wang Date: Wed, 17 Jul 2024 19:54:07 -0400 Subject: [PATCH 2/2] run_make_support: add trailing newline check for `rustc --print` --- src/tools/run-make-support/src/command.rs | 12 ++++++++++++ .../run-make-support/src/external_deps/rustc.rs | 6 ++++++ tests/run-make/link-arg/rmake.rs | 1 - 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/tools/run-make-support/src/command.rs b/src/tools/run-make-support/src/command.rs index e9cd7407e5ff0..51e9f551e2362 100644 --- a/src/tools/run-make-support/src/command.rs +++ b/src/tools/run-make-support/src/command.rs @@ -239,6 +239,18 @@ impl CompletedProcess { self } + /// Checks that `stdout` ends with a newline if it is not empty. + #[track_caller] + pub fn assert_stdout_ends_with_newline(&self) -> &Self { + if !self.stdout_utf8().is_empty() { + assert!( + self.stdout_utf8().ends_with(['\r', '\n'].as_slice()), + "stdout does not end with newline" + ); + } + self + } + /// Checks that trimmed `stderr` matches trimmed `expected`. #[track_caller] pub fn assert_stderr_equals>(&self, expected: S) -> &Self { diff --git a/src/tools/run-make-support/src/external_deps/rustc.rs b/src/tools/run-make-support/src/external_deps/rustc.rs index 71d28dd9675fb..4dc275613e2dc 100644 --- a/src/tools/run-make-support/src/external_deps/rustc.rs +++ b/src/tools/run-make-support/src/external_deps/rustc.rs @@ -272,9 +272,15 @@ impl Rustc { } /// Specify the print request. + /// + /// Also checks that the [`CompletedProcess`](crate::command::CompletedProcess) + /// `stdout` ends with a newline. pub fn print(&mut self, request: &str) -> &mut Self { self.cmd.arg("--print"); self.cmd.arg(request); + self.cmd.add_completed_process_check("`--print` ends with newline", |completed_process| { + completed_process.assert_stdout_ends_with_newline(); + }); self } diff --git a/tests/run-make/link-arg/rmake.rs b/tests/run-make/link-arg/rmake.rs index c0bf8d972af73..a6d68800792f8 100644 --- a/tests/run-make/link-arg/rmake.rs +++ b/tests/run-make/link-arg/rmake.rs @@ -17,5 +17,4 @@ fn main() { .run_unchecked(); out.assert_stdout_contains("lfoo"); out.assert_stdout_contains("lbar"); - assert!(out.stdout_utf8().ends_with('\n')); }