mirror of
https://github.com/rust-lang/cargo.git
synced 2025-09-25 11:14:46 +00:00
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.
This commit is contained in:
parent
64a1246070
commit
cf87e117ae
@ -302,7 +302,37 @@ fn check_command(command_path: &Path, args: &[&str]) -> bool {
|
||||
}
|
||||
|
||||
fn has_command(command: &str) -> bool {
|
||||
check_command(Path::new(command), &["--version"])
|
||||
use std::env::consts::EXE_EXTENSION;
|
||||
// ALLOWED: For testing cargo itself only.
|
||||
#[allow(clippy::disallowed_methods)]
|
||||
let Some(paths) = std::env::var_os("PATH") else {
|
||||
return false;
|
||||
};
|
||||
std::env::split_paths(&paths)
|
||||
.flat_map(|path| {
|
||||
let candidate = path.join(&command);
|
||||
let with_exe = if EXE_EXTENSION.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(candidate.with_extension(EXE_EXTENSION))
|
||||
};
|
||||
std::iter::once(candidate).chain(with_exe)
|
||||
})
|
||||
.find(|p| is_executable(p))
|
||||
.is_some()
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
fn is_executable<P: AsRef<Path>>(path: P) -> bool {
|
||||
use std::os::unix::prelude::*;
|
||||
std::fs::metadata(path)
|
||||
.map(|metadata| metadata.is_file() && metadata.permissions().mode() & 0o111 != 0)
|
||||
.unwrap_or(false)
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
fn is_executable<P: AsRef<Path>>(path: P) -> bool {
|
||||
path.as_ref().is_file()
|
||||
}
|
||||
|
||||
fn has_rustup_stable() -> bool {
|
||||
|
Loading…
x
Reference in New Issue
Block a user