use crate::tool::{AdmittedTool, ExecutionContext, OutputLimits, VersionSpec};
use std::{
ffi::OsString,
fs,
path::{Path, PathBuf},
sync::{Mutex, MutexGuard},
time::Duration,
};
static PROCESS_FIXTURES: Mutex<()> = Mutex::new(());
pub const LIMITS: OutputLimits = OutputLimits {
stdout: crate::tool::OutputLimit::Terminate(256 * 1024),
stderr: crate::tool::OutputLimit::Terminate(256 * 1024),
timeout: Some(Duration::from_secs(5)),
};
pub const VERSION: &str = "test-host-tool version 1";
pub struct Fixture {
pub root: PathBuf,
_guard: MutexGuard<'static, ()>,
}
impl Fixture {
pub fn new() -> Self {
let guard = PROCESS_FIXTURES
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
let root = ic_host_fs::testing::create_fixture_directory(
&std::env::temp_dir(),
"ic-host-process-test",
)
.expect("create process fixture");
Self {
root,
_guard: guard,
}
}
pub fn context<'a>(&'a self, environment: &'a [(OsString, OsString)]) -> ExecutionContext<'a> {
ExecutionContext {
current_dir: &self.root,
environment,
}
}
pub fn tool(&self) -> AdmittedTool {
admit(&tool_path(), &self.context(&[]), VERSION).unwrap()
}
}
impl Drop for Fixture {
fn drop(&mut self) {
if std::thread::panicking() {
eprintln!("Failed process fixture retained: {}", self.root.display());
return;
}
let _ = fs::remove_dir_all(&self.root);
}
}
pub fn tool_path() -> PathBuf {
Path::new(concat!(
env!("CARGO_MANIFEST_DIR"),
"/tests/fixtures/tool.sh"
))
.canonicalize()
.unwrap()
}
pub fn admit(
path: &Path,
context: &ExecutionContext<'_>,
version: &str,
) -> Result<AdmittedTool, crate::tool::ToolError> {
AdmittedTool::admit_version(
&VersionSpec {
executable: path,
executable_bytes: 1024 * 1024,
version_arguments: &[OsString::from("--version")],
version_identity: version,
},
context,
LIMITS,
)
}