helm-rs 0.1.2

Typed Rust API for Helm 4: swappable backends, native chart, values and release reading
Documentation
//! Chart and schema behaviour checked against the real `helm` binary (Helm 4).

use std::path::Path;
use std::process::Command;

use helm_rs::chart::{Chart, archive_digest, read_values_file};
use helm_rs::schema::final_values;

/// Runs `helm` in `dir` and returns stdout, failing the test with stderr on error.
fn helm(dir: &Path, args: &[&str]) -> String {
    let output = Command::new("helm")
        .args(args)
        .current_dir(dir)
        .output()
        .expect("helm runs");

    assert!(
        output.status.success(),
        "helm {args:?}: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    String::from_utf8(output.stdout).expect("utf-8 output")
}

#[test]
#[ignore = "needs helm 4"]
fn packaged_chart_loads_and_digest_matches_repo_index() {
    let dir = tempfile::tempdir().expect("tempdir");

    helm(dir.path(), &["create", "demo"]);
    helm(dir.path(), &["package", "demo"]);

    let archive = dir.path().join("demo-0.1.0.tgz");
    let index_dir = dir.path().join("index");

    std::fs::create_dir(&index_dir).expect("index dir");
    std::fs::copy(&archive, index_dir.join("demo-0.1.0.tgz")).expect("copied");
    helm(
        dir.path(),
        &["repo", "index", "index", "--url", "http://example.invalid"],
    );

    let index = std::fs::read_to_string(index_dir.join("index.yaml")).expect("index.yaml");
    let digest = index
        .lines()
        .find_map(|l| l.trim().strip_prefix("digest: "))
        .expect("digest line");
    let from_archive = Chart::load(&archive).expect("archive loads");
    let from_dir = Chart::load(dir.path().join("demo")).expect("directory loads");

    assert_eq!(archive_digest(&archive).expect("digest"), digest);
    assert_eq!(from_archive.metadata, from_dir.metadata);
    assert_eq!(from_archive.values, from_dir.values);
}

#[test]
#[ignore = "needs helm 4"]
fn values_and_final_values_match_helm_template() {
    let dir = tempfile::tempdir().expect("tempdir");
    let chart_dir = dir.path().join("vals");

    std::fs::create_dir_all(chart_dir.join("templates")).expect("dirs");
    std::fs::write(
        chart_dir.join("Chart.yaml"),
        "apiVersion: v2\nname: vals\nversion: 0.1.0\n",
    )
    .expect("written");
    std::fs::write(
        chart_dir.join("templates/v.yaml"),
        "{{ .Values | toJson }}\n",
    )
    .expect("written");
    std::fs::write(
        chart_dir.join("values.yaml"),
        "a: yes\nb: {c: 0777, d: 1_000, e: null, f: [1, two]}\nkeep: 1.5\ndrop: 1\n",
    )
    .expect("written");

    let user = dir.path().join("user.yaml");

    std::fs::write(&user, "b: {c: 7, g: 0x1F}\ndrop: null\n").expect("written");

    let rendered = helm(dir.path(), &["template", "vals", "-f", "user.yaml"]);
    let json = rendered
        .lines()
        .find(|l| l.starts_with('{'))
        .expect("values line");
    let expected: serde_json::Value = serde_json::from_str(json).expect("helm json");
    let chart = Chart::load(&chart_dir).expect("loads");
    let user_values = read_values_file(&user).expect("user values");

    assert_eq!(
        final_values(&chart, &user_values).expect("coalesces"),
        expected
    );
}