helm-rs 0.1.2

Typed Rust API for Helm 4: swappable backends, native chart, values and release reading
Documentation
use std::fmt::Write;

use super::*;

/// A `ConfigMap` of ten data lines, the fifth carrying `value`.
fn long(value: &str) -> String {
    let mut text = "apiVersion: v1\nkind: ConfigMap\nmetadata:\n  name: one\ndata:\n".to_owned();

    for line in 0..10 {
        writeln!(text, "  k{line}: {}", if line == 4 { value } else { "x" })
            .expect("writing to a String cannot fail");
    }

    text
}

fn hook(name: &str, events: &str) -> String {
    format!(
        "apiVersion: v1\nkind: ConfigMap\nmetadata:\n  name: {name}\n  annotations:\n    helm.sh/hook: {events}"
    )
}

fn names(diff: &Diff) -> Vec<&str> {
    diff.changes
        .iter()
        .map(|change| change.id.name.as_str())
        .collect()
}

#[test]
fn context_limits_the_text_diff() {
    let table = [
        (
            None,
            "  apiVersion: v1\n  kind: ConfigMap\n  metadata:\n    name: one\n  data:\n    k0: x\n    k1: x\n    k2: x\n    k3: x\n-   k4: a\n+   k4: b\n    k5: x\n    k6: x\n    k7: x\n    k8: x\n    k9: x\n",
        ),
        (Some(0), "...\n-   k4: a\n+   k4: b\n...\n"),
        (
            Some(1),
            "...\n    k3: x\n-   k4: a\n+   k4: b\n    k5: x\n...\n",
        ),
    ];

    for (context, expected) in table {
        let options = DiffOptions::builder().maybe_context(context).build();
        let diff = diff(&[&long("a")], &[&long("b")], &options);

        assert_eq!(diff.changes[0].text, expected, "{context:?}");
    }
}

#[test]
fn strip_trailing_cr_ignores_carriage_returns() {
    let unix = config_map("one", "a");
    let dos = unix.replace('\n', "\r\n");
    let plain = diff(&[&unix], &[&dos], &DiffOptions::default());
    let stripped = diff(
        &[&unix],
        &[&dos],
        &DiffOptions::builder().strip_trailing_cr(true).build(),
    );

    assert_eq!(plain.changes.len(), 1);
    assert!(stripped.is_empty());
}

#[test]
fn normalize_manifests_hides_style_differences() {
    let styled = "# Source: c/templates/a.yaml\napiVersion: v1\nkind: ConfigMap\nmetadata: {name: one}\ndata:\n  key: \"a\"";
    let other = "# Source: c/templates/b.yaml\nkind: ConfigMap\napiVersion: v1\ndata: {key: a}\nmetadata:\n  name: one";
    let normalized = DiffOptions::builder().normalize_manifests(true).build();

    assert_eq!(
        diff(&[styled], &[other], &DiffOptions::default())
            .changes
            .len(),
        1
    );
    assert!(diff(&[styled], &[other], &normalized).is_empty());
    assert_eq!(
        diff(&[styled], &[&config_map("one", "b")], &normalized).changes[0].text,
        "  apiVersion: v1\n  data:\n-   key: a\n+   key: b\n  kind: ConfigMap\n  metadata:\n    name: one\n\n"
    );
}

#[test]
fn hooks_are_diffed_unless_no_hooks_and_tests_only_when_included() {
    let documents = [
        config_map("plain", "a"),
        hook("pre", "pre-upgrade"),
        hook("t", "test"),
        hook("legacy", "test-success"),
        hook("mixed", "\"pre-upgrade,test\""),
    ];
    let after: Vec<&str> = documents.iter().map(String::as_str).collect();
    let table = [
        (
            "default",
            DiffOptions::default(),
            vec!["mixed", "plain", "pre"],
        ),
        (
            "no hooks",
            DiffOptions::builder().no_hooks(true).build(),
            vec!["plain"],
        ),
        (
            "include tests",
            DiffOptions::builder().include_tests(true).build(),
            vec!["legacy", "mixed", "plain", "pre", "t"],
        ),
        (
            "include tests, no hooks",
            DiffOptions::builder()
                .include_tests(true)
                .no_hooks(true)
                .build(),
            vec!["plain"],
        ),
    ];

    for (name, options, expected) in table {
        let diff = diff(&[], &after, &options);
        let mut names = names(&diff);

        names.sort_unstable();

        assert_eq!(names, expected, "{name}");
    }
}

#[test]
fn suppressed_kinds_are_reported_without_content() {
    let options = DiffOptions::builder().suppress_kind("ConfigMap").build();
    let diff = diff(
        &[&config_map("one", "a")],
        &[&config_map("one", "b")],
        &options,
    );
    let unchanged = self::diff(
        &[&config_map("one", "a")],
        &[&config_map("one", "a")],
        &options,
    );

    assert_eq!(diff.changes.len(), 1);
    assert!(diff.changes[0].suppressed);
    assert_eq!(diff.changes[0].fields.len(), 0);
    assert_eq!(
        diff.changes[0].text,
        "+ Changes suppressed on sensitive content of type ConfigMap\n"
    );
    assert!(unchanged.is_empty());
}

#[test]
fn the_builder_collects_suppressed_kinds() {
    let options = DiffOptions::builder()
        .suppress(["Secret"])
        .suppress_kind("Job")
        .build();

    assert_eq!(options.suppress, ["Secret", "Job"]);
}