Skip to content

Commit ebd79f1

Browse files
committed
fix(cargo): Panic, rather than return bad path
The application will error either way so this should be fine?
1 parent 79c9b0d commit ebd79f1

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

src/cargo.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ where
107107
/// - `>=1.91,<=1.93`: works with default `build-dir`
108108
/// - `<=1.92`: works
109109
///
110+
/// # Panic
111+
///
112+
/// Panicks if no binary is found
113+
///
110114
/// # Examples
111115
///
112116
/// ```rust,no_run
@@ -220,6 +224,10 @@ impl fmt::Display for NotFoundError {
220224
/// - `>1.94`: works
221225
/// - `>=1.91,<=1.93`: works with default `build-dir`
222226
/// - `<=1.92`: works
227+
///
228+
/// # Panic
229+
///
230+
/// Panicks if no binary is found
223231
pub fn cargo_bin<S: AsRef<str>>(name: S) -> path::PathBuf {
224232
cargo_bin_str(name.as_ref())
225233
}
@@ -261,6 +269,9 @@ help: available binary names are {names}"
261269
fn legacy_cargo_bin(name: &str) -> Option<path::PathBuf> {
262270
let target_dir = target_dir()?;
263271
let bin_path = target_dir.join(format!("{}{}", name, env::consts::EXE_SUFFIX));
272+
if !bin_path.exists() {
273+
return None;
274+
}
264275
Some(bin_path)
265276
}
266277

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

278289
/// The current process' target triplet.
279290
const CURRENT_TARGET: &str = include_str!(concat!(env!("OUT_DIR"), "/current_target.txt"));
291+
292+
#[test]
293+
#[should_panic = "`CARGO_BIN_EXE_non-existent` is unset
294+
help: if this is running within a unit test, move it to an integration test to gain access to `CARGO_BIN_EXE_non-existent`"]
295+
fn cargo_bin_in_unit_test() {
296+
cargo_bin("non-existent");
297+
}

src/cmd.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ impl Command {
4242
/// - `>=1.91,<=1.93`: works with default `build-dir`
4343
/// - `<=1.92`: works
4444
///
45+
/// # Panic
46+
///
47+
/// Panicks if no binary is found
48+
///
4549
/// # Examples
4650
///
4751
/// ```rust,no_run

tests/cargo.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,17 @@ fn mod_example() {
4040
}
4141

4242
#[test]
43-
#[should_panic] // No bin named `assert_cmd`
43+
#[should_panic = "`CARGO_BIN_EXE_assert_cmd` is unset
44+
help: available binary names are \"bin_fixture\""]
4445
fn trait_example() {
4546
let mut cmd = Command::cargo_bin(pkg_name!()).unwrap();
4647
let output = cmd.unwrap();
4748
println!("{output:?}");
4849
}
4950

5051
#[test]
51-
#[should_panic] // No bin named `assert_cmd`
52+
#[should_panic = "`CARGO_BIN_EXE_assert_cmd` is unset
53+
help: available binary names are \"bin_fixture\""]
5254
fn cargo_bin_example_1() {
5355
let mut cmd = Command::cargo_bin(pkg_name!()).unwrap();
5456
let output = cmd.unwrap();

0 commit comments

Comments
 (0)