Files
kyle 57734ebb9a Replace the runner stub with the Runtime seam and RunSpec
Implemented-By: OpenCode session (model recorded in docs/implementer-log.md)
2026-09-19 16:43:56 -07:00

71 lines
1.9 KiB
Rust

//! A runtime that records what it is asked to run, for the runner and broker tests. Do not edit.
//!
//! Included with `#[path = "support/runtime.rs"] mod runtime;`.
#![allow(dead_code)] // each test file uses a different part of this module
use std::sync::{Arc, Mutex};
use brokerd::args::{ToolArgs, ToolName};
use brokerd::runner::{Mount, RunError, RunOutput, RunSpec, Runtime};
/// What one `run` was given, copied out of the `RunSpec`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Seen {
pub tool: ToolName,
pub arguments: ToolArgs,
pub mounts: Vec<Mount>,
pub egress: Option<Vec<String>>,
}
pub struct Recording {
seen: Mutex<Vec<Seen>>,
answer: Result<RunOutput, RunError>,
}
impl Recording {
/// Answers every call with `content`, not truncated.
pub fn answering(content: &str) -> Arc<Recording> {
Recording::with(Ok(RunOutput {
content: content.to_string(),
truncated: false,
}))
}
pub fn with(answer: Result<RunOutput, RunError>) -> Arc<Recording> {
Arc::new(Recording {
seen: Mutex::new(Vec::new()),
answer,
})
}
pub fn seen(&self) -> Vec<Seen> {
self.seen.lock().unwrap().clone()
}
pub fn count(&self) -> usize {
self.seen.lock().unwrap().len()
}
}
impl Runtime for Recording {
fn run(&self, spec: &RunSpec) -> Result<RunOutput, RunError> {
self.seen.lock().unwrap().push(Seen {
tool: spec.tool(),
arguments: spec.arguments().clone(),
mounts: spec.mounts().to_vec(),
egress: spec.egress().map(<[String]>::to_vec),
});
self.answer.clone()
}
}
/// Lets a test keep its `Arc<Recording>` while the broker owns a `Box<dyn Runtime>`.
pub struct Shared(pub Arc<Recording>);
impl Runtime for Shared {
fn run(&self, spec: &RunSpec) -> Result<RunOutput, RunError> {
self.0.run(spec)
}
}