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

nordisk/edda

crates/nornir-release/src/selftest.rs

//! # Functional-status emit bridge — `nornir-release` → test matrix
//!
//! The single internal call site this crate uses to push a *functional* row into
//! the [`nornir_testmatrix`] matrix, so `nornir test --features testmatrix`
//! records whether `nornir-release`'s real surfaces (today: the [`plan_waves`]
//! deps-first ordering + cycle verdict) ACTUALLY behave — without eyeballing.
//!
//! ## Release strips everything
//! The `testmatrix` cargo feature gates the whole bridge: with it OFF (the release
//! default) [`emit`] is an `#[inline]` no-op and `nornir-testmatrix` is not even
//! linked. Lit ONLY under `cargo … --features testmatrix`.
//!
//! [`plan_waves`]: crate::waves::plan_waves

/// The aspect tag every self-emitted row carries.
pub const ASPECT_FUNCTIONAL: &str = "functional";

/// Push ONE functional-status row for a single real check (feature ON).
///
/// * `component` — the unit under test (becomes the row's `suite`).
/// * `check` — the specific assertion's name (becomes the row's `test_name`).
/// * `ok` — the **real** verdict.
/// * `detail` — the **measured** value.
#[cfg(feature = "testmatrix")]
#[inline]
pub fn emit(component: &str, check: &str, ok: bool, detail: &str) {
    nornir_testmatrix::functional_status(component, check, ok, detail);
}

/// No-op stub when the `testmatrix` feature is OFF (release builds). Inlined and
/// empty so the call — and, with the wrap-the-call-site pattern, the detail
/// string-building — is stripped entirely.
#[cfg(not(feature = "testmatrix"))]
#[inline]
pub fn emit(_component: &str, _check: &str, _ok: bool, _detail: &str) {}

/// `assert!`-with-emit: assert on the real return value / object state AND record
/// the verdict as a matrix row. The `assert!` is the test's actual gate; the emit
/// is the matrix observation.
#[macro_export]
macro_rules! assert_emit {
    ($component:expr, $check:expr, $ok:expr, $($detail:tt)+) => {{
        let __ok: bool = $ok;
        let __detail = ::std::format!($($detail)+);
        $crate::selftest::emit($component, $check, __ok, &__detail);
        ::std::assert!(__ok, "{}::{} — {}", $component, $check, __detail);
    }};
}

// ─── inject-and-assert wiring test (feature ON only) ─────────────────────────
#[cfg(all(test, feature = "testmatrix"))]
mod wiring {
    use crate::waves::{plan_waves, DepKind, WorkspaceCrate};

    /// Exercise `plan_waves`'s real ordering + cycle detection, emit an honest row
    /// per surface, then drain the process-global functional buffer and assert the
    /// rows landed — proving the emit bridge is wired end to end.
    #[test]
    fn emits_honest_rows_for_wave_surfaces() {
        // Real GREEN surface: a clean chain orders the dependency first.
        let clean = plan_waves(&[
            WorkspaceCrate::new("app").dep("core", DepKind::Normal),
            WorkspaceCrate::new("core"),
        ]);
        let ordered =
            clean.phase_of("core").zip(clean.phase_of("app")).is_some_and(|(c, a)| c < a);
        assert_emit!(
            "nornir-release",
            "dependency_publishes_before_dependent",
            ordered && !clean.has_cycle(),
            "phases={:?}",
            clean.phases
        );

        // Real RED-when-broken surface: a genuine a↔b cycle IS flagged.
        let cyclic = plan_waves(&[
            WorkspaceCrate::new("a").dep("b", DepKind::Normal),
            WorkspaceCrate::new("b").dep("a", DepKind::Normal),
        ]);
        assert_emit!(
            "nornir-release",
            "real_cycle_is_flagged",
            cyclic.has_cycle(),
            "cyclic={:?}",
            cyclic.cyclic
        );

        let rows = nornir_testmatrix::drain_functional_rows();
        let has = |t: &str| {
            rows.iter()
                .any(|r| r.suite == "nornir-release" && r.test_name == t && r.status == "pass")
        };
        assert!(has("dependency_publishes_before_dependent"), "rows={rows:?}");
        assert!(has("real_cycle_is_flagged"), "rows={rows:?}");
        assert!(
            rows.iter()
                .filter(|r| r.suite == "nornir-release")
                .all(|r| r.aspect == "functional"),
            "rows={rows:?}"
        );
    }
}