1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//! The one handle machine setup works through: the process runner every tool runs on, the host
//! it reads, and how long a probe may take. The crate keeps no statics: a test hands its own
//! (a strict `FakeProcesses` script, a hand-built [`Host`]).
use std::sync::Arc;
use std::time::Duration;
use isothrum_runner::{Captured, ProcessRunner, ProcessSpec};
use crate::exec::{self, Unanswered};
use crate::host::Host;
/// How long a probe (`VBoxManage --version`, `vagrant plugin list`...) may take before it is
/// dropped: a tool that started and ran out of it reads as installed and slow (a cold `VBoxSVC`),
/// not as missing.
pub const PROBE_TIMEOUT: Duration = Duration::from_secs(25);
/// What machine setup runs its tools on and reads.
#[non_exhaustive]
pub struct Context {
/// Every tool runs through it: `SystemProcesses`, or a fake in tests.
pub runner: Arc<dyn ProcessRunner>,
/// The computer it works on: [`Host::here`], or a hand-built one.
pub host: Host,
/// How long a probe may take ([`PROBE_TIMEOUT`] unless set).
pub probe_timeout: Duration,
}
impl Context {
/// Machine setup on `host`, its tools run by `runner`.
#[must_use]
pub fn new(runner: Arc<dyn ProcessRunner>, host: Host) -> Context {
Context {
runner,
host,
probe_timeout: PROBE_TIMEOUT,
}
}
/// Machine setup on this computer ([`Host::here`]), its tools run by `runner`.
pub async fn here(runner: Arc<dyn ProcessRunner>) -> Context {
let host = Host::here(runner.as_ref()).await;
Context::new(runner, host)
}
/// The same, a probe given `limit` to answer.
#[must_use]
pub fn with_probe_timeout(self, limit: Duration) -> Context {
Context { probe_timeout: limit, ..self }
}
/// Runs `program args` as a probe ([`exec::probe`], within [`Context::probe_timeout`]).
pub(crate) async fn probe(&self, program: &str, args: &[&str]) -> Result<Captured, Unanswered> {
exec::probe(self.runner.as_ref(), self.probe_timeout, program, args).await
}
/// Runs `spec` as a probe given `limit` to answer ([`exec::probe_spec`]).
pub(crate) async fn probe_within(&self, limit: Duration, spec: &ProcessSpec) -> Result<Captured, Unanswered> {
exec::probe_spec(self.runner.as_ref(), limit, spec).await
}
/// The standard output of `program args` when it exits 0 ([`exec::read_ok`], within
/// [`Context::probe_timeout`]), else why not.
pub(crate) async fn read_ok(&self, program: &str, args: &[&str]) -> Result<String, String> {
exec::read_ok(self.runner.as_ref(), self.probe_timeout, program, args).await
}
/// Whether `program` is installed: it is, as long as it could be started (a tool that prints
/// usage and exits non-zero, like `vmrun`, still exists, and so does one that ran out of the
/// probe's time).
pub(crate) async fn tool_starts(&self, program: &str, args: &[&str]) -> bool {
self.probe(program, args).await.as_ref().map_or_else(Unanswered::tool_is_there, |_| true)
}
}
impl std::fmt::Debug for Context {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Context")
.field("host", &self.host)
.field("probe_timeout", &self.probe_timeout)
.finish_non_exhaustive()
}
}