Skip to main content

isothrum_machine/
context.rs

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