2021-11-30 15:31:19 +00:00
|
|
|
use crate::utils;
|
2021-03-10 17:39:50 +00:00
|
|
|
|
2022-02-12 03:48:29 +00:00
|
|
|
use clap::{error::ErrorKind, Arg, Command};
|
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")
|
2021-03-10 17:39:50 +00:00
|
|
|
.arg(Arg::new("config").long("config").takes_value(true))
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(&["config", "--config", ""])
|
|
|
|
.unwrap();
|
2021-03-10 17:39:50 +00:00
|
|
|
assert_eq!(m.value_of("config"), Some(""));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn 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").takes_value(true))
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(&["config", "--config="])
|
|
|
|
.unwrap();
|
2021-03-10 17:39:50 +00:00
|
|
|
assert_eq!(m.value_of("config"), Some(""));
|
|
|
|
|
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').takes_value(true))
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(&["config", "-c="])
|
|
|
|
.unwrap();
|
2021-03-10 17:39:50 +00:00
|
|
|
assert_eq!(m.value_of("config"), Some(""))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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")
|
|
|
|
.takes_value(true)
|
|
|
|
.forbid_empty_values(true),
|
|
|
|
)
|
|
|
|
.try_get_matches_from(&["config", "--config", ""]);
|
|
|
|
assert!(m.is_err());
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(m.unwrap_err().kind(), ErrorKind::EmptyValue);
|
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')
|
|
|
|
.takes_value(true)
|
|
|
|
.forbid_empty_values(true),
|
|
|
|
)
|
|
|
|
.try_get_matches_from(&["config", "-c", ""]);
|
|
|
|
assert!(m.is_err());
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(m.unwrap_err().kind(), ErrorKind::EmptyValue)
|
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")
|
|
|
|
.takes_value(true)
|
2021-11-29 14:56:30 +00:00
|
|
|
.forbid_empty_values(true),
|
2021-03-10 17:39:50 +00:00
|
|
|
)
|
|
|
|
.try_get_matches_from(&["config", "--config="]);
|
|
|
|
assert!(m.is_err());
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(m.unwrap_err().kind(), ErrorKind::EmptyValue);
|
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')
|
|
|
|
.takes_value(true)
|
2021-11-29 14:56:30 +00:00
|
|
|
.forbid_empty_values(true),
|
2021-03-10 17:39:50 +00:00
|
|
|
)
|
|
|
|
.try_get_matches_from(&["config", "-c="]);
|
|
|
|
assert!(m.is_err());
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(m.unwrap_err().kind(), ErrorKind::EmptyValue);
|
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")
|
|
|
|
.takes_value(true)
|
2021-11-29 14:56:30 +00:00
|
|
|
.forbid_empty_values(true),
|
2021-03-10 17:39:50 +00:00
|
|
|
)
|
|
|
|
.try_get_matches_from(&["config", "--config"]);
|
|
|
|
assert!(m.is_err());
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(m.unwrap_err().kind(), ErrorKind::EmptyValue);
|
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')
|
|
|
|
.takes_value(true)
|
2021-11-29 14:56:30 +00:00
|
|
|
.forbid_empty_values(true),
|
2021-03-10 17:39:50 +00:00
|
|
|
)
|
|
|
|
.try_get_matches_from(&["config", "-c"]);
|
|
|
|
assert!(m.is_err());
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(m.unwrap_err().kind(), ErrorKind::EmptyValue)
|
2021-03-10 17:39:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn no_empty_values_without_equals_but_requires_equals() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let app = Command::new("config").arg(
|
2021-03-10 17:39:50 +00:00
|
|
|
Arg::new("config")
|
|
|
|
.long("config")
|
|
|
|
.takes_value(true)
|
2021-11-29 14:56:30 +00:00
|
|
|
.forbid_empty_values(true)
|
|
|
|
.require_equals(true),
|
2021-03-10 17:39:50 +00:00
|
|
|
);
|
|
|
|
let m = app.clone().try_get_matches_from(&["config", "--config"]);
|
|
|
|
// 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 =
|
|
|
|
"error: Equal sign is needed when assigning values to '--config=<config>'.
|
|
|
|
|
|
|
|
USAGE:
|
|
|
|
config [OPTIONS]
|
|
|
|
|
2021-09-24 15:58:39 +00:00
|
|
|
For more information try --help
|
|
|
|
";
|
2021-03-10 17:39:50 +00:00
|
|
|
|
|
|
|
assert!(utils::compare_output(
|
|
|
|
app,
|
|
|
|
"config --config",
|
|
|
|
NO_EUQALS_ERROR,
|
|
|
|
true
|
|
|
|
));
|
|
|
|
}
|