clap/tests/builder/groups.rs

335 lines
9.9 KiB
Rust
Raw Normal View History

use crate::utils;
2022-02-12 03:48:29 +00:00
use clap::{arg, error::ErrorKind, Arg, ArgGroup, Command};
static REQ_GROUP_USAGE: &str = "error: The following required arguments were not provided:
<base|--delete>
USAGE:
clap-test <base|--delete>
For more information try --help
";
static REQ_GROUP_CONFLICT_USAGE: &str =
2020-11-07 17:28:22 +00:00
"error: The argument '--delete' cannot be used with '<base>'
2020-04-23 17:56:42 +00:00
USAGE:
clap-test <base|--delete>
For more information try --help
";
2020-04-23 17:56:42 +00:00
static REQ_GROUP_CONFLICT_ONLY_OPTIONS: &str =
2020-11-07 17:28:22 +00:00
"error: The argument '--delete' cannot be used with '--all'
2020-04-23 17:19:11 +00:00
USAGE:
2020-11-07 17:28:22 +00:00
clap-test <--all|--delete>
For more information try --help
";
#[test]
fn required_group_missing_arg() {
2022-02-12 03:48:29 +00:00
let result = Command::new("group")
2021-11-19 20:33:11 +00:00
.arg(arg!(-f --flag "some flag"))
.arg(arg!( -c --color "some other flag"))
.group(ArgGroup::new("req").args(&["flag", "color"]).required(true))
.try_get_matches_from(vec![""]);
2015-09-28 20:23:17 +00:00
assert!(result.is_err());
let err = result.err().unwrap();
2022-01-25 22:19:28 +00:00
assert_eq!(err.kind(), ErrorKind::MissingRequiredArgument);
}
2020-04-09 14:21:45 +00:00
#[cfg(debug_assertions)]
#[test]
2020-04-09 22:33:16 +00:00
#[should_panic = "Argument group 'req' contains non-existent argument"]
fn non_existing_arg() {
2022-02-12 03:48:29 +00:00
let _ = Command::new("group")
2021-11-19 20:33:11 +00:00
.arg(arg!(-f --flag "some flag"))
.arg(arg!(-c --color "some other flag"))
.group(ArgGroup::new("req").args(&["flg", "color"]).required(true))
.try_get_matches_from(vec![""]);
}
2020-04-09 14:21:45 +00:00
#[cfg(debug_assertions)]
#[test]
2020-04-09 22:33:16 +00:00
#[should_panic = "Argument group name must be unique\n\n\t'req' is already in use"]
2020-04-09 14:21:45 +00:00
fn unique_group_name() {
2022-02-12 03:48:29 +00:00
let _ = Command::new("group")
2021-11-19 20:33:11 +00:00
.arg(arg!(-f --flag "some flag"))
.arg(arg!(-c --color "some other flag"))
.group(ArgGroup::new("req").args(&["flag"]).required(true))
.group(ArgGroup::new("req").args(&["color"]).required(true))
2020-04-09 14:21:45 +00:00
.try_get_matches_from(vec![""]);
}
2020-04-09 16:19:05 +00:00
#[cfg(debug_assertions)]
2020-04-09 14:21:45 +00:00
#[test]
2020-04-09 22:33:16 +00:00
#[should_panic = "Argument group name '' must not conflict with argument name"]
fn groups_new_of_arg_name() {
2022-02-12 03:48:29 +00:00
let _ = Command::new("group")
.arg(Arg::new("a").long("a").group("a"))
2020-04-09 14:21:45 +00:00
.try_get_matches_from(vec!["", "--a"]);
}
2020-04-09 16:19:05 +00:00
#[cfg(debug_assertions)]
2020-04-09 14:21:45 +00:00
#[test]
2020-04-09 22:33:16 +00:00
#[should_panic = "Argument group name 'a' must not conflict with argument name"]
fn arg_group_new_of_arg_name() {
2022-02-12 03:48:29 +00:00
let _ = Command::new("group")
.arg(Arg::new("a").long("a").group("a"))
.group(ArgGroup::new("a"))
2020-04-09 14:21:45 +00:00
.try_get_matches_from(vec!["", "--a"]);
}
#[test]
fn group_single_value() {
2022-02-12 03:48:29 +00:00
let res = Command::new("group")
2021-11-19 20:33:11 +00:00
.arg(arg!(-f --flag "some flag"))
.arg(arg!(-c --color [color] "some option"))
.group(ArgGroup::new("grp").args(&["flag", "color"]))
.try_get_matches_from(vec!["", "-c", "blue"]);
2021-12-27 19:57:38 +00:00
assert!(res.is_ok(), "{}", res.unwrap_err());
let m = res.unwrap();
assert!(m.is_present("grp"));
assert_eq!(
m.get_one::<String>("grp").map(|v| v.as_str()).unwrap(),
"blue"
);
}
#[test]
fn group_single_flag() {
2022-02-12 03:48:29 +00:00
let res = Command::new("group")
2021-11-19 20:33:11 +00:00
.arg(arg!(-f --flag "some flag"))
.arg(arg!(-c --color [color] "some option"))
.group(ArgGroup::new("grp").args(&["flag", "color"]))
.try_get_matches_from(vec!["", "-f"]);
2021-12-27 19:57:38 +00:00
assert!(res.is_ok(), "{}", res.unwrap_err());
let m = res.unwrap();
assert!(m.is_present("grp"));
assert!(matches!(
m.try_get_one::<String>("grp").unwrap_err(),
clap::parser::MatchesError::ExpectedOne { .. }
));
}
#[test]
fn group_empty() {
2022-02-12 03:48:29 +00:00
let res = Command::new("group")
2021-11-19 20:33:11 +00:00
.arg(arg!(-f --flag "some flag"))
.arg(arg!(-c --color [color] "some option"))
.group(ArgGroup::new("grp").args(&["flag", "color"]))
.try_get_matches_from(vec![""]);
2021-12-27 19:57:38 +00:00
assert!(res.is_ok(), "{}", res.unwrap_err());
let m = res.unwrap();
assert!(!m.is_present("grp"));
assert!(m.get_one::<String>("grp").map(|v| v.as_str()).is_none());
}
#[test]
fn group_reqired_flags_empty() {
2022-02-12 03:48:29 +00:00
let result = Command::new("group")
2021-11-19 20:33:11 +00:00
.arg(arg!(-f --flag "some flag"))
.arg(arg!(-c --color "some option"))
.group(ArgGroup::new("grp").required(true).args(&["flag", "color"]))
.try_get_matches_from(vec![""]);
assert!(result.is_err());
let err = result.err().unwrap();
2022-01-25 22:19:28 +00:00
assert_eq!(err.kind(), ErrorKind::MissingRequiredArgument);
}
#[test]
fn group_multi_value_single_arg() {
2022-02-12 03:48:29 +00:00
let res = Command::new("group")
2021-11-19 20:33:11 +00:00
.arg(arg!(-f --flag "some flag"))
.arg(arg!(-c --color <color> "some option").multiple_values(true))
.group(ArgGroup::new("grp").args(&["flag", "color"]))
.try_get_matches_from(vec!["", "-c", "blue", "red", "green"]);
2022-01-25 22:19:28 +00:00
assert!(res.is_ok(), "{:?}", res.unwrap_err().kind());
let m = res.unwrap();
assert!(m.is_present("grp"));
2018-01-25 04:05:05 +00:00
assert_eq!(
&*m.get_many::<String>("grp")
.unwrap()
.map(|v| v.as_str())
.collect::<Vec<_>>(),
2018-01-25 04:05:05 +00:00
&["blue", "red", "green"]
);
}
#[test]
fn empty_group() {
2022-02-12 03:48:29 +00:00
let r = Command::new("empty_group")
2021-11-19 20:33:11 +00:00
.arg(arg!(-f --flag "some flag"))
.group(ArgGroup::new("vers").required(true))
.try_get_matches_from(vec!["empty_prog"]);
assert!(r.is_err());
let err = r.err().unwrap();
2022-01-25 22:19:28 +00:00
assert_eq!(err.kind(), ErrorKind::MissingRequiredArgument);
}
#[test]
fn req_group_usage_string() {
2022-02-14 21:47:20 +00:00
let cmd = Command::new("req_group")
2021-11-19 20:33:11 +00:00
.arg(arg!([base] "Base commit"))
.arg(arg!(
-d --delete "Remove the base commit information"
))
2018-01-25 04:05:05 +00:00
.group(
ArgGroup::new("base_or_delete")
2018-01-25 04:05:05 +00:00
.args(&["base", "delete"])
.required(true),
);
2022-04-29 20:32:25 +00:00
utils::assert_output(cmd, "clap-test", REQ_GROUP_USAGE, true);
}
2016-05-09 20:00:44 +00:00
#[test]
fn req_group_with_conflict_usage_string() {
2022-02-14 21:47:20 +00:00
let cmd = Command::new("req_group")
2021-11-19 20:33:11 +00:00
.arg(arg!([base] "Base commit").conflicts_with("delete"))
.arg(arg!(
-d --delete "Remove the base commit information"
2018-01-25 04:05:05 +00:00
))
.group(
ArgGroup::new("base_or_delete")
2018-01-25 04:05:05 +00:00
.args(&["base", "delete"])
.required(true),
);
2022-04-29 20:32:25 +00:00
utils::assert_output(
2022-02-14 21:47:20 +00:00
cmd,
2018-01-25 04:05:05 +00:00
"clap-test --delete base",
2020-04-23 17:56:42 +00:00
REQ_GROUP_CONFLICT_USAGE,
2022-04-29 20:32:25 +00:00
true,
);
2020-04-23 17:56:42 +00:00
}
#[test]
fn req_group_with_conflict_usage_string_only_options() {
2022-02-14 21:47:20 +00:00
let cmd = Command::new("req_group")
2021-11-19 20:33:11 +00:00
.arg(arg!(-a --all "All").conflicts_with("delete"))
.arg(arg!(
-d --delete "Remove the base commit information"
2020-04-23 17:56:42 +00:00
))
.group(
ArgGroup::new("all_or_delete")
2020-04-23 17:56:42 +00:00
.args(&["all", "delete"])
.required(true),
);
2022-04-29 20:32:25 +00:00
utils::assert_output(
2022-02-14 21:47:20 +00:00
cmd,
2020-04-23 17:56:42 +00:00
"clap-test --delete --all",
REQ_GROUP_CONFLICT_ONLY_OPTIONS,
2022-04-29 20:32:25 +00:00
true,
);
2016-05-09 20:00:44 +00:00
}
#[test]
fn required_group_multiple_args() {
2022-02-12 03:48:29 +00:00
let result = Command::new("group")
2021-11-19 20:33:11 +00:00
.arg(arg!(-f --flag "some flag"))
.arg(arg!(-c --color "some other flag"))
2018-01-25 04:05:05 +00:00
.group(
ArgGroup::new("req")
2018-01-25 04:05:05 +00:00
.args(&["flag", "color"])
.required(true)
.multiple(true),
)
.try_get_matches_from(vec!["group", "-f", "-c"]);
2021-12-27 19:57:38 +00:00
assert!(result.is_ok(), "{}", result.unwrap_err());
let m = result.unwrap();
assert!(m.is_present("flag"));
assert!(m.is_present("color"));
}
#[test]
fn group_multiple_args_error() {
2022-02-12 03:48:29 +00:00
let result = Command::new("group")
2021-11-19 20:33:11 +00:00
.arg(arg!(-f --flag "some flag"))
.arg(arg!(-c --color "some other flag"))
.group(ArgGroup::new("req").args(&["flag", "color"]))
.try_get_matches_from(vec!["group", "-f", "-c"]);
assert!(result.is_err());
let err = result.unwrap_err();
2022-01-25 22:19:28 +00:00
assert_eq!(err.kind(), ErrorKind::ArgumentConflict);
}
#[test]
fn group_overrides_required() {
let command = Command::new("group")
.arg(arg!(--foo <FOO>))
.arg(arg!(--bar <BAR>))
.group(ArgGroup::new("group").args(&["foo", "bar"]).required(true));
let result = command.try_get_matches_from(vec!["group", "--foo", "value"]);
assert!(result.is_ok(), "{}", result.unwrap_err());
let m = result.unwrap();
assert!(m.is_present("foo"));
assert!(!m.is_present("bar"));
}
#[test]
fn group_usage_use_val_name() {
static GROUP_USAGE_USE_VAL_NAME: &str = "prog
USAGE:
prog <A>
ARGS:
<A>
OPTIONS:
-h, --help Print help information
";
2022-02-14 21:47:20 +00:00
let cmd = Command::new("prog")
.arg(Arg::new("a").value_name("A"))
.group(ArgGroup::new("group").arg("a").required(true));
2022-04-29 20:32:25 +00:00
utils::assert_output(cmd, "prog --help", GROUP_USAGE_USE_VAL_NAME, false);
}
#[test]
fn group_acts_like_arg() {
2022-02-12 03:48:29 +00:00
let result = Command::new("prog")
.arg(Arg::new("debug").long("debug").group("mode"))
.arg(Arg::new("verbose").long("verbose").group("mode"))
2021-10-25 17:36:01 +00:00
.try_get_matches_from(vec!["prog", "--debug"]);
2021-12-27 19:57:38 +00:00
assert!(result.is_ok(), "{}", result.unwrap_err());
2021-10-25 17:36:01 +00:00
let m = result.unwrap();
assert!(m.is_present("mode"));
2019-04-05 19:51:22 +00:00
}
2020-04-23 17:19:11 +00:00
/* This is used to be fixed in a hack, we need to find a better way to fix it.
2020-04-23 17:19:11 +00:00
#[test]
fn issue_1794() {
2022-02-14 21:47:20 +00:00
let cmd = clap::Command::new("hello")
2020-04-23 17:19:11 +00:00
.bin_name("deno")
.arg(Arg::new("option1").long("option1").takes_value(false))
.arg(Arg::new("pos1").takes_value(true))
.arg(Arg::new("pos2").takes_value(true))
2020-04-23 17:19:11 +00:00
.group(
ArgGroup::new("arg1")
2020-04-23 17:19:11 +00:00
.args(&["pos1", "option1"])
.required(true),
);
2022-02-14 21:47:20 +00:00
let m = cmd.clone().try_get_matches_from(&["cmd", "pos1", "pos2"]).unwrap();
assert_eq!(m.get_one::<String>("pos1").map(|v| v.as_str()), Some("pos1"));
assert_eq!(m.get_one::<String>("pos2").map(|v| v.as_str()), Some("pos2"));
2020-04-23 17:19:11 +00:00
assert!(!m.is_present("option1"));
2022-02-14 21:47:20 +00:00
let m = cmd
2020-04-23 17:19:11 +00:00
.clone()
2022-02-14 21:47:20 +00:00
.try_get_matches_from(&["cmd", "--option1", "positional"]).unwrap();
assert_eq!(m.get_one::<String>("pos1").map(|v| v.as_str()), None);
assert_eq!(m.get_one::<String>("pos2").map(|v| v.as_str()), Some("positional"));
2020-04-23 17:19:11 +00:00
assert!(m.is_present("option1"));
}
*/