Skip to main content

isothrum_runner/
checks.rs

1//! The run's checks: the manifest's `setup` steps, then each runner's step under its own timeout
2//! (Isoloom computes it from the checks' waits, so a long-waiting run isn't cut off at a
3//! fixed cap), its lines read with the `isoloom-check/1` grammar ([`CheckLine::parse`]), then the
4//! `teardown` (whose failure, after a setup that failed too, is a warning: the check ends with the
5//! setup's). A check its runner never reported by name fails, whatever else it reported; a
6//! runner with no `END` line (both counts in it) is unfinished.
7
8use std::collections::BTreeSet;
9
10use isoloom_manifest::{Check, CheckLine, CheckRunner};
11use serde::{Deserialize, Serialize};
12
13use crate::Result;
14use crate::error::Error;
15use crate::events::EventKind;
16use crate::events::WarningCode;
17use crate::executor::{Cx, run_argv, run_steps};
18
19/// One check's outcome.
20#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
21#[non_exhaustive]
22pub struct CheckResult {
23    /// The runner that ran it.
24    pub runner: String,
25    /// Where it ran from (`from web`).
26    pub from: String,
27    /// The check's name.
28    pub name: String,
29    /// Whether it passed.
30    pub ok: bool,
31    /// Why it failed (empty when it passed).
32    pub detail: String,
33}
34
35/// Every check's outcome.
36#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
37#[non_exhaustive]
38pub struct CheckReport {
39    /// The run has checks, every one ran, and every one passed.
40    pub ok: bool,
41    /// Every check, in the order it ran.
42    pub results: Vec<CheckResult>,
43    /// The runners that didn't run to their end (timed out, failed before `END`).
44    pub unfinished: Vec<String>,
45}
46
47/// Runs the run's checks.
48pub(crate) async fn run(cx: &mut Cx<'_>) -> Result<CheckReport> {
49    let checks = &cx.run.manifest().checks;
50    let mut report = CheckReport::default();
51    let setup = run_steps(cx, "checks.setup", &checks.setup).await;
52    if setup.is_ok() {
53        for runner in &checks.runners {
54            run_runner(cx, runner, &mut report).await;
55        }
56    }
57    let teardown = run_steps(cx, "checks.teardown", &checks.teardown).await;
58    if let (Err(_), Err(e)) = (&setup, &teardown) {
59        // The check ends with the setup's error: the teardown's is said, not dropped.
60        cx.events.warning(WarningCode::CheckCleanupFailed, &format!("checks.teardown: {e}"));
61    }
62    setup?;
63    teardown?;
64    report.ok = !checks.runners.is_empty() && report.unfinished.is_empty() && report.results.iter().all(|r| r.ok);
65    Ok(report)
66}
67
68/// Runs one check runner, reading its results into `report`.
69async fn run_runner(cx: &mut Cx<'_>, runner: &CheckRunner, report: &mut CheckReport) {
70    let id = runner.id.as_str();
71    let before = report.results.len();
72    let mut ended = false;
73    let mut record = |line: &str| match CheckLine::parse(line) {
74        Some(CheckLine::Pass { name }) => push(cx, &mut report.results, result(runner, name, true, String::new())),
75        Some(CheckLine::Fail { name, reason }) => {
76            push(cx, &mut report.results, result(runner, name, false, reason));
77        }
78        // Its counts aren't read: the report counts the results itself.
79        Some(CheckLine::End { .. }) => ended = true,
80        None => {}
81    };
82    let ran = run_argv(cx, "checks.runners", &runner.step, &mut record).await;
83    if let Err(e) = &ran {
84        cx.events.warning(WarningCode::CheckRunnerFailed, &format!("{id}: {e}"));
85        if matches!(e, Error::Timeout { .. }) {
86            // A runner that hung outlives its CLI: remove what it left (`on_timeout`). Its steps
87            // report their own failures; the runner's is the one the report holds.
88            if let Err(cleanup) = run_steps(cx, "checks.on_timeout", &runner.on_timeout).await {
89                cx.events.warning(WarningCode::CheckCleanupFailed, &format!("{id}: {cleanup}"));
90            }
91        }
92    }
93    let reported: BTreeSet<String> = report.results.iter().skip(before).map(|r| r.name.clone()).collect();
94    for check in runner.checks.iter().filter(|c| !reported.contains(&c.name)) {
95        push(cx, &mut report.results, unreported(runner, check));
96    }
97    if ran.is_err() || !ended {
98        report.unfinished.push(id.to_owned());
99    }
100}
101
102/// Keeps `r`, and emits it as it comes.
103fn push(cx: &Cx<'_>, results: &mut Vec<CheckResult>, r: CheckResult) {
104    cx.events.emit(EventKind::Check {
105        runner: r.runner.clone(),
106        from: r.from.clone(),
107        name: r.name.clone(),
108        ok: r.ok,
109        detail: r.detail.clone(),
110    });
111    results.push(r);
112}
113
114fn result(runner: &CheckRunner, name: String, ok: bool, detail: String) -> CheckResult {
115    CheckResult {
116        runner: runner.id.to_string(),
117        from: runner.from.clone(),
118        name,
119        ok,
120        detail,
121    }
122}
123
124/// A check its runner stopped before.
125fn unreported(runner: &CheckRunner, check: &Check) -> CheckResult {
126    result(runner, check.name.clone(), false, "the runner stopped before this check".into())
127}