diff --git a/src/cargo.rs b/src/cargo.rs index 1321602..22939ad 100644 --- a/src/cargo.rs +++ b/src/cargo.rs @@ -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 @@ -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>(name: S) -> path::PathBuf { cargo_bin_str(name.as_ref()) } @@ -261,6 +269,9 @@ help: available binary names are {names}" fn legacy_cargo_bin(name: &str) -> Option { 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) } @@ -277,3 +288,10 @@ fn target_dir() -> Option { /// 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"); +} diff --git a/src/cmd.rs b/src/cmd.rs index 0df8c0b..00f0da3 100644 --- a/src/cmd.rs +++ b/src/cmd.rs @@ -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 diff --git a/tests/cargo.rs b/tests/cargo.rs index 19838c3..5931ee0 100644 --- a/tests/cargo.rs +++ b/tests/cargo.rs @@ -40,7 +40,8 @@ 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(); @@ -48,7 +49,8 @@ fn trait_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 cargo_bin_example_1() { let mut cmd = Command::cargo_bin(pkg_name!()).unwrap(); let output = cmd.unwrap();