crates/nornir-xtask-support/src/selftest.rs
//! # Functional-status emit bridge — `nornir-xtask-support` → 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 the pure primitives (`target_dir`'s env fallback honours the
//! override — the `cargo metadata` road it now takes first needs a real crate and is
//! covered by the unit test rather than by this env-only probe,
//! `copy_dir` round-trips, `workspace_root_of` climbs one dir) ACTUALLY behave —
//! without eyeballing anything.
//!
//! ## Release strips everything
//! The `testmatrix` cargo feature gates the whole bridge: with it OFF (the default)
//! [`emit`] is an `#[inline]` no-op and `nornir-testmatrix` is not even linked, so
//! the leaf stays std-only.
/// 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).
#[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 (default / release builds).
#[cfg(not(feature = "testmatrix"))]
#[inline]
pub fn emit(_component: &str, _check: &str, _ok: bool, _detail: &str) {}
/// `assert!`-with-emit: assert on the real value AND record the verdict as a matrix
/// row. The `assert!` is the gate; the emit is the matrix observation.
#[macro_export]
macro_rules! xtask_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::{copy_dir, fresh_dir, target_dir, workspace_root_of};
use std::path::Path;
/// Exercise the real primitives, 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_xtask_primitives() {
// Shares the crate-level lock with the `CARGO_TARGET_DIR` tests in `lib.rs`:
// this test sets that variable too, from a different module.
let _g = crate::ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let root = workspace_root_of(Path::new("/home/x/repo/xtask"));
xtask_assert_emit!(
"nornir-xtask-support",
"workspace_root_climbs",
root.as_path() == Path::new("/home/x/repo"),
"root={}",
root.display()
);
std::env::set_var("CARGO_TARGET_DIR", "/mnt/scratch/t");
let td = target_dir(Path::new("/ws"));
xtask_assert_emit!(
"nornir-xtask-support",
"target_dir_honours_override",
td.as_path() == Path::new("/mnt/scratch/t"),
"target_dir={}",
td.display()
);
std::env::remove_var("CARGO_TARGET_DIR");
let base = std::env::temp_dir().join(format!("nxs-self-{}", std::process::id()));
let src = base.join("s");
let dst = base.join("d");
let _ = std::fs::remove_dir_all(&base);
fresh_dir(&src).unwrap();
fresh_dir(&dst).unwrap();
std::fs::write(src.join("f"), b"payload").unwrap();
copy_dir(&src, &dst).unwrap();
let round = std::fs::read(dst.join("f")).unwrap();
xtask_assert_emit!(
"nornir-xtask-support",
"copy_dir_round_trips",
round == b"payload",
"round={} bytes",
round.len()
);
let _ = std::fs::remove_dir_all(&base);
// RED surface: a WRONG expectation must NOT pass.
crate::selftest::emit(
"nornir-xtask-support",
"wrong_root_is_rejected",
workspace_root_of(Path::new("/a/b/xtask")).as_path() != Path::new("/wrong"),
"climb != /wrong",
);
let rows = nornir_testmatrix::drain_functional_rows();
let has = |t: &str| {
rows.iter()
.any(|r| r.suite == "nornir-xtask-support" && r.test_name == t && r.status == "pass")
};
assert!(has("workspace_root_climbs"), "rows={rows:?}");
assert!(has("target_dir_honours_override"), "rows={rows:?}");
assert!(has("copy_dir_round_trips"), "rows={rows:?}");
assert!(has("wrong_root_is_rejected"), "rows={rows:?}");
assert!(rows.iter().all(|r| r.aspect == "functional"), "rows={rows:?}");
}
}