use std::path::{Path, PathBuf};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum EnvironmentProbe {
Command { command: String, args: Vec<String> },
FileExists { path: PathBuf },
GitClean,
}
impl EnvironmentProbe {
pub fn command(command: String, args: Vec<String>) -> Self {
Self::Command { command, args }
}
pub fn file_exists(path: PathBuf) -> Self {
Self::FileExists { path }
}
pub fn git_clean() -> Self {
Self::GitClean
}
pub fn check(&self, workspace: &Path) -> bool {
match self {
Self::Command { command, args } => std::process::Command::new(command)
.args(args)
.current_dir(workspace)
.output()
.is_ok_and(|output| output.status.success()),
Self::FileExists { path } => workspace.join(path).exists(),
Self::GitClean => std::process::Command::new("git")
.args(["diff", "--quiet"])
.current_dir(workspace)
.output()
.is_ok_and(|output| output.status.success()),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn file_exists_probe_checks_both_sides() {
let workspace = std::env::current_dir().expect("current dir");
assert!(EnvironmentProbe::file_exists(workspace.clone()).check(&workspace));
let missing = workspace.join(format!("missing-probe-{}", std::process::id()));
assert!(!EnvironmentProbe::file_exists(missing).check(&workspace));
}
#[test]
fn file_exists_probe_resolves_relative_paths_against_workspace() {
let marker = format!("vtcode-probe-marker-{}", std::process::id());
let root = std::env::temp_dir().join(format!("vtcode-probe-root-{}", std::process::id()));
let workspace = root.join("worktree");
std::fs::create_dir_all(&workspace).expect("create probe workspace");
std::fs::write(workspace.join(&marker), b"present").expect("create probe marker");
assert!(EnvironmentProbe::file_exists(PathBuf::from(&marker)).check(&workspace));
assert!(EnvironmentProbe::file_exists(workspace.join(&marker)).check(&workspace));
assert!(!EnvironmentProbe::file_exists(PathBuf::from(format!("{marker}-missing"))).check(&workspace));
std::fs::remove_dir_all(&root).expect("clean up probe workspace");
}
#[cfg(unix)]
#[test]
fn command_probe_uses_exit_status() {
let workspace = std::env::current_dir().expect("current dir");
assert!(EnvironmentProbe::command("true".into(), Vec::new()).check(&workspace));
assert!(!EnvironmentProbe::command("false".into(), Vec::new()).check(&workspace));
}
}