Skip to content

Commit 64224e5

Browse files
Implement missing methods on Command and on CompletedProcess
1 parent 56e112a commit 64224e5

File tree

2 files changed

+64
-27
lines changed

2 files changed

+64
-27
lines changed

src/tools/run-make-support/src/command.rs

+23-1
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,22 @@ impl Command {
140140
}
141141
}
142142

143+
crate::impl_common_helpers_without_run!(Command);
144+
145+
impl Deref for Command {
146+
type Target = StdCommand;
147+
148+
fn deref(&self) -> &Self::Target {
149+
&self.cmd
150+
}
151+
}
152+
153+
impl DerefMut for Command {
154+
fn deref_mut(&mut self) -> &mut Self::Target {
155+
&mut self.cmd
156+
}
157+
}
158+
143159
/// Represents the result of an executed process.
144160
/// The various `assert_` helper methods should preferably be used for
145161
/// checking the contents of stdout/stderr.
@@ -168,7 +184,13 @@ impl CompletedProcess {
168184
}
169185

170186
#[track_caller]
171-
pub fn assert_stdout_not_contains<S: AsRef<str>>(&self, needle: S) -> &Self {
187+
pub fn assert_stdout_contains<S: AsRef<str>>(self, content: S) -> Self {
188+
assert!(self.stdout_utf8().contains(content.as_ref()));
189+
self
190+
}
191+
192+
#[track_caller]
193+
pub fn assert_stdout_not_contains<S: AsRef<str>>(self, needle: S) -> Self {
172194
assert_not_contains(&self.stdout_utf8(), needle.as_ref());
173195
self
174196
}

src/tools/run-make-support/src/lib.rs

+41-26
Original file line numberDiff line numberDiff line change
@@ -348,32 +348,7 @@ pub fn run_in_tmpdir<F: FnOnce()>(callback: F) {
348348
fs::remove_dir_all(tmpdir).unwrap();
349349
}
350350

351-
/// Implement common helpers for command wrappers. This assumes that the command wrapper is a struct
352-
/// containing a `cmd: Command` field. The provided helpers are:
353-
///
354-
/// 1. Generic argument acceptors: `arg` and `args` (delegated to [`Command`]). These are intended
355-
/// to be *fallback* argument acceptors, when specific helpers don't make sense. Prefer to add
356-
/// new specific helper methods over relying on these generic argument providers.
357-
/// 2. Environment manipulation methods: `env`, `env_remove` and `env_clear`: these delegate to
358-
/// methods of the same name on [`Command`].
359-
/// 3. Output and execution: `run` and `run_fail` are provided. These are
360-
/// higher-level convenience methods which wait for the command to finish running and assert
361-
/// that the command successfully ran or failed as expected. They return
362-
/// [`CompletedProcess`], which can be used to assert the stdout/stderr/exit code of the executed
363-
/// process.
364-
///
365-
/// Example usage:
366-
///
367-
/// ```ignore (illustrative)
368-
/// struct CommandWrapper { cmd: Command } // <- required `cmd` field
369-
///
370-
/// crate::impl_common_helpers!(CommandWrapper);
371-
///
372-
/// impl CommandWrapper {
373-
/// // ... additional specific helper methods
374-
/// }
375-
/// ```
376-
macro_rules! impl_common_helpers {
351+
macro_rules! impl_common_helpers_without_run {
377352
($wrapper: ident) => {
378353
impl $wrapper {
379354
/// Specify an environment variable.
@@ -426,6 +401,45 @@ macro_rules! impl_common_helpers {
426401
self.cmd.inspect(inspector);
427402
self
428403
}
404+
}
405+
};
406+
}
407+
408+
/// Implement common helpers for command wrappers. This assumes that the command wrapper is a struct
409+
/// containing a `cmd: Command` field. The provided helpers are:
410+
///
411+
/// 1. Generic argument acceptors: `arg` and `args` (delegated to [`Command`]). These are intended
412+
/// to be *fallback* argument acceptors, when specific helpers don't make sense. Prefer to add
413+
/// new specific helper methods over relying on these generic argument providers.
414+
/// 2. Environment manipulation methods: `env`, `env_remove` and `env_clear`: these delegate to
415+
/// methods of the same name on [`Command`].
416+
/// 3. Output and execution: `run` and `run_fail` are provided. These are
417+
/// higher-level convenience methods which wait for the command to finish running and assert
418+
/// that the command successfully ran or failed as expected. They return
419+
/// [`CompletedProcess`], which can be used to assert the stdout/stderr/exit code of the executed
420+
/// process.
421+
///
422+
/// Example usage:
423+
///
424+
/// ```ignore (illustrative)
425+
/// struct CommandWrapper { cmd: Command } // <- required `cmd` field
426+
///
427+
/// crate::impl_common_helpers!(CommandWrapper);
428+
///
429+
/// impl CommandWrapper {
430+
/// // ... additional specific helper methods
431+
/// }
432+
/// ```
433+
macro_rules! impl_common_helpers {
434+
($wrapper: ident) => {
435+
crate::impl_common_helpers_without_run!($wrapper);
436+
437+
impl $wrapper {
438+
/// Set the path where the command will be run.
439+
pub fn current_dir<P: AsRef<Path>>(&mut self, path: P) -> &mut Self {
440+
self.cmd.current_dir(path);
441+
self
442+
}
429443

430444
/// Run the constructed command and assert that it is successfully run.
431445
#[track_caller]
@@ -450,3 +464,4 @@ macro_rules! impl_common_helpers {
450464

451465
use crate::command::{Command, CompletedProcess};
452466
pub(crate) use impl_common_helpers;
467+
pub(crate) use impl_common_helpers_without_run;

0 commit comments

Comments
 (0)