Skip to content

Commit eab1076

Browse files
author
Andrii Radyk
committed
injecting builder implementation from CargoBuild
1 parent 82705d7 commit eab1076

File tree

3 files changed

+100
-7
lines changed

3 files changed

+100
-7
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ name = "bin_fixture"
1818
predicates = { version = "1.0", default-features = false, features = ["difference"] }
1919
predicates-core = "1.0"
2020
predicates-tree = "1.0"
21-
escargot = "0.3"
21+
escargot = "0.4"
2222

2323
[dev-dependencies]
2424
docmatic = "0.1"

src/cargo.rs

Lines changed: 97 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,29 @@
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

5960
use 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

149240
impl CommandCargoExt for process::Command {

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
//! [`Assert`]: assert/struct.Assert.html
104104
//! [`success()`]: assert/struct.Assert.html#method.success
105105
//! [`CommandCargoExt`]: cargo/trait.CommandCargoExt.html
106+
//! [`CommandCargoBuildExt`]: cargo/trait.CommandCargoBuildExt.html
106107
//! [`CommandStdInExt`]: stdin/trait.CommandStdInExt.html
107108
//! [`OutputOkExt`]: cmd/trait.OutputOkExt.html
108109
//! [`OutputAssertExt`]: assert/trait.OutputAssertExt.html
@@ -122,6 +123,7 @@ pub mod stdin;
122123
/// Extension traits that are useful to have available.
123124
pub mod prelude {
124125
pub use assert::OutputAssertExt;
126+
pub use cargo::CommandCargoBuildExt;
125127
pub use cargo::CommandCargoExt;
126128
pub use cmd::OutputOkExt;
127129
pub use stdin::CommandStdInExt;

0 commit comments

Comments
 (0)