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

nordisk/gunnar

xtask/tests/installer_pin.rs

//! **The installer-pin check reports red**, injury by injury.
//!
//! `xtask/src/installer_pin.rs` asserts that gunnar's three declarations of
//! `korp-installer` — the appliance's, the install test's and the grow bench's
//! — name one version and resolve to one version. This asserts the CHECKER:
//! the tree as it ships must be green, and every deliberate defect must turn it
//! red. A guard that cannot report red is worth nothing.

use xtask::installer_pin::{check, requirement, resolved_version, Injury, Pins, CRATE};

fn root() -> std::path::PathBuf {
    // `CARGO_MANIFEST_DIR` is `<root>/xtask`.
    std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
        .parent()
        .expect("xtask has a parent")
        .to_path_buf()
}

fn pins() -> Pins {
    Pins::read(&root()).expect("the three manifests and their lockfiles are part of the repository")
}

/// ★ **The tree that ships holds the rule**, and — the reason this is worth
/// having at all — it says WHICH installer that is, out of the files rather
/// than out of anybody's memory.
#[test]
fn the_shipped_tree_agrees_on_one_installer() {
    let p = pins();
    let findings = check(&p);
    assert!(
        findings.is_empty(),
        "gunnar's three korp-installer declarations disagree:\n{}",
        findings
            .iter()
            .map(|f| format!("  {}: {}", f.where_, f.what))
            .collect::<Vec<_>>()
            .join("\n")
    );
    let shipped = p.resolved[0]
        .1
        .clone()
        .expect("the appliance's lockfile carries the installer");
    for (path, got) in &p.resolved {
        assert_eq!(got.as_deref(), Some(shipped.as_str()), "{path}");
    }
    eprintln!("MEASURED: {} places, {CRATE} {shipped}", p.resolved.len());
}

/// **Every injury turns it red**, named one by one, so a failure says which
/// rule stopped biting rather than that "the sweep failed".
#[test]
fn every_injury_is_caught() {
    let pristine = pins();
    for injury in [
        Injury::DriftedRequirement,
        Injury::DriftedLock,
        Injury::LockBelowRequirement,
        Injury::NotDeclared,
    ] {
        let findings = check(&pristine.injured(injury));
        assert!(
            !findings.is_empty(),
            "{injury:?} was not caught: the check stayed green on a tree that is wrong"
        );
    }
    assert!(
        check(&pristine.injured(Injury::None)).is_empty(),
        "the pristine tree must stay green"
    );
}

/// The two readers, against the shapes these files actually carry — including
/// the one that must NOT be read: a commented line. The prose around these pins
/// is full of them, and a scanner that counted them would report drift that is
/// not there.
#[test]
fn the_readers_take_the_declaration_and_never_a_comment() {
    let manifest = "\
# korp-installer 0.1.4 (0.1.5 since, adding the plain one-member G2)
[dependencies]
korp-installer = { version = \"0.1.7\", default-features = false, optional = true }
";
    assert_eq!(requirement(manifest).as_deref(), Some("0.1.7"));
    assert_eq!(
        requirement("korp-installer = \"0.1.9\"").as_deref(),
        Some("0.1.9"),
        "the plain spelling too"
    );
    assert_eq!(
        requirement("# korp-installer = \"9.9.9\"\n"),
        None,
        "a comment is not a declaration"
    );
    assert_eq!(requirement("[dependencies]\nnix = \"0.29\"\n"), None);

    let lock = "\
[[package]]
name = \"korp-boot\"
version = \"0.4.0\"

[[package]]
name = \"korp-installer\"
version = \"0.1.12\"
source = \"registry+https://github.com/rust-lang/crates.io-index\"
";
    assert_eq!(
        resolved_version(lock).as_deref(),
        Some("0.1.12"),
        "the version of the block with THIS name"
    );
    assert_eq!(
        resolved_version("[[package]]\nname = \"serde\"\nversion = \"1.0\"\n"),
        None
    );
}