use std::path::PathBuf;
use crate::platform;
pub fn find_system_p4() -> Option<PathBuf> {
if let Some(path) = search_path()
&& is_working_p4(&path)
{
return Some(path);
}
for dir in platform::default_install_dirs() {
let path = PathBuf::from(dir);
if path.exists() && is_working_p4(&path) {
return Some(path);
}
}
None
}
fn search_path() -> Option<PathBuf> {
let name = platform::binary_name();
std::env::var_os("PATH").and_then(|paths| {
for dir in std::env::split_paths(&paths) {
let candidate = dir.join(name);
if candidate.is_file() {
return Some(candidate);
}
}
None
})
}
fn is_working_p4(path: &PathBuf) -> bool {
std::process::Command::new(path)
.arg("-h")
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.status()
.map(|s| s.success())
.unwrap_or(false)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_find_system_p4_at_least_checks() {
let result = find_system_p4();
let _ = result;
}
#[test]
fn test_is_working_p4_with_bogus_path() {
let bogus = PathBuf::from("/nonexistent/p4");
assert!(!is_working_p4(&bogus));
}
}