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,69 @@ 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+
118+ impl CommandCargoBuildExt for escargot:: CargoBuild {
119+ fn build_command ( self ) -> Result < process:: Command , CargoError > {
120+ let runner = self . run ( ) . map_err ( CargoError :: with_cause) ?;
121+ Ok ( runner. command ( ) )
122+ }
123+ }
124+
61125/// Create a [`Command`] for a `bin` in the Cargo project.
62126///
63127/// `CommandCargoExt` is an extension trait for [`Command`][Command] to easily launch a crate's
@@ -144,6 +208,34 @@ where
144208 /// [`Command`]: https://doc.rust-lang.org/std/process/struct.Command.html
145209 /// [`cargo`]: index.html
146210 fn cargo_example < S : AsRef < ffi:: OsStr > > ( name : S ) -> Result < Self , CargoError > ;
211+
212+ /// Create a [`CargoBuild`] builder to construct any cargo run command
213+ ///
214+ /// See the [`cargo` module documentation][`cargo`] for caveats and workarounds.
215+ ///
216+ /// # Examples
217+ ///
218+ /// ```rust
219+ /// use assert_cmd::prelude::*;
220+ ///
221+ /// use std::process::Command;
222+ ///
223+ /// let mut cmd = Command::cargo_builder()
224+ /// .bin("bin_fixture")
225+ /// .package("assert_cmd")
226+ /// .current_release()
227+ /// .current_target()
228+ /// .build_command()
229+ /// .unwrap();
230+ /// let output = cmd.unwrap();
231+ /// ```
232+ ///
233+ /// [`Command`]: https://doc.rust-lang.org/std/process/struct.Command.html
234+ /// [`CargoBuild`]: https://docs.rs/escargot/0.4.0/escargot/struct.CargoBuild.html
235+ /// [`cargo`]: index.html
236+ fn cargo_builder ( ) -> escargot:: CargoBuild {
237+ escargot:: CargoBuild :: new ( )
238+ }
147239}
148240
149241impl CommandCargoExt for process:: Command {
0 commit comments