Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(derive): Verify derive-genned errors are formatted #2964

Merged
merged 1 commit into from
Oct 29, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion clap_derive/tests/help.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use clap::{Args, IntoApp, Parser, Subcommand};
use clap::{AppSettings, Args, ColorChoice, IntoApp, Parser, Subcommand};

#[test]
fn arg_help_heading_applied() {
Expand Down Expand Up @@ -183,3 +183,45 @@ fn flatten_field_with_help_heading() {
.unwrap();
assert_eq!(should_be_in_section_a.get_help_heading(), Some("HEADING A"));
}

// The challenge with this test is creating an error situation not caught by `clap`'s error checking
// but by the code that `clap_derive` generates.
//
// Ultimately, the easiest way to confirm is to put a debug statement in the desired error path.
#[test]
fn derive_generated_error_has_full_context() {
#[derive(Debug, Parser)]
#[clap(setting(AppSettings::SubcommandsNegateReqs), color = ColorChoice::Never)]
struct Opts {
#[clap(long)]
req_str: String,

#[clap(subcommand)]
cmd: Option<SubCommands>,
}

#[derive(Debug, Parser)]
enum SubCommands {
Sub {
#[clap(short, long, parse(from_occurrences))]
verbose: u8,
},
}

let result = Opts::try_parse_from(&["test", "sub"]);
assert!(
result.is_err(),
"`SubcommandsNegateReqs` with non-optional `req_str` should fail: {:?}",
result.unwrap()
);

let expected = r#"error: The following required argument was not provided: req-str

USAGE:
clap_derive --req-str <REQ_STR>
clap_derive <SUBCOMMAND>

For more information try --help
"#;
assert_eq!(result.unwrap_err().to_string(), expected);
}