24
24
//! - Workaround the [per-call cargo overhead][cargo-overhead] by caching the binary location with [`lazy_static`].
25
25
//! - [If not using `--target <TRIPLET>`, bypass the first call overhead][first-call] by not
26
26
//! 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.
27
30
//!
28
31
//! ```rust
29
32
//! extern crate assert_cmd;
30
- //! extern crate escargot;
31
33
//!
32
34
//! use assert_cmd::prelude::*;
33
- //! use escargot;
34
35
//!
35
36
//! use std::process::Command;
36
37
//!
37
- //! let bin_under_test = escargot::CargoBuild::new ()
38
+ //! let mut bin_under_test = Command::cargo_builder ()
38
39
//! .bin("bin_fixture")
39
40
//! .current_release()
40
41
//! .current_target()
41
- //! .run ()
42
+ //! .build_command ()
42
43
//! .unwrap();
43
- //! let mut cmd = bin_under_test.command();
44
- //! let output = cmd.unwrap();
44
+ //! let output = bin_under_test.unwrap();
45
45
//! ```
46
46
//!
47
47
//! [`lazy_static`]: https://crates.io/crates/lazy_static
48
48
//! [`CommandCargoExt`]: trait.CommandCargoExt.html
49
+ //! [`CommandCargoBuildExt`]: trait.CommandCargoBuildExt.html
49
50
//! [`Command`]: https://doc.rust-lang.org/std/process/struct.Command.html
50
51
//! [`escargot`]: https://docs.rs/escargot/
51
52
//! [cargo-overhead]: https://github.com/assert-rs/assert_cmd/issues/6
@@ -58,6 +59,69 @@ use std::process;
58
59
59
60
use escargot;
60
61
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
+
61
125
/// Create a [`Command`] for a `bin` in the Cargo project.
62
126
///
63
127
/// `CommandCargoExt` is an extension trait for [`Command`][Command] to easily launch a crate's
@@ -144,6 +208,34 @@ where
144
208
/// [`Command`]: https://doc.rust-lang.org/std/process/struct.Command.html
145
209
/// [`cargo`]: index.html
146
210
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
+ }
147
239
}
148
240
149
241
impl CommandCargoExt for process:: Command {
0 commit comments