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
21 changes: 18 additions & 3 deletions src/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
//!
//! ```rust,no_run
//! use assert_cmd::prelude::*;
//! use assert_cmd::pkg_name;
//!
//! use std::process::Command;
//!
//! let mut cmd = Command::cargo_bin(env!("CARGO_PKG_NAME"))
//! let mut cmd = Command::cargo_bin(pkg_name!())
//! .unwrap();
//! let output = cmd.unwrap();
//! ```
Expand Down Expand Up @@ -62,6 +63,8 @@ use std::process;

#[doc(inline)]
pub use crate::cargo_bin;
#[doc(inline)]
pub use crate::cargo_bin_cmd;

/// Create a [`Command`] for a `bin` in the Cargo project.
///
Expand All @@ -74,10 +77,11 @@ pub use crate::cargo_bin;
///
/// ```rust,no_run
/// use assert_cmd::prelude::*;
/// use assert_cmd::pkg_name;
///
/// use std::process::Command;
///
/// let mut cmd = Command::cargo_bin(env!("CARGO_PKG_NAME"))
/// let mut cmd = Command::cargo_bin(pkg_name!())
/// .unwrap();
/// let output = cmd.unwrap();
/// println!("{:?}", output);
Expand All @@ -104,10 +108,11 @@ where
///
/// ```rust,no_run
/// use assert_cmd::prelude::*;
/// use assert_cmd::pkg_name;
///
/// use std::process::Command;
///
/// let mut cmd = Command::cargo_bin(env!("CARGO_PKG_NAME"))
/// let mut cmd = Command::cargo_bin(pkg_name!())
/// .unwrap();
/// let output = cmd.unwrap();
/// println!("{:?}", output);
Expand All @@ -125,11 +130,16 @@ where
/// ```
///
/// [`Command`]: std::process::Command
#[deprecated(
since = "2.1.0",
note = "incompatible with a custom cargo build-dir, see instead `cargo::cargo_bin!`"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am confused by this deprecation notice. Does "custom cargo build-dir" mean custom target directory, e.g., as one would specify with CARGO_TARGET_DIR?

The incompatibility is not related to changing the build directory within the target directory (e.g., target/debug/build), correct?

If it is neither of these, could you please point me to a reference?

Copy link
Contributor Author

@epage epage Oct 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rust 1.91 split a build-dir out of target-dir with the default falling back to target-dir, for now though I hope to change that in the future.

See https://doc.rust-lang.org/cargo/reference/build-cache.html

)]
fn cargo_bin<S: AsRef<str>>(name: S) -> Result<Self, CargoError>;
}

impl CommandCargoExt for crate::cmd::Command {
fn cargo_bin<S: AsRef<str>>(name: S) -> Result<Self, CargoError> {
#[allow(deprecated)]
crate::cmd::Command::cargo_bin(name)
}
}
Expand All @@ -141,6 +151,7 @@ impl CommandCargoExt for process::Command {
}

