2424//! - Workaround the [per-call cargo overhead][cargo-overhead] by caching the binary location with [`lazy_static`].
2525//! - [If not using `--target <TRIPLET>`, bypass the first call overhead][first-call] by not
2626//! passing `current_target()` to [`escargot`].
27+ //! - Using bin or example from another crate from workspaces
28+ //!
29+ //! This can be done by using [`CommandCargoBuildExt`] trait.
2730//!
2831//! ```rust
2932//! extern crate assert_cmd;
30- //! extern crate escargot;
3133//!
3234//! use assert_cmd::prelude::*;
33- //! use escargot;
3435//!
3536//! use std::process::Command;
3637//!
37- //! let bin_under_test = escargot::CargoBuild::new ()
38+ //! let mut bin_under_test = Command::cargo_builder ()
3839//! .bin("bin_fixture")
3940//! .current_release()
4041//! .current_target()
41- //! .run ()
42+ //! .build_command ()
4243//! .unwrap();
43- //! let mut cmd = bin_under_test.command();
44- //! let output = cmd.unwrap();
44+ //! let output = bin_under_test.unwrap();
4545//! ```
4646//!
4747//! [`lazy_static`]: https://crates.io/crates/lazy_static
4848//! [`CommandCargoExt`]: trait.CommandCargoExt.html
49+ //! [`CommandCargoBuildExt`]: trait.CommandCargoBuildExt.html
4950//! [`Command`]: https://doc.rust-lang.org/std/process/struct.Command.html
5051//! [`escargot`]: https://docs.rs/escargot/
5152//! [cargo-overhead]: https://github.com/assert-rs/assert_cmd/issues/6
@@ -58,6 +59,68 @@ use std::process;
5859
5960use escargot;
6061
62+ /// `CommandCargoBuildExt` is an extension trait for [`CargoBuild`][CargoBuild] to run
63+ /// command using CargoBuild builder
64+ ///
65+ /// See the [`cargo` module documentation][`cargo`] for caveats and workarounds.
66+ ///
67+ /// # Examples
68+ ///
69+ /// ```rust
70+ /// use assert_cmd::prelude::*;
71+ ///
72+ /// use std::process::Command;
73+ ///
74+ /// let mut cmd = Command::cargo_builder()
75+ /// .bin("bin_fixture")
76+ /// .package("assert_cmd")
77+ /// .current_release()
78+ /// .current_target()
79+ /// .build_command()
80+ /// .unwrap();
81+ /// let output = cmd.unwrap();
82+ /// ```
83+ ///
84+ /// [`Command`]: https://doc.rust-lang.org/std/process/struct.Command.html
85+ /// [`cargo`]: index.html
86+ pub trait CommandCargoBuildExt
87+ where
88+ Self : Sized ,
89+ {
90+ /// Create a [`Command`] using [`CargoBuild`] builder.
91+ ///
92+ /// See the [`cargo` module documentation][`cargo`] for caveats and workarounds.
93+ ///
94+ /// # Examples
95+ ///
96+ /// ```rust
97+ /// use assert_cmd::prelude::*;
98+ ///
99+ /// use std::process::Command;
100+ ///
101+ /// let mut cmd = Command::cargo_builder()
102+ /// .example("example_fixture")
103+ /// .package("assert_cmd")
104+ /// .current_release()
105+ /// .current_target()
106+ /// .build_command()
107+ /// .unwrap();
108+ /// let output = cmd.unwrap();
109+ /// ```
110+ ///
111+ /// [`Command`]: https://doc.rust-lang.org/std/process/struct.Command.html
112+ /// [`CargoBuild`]: https://docs.rs/escargot/0.4.0/escargot/struct.CargoBuild.html
113+ /// [`cargo`]: index.html
114+ fn build_command ( self ) -> Result < process:: Command , CargoError > ;
115+ }
116+
117+ impl CommandCargoBuildExt for escargot:: CargoBuild {
118+ fn build_command ( self ) -> Result < process:: Command , CargoError > {
119+ let runner = self . run ( ) . map_err ( CargoError :: with_cause) ?;
120+ Ok ( runner. command ( ) )
121+ }
122+ }
123+
61124/// Create a [`Command`] for a `bin` in the Cargo project.
62125///
63126/// `CommandCargoExt` is an extension trait for [`Command`][Command] to easily launch a crate's
@@ -144,6 +207,34 @@ where
144207 /// [`Command`]: https://doc.rust-lang.org/std/process/struct.Command.html
145208 /// [`cargo`]: index.html
146209 fn cargo_example < S : AsRef < ffi:: OsStr > > ( name : S ) -> Result < Self , CargoError > ;
210+
211+ /// Create a [`CargoBuild`] builder to construct any cargo run command
212+ ///
213+ /// See the [`cargo` module documentation][`cargo`] for caveats and workarounds.
214+ ///
215+ /// # Examples
216+ ///
217+ /// ```rust
218+ /// use assert_cmd::prelude::*;
219+ ///
220+ /// use std::process::Command;
221+ ///
222+ /// let mut cmd = Command::cargo_builder()
223+ /// .bin("bin_fixture")
224+ /// .package("assert_cmd")
225+ /// .current_release()
226+ /// .current_target()
227+ /// .build_command()
228+ /// .unwrap();
229+ /// let output = cmd.unwrap();
230+ /// ```
231+ ///
232+ /// [`Command`]: https://doc.rust-lang.org/std/process/struct.Command.html
233+ /// [`CargoBuild`]: https://docs.rs/escargot/0.4.0/escargot/struct.CargoBuild.html
234+ /// [`cargo`]: index.html
235+ fn cargo_builder ( ) -> escargot:: CargoBuild {
236+ escargot:: CargoBuild :: new ( )
237+ }
147238}
148239
149240impl CommandCargoExt for process:: Command {
0 commit comments