use std::path::PathBuf;
use std::time::Duration;
use crate::cli::CliCommand;
use crate::cli::testing::{Row, build, build_error, check};
use crate::command::{RepoAdd, RepoIndex, RepoList, RepoRemove, RepoUpdate};
use crate::error::Error;
const LIST: &str = include_str!("../../../tests/fixtures/commands/repo/repo-list.json");
#[test]
fn repo_add_maps_every_flag() {
let table: Vec<Row<RepoAdd>> = vec![
(
RepoAdd::new("web", "https://x"),
&["repo", "add", "web", "https://x"],
None,
),
(
RepoAdd {
username: Some("robot".into()),
..RepoAdd::new("web", "https://x")
},
&["repo", "add", "web", "https://x", "--username", "robot"],
None,
),
(
RepoAdd {
username: Some("robot".into()),
password: Some("s3cret".into()),
..RepoAdd::new("web", "https://x")
},
&[
"repo",
"add",
"web",
"https://x",
"--username",
"robot",
"--password-stdin",
],
Some("s3cret"),
),
(
RepoAdd {
pass_credentials: true,
force_update: true,
allow_deprecated_repos: true,
insecure_skip_tls_verify: true,
..RepoAdd::new("web", "https://x")
},
&[
"repo",
"add",
"web",
"https://x",
"--pass-credentials",
"--force-update",
"--allow-deprecated-repos",
"--insecure-skip-tls-verify",
],
None,
),
(
RepoAdd {
cert_file: Some("/c".into()),
key_file: Some("/k".into()),
ca_file: Some("/ca".into()),
timeout: Some(Duration::from_secs(300)),
..RepoAdd::new("web", "https://x")
},
&[
"repo",
"add",
"web",
"https://x",
"--cert-file",
"/c",
"--key-file",
"/k",
"--ca-file",
"/ca",
"--timeout",
"300s",
],
None,
),
];
check(table);
}
#[test]
fn repo_add_password_never_reaches_argv_and_needs_a_username() {
let with_password = RepoAdd {
username: Some("robot".into()),
password: Some("s3cret".into()),
..RepoAdd::new("web", "https://x")
};
let without_username = RepoAdd {
password: Some("s3cret".into()),
..RepoAdd::new("web", "https://x")
};
assert!(!build(&with_password).args.join(" ").contains("s3cret"));
assert!(matches!(
build_error(&without_username),
Error::InvalidCommand(_)
));
}
#[test]
fn the_other_repo_commands_map_their_flags() {
check(vec![
(RepoIndex::new("charts"), &["repo", "index", "charts"], None),
(
RepoIndex {
url: Some("https://c".into()),
merge: Some(PathBuf::from("old.yaml")),
json: true,
..RepoIndex::new("charts")
},
&[
"repo",
"index",
"charts",
"--url",
"https://c",
"--merge",
"old.yaml",
"--json",
],
None,
),
]);
check(vec![(RepoList, &["repo", "list", "-o", "json"], None)]);
check(vec![(
RepoRemove::new(["a", "b"]),
&["repo", "remove", "a", "b"],
None,
)]);
check(vec![
(RepoUpdate::default(), &["repo", "update"], None),
(
RepoUpdate {
names: vec!["a".into()],
timeout: Some(Duration::from_millis(1500)),
},
&["repo", "update", "a", "--timeout", "1500ms"],
None,
),
]);
assert!(matches!(
build_error(&RepoRemove::new(Vec::<String>::new())),
Error::InvalidCommand(_)
));
}
#[test]
fn repo_list_json_is_parsed_from_a_real_capture() {
let repos = RepoList.parse(LIST.to_owned()).expect("parses");
assert_eq!(repos.len(), 2);
assert_eq!(repos[0].name, "stable");
assert_eq!(repos[1].url, "https://charts.example.com/private");
assert_eq!(RepoList.parse("[]".to_owned()).expect("empty").len(), 0);
assert!(matches!(
RepoList.parse("nope".to_owned()),
Err(Error::Parse { .. })
));
}