2022-06-07 21:21:12 +00:00
|
|
|
use super::utils;
|
2021-10-15 21:06:21 +00:00
|
|
|
|
2023-08-16 19:50:08 +00:00
|
|
|
use clap::{arg, builder::ArgAction, error::Error, error::ErrorKind, value_parser, Arg, Command};
|
2021-10-15 21:06:21 +00:00
|
|
|
|
2022-07-26 00:17:01 +00:00
|
|
|
#[track_caller]
|
2022-08-24 17:26:56 +00:00
|
|
|
fn assert_error<F: clap::error::ErrorFormatter>(
|
|
|
|
err: Error<F>,
|
|
|
|
expected_kind: ErrorKind,
|
|
|
|
expected_output: &str,
|
|
|
|
stderr: bool,
|
|
|
|
) {
|
2021-10-15 21:06:21 +00:00
|
|
|
let actual_output = err.to_string();
|
|
|
|
assert_eq!(
|
|
|
|
stderr,
|
|
|
|
err.use_stderr(),
|
|
|
|
"Should Use STDERR failed. Should be {} but is {}",
|
|
|
|
stderr,
|
|
|
|
err.use_stderr()
|
|
|
|
);
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(expected_kind, err.kind());
|
2022-04-29 20:32:25 +00:00
|
|
|
utils::assert_eq(expected_output, actual_output)
|
2021-10-15 21:06:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn app_error() {
|
2023-01-03 16:22:40 +00:00
|
|
|
static MESSAGE: &str = "error: failed for mysterious reasons
|
2021-10-15 21:06:21 +00:00
|
|
|
|
2022-09-07 16:03:55 +00:00
|
|
|
Usage: test [OPTIONS] --all
|
2021-10-15 21:06:21 +00:00
|
|
|
|
2023-01-03 19:22:35 +00:00
|
|
|
For more information, try '--help'.
|
2021-10-15 21:06:21 +00:00
|
|
|
";
|
2022-02-14 21:47:20 +00:00
|
|
|
let cmd = Command::new("test")
|
2021-10-15 21:06:21 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("all")
|
|
|
|
.short('a')
|
|
|
|
.long("all")
|
|
|
|
.required(true)
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(clap::ArgAction::SetTrue)
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("Also do versioning for private crates (will not be published)"),
|
2021-10-15 21:06:21 +00:00
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("exact")
|
|
|
|
.long("exact")
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("Specify inter dependency version numbers exactly with `=`"),
|
2021-10-15 21:06:21 +00:00
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("no_git_commit")
|
|
|
|
.long("no-git-commit")
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("Do not commit version changes"),
|
2021-10-15 21:06:21 +00:00
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("no_git_push")
|
|
|
|
.long("no-git-push")
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("Do not push generated commit and tags to git remote"),
|
2021-10-15 21:06:21 +00:00
|
|
|
);
|
2022-02-14 21:47:20 +00:00
|
|
|
let mut cmd = cmd;
|
2021-10-15 21:06:21 +00:00
|
|
|
let expected_kind = ErrorKind::InvalidValue;
|
2023-01-03 16:22:40 +00:00
|
|
|
let err = cmd.error(expected_kind, "failed for mysterious reasons");
|
2022-04-29 20:32:25 +00:00
|
|
|
assert_error(err, expected_kind, MESSAGE, true);
|
2021-10-15 21:06:21 +00:00
|
|
|
}
|
2021-12-13 19:14:23 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn value_validation_has_newline() {
|
2022-05-24 15:16:50 +00:00
|
|
|
let res = Command::new("test")
|
|
|
|
.arg(
|
|
|
|
arg!(<PORT>)
|
|
|
|
.value_parser(value_parser!(usize))
|
|
|
|
.help("Network port to use"),
|
|
|
|
)
|
|
|
|
.try_get_matches_from(["test", "foo"]);
|
2021-12-13 19:18:51 +00:00
|
|
|
|
|
|
|
assert!(res.is_err());
|
|
|
|
let err = res.unwrap_err();
|
|
|
|
assert!(
|
|
|
|
err.to_string().ends_with('\n'),
|
|
|
|
"Errors should have a trailing newline, got {:?}",
|
|
|
|
err.to_string()
|
|
|
|
);
|
|
|
|
}
|
2022-08-24 17:26:56 +00:00
|
|
|
|
|
|
|
#[test]
|
2022-09-19 14:43:49 +00:00
|
|
|
fn kind_prints_help() {
|
2022-08-24 17:26:56 +00:00
|
|
|
let cmd = Command::new("test");
|
|
|
|
let res = cmd
|
|
|
|
.try_get_matches_from(["test", "--help"])
|
2022-09-19 14:43:49 +00:00
|
|
|
.map_err(|e| e.apply::<clap::error::KindFormatter>());
|
2022-08-24 17:26:56 +00:00
|
|
|
assert!(res.is_err());
|
|
|
|
let err = res.unwrap_err();
|
|
|
|
let expected_kind = ErrorKind::DisplayHelp;
|
|
|
|
static MESSAGE: &str = "\
|
2022-09-07 16:03:55 +00:00
|
|
|
Usage: test
|
2022-08-24 17:26:56 +00:00
|
|
|
|
2022-08-26 14:40:23 +00:00
|
|
|
Options:
|
2023-01-03 16:49:43 +00:00
|
|
|
-h, --help Print help
|
2022-08-24 17:26:56 +00:00
|
|
|
";
|
|
|
|
assert_error(err, expected_kind, MESSAGE, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-09-19 14:43:49 +00:00
|
|
|
fn kind_formats_validation_error() {
|
2022-08-24 17:26:56 +00:00
|
|
|
let cmd = Command::new("test");
|
|
|
|
let res = cmd
|
|
|
|
.try_get_matches_from(["test", "unused"])
|
2022-09-19 14:43:49 +00:00
|
|
|
.map_err(|e| e.apply::<clap::error::KindFormatter>());
|
2022-08-24 17:26:56 +00:00
|
|
|
assert!(res.is_err());
|
|
|
|
let err = res.unwrap_err();
|
|
|
|
let expected_kind = ErrorKind::UnknownArgument;
|
2022-09-19 14:43:49 +00:00
|
|
|
static MESSAGE: &str = "\
|
2023-01-14 03:29:49 +00:00
|
|
|
error: unexpected argument found
|
2022-09-19 14:43:49 +00:00
|
|
|
";
|
2022-08-24 17:26:56 +00:00
|
|
|
assert_error(err, expected_kind, MESSAGE, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-09-19 14:59:04 +00:00
|
|
|
#[cfg(feature = "error-context")]
|
2022-08-24 17:26:56 +00:00
|
|
|
fn rich_formats_validation_error() {
|
|
|
|
let cmd = Command::new("test");
|
|
|
|
let res = cmd.try_get_matches_from(["test", "unused"]);
|
|
|
|
assert!(res.is_err());
|
|
|
|
let err = res.unwrap_err();
|
|
|
|
let expected_kind = ErrorKind::UnknownArgument;
|
|
|
|
static MESSAGE: &str = "\
|
2023-01-14 03:29:49 +00:00
|
|
|
error: unexpected argument 'unused' found
|
2022-08-24 17:26:56 +00:00
|
|
|
|
2022-09-07 16:03:55 +00:00
|
|
|
Usage: test
|
2022-08-24 17:26:56 +00:00
|
|
|
|
2023-01-03 19:22:35 +00:00
|
|
|
For more information, try '--help'.
|
2022-08-24 17:26:56 +00:00
|
|
|
";
|
|
|
|
assert_error(err, expected_kind, MESSAGE, true);
|
|
|
|
}
|
2022-10-13 14:58:43 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg(feature = "error-context")]
|
|
|
|
fn suggest_trailing() {
|
|
|
|
let cmd = Command::new("rg").arg(arg!([PATTERN]));
|
|
|
|
|
|
|
|
let res = cmd.try_get_matches_from(["rg", "--foo"]);
|
|
|
|
assert!(res.is_err());
|
|
|
|
let err = res.unwrap_err();
|
|
|
|
let expected_kind = ErrorKind::UnknownArgument;
|
|
|
|
static MESSAGE: &str = "\
|
2023-01-14 03:29:49 +00:00
|
|
|
error: unexpected argument '--foo' found
|
2022-10-13 14:58:43 +00:00
|
|
|
|
2023-03-28 01:18:18 +00:00
|
|
|
tip: to pass '--foo' as a value, use '-- --foo'
|
2022-10-13 14:58:43 +00:00
|
|
|
|
|
|
|
Usage: rg [PATTERN]
|
|
|
|
|
2023-01-03 19:22:35 +00:00
|
|
|
For more information, try '--help'.
|
2022-10-13 14:58:43 +00:00
|
|
|
";
|
|
|
|
assert_error(err, expected_kind, MESSAGE, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg(feature = "error-context")]
|
|
|
|
fn trailing_already_in_use() {
|
|
|
|
let cmd = Command::new("rg").arg(arg!([PATTERN]));
|
|
|
|
|
|
|
|
let res = cmd.try_get_matches_from(["rg", "--", "--foo", "--foo"]);
|
|
|
|
assert!(res.is_err());
|
|
|
|
let err = res.unwrap_err();
|
|
|
|
let expected_kind = ErrorKind::UnknownArgument;
|
|
|
|
static MESSAGE: &str = "\
|
2023-01-14 03:29:49 +00:00
|
|
|
error: unexpected argument '--foo' found
|
2022-10-13 14:58:43 +00:00
|
|
|
|
|
|
|
Usage: rg [PATTERN]
|
|
|
|
|
2023-01-03 19:22:35 +00:00
|
|
|
For more information, try '--help'.
|
2022-10-13 14:58:43 +00:00
|
|
|
";
|
|
|
|
assert_error(err, expected_kind, MESSAGE, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg(feature = "error-context")]
|
|
|
|
fn cant_use_trailing() {
|
|
|
|
let cmd = Command::new("test");
|
|
|
|
|
|
|
|
let res = cmd.try_get_matches_from(["test", "--foo"]);
|
|
|
|
assert!(res.is_err());
|
|
|
|
let err = res.unwrap_err();
|
|
|
|
let expected_kind = ErrorKind::UnknownArgument;
|
|
|
|
static MESSAGE: &str = "\
|
2023-01-14 03:29:49 +00:00
|
|
|
error: unexpected argument '--foo' found
|
2022-10-13 14:58:43 +00:00
|
|
|
|
|
|
|
Usage: test
|
|
|
|
|
2023-01-03 19:22:35 +00:00
|
|
|
For more information, try '--help'.
|
2022-10-13 14:58:43 +00:00
|
|
|
";
|
|
|
|
assert_error(err, expected_kind, MESSAGE, true);
|
|
|
|
}
|
2023-07-21 19:21:45 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg(feature = "error-context")]
|
|
|
|
#[cfg(feature = "suggestions")]
|
|
|
|
fn cant_use_trailing_subcommand() {
|
|
|
|
let cmd = Command::new("test").subcommand(Command::new("bar"));
|
|
|
|
|
|
|
|
let res = cmd.try_get_matches_from(["test", "baz"]);
|
|
|
|
assert!(res.is_err());
|
|
|
|
let err = res.unwrap_err();
|
|
|
|
let expected_kind = ErrorKind::InvalidSubcommand;
|
|
|
|
static MESSAGE: &str = "\
|
|
|
|
error: unrecognized subcommand 'baz'
|
|
|
|
|
|
|
|
tip: a similar subcommand exists: 'bar'
|
|
|
|
|
|
|
|
Usage: test [COMMAND]
|
|
|
|
|
|
|
|
For more information, try '--help'.
|
|
|
|
";
|
|
|
|
assert_error(err, expected_kind, MESSAGE, true);
|
|
|
|
}
|
2023-08-16 19:50:08 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg(feature = "error-context")]
|
|
|
|
#[cfg(feature = "suggestions")]
|
|
|
|
fn unknown_argument_option() {
|
|
|
|
let cmd = Command::new("test").args([
|
|
|
|
Arg::new("current-dir").short('C'),
|
|
|
|
Arg::new("current-dir-unknown")
|
|
|
|
.long("cwd")
|
|
|
|
.aliases(["current-dir", "directory", "working-directory", "root"])
|
2023-08-16 19:54:45 +00:00
|
|
|
.value_parser(
|
|
|
|
clap::builder::UnknownArgumentValueParser::suggest_arg("-C")
|
|
|
|
.and_suggest("not much else to say"),
|
|
|
|
)
|
2023-08-16 19:50:08 +00:00
|
|
|
.hide(true),
|
|
|
|
]);
|
2023-08-18 19:19:54 +00:00
|
|
|
|
|
|
|
let res = cmd.clone().try_get_matches_from(["test"]);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
|
2023-08-16 19:50:08 +00:00
|
|
|
let res = cmd.try_get_matches_from(["test", "--cwd", ".."]);
|
|
|
|
assert!(res.is_err());
|
|
|
|
let err = res.unwrap_err();
|
|
|
|
let expected_kind = ErrorKind::UnknownArgument;
|
|
|
|
static MESSAGE: &str = "\
|
|
|
|
error: unexpected argument '--cwd <current-dir-unknown>' found
|
|
|
|
|
2023-08-16 19:54:45 +00:00
|
|
|
tip: a similar argument exists: '-C'
|
|
|
|
tip: not much else to say
|
2023-08-16 19:50:08 +00:00
|
|
|
|
|
|
|
Usage: test [OPTIONS]
|
|
|
|
|
|
|
|
For more information, try '--help'.
|
|
|
|
";
|
|
|
|
assert_error(err, expected_kind, MESSAGE, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg(feature = "error-context")]
|
|
|
|
#[cfg(feature = "suggestions")]
|
|
|
|
fn unknown_argument_flag() {
|
|
|
|
let cmd = Command::new("test").args([
|
|
|
|
Arg::new("ignore-rust-version").long("ignore-rust-version"),
|
|
|
|
Arg::new("libtest-ignore")
|
|
|
|
.long("ignored")
|
|
|
|
.action(ArgAction::SetTrue)
|
2023-08-16 19:54:45 +00:00
|
|
|
.value_parser(
|
|
|
|
clap::builder::UnknownArgumentValueParser::suggest_arg("-- --ignored")
|
|
|
|
.and_suggest("not much else to say"),
|
|
|
|
)
|
2023-08-16 19:50:08 +00:00
|
|
|
.hide(true),
|
|
|
|
]);
|
2023-08-18 19:19:54 +00:00
|
|
|
|
|
|
|
let res = cmd.clone().try_get_matches_from(["test"]);
|
2023-08-18 19:29:37 +00:00
|
|
|
assert!(res.is_ok());
|
2023-08-18 19:19:54 +00:00
|
|
|
|
2023-08-16 19:50:08 +00:00
|
|
|
let res = cmd.try_get_matches_from(["test", "--ignored"]);
|
|
|
|
assert!(res.is_err());
|
|
|
|
let err = res.unwrap_err();
|
|
|
|
let expected_kind = ErrorKind::UnknownArgument;
|
|
|
|
static MESSAGE: &str = "\
|
|
|
|
error: unexpected argument '--ignored' found
|
|
|
|
|
2023-08-16 19:54:45 +00:00
|
|
|
tip: a similar argument exists: '-- --ignored'
|
|
|
|
tip: not much else to say
|
2023-08-16 19:50:08 +00:00
|
|
|
|
|
|
|
Usage: test [OPTIONS]
|
|
|
|
|
|
|
|
For more information, try '--help'.
|
|
|
|
";
|
|
|
|
assert_error(err, expected_kind, MESSAGE, true);
|
|
|
|
}
|