1use std::process::{Command, Stdio};
6
7pub fn require_tool(name: &str, install_hint: &str) -> Result<(), String> {
18 match Command::new(name)
19 .arg("--version")
20 .stdout(Stdio::null())
21 .stderr(Stdio::null())
22 .status()
23 {
24 Ok(s) if s.success() => Ok(()),
25 Ok(s) => Err(format!(
26 "{name} exited with {s}; is it installed correctly? \
27 Install it with: {install_hint}"
28 )),
29 Err(_) => Err(format!("{name} not found. Install it with: {install_hint}")),
30 }
31}
32
33#[cfg(test)]
34mod tests {
35 use super::*;
36
37 #[test]
38 fn require_missing_tool() {
39 let result = require_tool("nonexistent_tool_xyz_123", "magic install");
40 assert!(result.is_err());
41 let msg = result.unwrap_err();
42 assert!(msg.contains("nonexistent_tool_xyz_123"));
43 assert!(msg.contains("magic install"));
44 }
45
46 #[test]
47 fn require_available_tool() {
48 let result = require_tool("sh", "should already be installed");
52 let _ = result;
55 }
56}