pub struct ScriptedRunner { /* private fields */ }Expand description
A ProcessRunner that returns canned Replys for matched commands.
Rules are tried in registration order; the first match wins. With no match,
the fallback reply is used, or an error is returned.
§Example
Drive a command through scripted replies — hermetic (no real subprocess), so
this example actually runs in cargo test on every OS:
use processkit::{Command, ProcessRunner};
use processkit::testing::{Reply, ScriptedRunner};
let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap();
rt.block_on(async {
let runner = ScriptedRunner::new()
.on(["tool", "--version"], Reply::ok("tool 1.2.3"))
.fallback(Reply::fail(1, "unexpected command"));
let out = runner
.output_string(&Command::new("tool").arg("--version"))
.await
.expect("scripted reply");
assert!(out.is_success());
assert_eq!(out.stdout().trim(), "tool 1.2.3");
});Implementations§
Source§impl ScriptedRunner
impl ScriptedRunner
Sourcepub fn on<I, S>(self, prefix: I, reply: Reply) -> Self
pub fn on<I, S>(self, prefix: I, reply: Reply) -> Self
Reply with reply when the command’s program name + arguments start
with prefix (the first element is the program). For example
.on(["git", "status"]) answers for git status …, not rm status.
Sourcepub fn on_sequence<I, S, R>(self, prefix: I, replies: R) -> Self
pub fn on_sequence<I, S, R>(self, prefix: I, replies: R) -> Self
Reply with each of replies in turn — the first match gets the first
reply, the second the second, and so on; once exhausted, the last
reply repeats forever. The declarative form for retry scenarios
(fail once, then succeed), matching a cassette’s “replay in order, then
repeat the last” model. Matches like on (program + arg
prefix).
The sequence advances once per matching call via a relaxed atomic counter, so the “first call gets reply 0, second gets reply 1, …” ordering is well-defined only for sequential calls. Concurrent calls to the same rule still each get a distinct, in-bounds reply, but which call sees which reply is unspecified — don’t rely on the order across overlapping tasks.
§Panics
If replies is empty.
Trait Implementations§
Source§impl Debug for ScriptedRunner
impl Debug for ScriptedRunner
Source§impl Default for ScriptedRunner
impl Default for ScriptedRunner
Source§fn default() -> ScriptedRunner
fn default() -> ScriptedRunner
Source§impl ProcessRunner for ScriptedRunner
impl ProcessRunner for ScriptedRunner
Source§fn start<'life0, 'life1, 'async_trait>(
&'life0 self,
command: &'life1 Command,
) -> Pin<Box<dyn Future<Output = Result<RunningProcess>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn start<'life0, 'life1, 'async_trait>(
&'life0 self,
command: &'life1 Command,
) -> Pin<Box<dyn Future<Output = Result<RunningProcess>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Start a scripted live handle: the canned stdout/stderr flow through the
command’s real pump machinery (handlers, encodings, buffer policy),
so stdout_lines / wait_for_line / finish behave exactly
as on a real child — no subprocess involved.
Source§fn output_string<'life0, 'life1, 'async_trait>(
&'life0 self,
command: &'life1 Command,
) -> Pin<Box<dyn Future<Output = Result<ProcessResult<String>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn output_string<'life0, 'life1, 'async_trait>(
&'life0 self,
command: &'life1 Command,
) -> Pin<Box<dyn Future<Output = Result<ProcessResult<String>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
command to completion, capturing stdout/stderr and the exit code.
A non-zero exit is reported in the result, not raised.Source§fn output_bytes<'life0, 'life1, 'async_trait>(
&'life0 self,
command: &'life1 Command,
) -> Pin<Box<dyn Future<Output = Result<ProcessResult<Vec<u8>>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn output_bytes<'life0, 'life1, 'async_trait>(
&'life0 self,
command: &'life1 Command,
) -> Pin<Box<dyn Future<Output = Result<ProcessResult<Vec<u8>>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
command to completion, capturing stdout as raw bytes (output_string
captures it as lossy-UTF-8 text); stderr is still text. For binary tools
— git cat-file, tar -c, an image transcoder — whose stdout is not
UTF-8. Read moreAuto Trait Implementations§
impl !RefUnwindSafe for ScriptedRunner
impl !UnwindSafe for ScriptedRunner
impl Freeze for ScriptedRunner
impl Send for ScriptedRunner
impl Sync for ScriptedRunner
impl Unpin for ScriptedRunner
impl UnsafeUnpin for ScriptedRunner
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> ProcessRunnerExt for Twhere
T: ProcessRunner + ?Sized,
impl<T> ProcessRunnerExt for Twhere
T: ProcessRunner + ?Sized,
Source§fn run<'life0, 'life1, 'async_trait>(
&'life0 self,
command: &'life1 Command,
) -> Pin<Box<dyn Future<Output = Result<String>> + Send + 'async_trait>>where
Self: Sync + 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn run<'life0, 'life1, 'async_trait>(
&'life0 self,
command: &'life1 Command,
) -> Pin<Box<dyn Future<Output = Result<String>> + Send + 'async_trait>>where
Self: Sync + 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
0 by default, widened by Command::ok_codes;
any other code is Error::Exit.Source§fn run_unit<'life0, 'life1, 'async_trait>(
&'life0 self,
command: &'life1 Command,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: Sync + 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn run_unit<'life0, 'life1, 'async_trait>(
&'life0 self,
command: &'life1 Command,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: Sync + 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
0, or any code in
Command::ok_codes), discard the output.Source§fn exit_code<'life0, 'life1, 'async_trait>(
&'life0 self,
command: &'life1 Command,
) -> Pin<Box<dyn Future<Output = Result<i32>> + Send + 'async_trait>>where
Self: Sync + 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn exit_code<'life0, 'life1, 'async_trait>(
&'life0 self,
command: &'life1 Command,
) -> Pin<Box<dyn Future<Output = Result<i32>> + Send + 'async_trait>>where
Self: Sync + 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Error::Timeout, a
signal-kill as Error::Signalled — rather than a
synthetic sentinel, mirroring
ensure_success.Source§fn probe<'life0, 'life1, 'async_trait>(
&'life0 self,
command: &'life1 Command,
) -> Pin<Box<dyn Future<Output = Result<bool>> + Send + 'async_trait>>where
Self: Sync + 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn probe<'life0, 'life1, 'async_trait>(
&'life0 self,
command: &'life1 Command,
) -> Pin<Box<dyn Future<Output = Result<bool>> + Send + 'async_trait>>where
Self: Sync + 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
0 →
Ok(true), exit 1 → Ok(false), anything else → Err (other code as
Error::Exit, timeout as
Error::Timeout, signal-kill as
Error::Signalled). For
commands whose exit code is the answer — git diff --quiet, grep -q, …Source§fn checked<'life0, 'life1, 'async_trait>(
&'life0 self,
command: &'life1 Command,
) -> Pin<Box<dyn Future<Output = Result<ProcessResult<String>>> + Send + 'async_trait>>where
Self: Sync + 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn checked<'life0, 'life1, 'async_trait>(
&'life0 self,
command: &'life1 Command,
) -> Pin<Box<dyn Future<Output = Result<ProcessResult<String>>> + Send + 'async_trait>>where
Self: Sync + 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
0 by default, widened by
Command::ok_codes), and return the full
captured result (untrimmed stdout). The building block for the
parse/try_parse helpers — use it when you need the whole
ProcessResult after success-checking, rather than just trimmed stdout
(run) or the raw result (output_string). Read moreSource§fn parse<'life0, 'life1, 'async_trait, T, F>(
&'life0 self,
command: &'life1 Command,
parse: F,
) -> Pin<Box<dyn Future<Output = Result<T>> + Send + 'async_trait>>
fn parse<'life0, 'life1, 'async_trait, T, F>( &'life0 self, command: &'life1 Command, parse: F, ) -> Pin<Box<dyn Future<Output = Result<T>> + Send + 'async_trait>>
parse closure — the shape of struct-returning CLI
commands (git/jj --format output). Built on checked,
but unlike it, fails loud on a bounded-buffer truncation so the
parser never silently sees a clipped tail; returns the parsed value. Read moreSource§fn try_parse<'life0, 'life1, 'async_trait, T, F>(
&'life0 self,
command: &'life1 Command,
parse: F,
) -> Pin<Box<dyn Future<Output = Result<T>> + Send + 'async_trait>>
fn try_parse<'life0, 'life1, 'async_trait, T, F>( &'life0 self, command: &'life1 Command, parse: F, ) -> Pin<Box<dyn Future<Output = Result<T>> + Send + 'async_trait>>
parse closure — the shape of JSON deserialization, where a
parse failure becomes Error::Parse (or whatever
error the closure returns). Like parse it is built on
checked, fails loud on truncation, and — being generic
over F — cannot be dispatched through a dyn ProcessRunnerExt object
(the trait isn’t object-safe), though it is callable on a
&dyn ProcessRunner. The Command::try_parse /
CliClient::try_parse wrappers are the
ergonomic path.Source§fn first_line<'life0, 'life1, 'async_trait, F>(
&'life0 self,
command: &'life1 Command,
predicate: F,
) -> Pin<Box<dyn Future<Output = Result<Option<String>>> + Send + 'async_trait>>
fn first_line<'life0, 'life1, 'async_trait, F>( &'life0 self, command: &'life1 Command, predicate: F, ) -> Pin<Box<dyn Future<Output = Result<Option<String>>> + Send + 'async_trait>>
command’s stdout and return the first line matching predicate
(None if the stream ends first), bounded by the command’s
timeout: a Some deadline surfaces as
Error::Timeout and tears the process down. On an
own-group runner (JobRunner, the default) that teardown covers the
whole tree; on a shared ProcessGroup it reaches
the run’s direct child by pid — a forking child’s grandchildren (and, on the
Linux cgroup mechanism, a direct child that catches the graceful signal and
closes stdout but keeps running) may outlive the probe until the group is
dropped. Bound such a run with a whole-chain owner instead. Read more