crates/nornir-coverage/src/selftest.rs
//! # Functional-status emit bridge — `nornir-coverage` → 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-coverage`'s real surfaces (the [`CoverageDiff`] gate
//! verdict, the [`CoverageReport`] roll-up) 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`.
//!
//! [`CoverageDiff`]: crate::diff::CoverageDiff
//! [`CoverageReport`]: crate::CoverageReport
/// The aspect tag every self-emitted row carries (matches `functional_row`).
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::{summarise, CoverageReport, FileCoverage};
fn report(krate: &str, lines: u64, lines_cov: u64) -> CoverageReport {
let file = FileCoverage {
file: format!("{krate}/src/lib.rs"),
krate: krate.to_string(),
lines,
lines_covered: lines_cov,
regions: lines,
regions_covered: lines_cov,
functions: vec![],
};
summarise("r", vec![file]).unwrap()
}
/// Exercise `CoverageDiff`'s real regression gate, 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_coverage_diff_surfaces() {
// Real RED surface: coverage dropped 80% → 40% ⇒ IS a regression.
let dropped = report("a", 10, 4).diff(&report("a", 10, 8));
assert_emit!(
"nornir-coverage",
"coverage_drop_is_regression",
dropped.is_regression(0.0),
"worst_delta={:.1}",
dropped.worst().map(|d| d.line_pct_delta()).unwrap_or(0.0)
);
// Real GREEN surface: identical reports do NOT regress.
let same = report("a", 10, 8).diff(&report("a", 10, 8));
assert_emit!(
"nornir-coverage",
"no_change_is_not_regression",
!same.is_regression(0.0),
"regressions={}",
same.regressions(0.0).len()
);
let rows = nornir_testmatrix::drain_functional_rows();
let has = |t: &str| {
rows.iter().any(|r| {
r.suite == "nornir-coverage" && r.test_name == t && r.status == "pass"
})
};
assert!(has("coverage_drop_is_regression"), "rows={rows:?}");
assert!(has("no_change_is_not_regression"), "rows={rows:?}");
assert!(
rows.iter()
.filter(|r| r.suite == "nornir-coverage")
.all(|r| r.aspect == "functional"),
"rows={rows:?}"
);
}
}