helm-rs 0.1.2

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

use super::*;
use crate::cli::chart_rows::capture;
use crate::cli::testing::{FlagRow, check_flags};

fn chart() -> ChartRef {
    ChartRef::Repo("repo/web".into())
}

fn source(file: &str) -> String {
    let path = format!(
        "{}/tests/fixtures/commands/fetch/demo/{file}",
        env!("CARGO_MANIFEST_DIR")
    );

    fs::read_to_string(path).expect("chart file")
}

macro_rules! show_flags {
    ($command:ident, $word:literal) => {
        check_flags(
            &$command::new(chart()),
            &["show", $word, "repo/web"],
            &[],
            &download_rows!($command),
        );
    };
}

#[test]
fn every_show_command_maps_the_chart_path_flags_and_devel() {
    show_flags!(ShowChart, "chart");
    show_flags!(ShowCrds, "crds");
    show_flags!(ShowReadme, "readme");
    show_flags!(ShowValues, "values");
}

#[test]
fn show_values_maps_jsonpath() {
    let table: Vec<FlagRow<ShowValues>> = vec![(
        "jsonpath",
        |c| c.jsonpath = Some("{.image.tag}".into()),
        &["--jsonpath", "{.image.tag}"],
    )];

    check_flags(
        &ShowValues::new(chart()),
        &["show", "values", "repo/web"],
        &[],
        &table,
    );
}

#[test]
fn a_show_password_goes_to_the_registry_config_for_oci_charts_only() {
    let command = ShowChart {
        chart_path: ChartPathOptions {
            username: Some("alice".into()),
            password: Some("s3cret".into()),
            ..ChartPathOptions::default()
        },
        ..ShowChart::new(ChartRef::Oci("oci://r.example/web".into()))
    };
    let args = crate::cli::testing::argv(&command);

    assert_eq!(args[..3], ["show", "chart", "oci://r.example/web"]);
    assert!(args.contains(&"--registry-config".to_owned()));
    assert!(!args.join(" ").contains("s3cret"));
    assert!(matches!(
        crate::cli::testing::build_error(&ShowChart {
            chart_path: command.chart_path.clone(),
            ..ShowChart::new(chart())
        }),
        Error::InvalidCommand(_)
    ));
}

#[test]
fn chart_values_readme_and_crds_are_the_files_of_the_chart() {
    let metadata = ShowChart::new(chart())
        .parse(capture("show-chart.yaml"))
        .expect("metadata");
    let values = ShowValues::new(chart())
        .parse(capture("show-values.yaml"))
        .expect("values");
    let jsonpath = ShowValues {
        jsonpath: Some("{.image.tag}".into()),
        ..ShowValues::new(chart())
    }
    .parse(capture("show-values-jsonpath.txt"))
    .expect("jsonpath");
    let readme = ShowReadme::new(chart())
        .parse(capture("show-readme.md"))
        .expect("readme");
    let crds = ShowCrds::new(chart())
        .parse(capture("show-crds.yaml"))
        .expect("crds");
    let names: Vec<_> = crds.objects.iter().filter_map(|o| o.name()).collect();

    assert_eq!(metadata.name, "demo");
    assert_eq!(metadata.version, "0.1.0");
    assert_eq!(metadata.app_version, "1.2.3");
    assert_eq!(metadata.annotations["example.com/note"], "a < b");
    assert_eq!(metadata.maintainers.len(), 1);
    assert_eq!(values, Some(source("values.yaml")));
    assert_eq!(jsonpath.as_deref(), Some("1.27"));
    assert_eq!(readme, Some(source("README.md")));
    assert_eq!(names, ["gadgets.example.com", "widgets.example.com"]);
    assert!(ShowChart::new(chart()).parse("name: [".into()).is_err());
}

#[test]
fn an_absent_file_is_none_and_an_empty_one_is_some_empty() {
    let jsonpath = ShowValues {
        jsonpath: Some("{.none}".into()),
        ..ShowValues::new(chart())
    };

    for (stdout, expected) in [("", None), ("\n", Some("")), ("a: 1\n\n", Some("a: 1\n"))] {
        let expected = expected.map(str::to_owned);

        assert_eq!(
            ShowValues::new(chart())
                .parse(stdout.into())
                .expect("values"),
            expected
        );
        assert_eq!(
            ShowReadme::new(chart())
                .parse(stdout.into())
                .expect("readme"),
            expected
        );
    }

    assert_eq!(jsonpath.parse(String::new()).expect("jsonpath"), None);
}