pub(crate) fn cargo_bin_cmd<S: AsRef<str>>(name: S) -> Result<process::Command, CargoError> {
#[allow(deprecated)]
let path = cargo_bin(name);
if path.is_file() {
if let Some(runner) = cargo_runner() {
Expand Down Expand Up @@ -224,6 +235,10 @@ 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
#[deprecated(
since = "2.1.0",
note = "incompatible with a custom cargo build-dir, see instead `cargo::cargo_bin!`"
)]
pub fn cargo_bin<S: AsRef<str>>(name: S) -> path::PathBuf {
cargo_bin_str(name.as_ref())
}
Expand Down
7 changes: 6 additions & 1 deletion src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ impl Command {
///
/// ```rust,no_run
/// use assert_cmd::Command;
/// use assert_cmd::pkg_name;
///
/// let mut cmd = Command::cargo_bin(env!("CARGO_PKG_NAME"))
/// let mut cmd = Command::cargo_bin(pkg_name!())
/// .unwrap();
/// let output = cmd.unwrap();
/// println!("{:?}", output);
Expand All @@ -59,6 +60,10 @@ impl Command {
/// println!("{:?}", output);
/// ```
///
#[deprecated(
since = "2.1.0",
note = "incompatible with a custom cargo build-dir, see instead `cargo::cargo_bin_cmd!`"
)]
pub fn cargo_bin<S: AsRef<str>>(name: S) -> Result<Self, crate::cargo::CargoError> {
let cmd = crate::cargo::cargo_bin_cmd(name)?;
Ok(Self::from_std(cmd))
Expand Down
60 changes: 52 additions & 8 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/// ```should_panic
/// use assert_cmd::Command;
///
/// let mut cmd = Command::cargo_bin(assert_cmd::crate_name!()).unwrap();
/// let mut cmd = Command::cargo_bin(assert_cmd::pkg_name!()).unwrap();
/// let assert = cmd
/// .arg("-A")
/// .env("stdout", "hello")
Expand All @@ -18,6 +18,15 @@
/// .stdout("hello\n");
/// ```
#[macro_export]
macro_rules! pkg_name {
() => {
env!("CARGO_PKG_NAME")
};
}

/// Deprecated, replaced with [`pkg_name`]
#[deprecated(since = "2.1.0", note = "replaced with `pkg_name!`")]
#[macro_export]
macro_rules! crate_name {
() => {
env!("CARGO_PKG_NAME")
Expand All @@ -33,18 +42,53 @@ macro_rules! crate_name {
///
/// ## Example
///
/// ```rust,no_run
/// #[test]
/// fn cli_tests() {
/// trycmd::TestCases::new()
/// .default_bin_path(trycmd::cargo_bin!("bin-fixture"))
/// .case("tests/cmd/*.trycmd");
/// }
/// ```rust,ignore
/// use assert_cmd::prelude::*;
/// use assert_cmd::cargo::cargo_bin;
///
/// use std::process::Command;
///
/// let mut cmd = Command::new(cargo_bin!())
/// .unwrap();
/// let output = cmd.unwrap();
/// ```
#[macro_export]
#[doc(hidden)]
macro_rules! cargo_bin {
() => {
$crate::cargo::cargo_bin!($crate::pkg_name!())
};
($bin_target_name:expr) => {
::std::path::Path::new(env!(concat!("CARGO_BIN_EXE_", $bin_target_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,ignore
/// use assert_cmd::prelude::*;
/// use assert_cmd::cargo::cargo_bin_cmd;
///
/// use std::process::Command;
///
/// let mut cmd = cargo_bin_cmd!()
/// .unwrap();
/// let output = cmd.unwrap();
/// ```
#[macro_export]
#[doc(hidden)]
macro_rules! cargo_bin_cmd {
() => {
$crate::cargo::cargo_bin_cmd!($crate::pkg_name!())
};
($bin_target_name:expr) => {
$crate::Command::new($crate::cargo::cargo_bin!($bin_target_name))
};
}
60 changes: 21 additions & 39 deletions tests/assert.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use std::process::Command;

use assert_cmd::cargo_bin;
use assert_cmd::prelude::*;
use predicates::prelude::*;

#[test]
fn stdout_string() {
let expected = "hello\n".to_owned();
Command::cargo_bin("bin_fixture")
.unwrap()
Command::new(cargo_bin!("bin_fixture"))
.env("stdout", "hello")
.env("stderr", "world")
.assert()
Expand All @@ -16,101 +16,88 @@ fn stdout_string() {

#[test]
fn trait_example() {
let mut cmd = Command::cargo_bin("bin_fixture").unwrap();
let mut cmd = Command::new(cargo_bin!("bin_fixture"));
cmd.assert().success();
}

#[test]
fn trait_assert_example() {
let mut cmd = Command::cargo_bin("bin_fixture").unwrap();
let mut cmd = Command::new(cargo_bin!("bin_fixture"));
cmd.assert().success();
}

#[test]
fn struct_example() {
let mut cmd = Command::cargo_bin("bin_fixture").unwrap();
let mut cmd = Command::new(cargo_bin!("bin_fixture"));
cmd.assert().success();
}

#[test]
fn append_context_example() {
Command::cargo_bin("bin_fixture")
.unwrap()
Command::new(cargo_bin!("bin_fixture"))
.assert()
.append_context("main", "no args")
.success();
}

#[test]
fn success_example() {
Command::cargo_bin("bin_fixture")
.unwrap()
.assert()
.success();
Command::new(cargo_bin!("bin_fixture")).assert().success();
}

#[test]
fn failure_example() {
Command::cargo_bin("bin_fixture")
.unwrap()
Command::new(cargo_bin!("bin_fixture"))
.env("exit", "1")
.assert()
.failure();
}

#[test]
fn code_example() {
Command::cargo_bin("bin_fixture")
.unwrap()
Command::new(cargo_bin!("bin_fixture"))
.env("exit", "42")
.assert()
.code(predicate::eq(42));

Command::cargo_bin("bin_fixture")
.unwrap()
Command::new(cargo_bin!("bin_fixture"))
.env("exit", "42")
.assert()
.code(42);

Command::cargo_bin("bin_fixture")
.unwrap()
Command::new(cargo_bin!("bin_fixture"))
.env("exit", "42")
.assert()
.code(&[2, 42] as &[i32]);
}

#[test]
fn stdout_example() {
Command::cargo_bin("bin_fixture")
.unwrap()
Command::new(cargo_bin!("bin_fixture"))
.env("stdout", "hello")
.env("stderr", "world")
.assert()
.stdout(predicate::eq(b"hello\n" as &[u8]));

Command::cargo_bin("bin_fixture")
.unwrap()
Command::new(cargo_bin!("bin_fixture"))
.env("stdout", "hello")
.env("stderr", "world")
.assert()
.stdout(predicate::str::diff("hello\n"));

Command::cargo_bin("bin_fixture")
.unwrap()
Command::new(cargo_bin!("bin_fixture"))
.env("stdout", "hello")
.env("stderr", "world")
.assert()
.stdout(b"hello\n" as &[u8]);

Command::cargo_bin("bin_fixture")
.unwrap()
Command::new(cargo_bin!("bin_fixture"))
.env("stdout", "hello")
.env("stderr", "world")
.assert()
.stdout(vec![b'h', b'e', b'l', b'l', b'o', b'\n']);

Command::cargo_bin("bin_fixture")
.unwrap()
Command::new(cargo_bin!("bin_fixture"))
.env("stdout", "hello")
.env("stderr", "world")
.assert()
Expand All @@ -119,36 +106,31 @@ fn stdout_example() {

#[test]
fn stderr_example() {
Command::cargo_bin("bin_fixture")
.unwrap()
Command::new(cargo_bin!("bin_fixture"))
.env("stdout", "hello")
.env("stderr", "world")
.assert()
.stderr(predicate::eq(b"world\n" as &[u8]));

Command::cargo_bin("bin_fixture")
.unwrap()
Command::new(cargo_bin!("bin_fixture"))
.env("stdout", "hello")
.env("stderr", "world")
.assert()
.stderr(predicate::str::diff("world\n"));

Command::cargo_bin("bin_fixture")
.unwrap()
Command::new(cargo_bin!("bin_fixture"))
.env("stdout", "hello")
.env("stderr", "world")
.assert()
.stderr(b"world\n" as &[u8]);

Command::cargo_bin("bin_fixture")
.unwrap()
Command::new(cargo_bin!("bin_fixture"))
.env("stdout", "hello")
.env("stderr", "world")
.assert()
.stderr(vec![b'w', b'o', b'r', b'l', b'd', b'\n']);

Command::cargo_bin("bin_fixture")
.unwrap()
Command::new(cargo_bin!("bin_fixture"))
.env("stdout", "hello")
.env("stderr", "world")
.assert()
Expand Down
Loading
Loading