2022-07-26 00:17:01 +00:00
|
|
|
use clap::{error::ErrorKind, Arg, ArgAction, Command};
|
2021-03-10 17:39:50 +00:00
|
|
|
|
2022-09-19 14:59:04 +00:00
|
|
|
#[cfg(feature = "error-context")]
|
|
|
|
use super::utils;
|
|
|
|
|
2021-03-10 17:39:50 +00:00
|
|
|
#[test]
|
|
|
|
fn empty_values() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("config")
|
2022-07-26 00:17:01 +00:00
|
|
|
.arg(Arg::new("config").long("config").action(ArgAction::Set))
|
2022-11-24 13:54:25 +00:00
|
|
|
.try_get_matches_from(["config", "--config", ""])
|
2021-12-27 18:56:12 +00:00
|
|
|
.unwrap();
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(m.get_one::<String>("config").map(|v| v.as_str()), Some(""));
|
2021-03-10 17:39:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn empty_values_with_equals() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("config")
|
2022-07-26 00:17:01 +00:00
|
|
|
.arg(Arg::new("config").long("config").action(ArgAction::Set))
|
2022-11-24 13:54:25 +00:00
|
|
|
.try_get_matches_from(["config", "--config="])
|
2021-12-27 18:56:12 +00:00
|
|
|
.unwrap();
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(m.get_one::<String>("config").map(|v| v.as_str()), Some(""));
|
2021-03-10 17:39:50 +00:00
|
|
|
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("config")
|
2022-07-26 00:17:01 +00:00
|
|
|
.arg(Arg::new("config").short('c').action(ArgAction::Set))
|
2022-11-24 13:54:25 +00:00
|
|
|
.try_get_matches_from(["config", "-c="])
|
2021-12-27 18:56:12 +00:00
|
|
|
.unwrap();
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(m.get_one::<String>("config").map(|v| v.as_str()), Some(""))
|
2021-03-10 17:39:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn no_empty_values() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("config")
|
2021-03-10 17:39:50 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("config")
|
|
|
|
.long("config")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2022-05-23 21:58:07 +00:00
|
|
|
.value_parser(clap::builder::NonEmptyStringValueParser::new()),
|
2021-03-10 17:39:50 +00:00
|
|
|
)
|
2022-11-24 13:54:25 +00:00
|
|
|
.try_get_matches_from(["config", "--config", ""]);
|
2021-03-10 17:39:50 +00:00
|
|
|
assert!(m.is_err());
|
2022-07-22 14:03:28 +00:00
|
|
|
assert_eq!(m.unwrap_err().kind(), ErrorKind::InvalidValue);
|
2021-03-10 17:39:50 +00:00
|
|
|
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("config")
|
2021-03-10 17:39:50 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("config")
|
|
|
|
.short('c')
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2022-05-23 21:58:07 +00:00
|
|
|
.value_parser(clap::builder::NonEmptyStringValueParser::new()),
|
2021-03-10 17:39:50 +00:00
|
|
|
)
|
2022-11-24 13:54:25 +00:00
|
|
|
.try_get_matches_from(["config", "-c", ""]);
|
2021-03-10 17:39:50 +00:00
|
|
|
assert!(m.is_err());
|
2022-07-22 14:03:28 +00:00
|
|
|
assert_eq!(m.unwrap_err().kind(), ErrorKind::InvalidValue)
|
2021-03-10 17:39:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn no_empty_values_with_equals() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("config")
|
2021-03-10 17:39:50 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("config")
|
|
|
|
.long("config")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2022-05-23 21:58:07 +00:00
|
|
|
.value_parser(clap::builder::NonEmptyStringValueParser::new()),
|
2021-03-10 17:39:50 +00:00
|
|
|
)
|
2022-11-24 13:54:25 +00:00
|
|
|
.try_get_matches_from(["config", "--config="]);
|
2021-03-10 17:39:50 +00:00
|
|
|
assert!(m.is_err());
|
2022-07-22 14:03:28 +00:00
|
|
|
assert_eq!(m.unwrap_err().kind(), ErrorKind::InvalidValue);
|
2021-03-10 17:39:50 +00:00
|
|
|
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("config")
|
2021-03-10 17:39:50 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("config")
|
|
|
|
.short('c')
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2022-05-23 21:58:07 +00:00
|
|
|
.value_parser(clap::builder::NonEmptyStringValueParser::new()),
|
2021-03-10 17:39:50 +00:00
|
|
|
)
|
2022-11-24 13:54:25 +00:00
|
|
|
.try_get_matches_from(["config", "-c="]);
|
2021-03-10 17:39:50 +00:00
|
|
|
assert!(m.is_err());
|
2022-07-22 14:03:28 +00:00
|
|
|
assert_eq!(m.unwrap_err().kind(), ErrorKind::InvalidValue);
|
2021-03-10 17:39:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn no_empty_values_without_equals() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("config")
|
2021-03-10 17:39:50 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("config")
|
|
|
|
.long("config")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2022-05-23 21:58:07 +00:00
|
|
|
.value_parser(clap::builder::NonEmptyStringValueParser::new()),
|
2021-03-10 17:39:50 +00:00
|
|
|
)
|
2022-11-24 13:54:25 +00:00
|
|
|
.try_get_matches_from(["config", "--config"]);
|
2021-03-10 17:39:50 +00:00
|
|
|
assert!(m.is_err());
|
2022-07-22 14:03:28 +00:00
|
|
|
assert_eq!(m.unwrap_err().kind(), ErrorKind::InvalidValue);
|
2021-03-10 17:39:50 +00:00
|
|
|
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("config")
|
2021-03-10 17:39:50 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("config")
|
|
|
|
.short('c')
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2022-05-23 21:58:07 +00:00
|
|
|
.value_parser(clap::builder::NonEmptyStringValueParser::new()),
|
2021-03-10 17:39:50 +00:00
|
|
|
)
|
2022-11-24 13:54:25 +00:00
|
|
|
.try_get_matches_from(["config", "-c"]);
|
2021-03-10 17:39:50 +00:00
|
|
|
assert!(m.is_err());
|
2022-07-22 14:03:28 +00:00
|
|
|
assert_eq!(m.unwrap_err().kind(), ErrorKind::InvalidValue)
|
2021-03-10 17:39:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-09-19 14:59:04 +00:00
|
|
|
#[cfg(feature = "error-context")]
|
2021-03-10 17:39:50 +00:00
|
|
|
fn no_empty_values_without_equals_but_requires_equals() {
|
2022-02-14 21:47:20 +00:00
|
|
|
let cmd = Command::new("config").arg(
|
2021-03-10 17:39:50 +00:00
|
|
|
Arg::new("config")
|
|
|
|
.long("config")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2022-05-23 21:58:07 +00:00
|
|
|
.value_parser(clap::builder::NonEmptyStringValueParser::new())
|
2021-11-29 14:56:30 +00:00
|
|
|
.require_equals(true),
|
2021-03-10 17:39:50 +00:00
|
|
|
);
|
2022-11-24 13:54:25 +00:00
|
|
|
let m = cmd.clone().try_get_matches_from(["config", "--config"]);
|
2021-03-10 17:39:50 +00:00
|
|
|
// Should error on no equals rather than empty value.
|
|
|
|
assert!(m.is_err());
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(m.unwrap_err().kind(), ErrorKind::NoEquals);
|
2021-03-10 17:39:50 +00:00
|
|
|
|
|
|
|
static NO_EUQALS_ERROR: &str =
|
2023-01-03 16:22:40 +00:00
|
|
|
"error: equal sign is needed when assigning values to '--config=<config>'
|
2021-03-10 17:39:50 +00:00
|
|
|
|
2022-09-07 16:03:55 +00:00
|
|
|
Usage: config [OPTIONS]
|
2021-03-10 17:39:50 +00:00
|
|
|
|
2023-01-03 19:22:35 +00:00
|
|
|
For more information, try '--help'.
|
2021-09-24 15:58:39 +00:00
|
|
|
";
|
2021-03-10 17:39:50 +00:00
|
|
|
|
2022-04-29 20:32:25 +00:00
|
|
|
utils::assert_output(cmd, "config --config", NO_EUQALS_ERROR, true);
|
2021-03-10 17:39:50 +00:00
|
|
|
}
|