use std::sync::Arc;
use theway_core::executor::ToolExecutor;
pub mod file_lock;
#[cfg(feature = "local")]
pub mod local;
#[cfg(feature = "sandbox")]
pub mod sandbox;
pub fn default_executor() -> Arc<dyn ToolExecutor> {
executor_for_cwd(std::env::current_dir().unwrap_or_else(|_| std::path::PathBuf::from(".")))
}
pub fn executor_for_cwd(cwd: impl Into<std::path::PathBuf>) -> Arc<dyn ToolExecutor> {
#[cfg(feature = "local")]
{
Arc::new(local::LocalExecutor::with_cwd(cwd))
}
#[cfg(all(not(feature = "local"), feature = "sandbox"))]
{
let _ = cwd.into();
Arc::new(sandbox::SandboxExecutor::new())
}
#[cfg(not(any(feature = "local", feature = "sandbox")))]
{
compile_error!("theway-daemon requires at least one of the `local` or `sandbox` features");
#[allow(unreachable_code)]
unreachable!()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(feature = "local")]
#[tokio::test]
async fn executor_for_cwd_roots_local_executor_at_cwd() {
let dir = tempfile::tempdir().unwrap();
std::fs::write(dir.path().join("probe.txt"), "ok").unwrap();
let executor = executor_for_cwd(dir.path());
assert_eq!(
executor.kind().await,
theway_core::executor::ExecutorKind::Local
);
assert_eq!(
executor
.read_file(std::path::Path::new("probe.txt"))
.await
.unwrap(),
"ok"
);
}
#[cfg(all(not(feature = "local"), feature = "sandbox"))]
#[tokio::test]
async fn executor_for_cwd_returns_sandbox_stub() {
let dir = tempfile::tempdir().unwrap();
let executor = executor_for_cwd(dir.path());
assert_eq!(
executor.kind().await,
theway_core::executor::ExecutorKind::Sandbox
);
}
}