use std::path::{Path, PathBuf};
use isothrum_machine::HostOs;
use isothrum_machine::host::tool_path::{self, Env};
use crate::here;
const TOOLS: [&str; 7] = ["docker", "vagrant", "VBoxManage", "utmctl", "ovftool", "virsh", "colima"];
fn program(os: HostOs, name: &str) -> String {
if os == HostOs::Windows { format!("{name}.exe") } else { name.to_owned() }
}
fn found_in<'a>(dirs: &'a [PathBuf], os: HostOs, name: &str) -> Option<&'a Path> {
dirs.iter().map(PathBuf::as_path).find(|d| d.join(program(os, name)).is_file())
}
fn bare(os: HostOs) -> Vec<PathBuf> {
match os {
HostOs::Windows => vec![PathBuf::from(r"C:\Windows\system32")],
_ => ["/usr/bin", "/bin", "/usr/sbin", "/sbin"].map(PathBuf::from).to_vec(),
}
}
#[tokio::test]
#[ignore = "real machine: cargo xtask e2e --tier machine"]
async fn the_tools_installed_here_are_on_the_path_an_app_works_out() {
let cx = here().await;
let os = cx.host.os;
let mine = Env::here();
let env = Env {
path: bare(os),
..mine.clone()
};
let worked_out = match os {
HostOs::Windows => tool_path::refreshed(&cx, &env).await,
_ => tool_path::augmented(os, &env, &[]),
}
.unwrap_or_else(|| env.path.clone());
let known: Vec<PathBuf> = tool_path::tool_dirs(os, &mine).into_iter().chain(bare(os)).collect();
let mut checked = Vec::new();
for name in TOOLS {
let Some(at) = found_in(&mine.path, os, name).or_else(|| found_in(&known, os, name)) else {
continue;
};
if !known.iter().any(|d| d == at) && os != HostOs::Windows {
println!("{name}: in {} only, a folder of this account's own", at.display());
continue;
}
println!("{name}: in {}", at.display());
assert!(found_in(&worked_out, os, name).is_some(), "{name} (in {}) is on {worked_out:?}", at.display());
checked.push(name);
}
if checked.is_empty() {
skip!("none of {TOOLS:?} is installed here")
}
}
#[tokio::test]
#[ignore = "real machine: cargo xtask e2e --tier machine"]
async fn the_registry_path_is_read_back_on_windows() {
let cx = here().await;
if cx.host.os != HostOs::Windows {
skip!("the registry's PATH is Windows'")
}
let env = Env {
path: Vec::new(),
..Env::here()
};
let refreshed = tool_path::refreshed(&cx, &env).await.expect("the registry's PATH");
println!("{refreshed:#?}");
let system = env.var("SystemRoot").map(|r| Path::new(r).join("system32")).expect("SystemRoot");
let lower = |p: &Path| p.to_string_lossy().trim_end_matches('\\').to_lowercase();
assert!(refreshed.iter().any(|d| lower(d) == lower(&system)), "{} is on {refreshed:?}", system.display());
}