2021-11-30 15:31:19 +00:00
|
|
|
use crate::utils;
|
2021-10-15 21:06:21 +00:00
|
|
|
|
2022-05-24 15:16:50 +00:00
|
|
|
use clap::{arg, error::ErrorKind, value_parser, Arg, Command, Error};
|
2021-10-15 21:06:21 +00:00
|
|
|
|
2022-04-29 20:32:25 +00:00
|
|
|
fn assert_error(err: Error, 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() {
|
|
|
|
static MESSAGE: &str = "error: Failed for mysterious reasons
|
|
|
|
|
|
|
|
USAGE:
|
|
|
|
test [OPTIONS] --all
|
|
|
|
|
|
|
|
For more information try --help
|
|
|
|
";
|
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)
|
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;
|
2022-02-14 21:47:20 +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()
|
|
|
|
);
|
|
|
|
}
|