clap/tests/builder/version.rs

168 lines
4.9 KiB
Rust
Raw Normal View History

use clap::{error::ErrorKind, ArgAction, Command};
2022-02-12 03:48:29 +00:00
fn common() -> Command<'static> {
Command::new("foo")
}
2022-02-12 03:48:29 +00:00
fn with_version() -> Command<'static> {
common().version("3.0")
}
2022-02-12 03:48:29 +00:00
fn with_long_version() -> Command<'static> {
common().long_version("3.0 (abcdefg)")
}
2022-02-12 03:48:29 +00:00
fn with_subcommand() -> Command<'static> {
with_version().subcommand(Command::new("bar").subcommand(Command::new("baz")))
}
#[test]
fn no_version_flag_short() {
2021-11-17 20:23:22 +00:00
let res = common().try_get_matches_from("foo -V".split(' '));
assert!(res.is_err());
let err = res.unwrap_err();
assert_eq!(err.kind(), clap::error::ErrorKind::UnknownArgument);
}
#[test]
fn no_version_flag_long() {
2021-11-17 20:23:22 +00:00
let res = common().try_get_matches_from("foo --version".split(' '));
assert!(res.is_err());
let err = res.unwrap_err();
assert_eq!(err.kind(), clap::error::ErrorKind::UnknownArgument);
}
#[test]
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(' '));
assert!(res.is_err());
let err = res.unwrap_err();
2022-01-25 22:19:28 +00:00
assert_eq!(err.kind(), ErrorKind::DisplayVersion);
assert_eq!(err.to_string(), "foo 3.0\n");
}
#[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(' '));
assert!(res.is_err());
let err = res.unwrap_err();
2022-01-25 22:19:28 +00:00
assert_eq!(err.kind(), ErrorKind::DisplayVersion);
assert_eq!(err.to_string(), "foo 3.0\n");
}
#[test]
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(' '));
assert!(res.is_err());
let err = res.unwrap_err();
2022-01-25 22:19:28 +00:00
assert_eq!(err.kind(), ErrorKind::DisplayVersion);
assert_eq!(err.to_string(), "foo 3.0 (abcdefg)\n");
}
#[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(' '));
assert!(res.is_err());
let err = res.unwrap_err();
2022-01-25 22:19:28 +00:00
assert_eq!(err.kind(), ErrorKind::DisplayVersion);
assert_eq!(err.to_string(), "foo 3.0 (abcdefg)\n");
}
#[test]
#[cfg(debug_assertions)]
#[should_panic = "Command foo: Long option names must be unique for each argument, but '--version' is in use by both 'ver' and 'version' (call `cmd.disable_version_flag(true)` to remove the auto-generated `--version`)"]
fn override_version_long_with_user_flag() {
with_version()
.arg(
clap::Arg::new("ver")
.long("version")
.action(ArgAction::SetTrue),
)
.debug_assert();
}
#[test]
#[cfg(debug_assertions)]
#[should_panic = "Command foo: Short option names must be unique for each argument, but '-V' is in use by both 'ver' and 'version' (call `cmd.disable_version_flag(true)` to remove the auto-generated `--version`)"]
fn override_version_short_with_user_flag() {
with_version()
.arg(clap::Arg::new("ver").short('V').action(ArgAction::SetTrue))
.debug_assert();
}
#[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(' '));
assert!(res.is_err());
let err = res.unwrap_err();
2022-01-25 22:19:28 +00:00
assert_eq!(err.kind(), ErrorKind::UnknownArgument);
}
#[test]
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(' '));
assert!(res.is_err());
let err = res.unwrap_err();
2022-01-25 22:19:28 +00:00
assert_eq!(err.kind(), ErrorKind::UnknownArgument);
2018-01-25 04:05:05 +00:00
}
#[test]
fn propagate_version_long() {
let res = with_subcommand()
.propagate_version(true)
2021-11-17 20:23:22 +00:00
.try_get_matches_from("foo bar --version".split(' '));
assert!(res.is_err());
let err = res.unwrap_err();
2022-01-25 22:19:28 +00:00
assert_eq!(err.kind(), ErrorKind::DisplayVersion);
}
#[test]
fn propagate_version_short() {
let res = with_subcommand()
.propagate_version(true)
2021-11-17 20:23:22 +00:00
.try_get_matches_from("foo bar -V".split(' '));
assert!(res.is_err());
let err = res.unwrap_err();
2022-01-25 22:19:28 +00:00
assert_eq!(err.kind(), ErrorKind::DisplayVersion);
}
#[cfg(debug_assertions)]
#[test]
#[should_panic = "`ArgAction::Version` used without providing Command::version or Command::long_version"]
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(' '));
}
#[test]
fn mut_arg_version_no_auto_version() {
let res = common()
.mut_arg("version", |v| v.short('z').action(ArgAction::SetTrue))
2021-11-17 20:23:22 +00:00
.try_get_matches_from("foo -z".split(' '));
2021-12-27 19:57:38 +00:00
assert!(res.is_ok(), "{}", res.unwrap_err());
assert_eq!(res.unwrap().get_one::<bool>("version").copied(), Some(true));
}
#[cfg(debug_assertions)]
#[test]
2022-02-12 03:48:29 +00:00
#[should_panic = "No version information via Command::version or Command::long_version to propagate"]
fn propagate_version_no_version_info() {
let _res = common()
.propagate_version(true)
2022-02-12 03:48:29 +00:00
.subcommand(Command::new("bar"))
2021-11-17 20:23:22 +00:00
.try_get_matches_from("foo".split(' '));
}