isothrum_runner/tool_output.rs
1//! What the tools the runner runs print, read in one place per tool, so no driver matches a
2//! tool's words where it acts on them: each reader returns a typed value (an enum with an
3//! explicit `Other` or `Unknown`, an `Option` or a `Result` that says why it couldn't read), and
4//! each wording it knows is backed by a fixture of the text as a named version of the tool prints
5//! it (`tool_output/fixtures/<tool>/<version>-<case>`): a release that rewords a line then fails
6//! one test, not a run. The tools are spawned with their messages in the C locale
7//! (`process::locale`), so what they say is in the words these readers know.
8//!
9//! - [`vagrant`]: `status --machine-readable`, `ssh-config`, `plugin list`, `box list`, the `error-exit`
10//! record, a held lock and ESXi's refused halt ([`vagrant::Failure`]).
11//! - [`vboxmanage`]: `list vms`, `list systemproperties`, `showvminfo --machinereadable`.
12//! - [`virsh`]: `domdisplay`. [`qemu`]: the monitor's `info vnc`, vagrant-qemu's `options.yml`.
13//! - [`prlctl`]: where a Parallels VM's bundle is.
14//! - [`osascript`]: an Apple event's error number (UTM's Vagrant provider drives UTM with it).
15//! - [`ruby`]: a Ruby tool's backtrace frames and fog's warnings.
16//! - [`az`]: the Azure CLI's version (`az version --output json`).
17//! - [`sign_in`]: a cloud's sign-in run out or missing, and the cloud a Terraform failure names.
18//!
19//! Tools whose whole answer is a document are read in their adapter ([`crate::cli`]): Compose's
20//! `ps`, Terraform's JSON UI, state and outputs, Proxmox VE's API.
21
22pub(crate) mod az;
23pub(crate) mod osascript;
24pub(crate) mod prlctl;
25pub(crate) mod qemu;
26pub(crate) mod ruby;
27pub(crate) mod sign_in;
28pub(crate) mod vagrant;
29pub(crate) mod vboxmanage;
30pub(crate) mod virsh;
31
32pub use az::version as azure_cli_version;
33pub use sign_in::expired as sign_in_expired;
34pub use vagrant::plugins as vagrant_plugins;
35pub use vboxmanage::vms as virtualbox_vms;
36
37/// The failure `program` printed as a record of its own among its standard output (`stdout`),
38/// for the tools that print one: Vagrant's `error-exit` (with `--machine-readable`, Vagrant prints
39/// its error there, and nothing of it on standard error). What a failed tool's reason leads with
40/// ([`Captured::reason`](crate::Captured::reason)).
41pub(crate) fn failure_record(program: &str, stdout: &[String]) -> Option<String> {
42 let name = std::path::Path::new(program).file_stem().map(|s| s.to_string_lossy().to_ascii_lowercase());
43 match name.as_deref() {
44 Some("vagrant") => vagrant::error(stdout),
45 _ => None,
46 }
47}
48
49use crate::project::platform::Version;
50
51/// The version `text` says (what a tool prints: `Vagrant 2.4.1`, `v2.29.1`, `1.0.0-rc.1`), as
52/// a [`Version`], so versions compare in its order (a pre-release before its release):
53/// its first word that is one, else its first `<major>.<minor>[.<patch>]` (`2.3.8.dev` is 2.3.8, a
54/// missing patch 0). `None` when it says none: nothing is held back then. The one reading of a
55/// version for the runner's tool probes and `isothrum-machine`'s minimums.
56#[must_use]
57pub fn version_found(text: &str) -> Option<Version> {
58 text.split(|c: char| c.is_whitespace() || matches!(c, ',' | '(' | ')'))
59 .map(|w| w.strip_prefix('v').unwrap_or(w))
60 .find_map(Version::parse)
61 .or_else(|| {
62 let (major, minor, patch) = numbers_in(text)?;
63 Some(Version::new(major, minor, patch))
64 })
65}
66
67/// The first `<major>.<minor>[.<patch>]` in `text` (`Docker Compose version 2.17`,
68/// `2.3.8.dev`), its suffix dropped and a missing patch 0.
69fn numbers_in(text: &str) -> Option<(u64, u64, u64)> {
70 text.split(|c: char| !(c.is_ascii_digit() || c == '.')).find_map(|word| {
71 let mut parts = word.split('.').filter(|p| !p.is_empty()).map(str::parse::<u64>);
72 let major = parts.next()?.ok()?;
73 let minor = parts.next()?.ok()?;
74 let patch = parts.next().and_then(Result::ok).unwrap_or(0);
75 Some((major, minor, patch))
76 })
77}
78
79#[cfg(test)]
80mod tests {
81 use super::*;
82
83 /// A version as each tool prints it, compared in version order: by its numbers (a missing
84 /// patch 0), a pre-release before its release.
85 #[test]
86 fn versions_are_read_from_what_a_tool_prints_in_version_order() {
87 let v = |text: &str| version_found(text).unwrap();
88 let parsed = |text: &str| Version::parse(text).unwrap();
89 assert_eq!(v("5.5.1"), parsed("5.5.1"));
90 assert_eq!(v("Vagrant 2.4.1"), parsed("2.4.1"));
91 assert_eq!(v("v2.29.1-desktop.1"), parsed("2.29.1-desktop.1"));
92 assert_eq!(v("Docker Compose version 2.17"), parsed("2.17.0"));
93 assert_eq!(v("2.3.8.dev"), parsed("2.3.8"), "a suffix that isn't a pre-release is dropped");
94 assert_eq!(version_found("no version here"), None);
95 assert!(v("2.16.9") < v("2.17.0"));
96 assert!(v("4.7") > v("4.6.5"), "a missing part is 0");
97 assert!(v("4.7.5") > v("4.6.5") && v("4.6.5") >= v("4.6.5"));
98 assert!(v("0.1.6") < v("0.2.0"));
99 assert!(v("0.2.0-beta") < v("0.2.0"), "a pre-release comes before its release");
100 }
101}