Skip to content

Commit cf87e11

Browse files
committed
fix(test-support): check executability instead of running --version
Not every executable has a `--version` flag. Let's check the exectuability instead. Note that on Windows we check if it is file only. There is a `is_executable` crate on crates.io, though that still depend on winapi so don't bother using it.
1 parent 64a1246 commit cf87e11

File tree

1 file changed

+31
-1
lines changed
  • crates/cargo-test-macro/src

1 file changed

+31
-1
lines changed

crates/cargo-test-macro/src/lib.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,37 @@ fn check_command(command_path: &Path, args: &[&str]) -> bool {
302302
}
303303

304304
fn has_command(command: &str) -> bool {
305-
check_command(Path::new(command), &["--version"])
305+
use std::env::consts::EXE_EXTENSION;
306+
// ALLOWED: For testing cargo itself only.
307+
#[allow(clippy::disallowed_methods)]
308+
let Some(paths) = std::env::var_os("PATH") else {
309+
return false;
310+
};
311+
std::env::split_paths(&paths)
312+
.flat_map(|path| {
313+
let candidate = path.join(&command);
314+
let with_exe = if EXE_EXTENSION.is_empty() {
315+
None
316+
} else {
317+
Some(candidate.with_extension(EXE_EXTENSION))
318+
};
319+
std::iter::once(candidate).chain(with_exe)
320+
})
321+
.find(|p| is_executable(p))
322+
.is_some()
323+
}
324+
325+
#[cfg(unix)]
326+
fn is_executable<P: AsRef<Path>>(path: P) -> bool {
327+
use std::os::unix::prelude::*;
328+
std::fs::metadata(path)
329+
.map(|metadata| metadata.is_file() && metadata.permissions().mode() & 0o111 != 0)
330+
.unwrap_or(false)
331+
}
332+
333+
#[cfg(windows)]
334+
fn is_executable<P: AsRef<Path>>(path: P) -> bool {
335+
path.as_ref().is_file()
306336
}
307337

308338
fn has_rustup_stable() -> bool {

0 commit comments

Comments
 (0)