use std::path::PathBuf;
use which::which;
use crate::Error;
pub fn get_command(command: &str) -> Result<PathBuf, Error> {
which(command).map_err(|source| Error::ExecutableNotFound {
command: command.to_string(),
source,
})
}
#[cfg(test)]
#[cfg(target_os = "linux")]
mod tests {
use rstest::rstest;
use testresult::TestResult;
use super::*;
#[rstest]
#[case("whoami")]
#[case("/usr/bin/whoami")]
fn get_command_succeeds(#[case] cmd: &str) -> TestResult {
get_command(cmd)?;
Ok(())
}
#[test]
fn get_command_fails() -> TestResult {
let bogus_cmd = "d202d7951df2c4b711ca44b4bcc9d7b363fa4252127e058c1a910ec05b6cd038d71cc21221c031c0359f993e746b07f5965cf8c5c3746a58337ad9ab65278e77";
if let Ok(path) = get_command(bogus_cmd) {
panic!("The command {bogus_cmd} shouldn't exist, but {path:?} is found!");
}
Ok(())
}
}