Skip to content

Commit 0a0a64a

Browse files
Implement missing methods on Command and on CompletedProcess
1 parent d402830 commit 0a0a64a

File tree

2 files changed

+49
-32
lines changed

2 files changed

+49
-32
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ impl Command {
7171
}
7272
}
7373

74+
crate::impl_common_helpers_without_run!(Command);
75+
7476
impl Deref for Command {
7577
type Target = StdCommand;
7678

@@ -112,6 +114,12 @@ impl CompletedProcess {
112114
self
113115
}
114116

117+
#[track_caller]
118+
pub fn assert_stdout_contains<S: AsRef<str>>(self, content: S) -> Self {
119+
assert!(self.stdout_utf8().contains(content.as_ref()));
120+
self
121+
}
122+
115123
#[track_caller]
116124
pub fn assert_stdout_not_contains<S: AsRef<str>>(self, needle: S) -> Self {
117125
assert_not_contains(&self.stdout_utf8(), needle.as_ref());

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

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -317,32 +317,7 @@ pub fn run_in_tmpdir<F: FnOnce()>(callback: F) {
317317
fs::remove_dir_all(tmpdir).unwrap();
318318
}
319319

320-
/// Implement common helpers for command wrappers. This assumes that the command wrapper is a struct
321-
/// containing a `cmd: Command` field. The provided helpers are:
322-
///
323-
/// 1. Generic argument acceptors: `arg` and `args` (delegated to [`Command`]). These are intended
324-
/// to be *fallback* argument acceptors, when specific helpers don't make sense. Prefer to add
325-
/// new specific helper methods over relying on these generic argument providers.
326-
/// 2. Environment manipulation methods: `env`, `env_remove` and `env_clear`: these delegate to
327-
/// methods of the same name on [`Command`].
328-
/// 3. Output and execution: `run` and `run_fail` are provided. These are
329-
/// higher-level convenience methods which wait for the command to finish running and assert
330-
/// that the command successfully ran or failed as expected. They return
331-
/// [`CompletedProcess`], which can be used to assert the stdout/stderr/exit code of the executed
332-
/// process.
333-
///
334-
/// Example usage:
335-
///
336-
/// ```ignore (illustrative)
337-
/// struct CommandWrapper { cmd: Command } // <- required `cmd` field
338-
///
339-
/// crate::impl_common_helpers!(CommandWrapper);
340-
///
341-
/// impl CommandWrapper {
342-
/// // ... additional specific helper methods
343-
/// }
344-
/// ```
345-
macro_rules! impl_common_helpers {
320+
macro_rules! impl_common_helpers_without_run {
346321
($wrapper: ident) => {
347322
impl $wrapper {
348323
/// Specify an environment variable.
@@ -401,6 +376,45 @@ macro_rules! impl_common_helpers {
401376
inspector(&self.cmd);
402377
self
403378
}
379+
}
380+
};
381+
}
382+
383+
/// Implement common helpers for command wrappers. This assumes that the command wrapper is a struct
384+
/// containing a `cmd: Command` field. The provided helpers are:
385+
///
386+
/// 1. Generic argument acceptors: `arg` and `args` (delegated to [`Command`]). These are intended
387+
/// to be *fallback* argument acceptors, when specific helpers don't make sense. Prefer to add
388+
/// new specific helper methods over relying on these generic argument providers.
389+
/// 2. Environment manipulation methods: `env`, `env_remove` and `env_clear`: these delegate to
390+
/// methods of the same name on [`Command`].
391+
/// 3. Output and execution: `run` and `run_fail` are provided. These are
392+
/// higher-level convenience methods which wait for the command to finish running and assert
393+
/// that the command successfully ran or failed as expected. They return
394+
/// [`CompletedProcess`], which can be used to assert the stdout/stderr/exit code of the executed
395+
/// process.
396+
///
397+
/// Example usage:
398+
///
399+
/// ```ignore (illustrative)
400+
/// struct CommandWrapper { cmd: Command } // <- required `cmd` field
401+
///
402+
/// crate::impl_common_helpers!(CommandWrapper);
403+
///
404+
/// impl CommandWrapper {
405+
/// // ... additional specific helper methods
406+
/// }
407+
/// ```
408+
macro_rules! impl_common_helpers {
409+
($wrapper: ident) => {
410+
crate::impl_common_helpers_without_run!($wrapper);
411+
412+
impl $wrapper {
413+
/// Set the path where the command will be run.
414+
pub fn current_dir<P: AsRef<Path>>(&mut self, path: P) -> &mut Self {
415+
self.cmd.current_dir(path);
416+
self
417+
}
404418

405419
/// Run the constructed command and assert that it is successfully run.
406420
#[track_caller]
@@ -413,15 +427,10 @@ macro_rules! impl_common_helpers {
413427
pub fn run_fail(&mut self) -> crate::command::CompletedProcess {
414428
self.cmd.run_fail()
415429
}
416-
417-
/// Set the path where the command will be run.
418-
pub fn current_dir<P: AsRef<Path>>(&mut self, path: P) -> &mut Self {
419-
self.cmd.current_dir(path);
420-
self
421-
}
422430
}
423431
};
424432
}
425433

426434
use crate::command::{Command, CompletedProcess};
427435
pub(crate) use impl_common_helpers;
436+
pub(crate) use impl_common_helpers_without_run;

0 commit comments

Comments
 (0)