verctl-core 0.9.5

Core configuration, manifest, fragment, changelog, and version domain logic for verctl
Documentation
use std::slice;

use ctl_core::input::Input;

use super::{Machine, check_name, declared, of, one};
use crate::config::Config;

fn load(yaml: &str) -> Config {
    Config::parse(yaml).expect("parse")
}

/// Shape only. The rules below are the ones `declared` owns, so a fixture
/// that breaks one must reach it rather than stopping at the boundary.
fn raw(yaml: &str) -> Config {
    Input::new(".ctl/ver.yaml", yaml).parse().expect("parse")
}

const REGISTRY: &str = indoc::indoc! {"
    packages:
      - name: verctl
        path: Cargo.toml

    runners:
      linux:
        labels: [ubuntu-26.04]
      big:
        labels: [self-hosted, linux, x64]
"};

fn named(names: &[&str]) -> Vec<String> {
    names.iter().map(|name| (*name).to_owned()).collect()
}

#[test]
fn omitted_takes_the_default_machine() {
    let machines = of(
        &load(REGISTRY),
        "ci job",
        "verify",
        None,
        "hosted",
        &["ubuntu-26.04"],
    )
    .unwrap();
    assert_eq!(
        machines,
        [Machine {
            name: "hosted".to_owned(),
            labels: vec!["ubuntu-26.04".to_owned()],
        }]
    );
}

#[test]
fn labels_reach_the_caller_verbatim_and_in_order() {
    let machines = of(
        &load(REGISTRY),
        "ci job",
        "verify",
        Some(&named(&["big"])),
        "hosted",
        &[],
    )
    .unwrap();
    assert_eq!(machines[0].labels, ["self-hosted", "linux", "x64"]);
}

#[test]
fn one_machine_with_three_labels_is_one_check() {
    let machines = of(
        &load(REGISTRY),
        "ci job",
        "verify",
        Some(&named(&["big"])),
        "hosted",
        &[],
    )
    .unwrap();
    assert_eq!(machines.len(), 1);
}

#[test]
fn two_machines_are_two_checks() {
    let machines = of(
        &load(REGISTRY),
        "ci job",
        "verify",
        Some(&named(&["linux", "big"])),
        "hosted",
        &[],
    )
    .unwrap();
    assert_eq!(machines.len(), 2);
}

#[test]
fn an_undeclared_machine_names_the_declared_ones() {
    let err = one(&load(REGISTRY), "ci job", "verify", "macos").unwrap_err();
    let text = format!("{err:#}");
    assert!(text.contains("not declared in runners"), "{text}");
    assert!(text.contains("declared: big, linux"), "{text}");
}

#[test]
fn an_empty_registry_says_so_rather_than_listing_nothing() {
    let config = load(indoc::indoc! {"
        packages:
          - name: verctl
            path: Cargo.toml
    "});
    let err = one(&config, "ci job", "verify", "linux").unwrap_err();
    assert!(format!("{err:#}").contains("none declared"), "{err:#}");
}

#[test]
fn an_empty_runners_list_is_not_a_default() {
    let err = of(
        &load(REGISTRY),
        "ci job",
        "verify",
        Some(&Vec::new()),
        "hosted",
        &["ubuntu-26.04"],
    )
    .unwrap_err();
    assert!(
        format!("{err:#}").contains("at least one machine"),
        "{err:#}"
    );
}

#[test]
fn naming_one_machine_twice_is_rejected() {
    let err = of(
        &load(REGISTRY),
        "ci job",
        "verify",
        Some(&named(&["linux", "linux"])),
        "hosted",
        &[],
    )
    .unwrap_err();
    assert!(format!("{err:#}").contains("named twice"), "{err:#}");
}

#[test]
fn a_machine_without_labels_is_rejected_even_when_unused() {
    let config = raw(indoc::indoc! {"
        packages:
          - name: verctl
            path: Cargo.toml
        runners:
          broken:
            labels: []
    "});
    let err = declared(&config).unwrap_err();
    assert!(format!("{err:#}").contains("at least one label"), "{err:#}");
}

#[test]
fn a_blank_label_is_rejected() {
    let config = load(indoc::indoc! {r#"
        packages:
          - name: verctl
            path: Cargo.toml
        runners:
          broken:
            labels: ["  "]
    "#});
    let err = declared(&config).unwrap_err();
    assert!(format!("{err:#}").contains("empty label"), "{err:#}");
}

#[test]
fn a_repeated_label_inside_one_machine_is_rejected() {
    let config = load(indoc::indoc! {"
        packages:
          - name: verctl
            path: Cargo.toml
        runners:
          broken:
            labels: [linux, linux]
    "});
    let err = declared(&config).unwrap_err();
    assert!(format!("{err:#}").contains("repeated"), "{err:#}");
}

#[test]
fn a_bare_label_list_is_not_a_machine() {
    let err = Config::parse(indoc::indoc! {"
        packages:
          - name: verctl
            path: Cargo.toml
        runners:
          linux: [ubuntu-26.04]
    "})
    .unwrap_err();
    assert!(
        format!("{err:#}").contains(".ctl/ver.yaml:5:10: expected a mapping, found a sequence"),
        "{err:#}"
    );
}

#[test]
fn only_a_fanned_out_job_carries_its_machine_in_the_name() {
    let linux = Machine {
        name: "linux".into(),
        labels: vec!["ubuntu-26.04".into()],
    };
    let macos = Machine {
        name: "macos".into(),
        labels: vec!["macos-27".into()],
    };
    assert_eq!(
        check_name("verify", "linux", slice::from_ref(&linux)),
        "verify"
    );
    assert_eq!(
        check_name("verify", "linux", &[linux, macos]),
        "verify (linux)"
    );
}