diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/tools/run-make-support/src/cc.rs | 2 | ||||
| -rw-r--r-- | src/tools/run-make-support/src/clang.rs | 2 | ||||
| -rw-r--r-- | src/tools/run-make-support/src/command.rs | 76 | ||||
| -rw-r--r-- | src/tools/run-make-support/src/lib.rs | 25 | ||||
| -rw-r--r-- | src/tools/run-make-support/src/llvm_readobj.rs | 2 | ||||
| -rw-r--r-- | src/tools/run-make-support/src/run.rs | 11 | ||||
| -rw-r--r-- | src/tools/run-make-support/src/rustc.rs | 4 | ||||
| -rw-r--r-- | src/tools/run-make-support/src/rustdoc.rs | 2 |
8 files changed, 90 insertions, 34 deletions
diff --git a/src/tools/run-make-support/src/cc.rs b/src/tools/run-make-support/src/cc.rs index bfed6d922ab..5c0158d7547 100644 --- a/src/tools/run-make-support/src/cc.rs +++ b/src/tools/run-make-support/src/cc.rs @@ -1,7 +1,7 @@ use std::path::Path; -use crate::{bin_name, cygpath_windows, env_var, handle_failed_output, is_msvc, is_windows, uname}; use crate::command::Command; +use crate::{bin_name, cygpath_windows, env_var, is_msvc, is_windows, uname}; /// Construct a new platform-specific C compiler invocation. /// diff --git a/src/tools/run-make-support/src/clang.rs b/src/tools/run-make-support/src/clang.rs index 2ceea3df95e..d2ebed7ab06 100644 --- a/src/tools/run-make-support/src/clang.rs +++ b/src/tools/run-make-support/src/clang.rs @@ -1,7 +1,7 @@ use std::path::Path; -use crate::{bin_name, env_var, handle_failed_output}; use crate::command::Command; +use crate::{bin_name, env_var}; /// Construct a new `clang` invocation. `clang` is not always available for all targets. pub fn clang() -> Clang { diff --git a/src/tools/run-make-support/src/command.rs b/src/tools/run-make-support/src/command.rs index c142c80024b..b9e56ab632a 100644 --- a/src/tools/run-make-support/src/command.rs +++ b/src/tools/run-make-support/src/command.rs @@ -1,8 +1,12 @@ +use crate::{assert_not_contains, handle_failed_output}; use std::ffi::OsStr; use std::io::Write; use std::ops::{Deref, DerefMut}; use std::process::{Command as StdCommand, ExitStatus, Output, Stdio}; +/// 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. #[derive(Debug)] pub struct Command { cmd: StdCommand, @@ -11,16 +15,39 @@ pub struct Command { impl Command { pub fn new<S: AsRef<OsStr>>(program: S) -> Self { - Self { - cmd: StdCommand::new(program), - stdin: None, - } + Self { cmd: StdCommand::new(program), stdin: None } } pub fn set_stdin(&mut self, stdin: Box<[u8]>) { self.stdin = Some(stdin); } + /// Run the constructed command and assert that it is successfully run. + #[track_caller] + pub fn run(&mut self) -> CompletedProcess { + let caller_location = std::panic::Location::caller(); + let caller_line_number = caller_location.line(); + + let output = self.command_output(); + if !output.status().success() { + handle_failed_output(&self, output, caller_line_number); + } + output + } + + /// Run the constructed command and assert that it does not successfully run. + #[track_caller] + pub fn run_fail(&mut self) -> CompletedProcess { + let caller_location = std::panic::Location::caller(); + let caller_line_number = caller_location.line(); + + let output = self.command_output(); + if output.status().success() { + handle_failed_output(&self, output, caller_line_number); + } + output + } + #[track_caller] pub(crate) fn command_output(&mut self) -> CompletedProcess { // let's make sure we piped all the input and outputs @@ -59,6 +86,8 @@ impl DerefMut for Command { } /// Represents the result of an executed process. +/// The various `assert_` helper methods should preferably be used for +/// checking the contents of stdout/stderr. pub struct CompletedProcess { output: Output, } @@ -76,16 +105,47 @@ impl CompletedProcess { self.output.status } + /// Checks that trimmed `stdout` matches trimmed `content`. + #[track_caller] + pub fn assert_stdout_equals<S: AsRef<str>>(self, content: S) -> Self { + assert_eq!(self.stdout_utf8().trim(), content.as_ref().trim()); + self + } + #[track_caller] - pub fn assert_exit_code(&self, code: i32) { + pub fn assert_stdout_not_contains<S: AsRef<str>>(self, needle: S) -> Self { + assert_not_contains(&self.stdout_utf8(), needle.as_ref()); + self + } + + /// Checks that trimmed `stderr` matches trimmed `content`. + #[track_caller] + pub fn assert_stderr_equals<S: AsRef<str>>(self, content: S) -> Self { + assert_eq!(self.stderr_utf8().trim(), content.as_ref().trim()); + self + } + + #[track_caller] + pub fn assert_stderr_contains<S: AsRef<str>>(self, needle: S) -> Self { + assert!(self.stderr_utf8().contains(needle.as_ref())); + self + } + + #[track_caller] + pub fn assert_stderr_not_contains<S: AsRef<str>>(self, needle: S) -> Self { + assert_not_contains(&self.stdout_utf8(), needle.as_ref()); + self + } + + #[track_caller] + pub fn assert_exit_code(self, code: i32) -> Self { assert!(self.output.status.code() == Some(code)); + self } } impl From<Output> for CompletedProcess { fn from(output: Output) -> Self { - Self { - output - } + Self { output } } } diff --git a/src/tools/run-make-support/src/lib.rs b/src/tools/run-make-support/src/lib.rs index c22ff12814b..b17f217c133 100644 --- a/src/tools/run-make-support/src/lib.rs +++ b/src/tools/run-make-support/src/lib.rs @@ -5,12 +5,12 @@ pub mod cc; pub mod clang; +mod command; pub mod diff; pub mod llvm_readobj; pub mod run; pub mod rustc; pub mod rustdoc; -mod command; use std::env; use std::ffi::OsString; @@ -27,7 +27,7 @@ pub use cc::{cc, extra_c_flags, extra_cxx_flags, Cc}; pub use clang::{clang, Clang}; pub use diff::{diff, Diff}; pub use llvm_readobj::{llvm_readobj, LlvmReadobj}; -pub use run::{run, run_fail}; +pub use run::{cmd, run, run_fail}; pub use rustc::{aux_build, rustc, Rustc}; pub use rustdoc::{bare_rustdoc, rustdoc, Rustdoc}; @@ -285,6 +285,7 @@ pub fn read_dir<F: Fn(&Path)>(dir: impl AsRef<Path>, callback: F) { } /// Check that `haystack` does not contain `needle`. Panic otherwise. +#[track_caller] pub fn assert_not_contains(haystack: &str, needle: &str) { if haystack.contains(needle) { eprintln!("=== HAYSTACK ==="); @@ -412,27 +413,13 @@ macro_rules! impl_common_helpers { /// Run the constructed command and assert that it is successfully run. #[track_caller] pub fn run(&mut self) -> crate::command::CompletedProcess { - let caller_location = ::std::panic::Location::caller(); - let caller_line_number = caller_location.line(); - - let output = self.cmd.command_output(); - if !output.status().success() { - handle_failed_output(&self.cmd, output, caller_line_number); - } - output + self.cmd.run() } /// Run the constructed command and assert that it does not successfully run. #[track_caller] pub fn run_fail(&mut self) -> crate::command::CompletedProcess { - let caller_location = ::std::panic::Location::caller(); - let caller_line_number = caller_location.line(); - - let output = self.cmd.command_output(); - if output.status().success() { - handle_failed_output(&self.cmd, output, caller_line_number); - } - output + self.cmd.run_fail() } /// Set the path where the command will be run. @@ -444,5 +431,5 @@ macro_rules! impl_common_helpers { }; } -pub(crate) use impl_common_helpers; use crate::command::{Command, CompletedProcess}; +pub(crate) use impl_common_helpers; diff --git a/src/tools/run-make-support/src/llvm_readobj.rs b/src/tools/run-make-support/src/llvm_readobj.rs index a834d3a2e19..db2f9db6e41 100644 --- a/src/tools/run-make-support/src/llvm_readobj.rs +++ b/src/tools/run-make-support/src/llvm_readobj.rs @@ -1,7 +1,7 @@ use std::path::{Path, PathBuf}; -use crate::{env_var, handle_failed_output}; use crate::command::Command; +use crate::env_var; /// Construct a new `llvm-readobj` invocation. This assumes that `llvm-readobj` is available /// at `$LLVM_BIN_DIR/llvm-readobj`. diff --git a/src/tools/run-make-support/src/run.rs b/src/tools/run-make-support/src/run.rs index 09ad98c2451..4a6fd7c432e 100644 --- a/src/tools/run-make-support/src/run.rs +++ b/src/tools/run-make-support/src/run.rs @@ -1,8 +1,9 @@ use std::env; +use std::ffi::OsStr; use std::path::{Path, PathBuf}; -use crate::{cwd, env_var, is_windows}; use crate::command::{Command, CompletedProcess}; +use crate::{cwd, env_var, is_windows, set_host_rpath}; use super::handle_failed_output; @@ -62,3 +63,11 @@ pub fn run_fail(name: &str) -> CompletedProcess { } output } + +/// Create a new custom Command. +/// This should be preferred to creating `std::process::Command` directly. +pub fn cmd<S: AsRef<OsStr>>(program: S) -> Command { + let mut command = Command::new(program); + set_host_rpath(&mut command); + command +} diff --git a/src/tools/run-make-support/src/rustc.rs b/src/tools/run-make-support/src/rustc.rs index 9bbe30bcb2d..d4c00d23b8b 100644 --- a/src/tools/run-make-support/src/rustc.rs +++ b/src/tools/run-make-support/src/rustc.rs @@ -1,8 +1,8 @@ +use command::Command; use std::ffi::{OsStr, OsString}; use std::path::Path; -use command::Command; -use crate::{command, cwd, env_var, handle_failed_output, set_host_rpath}; +use crate::{command, cwd, env_var, set_host_rpath}; /// Construct a new `rustc` invocation. pub fn rustc() -> Rustc { diff --git a/src/tools/run-make-support/src/rustdoc.rs b/src/tools/run-make-support/src/rustdoc.rs index c75bb1d1196..39a698a47b4 100644 --- a/src/tools/run-make-support/src/rustdoc.rs +++ b/src/tools/run-make-support/src/rustdoc.rs @@ -1,8 +1,8 @@ use std::ffi::OsStr; use std::path::Path; -use crate::{env_var, env_var_os, handle_failed_output, set_host_rpath}; use crate::command::Command; +use crate::{env_var, env_var_os, set_host_rpath}; /// Construct a plain `rustdoc` invocation with no flags set. pub fn bare_rustdoc() -> Rustdoc { |
