helm-rs 0.1.2

Typed Rust API for Helm 4: swappable backends, native chart, values and release reading
Documentation
//! Shared scaffolding of the per-command argv tests.

use super::{CliCommand, Invocation};

/// What a command asks of the process: argv and stdin.
pub(super) struct Built {
    pub(super) args: Vec<String>,
    pub(super) stdin: Option<String>,
}

/// The invocation `command` builds the way the backend does, without global options. Temporary
/// files it wrote (values, credentials) live as long as the returned value.
pub(super) fn invocation<C: CliCommand>(command: &C) -> Invocation {
    let mut inv = Invocation::default();

    command.build(&mut inv).expect("builds");

    inv
}

/// The argv `command` builds, as strings.
pub(super) fn argv<C: CliCommand>(command: &C) -> Vec<String> {
    strings(&invocation(command))
}

/// Builds `command` and keeps its argv and stdin.
pub(super) fn build<C: CliCommand>(command: &C) -> Built {
    let inv = invocation(command);

    Built {
        args: strings(&inv),
        stdin: inv
            .stdin
            .as_ref()
            .map(|s| String::from_utf8_lossy(s).into_owned()),
    }
}

fn strings(inv: &Invocation) -> Vec<String> {
    inv.args
        .iter()
        .map(|a| a.to_string_lossy().into_owned())
        .collect()
}

/// The error of building `command`.
pub(super) fn build_error<C: CliCommand>(command: &C) -> crate::error::Error {
    command
        .build(&mut Invocation::default())
        .expect_err("rejected")
}

/// A table row of an argv test: the command and the argv, stdin it must produce.
pub(super) type Row<C> = (C, &'static [&'static str], Option<&'static str>);

/// Asserts every row of an argv table.
pub(super) fn check<C: CliCommand>(table: Vec<Row<C>>) {
    for (command, args, stdin) in table {
        let built = build(&command);

        assert_eq!(built.args, args);
        assert_eq!(built.stdin.as_deref(), stdin);
    }
}

/// A row of a flag table: a label, a change to the base command and the flags it must add.
pub(super) type FlagRow<C> = (&'static str, fn(&mut C), &'static [&'static str]);

/// Asserts the exact argv of `base` after each row's change: `prefix`, the row's flags, `suffix`.
pub(super) fn check_flags<C: CliCommand + Clone>(
    base: &C,
    prefix: &[&str],
    suffix: &[&str],
    table: &[FlagRow<C>],
) {
    for (label, change, flags) in table {
        let mut command = base.clone();

        change(&mut command);

        let expected: Vec<&str> = prefix
            .iter()
            .chain(flags.iter())
            .chain(suffix.iter())
            .copied()
            .collect();

        assert_eq!(argv(&command), expected, "{label}");
    }
}

/// A temporary chart directory whose `Chart.yaml` lists `dependencies` (YAML list items).
pub(super) fn chart_with(dependencies: &str) -> tempfile::TempDir {
    let dir = tempfile::TempDir::new().expect("temp dir");

    std::fs::write(
        dir.path().join("Chart.yaml"),
        format!("apiVersion: v2\nname: c\nversion: 0.1.0\ndependencies:\n{dependencies}"),
    )
    .expect("Chart.yaml");

    dir
}

/// The registry config the invocation passes as its last `--registry-config`: its `auths` map as
/// `(host, base64 auth)` pairs and its file mode.
pub(super) fn registry_auths(inv: &Invocation) -> (Vec<(String, String)>, u32) {
    use std::os::unix::fs::PermissionsExt;

    let args = strings(inv);
    let at = args
        .iter()
        .rposition(|arg| arg == "--registry-config")
        .expect("--registry-config");
    let path = &args[at + 1];
    let config: serde_json::Value =
        serde_json::from_str(&std::fs::read_to_string(path).expect("config")).expect("json");
    let mode = std::fs::metadata(path)
        .expect("metadata")
        .permissions()
        .mode()
        & 0o777;
    let auths = config["auths"]
        .as_object()
        .expect("auths")
        .iter()
        .map(|(host, entry)| {
            (
                host.clone(),
                entry["auth"].as_str().expect("auth").to_owned(),
            )
        })
        .collect();

    (auths, mode)
}