Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 9d9edf6

Browse files
author
Andrii Radyk
committedJan 6, 2019
injecting builder implementation from CargoBuild
1 parent 82705d7 commit 9d9edf6

File tree

3 files changed

+101
-7
lines changed

3 files changed

+101
-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: 98 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,69 @@ 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+
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

149241
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)
Please sign in to comment.