#![allow(dead_code)]
use std::path::{Path, PathBuf};
pub(crate) fn should_run_e2e() -> bool {
if std::env::var_os("XCHECKER_E2E").is_none() {
return false;
}
claude_stub_path().is_some()
}
pub(crate) struct CwdGuard(PathBuf);
impl CwdGuard {
pub fn new(to: &Path) -> std::io::Result<Self> {
let prev = std::env::current_dir()?;
std::env::set_current_dir(to)?;
Ok(Self(prev))
}
}
impl Drop for CwdGuard {
fn drop(&mut self) {
let _ = std::env::set_current_dir(&self.0);
}
}
pub(crate) struct EnvVarGuard {
key: String,
original: Option<std::ffi::OsString>,
}
impl EnvVarGuard {
pub fn set(key: &str, value: &str) -> Self {
let original = std::env::var_os(key);
unsafe {
std::env::set_var(key, value);
}
Self {
key: key.to_string(),
original,
}
}
pub fn cleared(key: &str) -> Self {
let original = std::env::var_os(key);
unsafe {
std::env::remove_var(key);
}
Self {
key: key.to_string(),
original,
}
}
}
impl Drop for EnvVarGuard {
fn drop(&mut self) {
match &self.original {
Some(value) => unsafe { std::env::set_var(&self.key, value) },
None => unsafe { std::env::remove_var(&self.key) },
}
}
}
pub(crate) fn claude_stub_path() -> Option<String> {
if let Ok(path) = std::env::var("CARGO_BIN_EXE_claude_stub") {
return Some(path);
}
if let Ok(path) = std::env::var("CARGO_BIN_EXE_claude-stub") {
return Some(path);
}
if let Ok(manifest_dir) = std::env::var("CARGO_MANIFEST_DIR") {
let root = PathBuf::from(manifest_dir);
let candidates = [
root.join("target/debug/claude-stub"),
root.join("target/debug/claude-stub.exe"),
root.join("target/release/claude-stub"),
root.join("target/release/claude-stub.exe"),
root.join("../target/debug/claude-stub"),
root.join("../target/debug/claude-stub.exe"),
root.join("../target/release/claude-stub"),
root.join("../target/release/claude-stub.exe"),
];
for candidate in &candidates {
if candidate.exists() {
return Some(candidate.to_string_lossy().to_string());
}
}
}
if let Ok(path) = which::which("claude-stub") {
return Some(path.to_string_lossy().to_string());
}
let current_dir = std::env::current_dir().ok()?;
let mut dir = current_dir.as_path();
loop {
let debug_path = dir.join("target/debug/claude-stub");
if debug_path.exists() {
return Some(debug_path.to_string_lossy().to_string());
}
let release_path = dir.join("target/release/claude-stub");
if release_path.exists() {
return Some(release_path.to_string_lossy().to_string());
}
match dir.parent() {
Some(parent) => dir = parent,
None => break,
}
}
None
}