Your browser sucks, JavaScript disabled! Gunnar's console needs JavaScript and WebAssembly. Git over SSH on port 2222 does not.

nordisk/edda

crates/nornir-testmatrix/tests/inventory_constellation.rs

//! **The text matrix for [`nornir_testmatrix::inventory`]** — a constellation of
//! three tiny fake repos, and the exact table the verb must print for them.
//!
//! Asserted BYTE-FOR-BYTE, on purpose. A discovery whose output is only checked
//! by `contains()` drifts a column at a time and nobody notices; the whole point
//! of this module is that the rendered inventory is a stable artefact somebody
//! commits and diffs. The fixtures cover, deliberately, one of each thing that
//! goes silent:
//!
//! | fixture | what it proves |
//! |---|---|
//! | `alpha` | a cargo `[[bench]]` AND a registered `nornir-bench` arm in one repo |
//! | `beta`  | a `[[bench]]` whose source drives criterion is reported as criterion, not as cargo-bench |
//! | `gamma` | a repo with NO bench yields exactly ONE `absent` line — never an empty table — and carries three matrix rows: one live, one whose only emitter is `#[ignore]`d, and one whose `#[ignore]` DECLARES what it requires (`gated`, not dark) |
//!
//! The roster is handed in ([`resolve_rows_with`]) so the grid here is 1 × 3
//! rather than the canonical 5 × 8; the canonical shape is pinned by the unit
//! test `the_declared_grid_is_the_roster_product`.

use std::path::{Path, PathBuf};

use nornir_testmatrix::inventory::{
    forbidden_vocabulary, render, scan_roots_with, State, FORBIDDEN_WORD,
};

fn fixtures() -> PathBuf {
    Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/inventory")
}

fn roots() -> Vec<PathBuf> {
    let b = fixtures();
    vec![b.join("alpha"), b.join("beta"), b.join("gamma")]
}

/// The gamma appliance's three rungs. `boots` has a live emitter; `serves` has
/// only an `#[ignore]`d one; `armed` has an `#[ignore]`d one that DECLARES what
/// it requires. Those three distinctions are the whole point of this fixture.
const PROOFS: [&str; 3] = ["boots", "serves", "armed"];

#[test]
fn the_constellation_renders_exactly_this_table() {
    let inv = scan_roots_with(&roots(), false, &["gamma"], &PROOFS);
    let got = render(&inv);

    let want = "\
SUMMARY
alpha                                          present               1 arms · 1 cargo · 0 criterion · 0 house · 0 detached | 0 rows · 0 declared-not-emitted · 0 gated · 0 n/a · 0 prose
beta                                           present               0 arms · 0 cargo · 1 criterion · 0 house · 0 detached | 0 rows · 0 declared-not-emitted · 0 gated · 0 n/a · 0 prose
gamma                                          declared-not-emitted  0 arms · 0 cargo · 0 criterion · 0 house · 0 detached | 3 rows · 1 declared-not-emitted · 1 gated · 0 n/a · 0 prose

BENCHES
alpha·alpha.fold                               present               nornir-bench arm · src/bench.rs · Bench the alpha hot path — one registered nornir-bench arm. · register_bench!(AlphaFold)
alpha·alpha::alpha_fold                        present               cargo-bench · benches/alpha_fold.rs · Fold a slice of alphas — the cargo-bench shape, no criterion anywhere.
beta·beta::beta_scan                           present               criterion · benches/beta_scan.rs · Scan throughput, measured by criterion.
gamma                                          absent                no bench target of any kind under this checkout

ROWS
gamma·gamma-appliance/armed                    gated                 tests/appliance_rows.rs · requires: enforcing firmware on real hardware — declared by the_appliance_is_armed
gamma·gamma-appliance/boots                    present               tests/appliance_rows.rs · emitted by the_appliance_boots
gamma·gamma-appliance/serves                   declared-not-emitted  tests/appliance_rows.rs · only emitter is #[ignore]d: the_appliance_serves
";

    assert_eq!(got, want, "\n--- got ---\n{got}\n--- want ---\n{want}");
}

#[test]
fn a_repo_with_no_benches_is_one_absent_line_never_an_empty_table() {
    let inv = scan_roots_with(&[fixtures().join("gamma")], false, &["gamma"], &PROOFS);
    let gamma = inv.repos.iter().find(|r| r.repo == "gamma").expect("gamma was scanned");
    assert_eq!(gamma.benches.len(), 1, "exactly one line, not zero: {:?}", gamma.benches);
    assert_eq!(gamma.benches[0].state, State::Absent);
    assert!(gamma.benches[0].id.is_empty(), "the placeholder names no bench");
    assert!(
        render(&inv).contains("gamma                                          absent"),
        "an empty table reads as `not looked at`; an absent line reads as `looked, found none`"
    );
}

#[test]
fn a_row_that_speaks_the_forbidden_coordinate_word_fails_by_name() {
    let inv = scan_roots_with(&roots(), false, &["gamma"], &PROOFS);
    let text = render(&inv);
    let bad = forbidden_vocabulary(&text);
    assert!(
        bad.is_empty(),
        "the model's word is typ, never {FORBIDDEN_WORD:?} — offending lines: {bad:#?}"
    );

    // …and the guard is not vacuous: it does find the word when it is there.
    let planted = format!("gunnar·x   present   one {FORBIDDEN_WORD} of the grid\n");
    assert_eq!(
        forbidden_vocabulary(&planted).len(),
        1,
        "a vacuous guard reads exactly like a clean one"
    );
}

#[test]
fn the_declared_row_that_nothing_runs_is_red_and_names_its_test() {
    let inv = scan_roots_with(&roots(), false, &["gamma"], &PROOFS);
    let dne = inv.declared_not_emitted();
    assert_eq!(dne.len(), 1, "exactly one dark rung: {dne:#?}");
    assert_eq!(dne[0].row_id, "gamma-appliance/serves");
    assert_eq!(dne[0].test, "tests/appliance_rows.rs", "the file is named");
    assert!(
        dne[0].note.contains("the_appliance_serves"),
        "the TEST is named, so somebody can go and un-ignore it: {}",
        dne[0].note
    );
    assert_eq!(dne[0].state.status(), "fail", "declared-not-emitted is RED, not a footnote");
}

#[test]
fn the_counts_are_what_the_fixtures_declare() {
    let inv = scan_roots_with(&roots(), false, &["gamma"], &PROOFS);
    assert_eq!(inv.bench_count(), 3, "one cargo bench, one criterion bench, one arm");
    assert_eq!(inv.row_count(), 3, "the 1 x 3 roster, and no phantom extras");
    assert!(inv.broken_benches().is_empty(), "no probe was asked for, and every arm is registered");
    for r in &inv.repos {
        assert!(r.errors.is_empty(), "{}: cargo metadata refused: {:?}", r.repo, r.errors);
    }
}