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

nordisk/knut

crates/knut-build-thing/tests/toml_parity.rs

//! **`.nornir/build-variants.toml` IS the code's emission — and this test fails
//! for any reason it is not.**
//!
//! Four properties:
//!
//! 1. the committed bytes ARE the emission, byte for byte — the assertion that
//!    fails for ANY reason the two differ: a row added to the code and not
//!    re-emitted, a hand edit, a reordering, a comment typed into the output. The
//!    fix is never to edit the toml; if the emission does not say what you wanted
//!    the file to say, the recipe is missing something and THAT is the bug;
//! 2. the file is INERT — an edit is visible to a reader and invisible to the
//!    recipe, which is what "never read back as authority" means, measured. The
//!    perturbation is asserted visible first, or this proves nothing. This is the
//!    property knut did not have: `include_str!` + `from_toml_str` made an edit
//!    to the file change the declaration, which is the definition of authority;
//! 3. a group at a path the reader would DROP is a REFUSAL that names the path,
//!    not a silence — gunnar lost two whole groups for four pin generations with
//!    its parity test green, because the loader discarded them before the
//!    comparison ran;
//! 4. the loader is really reading this file (LAW 2), so the three above cannot
//!    be green about a file nobody opened.

use knut_build_thing::{emit::repo_root, emitted_toml, KnutBuild};
use nornir_build_thing::emit::first_difference;
use nornir_build_thing::plugin::BuildThing;
use nornir_build_thing::variant::BuildVariantSet;

fn committed() -> String {
    let path = BuildVariantSet::conventional_path(&repo_root());
    std::fs::read_to_string(&path).unwrap_or_else(|e| panic!("read {}: {e}", path.display()))
}

#[test]
fn the_committed_toml_is_the_codes_emission_byte_for_byte() {
    let emitted = emitted_toml().expect("the recipe emits");
    if let Some(diff) = first_difference(&committed(), &emitted) {
        panic!(
            "{} is not the code's emission — the code is the source. Run \
             `cargo run -p knut-build-thing -- emit` and commit the result; never edit the \
             toml by hand. {diff}",
            BuildVariantSet::conventional_path(&repo_root()).display()
        );
    }
    let back = BuildVariantSet::from_toml_str(&emitted).expect("the emission parses back");
    assert_eq!(back, KnutBuild.variants());
    back.validate_all().expect("and it validates");
}

#[test]
fn an_edited_toml_has_zero_effect_on_what_the_recipe_declares() {
    let declared = KnutBuild.variants();
    let first = declared
        .variants
        .first()
        .expect("this repo declares at least one variant")
        .name
        .clone();
    let edited = emitted_toml()
        .unwrap()
        .replace(&format!("name = \"{first}\""), "name = \"edited-by-a-human\"");
    let read = BuildVariantSet::from_toml_str(&edited).expect("still parses");
    assert_ne!(
        read, declared,
        "the edit must be visible to a reader, else this test proves nothing"
    );
    assert_eq!(
        KnutBuild.variants(),
        declared,
        "the recipe read the file — it must not"
    );
}

/// **Nothing this file says is silently discarded.**
#[test]
fn a_key_no_reader_has_a_field_for_is_refused_by_name() {
    let emitted = emitted_toml().unwrap();
    BuildVariantSet::from_toml_str(&emitted).expect("the real emission loads");
    let with_an_unknown_key =
        format!("{emitted}\n[build.a_group_no_reader_has_a_field_for]\nk = 1\n");
    let err = BuildVariantSet::from_toml_str(&with_an_unknown_key)
        .expect_err("a key the reader cannot read must be a refusal, not a silence");
    let chain = format!("{err:#}");
    assert!(
        chain.contains("a_group_no_reader_has_a_field_for"),
        "the refusal must NAME the key — an unnamed one is not much better than \
         the silence it replaces: {chain}"
    );
}

/// LAW 2, on the comparisons above: a loader that returned a default would make
/// every one of them green about a file it never read.
#[test]
fn the_loader_is_reading_what_this_test_thinks_it_is() {
    let path = BuildVariantSet::conventional_path(&repo_root());
    let set = BuildVariantSet::load_file(&path)
        .unwrap_or_else(|e| panic!("load {}: {e:#}", path.display()));
    assert_eq!(
        set.variants.len(),
        14,
        "the file parsed to {} variants; knut declares 14",
        set.variants.len()
    );
    let mut perturbed = set.clone();
    perturbed.variants.pop().expect("a variant to drop");
    assert_ne!(
        perturbed, set,
        "BuildVariantSet compares equal after a variant was removed, so the \
         assertions here can never report red"
    );
}

/// **The file is a real file, not a symlink into this crate.**
///
/// It was one until 2026-09-20: `.nornir/build-variants.toml` pointed at
/// `crates/knut-build/build-variants.toml` so that `cargo package` could reach
/// the text `include_str!` compiled in. Nothing is compiled in any more, and a
/// symlink would mean the emitter writes through to a second committed copy —
/// two files that a reader must keep in step, which is the shape this whole turn
/// removed.
#[test]
fn the_emitted_file_is_not_a_symlink_to_a_second_copy() {
    let path = BuildVariantSet::conventional_path(&repo_root());
    let meta = std::fs::symlink_metadata(&path).expect("the emitted file exists");
    assert!(
        !meta.file_type().is_symlink(),
        "{} is a symlink — the emission has exactly one home",
        path.display()
    );
    assert!(
        !repo_root()
            .join("crates/knut-build-thing/build-variants.toml")
            .exists(),
        "the in-crate copy is back; the recipe is the source and there is nothing to embed"
    );
}