From 148c026b1966bc2a398ad3466b10dd041d424539 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 16 Apr 2025 10:03:14 -0500 Subject: [PATCH 1/2] refactor: Move crate_name out into a file --- src/lib.rs | 26 +------------------------- src/macros.rs | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 25 deletions(-) create mode 100644 src/macros.rs diff --git a/src/lib.rs b/src/lib.rs index bb2e6d6..3e7b41b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -104,31 +104,7 @@ #![warn(clippy::print_stderr)] #![warn(clippy::print_stdout)] -/// Allows you to pull the name from your Cargo.toml at compile time. -/// -/// # Examples -/// -/// ```should_panic -/// use assert_cmd::Command; -/// -/// let mut cmd = Command::cargo_bin(assert_cmd::crate_name!()).unwrap(); -/// let assert = cmd -/// .arg("-A") -/// .env("stdout", "hello") -/// .env("exit", "42") -/// .write_stdin("42") -/// .assert(); -/// assert -/// .failure() -/// .code(42) -/// .stdout("hello\n"); -/// ``` -#[macro_export] -macro_rules! crate_name { - () => { - env!("CARGO_PKG_NAME") - }; -} +mod macros; pub mod assert; pub mod cargo; diff --git a/src/macros.rs b/src/macros.rs new file mode 100644 index 0000000..0a53739 --- /dev/null +++ b/src/macros.rs @@ -0,0 +1,25 @@ +/// Allows you to pull the name from your Cargo.toml at compile time. +/// +/// # Examples +/// +/// ```should_panic +/// use assert_cmd::Command; +/// +/// let mut cmd = Command::cargo_bin(assert_cmd::crate_name!()).unwrap(); +/// let assert = cmd +/// .arg("-A") +/// .env("stdout", "hello") +/// .env("exit", "42") +/// .write_stdin("42") +/// .assert(); +/// assert +/// .failure() +/// .code(42) +/// .stdout("hello\n"); +/// ``` +#[macro_export] +macro_rules! crate_name { + () => { + env!("CARGO_PKG_NAME") + }; +} From abe77aa131d775a7ba1f8e0e44b0ef8f5805ecb4 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 16 Apr 2025 10:09:48 -0500 Subject: [PATCH 2/2] feat(cargo): Add `cargo_bin!` --- src/cargo.rs | 7 +++++++ src/cmd.rs | 2 ++ src/macros.rs | 25 +++++++++++++++++++++++++ 3 files changed, 34 insertions(+) diff --git a/src/cargo.rs b/src/cargo.rs index 2c1604d..ce9bfe4 100644 --- a/src/cargo.rs +++ b/src/cargo.rs @@ -60,6 +60,9 @@ use std::fmt; use std::path; use std::process; +#[doc(inline)] +pub use crate::cargo_bin; + /// Create a [`Command`] for a `bin` in the Cargo project. /// /// `CommandCargoExt` is an extension trait for [`Command`][std::process::Command] to easily launch a crate's @@ -95,6 +98,8 @@ where /// this method with [cross](https://github.com/cross-rs/cross), no extra configuration is /// needed. /// + /// **NOTE:** Prefer [`cargo_bin!`] as this makes assumptions about cargo + /// /// # Examples /// /// ```rust,no_run @@ -217,6 +222,8 @@ fn target_dir() -> path::PathBuf { } /// Look up the path to a cargo-built binary within an integration test. +/// +/// **NOTE:** Prefer [`cargo_bin!`] as this makes assumptions about cargo pub fn cargo_bin>(name: S) -> path::PathBuf { cargo_bin_str(name.as_ref()) } diff --git a/src/cmd.rs b/src/cmd.rs index 719efab..ff023c6 100644 --- a/src/cmd.rs +++ b/src/cmd.rs @@ -37,6 +37,8 @@ impl Command { /// /// See the [`cargo` module documentation][crate::cargo] for caveats and workarounds. /// + /// **NOTE:** Prefer [`cargo_bin!`][crate::cargo::cargo_bin!] as this makes assumptions about cargo + /// /// # Examples /// /// ```rust,no_run diff --git a/src/macros.rs b/src/macros.rs index 0a53739..9b1c511 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -23,3 +23,28 @@ macro_rules! crate_name { env!("CARGO_PKG_NAME") }; } + +/// The absolute path to a binary target's executable. +/// +/// The `bin_target_name` is the name of the binary +/// target, exactly as-is. +/// +/// **NOTE:** This is only set when building an integration test or benchmark. +/// +/// ## Example +/// +/// ```rust,no_run +/// #[test] +/// fn cli_tests() { +/// trycmd::TestCases::new() +/// .default_bin_path(trycmd::cargo_bin!("bin-fixture")) +/// .case("tests/cmd/*.trycmd"); +/// } +/// ``` +#[macro_export] +#[doc(hidden)] +macro_rules! cargo_bin { + ($bin_target_name:expr) => { + ::std::path::Path::new(env!(concat!("CARGO_BIN_EXE_", $bin_target_name))) + }; +}