From 330141c2845e70cd3955592037a81d1c34bf084e Mon Sep 17 00:00:00 2001 From: Tyler Calder Date: Tue, 29 Nov 2022 20:41:34 -0700 Subject: [PATCH] test: Add test of derive functionality It was noticed that between #4443 and #4432, an issue in the behavior was that the derive api handles tasks with values slightly differently than the declarative api. Added a test to show parity between declaritive and derive api. --- clap_mangen/Cargo.toml | 2 +- clap_mangen/tests/common.rs | 14 -------------- clap_mangen/tests/derive.rs | 14 ++++++++++++++ clap_mangen/tests/roff.rs | 6 ++++-- .../tests/snapshots/flag_without_value.bash.roff | 11 +++++++---- 5 files changed, 26 insertions(+), 21 deletions(-) create mode 100644 clap_mangen/tests/derive.rs diff --git a/clap_mangen/Cargo.toml b/clap_mangen/Cargo.toml index 8f82e5c0f19f..74922c9e7a76 100644 --- a/clap_mangen/Cargo.toml +++ b/clap_mangen/Cargo.toml @@ -45,7 +45,7 @@ clap = { path = "../", version = "4.0.0", default-features = false, features = [ [dev-dependencies] snapbox = { version = "0.4", features = ["diff"] } -clap = { path = "../", version = "4.0.0", default-features = false, features = ["std", "help"] } +clap = { path = "../", version = "4.0.0", default-features = false, features = ["std", "help", "derive"] } [features] default = [] diff --git a/clap_mangen/tests/common.rs b/clap_mangen/tests/common.rs index 8c8303384008..ca9f1f9b4fb3 100644 --- a/clap_mangen/tests/common.rs +++ b/clap_mangen/tests/common.rs @@ -312,17 +312,3 @@ pub fn possible_values_command(name: &'static str) -> clap::Command { ]), ) } - -/// Checks to make sure boolean valued "Flag options" do not generate -/// suggestions for a parameter. i.e: -/// --boolean_flag=BOOLEAN_FLAG -/// -/// This is both confusing and suggest erroneous behavior as clap will fail if you -/// pass a value to a boolean flag -pub fn flag_without_value(name: &'static str) -> clap::Command { - clap::Command::new(name).arg( - clap::Arg::new("is_bool") - .long("is_bool") - .action(clap::ArgAction::SetTrue), - ) -} diff --git a/clap_mangen/tests/derive.rs b/clap_mangen/tests/derive.rs new file mode 100644 index 000000000000..8c309bd41c78 --- /dev/null +++ b/clap_mangen/tests/derive.rs @@ -0,0 +1,14 @@ +use clap::Parser; + +/// Checks to make sure boolean valued "Flag options" do not generate +/// suggestions for a parameter. i.e: +/// --boolean_flag=BOOLEAN_FLAG +/// +/// This is both confusing and suggest erroneous behavior as clap will fail if you +/// pass a value to a boolean flag +#[derive(Parser, Debug)] +#[command(name = "my-app")] +pub struct FlagWithoutValue { + #[clap(long)] + boolean_flag: bool, +} diff --git a/clap_mangen/tests/roff.rs b/clap_mangen/tests/roff.rs index 4d47d249f457..2a2e59eabb21 100644 --- a/clap_mangen/tests/roff.rs +++ b/clap_mangen/tests/roff.rs @@ -1,4 +1,7 @@ mod common; +mod derive; + +use clap::CommandFactory; #[test] fn basic() { @@ -72,8 +75,7 @@ fn possible_values() { #[test] fn flag_without_value() { - let name = "my-app"; - let cmd = common::flag_without_value(name); + let cmd = derive::FlagWithoutValue::command(); common::assert_matches_path("tests/snapshots/flag_without_value.bash.roff", cmd); } diff --git a/clap_mangen/tests/snapshots/flag_without_value.bash.roff b/clap_mangen/tests/snapshots/flag_without_value.bash.roff index b4b7f45cb68e..af4e0de7b894 100644 --- a/clap_mangen/tests/snapshots/flag_without_value.bash.roff +++ b/clap_mangen/tests/snapshots/flag_without_value.bash.roff @@ -2,14 +2,17 @@ .el .ds Aq ' .TH my-app 1 "my-app " .SH NAME -my/-app +my/-app /- Checks to make sure boolean valued "Flag options" do not generate suggestions for a parameter. i.e: /-/-boolean_flag=BOOLEAN_FLAG .SH SYNOPSIS -/fBmy/-app/fR [/fB/-/-is_bool/fR] [/fB/-h/fR|/fB/-/-help/fR] +/fBmy/-app/fR [/fB/-/-boolean/-flag/fR] [/fB/-h/fR|/fB/-/-help/fR] .SH DESCRIPTION +Checks to make sure boolean valued "Flag options" do not generate suggestions for a parameter. i.e: /-/-boolean_flag=BOOLEAN_FLAG +.PP +This is both confusing and suggest erroneous behavior as clap will fail if you pass a value to a boolean flag .SH OPTIONS .TP -/fB/-/-is_bool/fR +/fB/-/-boolean/-flag/fR .TP /fB/-h/fR, /fB/-/-help/fR -Print help information +Print help information (use `/-h` for a summary)