vtcode_eval/
environment.rs1use std::path::{Path, PathBuf};
9
10#[derive(Debug, Clone, PartialEq, Eq)]
12pub enum EnvironmentProbe {
13 Command { command: String, args: Vec<String> },
15 FileExists { path: PathBuf },
17 GitClean,
19}
20
21impl EnvironmentProbe {
22 pub fn command(command: String, args: Vec<String>) -> Self {
24 Self::Command { command, args }
25 }
26
27 pub fn file_exists(path: PathBuf) -> Self {
29 Self::FileExists { path }
30 }
31
32 pub fn git_clean() -> Self {
34 Self::GitClean
35 }
36
37 pub fn check(&self, workspace: &Path) -> bool {
42 match self {
43 Self::Command { command, args } => std::process::Command::new(command)
44 .args(args)
45 .current_dir(workspace)
46 .output()
47 .is_ok_and(|output| output.status.success()),
48 Self::FileExists { path } => workspace.join(path).exists(),
51 Self::GitClean => std::process::Command::new("git")
52 .args(["diff", "--quiet"])
53 .current_dir(workspace)
54 .output()
55 .is_ok_and(|output| output.status.success()),
56 }
57 }
58}
59
60#[cfg(test)]
61mod tests {
62 use super::*;
63
64 #[test]
65 fn file_exists_probe_checks_both_sides() {
66 let workspace = std::env::current_dir().expect("current dir");
67 assert!(EnvironmentProbe::file_exists(workspace.clone()).check(&workspace));
68
69 let missing = workspace.join(format!("missing-probe-{}", std::process::id()));
70 assert!(!EnvironmentProbe::file_exists(missing).check(&workspace));
71 }
72
73 #[test]
74 fn file_exists_probe_resolves_relative_paths_against_workspace() {
75 let marker = format!("vtcode-probe-marker-{}", std::process::id());
76 let root = std::env::temp_dir().join(format!("vtcode-probe-root-{}", std::process::id()));
77 let workspace = root.join("worktree");
78 std::fs::create_dir_all(&workspace).expect("create probe workspace");
79 std::fs::write(workspace.join(&marker), b"present").expect("create probe marker");
80
81 assert!(EnvironmentProbe::file_exists(PathBuf::from(&marker)).check(&workspace));
83 assert!(EnvironmentProbe::file_exists(workspace.join(&marker)).check(&workspace));
85 assert!(!EnvironmentProbe::file_exists(PathBuf::from(format!("{marker}-missing"))).check(&workspace));
87
88 std::fs::remove_dir_all(&root).expect("clean up probe workspace");
89 }
90
91 #[cfg(unix)]
92 #[test]
93 fn command_probe_uses_exit_status() {
94 let workspace = std::env::current_dir().expect("current dir");
95 assert!(EnvironmentProbe::command("true".into(), Vec::new()).check(&workspace));
96 assert!(!EnvironmentProbe::command("false".into(), Vec::new()).check(&workspace));
97 }
98}