2021-11-30 15:31:19 +00:00
|
|
|
use crate::utils;
|
2016-01-13 17:03:27 +00:00
|
|
|
|
2017-01-03 04:05:23 +00:00
|
|
|
use std::str;
|
|
|
|
|
2021-10-04 14:01:09 +00:00
|
|
|
use clap::{App, AppSettings, Arg, ErrorKind};
|
2016-01-13 17:03:27 +00:00
|
|
|
|
2021-10-08 17:05:40 +00:00
|
|
|
fn common() -> App<'static> {
|
|
|
|
App::new("foo")
|
2016-01-13 17:03:27 +00:00
|
|
|
}
|
|
|
|
|
2021-10-08 17:05:40 +00:00
|
|
|
fn with_version() -> App<'static> {
|
|
|
|
common().version("3.0")
|
2016-01-13 17:03:27 +00:00
|
|
|
}
|
2016-05-30 08:49:13 +00:00
|
|
|
|
2021-10-08 17:05:40 +00:00
|
|
|
fn with_long_version() -> App<'static> {
|
|
|
|
common().long_version("3.0 (abcdefg)")
|
2016-05-30 08:49:13 +00:00
|
|
|
}
|
2017-04-04 23:54:20 +00:00
|
|
|
|
2021-10-08 17:05:40 +00:00
|
|
|
fn with_subcommand() -> App<'static> {
|
|
|
|
with_version().subcommand(App::new("bar").subcommand(App::new("baz")))
|
2021-02-07 11:58:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-10-08 17:05:40 +00:00
|
|
|
fn no_version_flag_short() {
|
2021-11-17 20:23:22 +00:00
|
|
|
let res = common().try_get_matches_from("foo -V".split(' '));
|
2021-02-07 11:58:43 +00:00
|
|
|
|
2021-10-08 17:05:40 +00:00
|
|
|
assert!(res.is_err());
|
|
|
|
let err = res.unwrap_err();
|
|
|
|
assert_eq!(err.kind, clap::ErrorKind::UnknownArgument);
|
|
|
|
assert_eq!(err.info, ["-V"]);
|
2021-02-07 11:58:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-10-08 17:05:40 +00:00
|
|
|
fn no_version_flag_long() {
|
2021-11-17 20:23:22 +00:00
|
|
|
let res = common().try_get_matches_from("foo --version".split(' '));
|
2021-02-07 11:58:43 +00:00
|
|
|
|
2021-10-08 17:05:40 +00:00
|
|
|
assert!(res.is_err());
|
|
|
|
let err = res.unwrap_err();
|
|
|
|
assert_eq!(err.kind, clap::ErrorKind::UnknownArgument);
|
|
|
|
assert_eq!(err.info, ["--version"]);
|
2021-02-07 11:58:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-10-08 17:05:40 +00:00
|
|
|
fn version_flag_from_version_short() {
|
2021-11-17 20:23:22 +00:00
|
|
|
let res = with_version().try_get_matches_from("foo -V".split(' '));
|
2021-02-07 11:58:43 +00:00
|
|
|
|
2021-10-08 17:05:40 +00:00
|
|
|
assert!(res.is_err());
|
|
|
|
let err = res.unwrap_err();
|
|
|
|
assert_eq!(err.kind, ErrorKind::DisplayVersion);
|
|
|
|
assert_eq!(err.to_string(), "foo 3.0\n");
|
2021-02-07 11:58:43 +00:00
|
|
|
}
|
|
|
|
|
2021-10-08 17:05:40 +00:00
|
|
|
#[test]
|
|
|
|
fn version_flag_from_version_long() {
|
2021-11-17 20:23:22 +00:00
|
|
|
let res = with_version().try_get_matches_from("foo --version".split(' '));
|
2020-10-26 08:37:45 +00:00
|
|
|
|
2021-10-08 17:05:40 +00:00
|
|
|
assert!(res.is_err());
|
|
|
|
let err = res.unwrap_err();
|
|
|
|
assert_eq!(err.kind, ErrorKind::DisplayVersion);
|
|
|
|
assert_eq!(err.to_string(), "foo 3.0\n");
|
|
|
|
}
|
2020-10-26 08:37:45 +00:00
|
|
|
|
2017-04-04 23:54:20 +00:00
|
|
|
#[test]
|
2021-10-08 17:05:40 +00:00
|
|
|
fn version_flag_from_long_version_short() {
|
2021-11-17 20:23:22 +00:00
|
|
|
let res = with_long_version().try_get_matches_from("foo -V".split(' '));
|
2020-10-26 08:37:45 +00:00
|
|
|
|
2021-10-08 17:05:40 +00:00
|
|
|
assert!(res.is_err());
|
|
|
|
let err = res.unwrap_err();
|
2020-10-26 08:37:45 +00:00
|
|
|
assert_eq!(err.kind, ErrorKind::DisplayVersion);
|
2021-10-08 17:05:40 +00:00
|
|
|
assert_eq!(err.to_string(), "foo 3.0 (abcdefg)\n");
|
|
|
|
}
|
2020-10-26 08:37:45 +00:00
|
|
|
|
2021-10-08 17:05:40 +00:00
|
|
|
#[test]
|
|
|
|
fn version_flag_from_long_version_long() {
|
2021-11-17 20:23:22 +00:00
|
|
|
let res = with_long_version().try_get_matches_from("foo --version".split(' '));
|
2020-10-26 08:37:45 +00:00
|
|
|
|
2021-10-08 17:05:40 +00:00
|
|
|
assert!(res.is_err());
|
|
|
|
let err = res.unwrap_err();
|
2020-10-26 08:37:45 +00:00
|
|
|
assert_eq!(err.kind, ErrorKind::DisplayVersion);
|
2021-10-08 17:05:40 +00:00
|
|
|
assert_eq!(err.to_string(), "foo 3.0 (abcdefg)\n");
|
2020-10-26 08:37:45 +00:00
|
|
|
}
|
|
|
|
|
2021-10-08 17:05:40 +00:00
|
|
|
#[test]
|
|
|
|
fn override_version_long_with_user_flag() {
|
|
|
|
let res = with_version()
|
|
|
|
.arg(Arg::new("ver").long("version"))
|
2021-11-17 20:23:22 +00:00
|
|
|
.try_get_matches_from("foo --version".split(' '));
|
2020-10-26 08:37:45 +00:00
|
|
|
|
2021-10-08 17:05:40 +00:00
|
|
|
assert!(res.is_ok());
|
|
|
|
let m = res.unwrap();
|
|
|
|
assert!(m.is_present("ver"));
|
|
|
|
}
|
2017-04-04 23:54:20 +00:00
|
|
|
|
2020-10-26 08:37:45 +00:00
|
|
|
#[test]
|
2021-10-08 17:05:40 +00:00
|
|
|
fn override_version_long_with_user_flag_no_version_flag() {
|
|
|
|
let res = with_version()
|
|
|
|
.arg(Arg::new("ver").long("version"))
|
2021-11-17 20:23:22 +00:00
|
|
|
.try_get_matches_from("foo -V".split(' '));
|
2020-10-26 08:37:45 +00:00
|
|
|
|
2021-10-08 17:05:40 +00:00
|
|
|
assert!(res.is_err());
|
|
|
|
let err = res.unwrap_err();
|
|
|
|
assert_eq!(err.kind, ErrorKind::UnknownArgument);
|
|
|
|
}
|
2020-10-26 08:37:45 +00:00
|
|
|
|
2021-10-08 17:05:40 +00:00
|
|
|
#[test]
|
|
|
|
fn override_version_short_with_user_flag() {
|
|
|
|
let res = with_version()
|
|
|
|
.arg(Arg::new("ver").short('V'))
|
2021-11-17 20:23:22 +00:00
|
|
|
.try_get_matches_from("foo -V".split(' '));
|
2020-10-26 08:37:45 +00:00
|
|
|
|
2021-10-08 17:05:40 +00:00
|
|
|
assert!(res.is_ok());
|
|
|
|
let m = res.unwrap();
|
|
|
|
assert!(m.is_present("ver"));
|
|
|
|
}
|
2020-10-26 08:37:45 +00:00
|
|
|
|
2021-10-08 17:05:40 +00:00
|
|
|
#[test]
|
|
|
|
fn override_version_short_with_user_flag_long_still_works() {
|
|
|
|
let res = with_version()
|
|
|
|
.arg(Arg::new("ver").short('V'))
|
2021-11-17 20:23:22 +00:00
|
|
|
.try_get_matches_from("foo --version".split(' '));
|
2020-10-26 08:37:45 +00:00
|
|
|
|
2021-10-08 17:05:40 +00:00
|
|
|
assert!(res.is_err());
|
|
|
|
let err = res.unwrap_err();
|
|
|
|
assert_eq!(err.kind, ErrorKind::DisplayVersion);
|
2020-10-26 08:37:45 +00:00
|
|
|
}
|
|
|
|
|
2021-10-08 17:05:40 +00:00
|
|
|
#[test]
|
|
|
|
fn mut_version_short() {
|
|
|
|
let res = with_version()
|
|
|
|
.mut_arg("version", |a| a.short('z'))
|
2021-11-17 20:23:22 +00:00
|
|
|
.try_get_matches_from("foo -z".split(' '));
|
2020-10-26 08:37:45 +00:00
|
|
|
|
2021-10-08 17:05:40 +00:00
|
|
|
assert!(res.is_err());
|
|
|
|
let err = res.unwrap_err();
|
|
|
|
assert_eq!(err.kind, ErrorKind::DisplayVersion);
|
|
|
|
}
|
2020-10-26 08:37:45 +00:00
|
|
|
|
|
|
|
#[test]
|
2021-10-08 17:05:40 +00:00
|
|
|
fn mut_version_long() {
|
|
|
|
let res = with_version()
|
|
|
|
.mut_arg("version", |a| a.long("qux"))
|
2021-11-17 20:23:22 +00:00
|
|
|
.try_get_matches_from("foo --qux".split(' '));
|
2020-10-26 08:37:45 +00:00
|
|
|
|
2021-10-08 17:05:40 +00:00
|
|
|
assert!(res.is_err());
|
|
|
|
let err = res.unwrap_err();
|
|
|
|
assert_eq!(err.kind, ErrorKind::DisplayVersion);
|
2021-02-07 16:14:07 +00:00
|
|
|
}
|
|
|
|
|
2021-10-08 17:05:40 +00:00
|
|
|
static VERSION_ABOUT_MULTI_SC: &str = "foo-bar-baz 3.0
|
2021-02-07 16:14:07 +00:00
|
|
|
|
|
|
|
USAGE:
|
2021-10-08 17:05:40 +00:00
|
|
|
foo bar baz
|
2021-02-07 16:14:07 +00:00
|
|
|
|
2021-10-11 22:01:33 +00:00
|
|
|
OPTIONS:
|
2021-07-30 03:23:25 +00:00
|
|
|
-h, --help Print help information
|
2021-09-24 15:58:39 +00:00
|
|
|
-V, --version Print custom version about text
|
|
|
|
";
|
2021-02-07 16:14:07 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn version_about_multi_subcmd() {
|
2021-10-08 17:05:40 +00:00
|
|
|
let app = with_subcommand()
|
2021-11-18 16:17:15 +00:00
|
|
|
.mut_arg("version", |a| a.help("Print custom version about text"))
|
2021-10-08 17:05:40 +00:00
|
|
|
.global_setting(AppSettings::PropagateVersion);
|
2021-02-07 16:14:07 +00:00
|
|
|
|
|
|
|
assert!(utils::compare_output(
|
|
|
|
app,
|
2021-10-08 17:05:40 +00:00
|
|
|
"foo bar baz -h",
|
2021-02-07 16:14:07 +00:00
|
|
|
VERSION_ABOUT_MULTI_SC,
|
|
|
|
false
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2021-10-08 17:05:40 +00:00
|
|
|
#[test]
|
|
|
|
fn no_propagation_by_default_long() {
|
|
|
|
// Version Flag should not be propagated to subcommands
|
2021-11-17 20:23:22 +00:00
|
|
|
let res = with_subcommand().try_get_matches_from("foo bar --version".split(' '));
|
2021-02-07 16:14:07 +00:00
|
|
|
|
2021-10-08 17:05:40 +00:00
|
|
|
assert!(res.is_err());
|
|
|
|
let err = res.unwrap_err();
|
|
|
|
assert_eq!(err.kind, ErrorKind::UnknownArgument);
|
|
|
|
assert_eq!(err.info, &["--version"]);
|
|
|
|
}
|
2021-02-07 16:14:07 +00:00
|
|
|
|
|
|
|
#[test]
|
2021-10-08 17:05:40 +00:00
|
|
|
fn no_propagation_by_default_short() {
|
2021-11-17 20:23:22 +00:00
|
|
|
let res = with_subcommand().try_get_matches_from("foo bar -V".split(' '));
|
2021-02-07 16:14:07 +00:00
|
|
|
|
2021-10-08 17:05:40 +00:00
|
|
|
assert!(res.is_err());
|
|
|
|
let err = res.unwrap_err();
|
|
|
|
assert_eq!(err.kind, ErrorKind::UnknownArgument);
|
|
|
|
assert_eq!(err.info, &["-V"]);
|
2018-01-25 04:05:05 +00:00
|
|
|
}
|
2021-10-04 14:01:09 +00:00
|
|
|
|
|
|
|
#[test]
|
2021-10-08 17:05:40 +00:00
|
|
|
fn propagate_version_long() {
|
|
|
|
let res = with_subcommand()
|
|
|
|
.setting(AppSettings::PropagateVersion)
|
2021-11-17 20:23:22 +00:00
|
|
|
.try_get_matches_from("foo bar --version".split(' '));
|
2021-10-08 17:05:40 +00:00
|
|
|
|
2021-10-04 14:01:09 +00:00
|
|
|
assert!(res.is_err());
|
|
|
|
let err = res.unwrap_err();
|
2021-10-08 17:05:40 +00:00
|
|
|
assert_eq!(err.kind, ErrorKind::DisplayVersion);
|
2021-10-04 14:01:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-10-08 17:05:40 +00:00
|
|
|
fn propagate_version_short() {
|
|
|
|
let res = with_subcommand()
|
|
|
|
.setting(AppSettings::PropagateVersion)
|
2021-11-17 20:23:22 +00:00
|
|
|
.try_get_matches_from("foo bar -V".split(' '));
|
2021-10-08 17:05:40 +00:00
|
|
|
|
2021-10-04 14:01:09 +00:00
|
|
|
assert!(res.is_err());
|
|
|
|
let err = res.unwrap_err();
|
2021-10-08 17:05:40 +00:00
|
|
|
assert_eq!(err.kind, ErrorKind::DisplayVersion);
|
2021-10-04 14:01:09 +00:00
|
|
|
}
|
|
|
|
|
2021-10-08 17:05:40 +00:00
|
|
|
#[cfg(debug_assertions)]
|
2021-10-04 14:01:09 +00:00
|
|
|
#[test]
|
2021-10-08 17:05:40 +00:00
|
|
|
#[should_panic = "Used App::mut_arg(\"version\", ..) without providing App::version, App::long_version or using AppSettings::NoAutoVersion"]
|
|
|
|
fn mut_arg_version_panic() {
|
|
|
|
let _res = common()
|
|
|
|
.mut_arg("version", |v| v.short('z'))
|
2021-11-17 20:23:22 +00:00
|
|
|
.try_get_matches_from("foo -z".split(' '));
|
2021-10-08 17:05:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn mut_arg_version_no_auto_version() {
|
|
|
|
let res = common()
|
|
|
|
.mut_arg("version", |v| v.short('z'))
|
|
|
|
.setting(AppSettings::NoAutoVersion)
|
2021-11-17 20:23:22 +00:00
|
|
|
.try_get_matches_from("foo -z".split(' '));
|
2021-10-08 17:05:40 +00:00
|
|
|
|
|
|
|
assert!(res.is_ok());
|
|
|
|
assert!(res.unwrap().is_present("version"));
|
2021-10-04 14:01:09 +00:00
|
|
|
}
|
|
|
|
|
2021-10-08 17:05:40 +00:00
|
|
|
#[cfg(debug_assertions)]
|
2021-10-04 14:01:09 +00:00
|
|
|
#[test]
|
2021-10-08 17:05:40 +00:00
|
|
|
#[should_panic = "No version information via App::version or App::long_version to propagate"]
|
|
|
|
fn propagate_version_no_version_info() {
|
|
|
|
let _res = common()
|
|
|
|
.setting(AppSettings::PropagateVersion)
|
|
|
|
.subcommand(App::new("bar"))
|
2021-11-17 20:23:22 +00:00
|
|
|
.try_get_matches_from("foo".split(' '));
|
2021-10-04 14:01:09 +00:00
|
|
|
}
|