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
18 changes: 18 additions & 0 deletions src/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ where
/// - `>=1.91,<=1.93`: works with default `build-dir`
/// - `<=1.92`: works
///
/// # Panic
///
/// Panicks if no binary is found
///
/// # Examples
///
/// ```rust,no_run
Expand Down Expand Up @@ -220,6 +224,10 @@ impl fmt::Display for NotFoundError {
/// - `>1.94`: works
/// - `>=1.91,<=1.93`: works with default `build-dir`
/// - `<=1.92`: works
///
/// # Panic
///
/// Panicks if no binary is found
pub fn cargo_bin<S: AsRef<str>>(name: S) -> path::PathBuf {
cargo_bin_str(name.as_ref())
}
Expand Down Expand Up @@ -261,6 +269,9 @@ help: available binary names are {names}"
fn legacy_cargo_bin(name: &str) -> Option<path::PathBuf> {
let target_dir = target_dir()?;
let bin_path = target_dir.join(format!("{}{}", name, env::consts::EXE_SUFFIX));
if !bin_path.exists() {
return None;
}
Some(bin_path)
}

Expand All @@ -277,3 +288,10 @@ fn target_dir() -> Option<path::PathBuf> {

/// The current process' target triplet.
const CURRENT_TARGET: &str = include_str!(concat!(env!("OUT_DIR"), "/current_target.txt"));

#[test]
#[should_panic = "`CARGO_BIN_EXE_non-existent` is unset
help: if this is running within a unit test, move it to an integration test to gain access to `CARGO_BIN_EXE_non-existent`"]
fn cargo_bin_in_unit_test() {
cargo_bin("non-existent");
}
4 changes: 4 additions & 0 deletions src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ impl Command {
/// - `>=1.91,<=1.93`: works with default `build-dir`
/// - `<=1.92`: works
///
/// # Panic
///
/// Panicks if no binary is found
///
/// # Examples
///
/// ```rust,no_run
Expand Down
6 changes: 4 additions & 2 deletions tests/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,17 @@ fn mod_example() {
}

#[test]
#[should_panic] // No bin named `assert_cmd`
#[should_panic = "`CARGO_BIN_EXE_assert_cmd` is unset
help: available binary names are \"bin_fixture\""]
fn trait_example() {
let mut cmd = Command::cargo_bin(pkg_name!()).unwrap();
let output = cmd.unwrap();
println!("{output:?}");
}

#[test]
#[should_panic] // No bin named `assert_cmd`
#[should_panic = "`CARGO_BIN_EXE_assert_cmd` is unset
help: available binary names are \"bin_fixture\""]
fn cargo_bin_example_1() {
let mut cmd = Command::cargo_bin(pkg_name!()).unwrap();
let output = cmd.unwrap();
Expand Down
Loading