use super::{CliCommand, Invocation};
pub(super) struct Built {
pub(super) args: Vec<String>,
pub(super) stdin: Option<String>,
}
pub(super) fn invocation<C: CliCommand>(command: &C) -> Invocation {
let mut inv = Invocation::default();
command.build(&mut inv).expect("builds");
inv
}
pub(super) fn argv<C: CliCommand>(command: &C) -> Vec<String> {
strings(&invocation(command))
}
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()
}
pub(super) fn build_error<C: CliCommand>(command: &C) -> crate::error::Error {
command
.build(&mut Invocation::default())
.expect_err("rejected")
}
pub(super) type Row<C> = (C, &'static [&'static str], Option<&'static str>);
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);
}
}
pub(super) type FlagRow<C> = (&'static str, fn(&mut C), &'static [&'static str]);
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}");
}
}
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
}
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)
}