use std::future::Future;
use std::path::Path;
use std::time::Duration;
use isothrum_runner::{ProcessRunner, ProcessSpec, Stream};
use crate::error::{Error, Result};
pub const STATUS_TIMEOUT: Duration = Duration::from_secs(25);
pub async fn read(
runner: &dyn ProcessRunner,
program: &str,
args: &[&str],
cwd: Option<&Path>,
) -> Result<String> {
Ok(collect(runner, &spec(program, args, cwd)).await?.out)
}
pub async fn run(
runner: &dyn ProcessRunner,
program: &str,
args: &[&str],
cwd: Option<&Path>,
) -> Result<String> {
let s = spec(program, args, cwd);
let done = collect(runner, &s).await?;
if done.ok {
return Ok(done.out);
}
let reason = done
.err
.lines()
.last()
.or_else(|| done.out.lines().last())
.unwrap_or("exited non-zero")
.to_owned();
Err(Error::Exit {
command: s.line(),
reason,
})
}
pub async fn run_read(
runner: &dyn ProcessRunner,
program: &str,
args: &[&str],
cwd: Option<&Path>,
) -> Result<String> {
within(program, STATUS_TIMEOUT, run(runner, program, args, cwd)).await
}
pub async fn stream(
runner: &dyn ProcessRunner,
program: &str,
args: &[&str],
cwd: Option<&Path>,
mut on_line: impl FnMut(&str) + Send,
) -> Result<()> {
let s = spec(program, args, cwd);
let mut last_err = String::new();
let exit = runner
.run(&s, &mut |stream, line| {
if stream == Stream::Stderr {
line.clone_into(&mut last_err);
}
on_line(line);
})
.await?;
if exit.success() {
return Ok(());
}
let reason = if last_err.is_empty() {
"exited non-zero".to_owned()
} else {
last_err
};
Err(Error::Exit {
command: s.line(),
reason,
})
}
pub async fn within<T>(
what: &str,
limit: Duration,
future: impl Future<Output = Result<T>>,
) -> Result<T> {
match tokio::time::timeout(limit, future).await {
Ok(result) => result,
Err(_elapsed) => Err(Error::Timeout {
what: what.to_owned(),
limit,
}),
}
}
fn spec(program: &str, args: &[&str], cwd: Option<&Path>) -> ProcessSpec {
let mut s = ProcessSpec::new(program, args.iter().copied());
s.cwd = cwd.map(Path::to_path_buf);
s
}
struct Done {
out: String,
err: String,
ok: bool,
}
async fn collect(runner: &dyn ProcessRunner, spec: &ProcessSpec) -> Result<Done> {
let mut out = String::new();
let mut err = String::new();
let exit = runner
.run(spec, &mut |stream, line| {
let buffer = if stream == Stream::Stdout {
&mut out
} else {
&mut err
};
buffer.push_str(line);
buffer.push('\n');
})
.await?;
Ok(Done {
out,
err,
ok: exit.success(),
})
}
#[cfg(test)]
mod tests {
use isothrum_runner::process::fake::{FakeProcesses, Reply};
use super::*;
#[tokio::test]
async fn read_returns_the_output_whatever_the_exit() {
let fake = FakeProcesses::rules(&[("tool one", Reply::ok("line-a\nline-b"))]);
let out = read(&fake, "tool", &["one"], None).await.unwrap();
assert!(out.contains("line-a") && out.contains("line-b"), "{out}");
}
#[tokio::test]
async fn run_fails_with_the_command_and_its_reason_on_a_non_zero_exit() {
let fake = FakeProcesses::rules(&[("tool go", Reply::fail(2, "the reason"))]);
let err = run(&fake, "tool", &["go"], None).await.unwrap_err();
let Error::Exit { command, reason } = err else {
panic!("{err:?}")
};
assert_eq!(command, "tool go");
assert_eq!(reason, "the reason");
}
#[tokio::test]
async fn stream_forwards_each_line_and_fails_with_the_last_error_line() {
let fake = FakeProcesses::rules(&[("tool go", Reply::ok("one\ntwo"))]);
let mut lines = Vec::new();
stream(&fake, "tool", &["go"], None, |l| lines.push(l.to_owned()))
.await
.unwrap();
assert_eq!(lines, ["one", "two"]);
let bad = FakeProcesses::rules(&[("tool x", Reply::fail(1, "boom"))]);
let err = stream(&bad, "tool", &["x"], None, |_| {})
.await
.unwrap_err();
assert!(
matches!(err, Error::Exit { ref reason, .. } if reason == "boom"),
"{err:?}"
);
}
#[tokio::test]
async fn within_returns_a_timeout_once_the_limit_passes() {
let never = std::future::pending::<Result<()>>();
let outcome = within("a slow thing", Duration::from_millis(10), never).await;
assert!(matches!(outcome, Err(Error::Timeout { .. })), "{outcome:?}");
}
#[tokio::test]
async fn within_passes_a_quick_result_through() {
let quick = async { Ok::<_, Error>(7) };
assert_eq!(within("x", Duration::from_secs(5), quick).await.unwrap(), 7);
}
}