Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<S: AsRef<str>>(name: S) -> path::PathBuf {
cargo_bin_str(name.as_ref())
}
Expand Down
2 changes: 2 additions & 0 deletions src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 1 addition & 25 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
50 changes: 50 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/// 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")
};
}

/// 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)))
};
}