Skip to content

Commit abe77aa

Browse files
committed
feat(cargo): Add cargo_bin!
1 parent 148c026 commit abe77aa

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
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/macros.rs

+25
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,28 @@ macro_rules! crate_name {
2323
env!("CARGO_PKG_NAME")
2424
};
2525
}
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)