Skip to content

Commit 26cddcf

Browse files
authored
Merge pull request #234 from epage/macro
feat(cargo): Add `cargo_bin!`
2 parents a096861 + abe77aa commit 26cddcf

File tree

4 files changed

+60
-25
lines changed

4 files changed

+60
-25
lines changed

src/cargo.rs

+7
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ use std::fmt;
6060
use std::path;
6161
use std::process;
6262

63+
#[doc(inline)]
64+
pub use crate::cargo_bin;
65+
6366
/// Create a [`Command`] for a `bin` in the Cargo project.
6467
///
6568
/// `CommandCargoExt` is an extension trait for [`Command`][std::process::Command] to easily launch a crate's
@@ -95,6 +98,8 @@ where
9598
/// this method with [cross](https://github.com/cross-rs/cross), no extra configuration is
9699
/// needed.
97100
///
101+
/// **NOTE:** Prefer [`cargo_bin!`] as this makes assumptions about cargo
102+
///
98103
/// # Examples
99104
///
100105
/// ```rust,no_run
@@ -217,6 +222,8 @@ fn target_dir() -> path::PathBuf {
217222
}
218223

219224
/// Look up the path to a cargo-built binary within an integration test.
225+
///
226+
/// **NOTE:** Prefer [`cargo_bin!`] as this makes assumptions about cargo
220227
pub fn cargo_bin<S: AsRef<str>>(name: S) -> path::PathBuf {
221228
cargo_bin_str(name.as_ref())
222229
}

src/cmd.rs

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ impl Command {
3737
///
3838
/// See the [`cargo` module documentation][crate::cargo] for caveats and workarounds.
3939
///
40+
/// **NOTE:** Prefer [`cargo_bin!`][crate::cargo::cargo_bin!] as this makes assumptions about cargo
41+
///
4042
/// # Examples
4143
///
4244
/// ```rust,no_run

src/lib.rs

+1-25
Original file line numberDiff line numberDiff line change
@@ -104,31 +104,7 @@
104104
#![warn(clippy::print_stderr)]
105105
#![warn(clippy::print_stdout)]
106106

107-
/// Allows you to pull the name from your Cargo.toml at compile time.
108-
///
109-
/// # Examples
110-
///
111-
/// ```should_panic
112-
/// use assert_cmd::Command;
113-
///
114-
/// let mut cmd = Command::cargo_bin(assert_cmd::crate_name!()).unwrap();
115-
/// let assert = cmd
116-
/// .arg("-A")
117-
/// .env("stdout", "hello")
118-
/// .env("exit", "42")
119-
/// .write_stdin("42")
120-
/// .assert();
121-
/// assert
122-
/// .failure()
123-
/// .code(42)
124-
/// .stdout("hello\n");
125-
/// ```
126-
#[macro_export]
127-
macro_rules! crate_name {
128-
() => {
129-
env!("CARGO_PKG_NAME")
130-
};
131-
}
107+
mod macros;
132108

133109
pub mod assert;
134110
pub mod cargo;

src/macros.rs

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/// Allows you to pull the name from your Cargo.toml at compile time.
2+
///
3+
/// # Examples
4+
///
5+
/// ```should_panic
6+
/// use assert_cmd::Command;
7+
///
8+
/// let mut cmd = Command::cargo_bin(assert_cmd::crate_name!()).unwrap();
9+
/// let assert = cmd
10+
/// .arg("-A")
11+
/// .env("stdout", "hello")
12+
/// .env("exit", "42")
13+
/// .write_stdin("42")
14+
/// .assert();
15+
/// assert
16+
/// .failure()
17+
/// .code(42)
18+
/// .stdout("hello\n");
19+
/// ```
20+
#[macro_export]
21+
macro_rules! crate_name {
22+
() => {
23+
env!("CARGO_PKG_NAME")
24+
};
25+
}
26+
27+
/// The absolute path to a binary target's executable.
28+
///
29+
/// The `bin_target_name` is the name of the binary
30+
/// target, exactly as-is.
31+
///
32+
/// **NOTE:** This is only set when building an integration test or benchmark.
33+
///
34+
/// ## Example
35+
///
36+
/// ```rust,no_run
37+
/// #[test]
38+
/// fn cli_tests() {
39+
/// trycmd::TestCases::new()
40+
/// .default_bin_path(trycmd::cargo_bin!("bin-fixture"))
41+
/// .case("tests/cmd/*.trycmd");
42+
/// }
43+
/// ```
44+
#[macro_export]
45+
#[doc(hidden)]
46+
macro_rules! cargo_bin {
47+
($bin_target_name:expr) => {
48+
::std::path::Path::new(env!(concat!("CARGO_BIN_EXE_", $bin_target_name)))
49+
};
50+
}

0 commit comments

Comments
 (0)