From 9dd75987df34d8a7efa34395277a40036a6b241e Mon Sep 17 00:00:00 2001 From: Pavan Kumar Sunkara Date: Sat, 14 Aug 2021 22:25:22 +0100 Subject: [PATCH] Remove TakesValue restriction for HideEnv* --- src/build/arg/debug_asserts.rs | 4 - src/build/arg/mod.rs | 4 - tests/help.rs | 162 --------------------- tests/help_env.rs | 257 +++++++++++++++++++++++++++++++++ 4 files changed, 257 insertions(+), 170 deletions(-) create mode 100644 tests/help_env.rs diff --git a/src/build/arg/debug_asserts.rs b/src/build/arg/debug_asserts.rs index 56a5e2c4..30bb814c 100644 --- a/src/build/arg/debug_asserts.rs +++ b/src/build/arg/debug_asserts.rs @@ -84,9 +84,5 @@ fn assert_app_flags(arg: &Arg) { checker!(Last requires TakesValue); checker!(HideDefaultValue requires TakesValue); checker!(MultipleValues requires TakesValue); - #[cfg(feature = "env")] - checker!(HideEnv requires TakesValue); - #[cfg(feature = "env")] - checker!(HideEnvValues requires TakesValue); checker!(IgnoreCase requires TakesValue); } diff --git a/src/build/arg/mod.rs b/src/build/arg/mod.rs index d635e8b9..1db343fe 100644 --- a/src/build/arg/mod.rs +++ b/src/build/arg/mod.rs @@ -3927,8 +3927,6 @@ impl<'help> Arg<'help> { /// /// This is useful when the variable option is explained elsewhere in the help text. /// - /// **NOTE:** Setting this requires [`ArgSettings::TakesValue`] - /// /// # Examples /// /// ```rust @@ -3967,8 +3965,6 @@ impl<'help> Arg<'help> { /// /// This is useful when ENV vars contain sensitive values. /// - /// **NOTE:** Setting this requires [`ArgSettings::TakesValue`] - /// /// # Examples /// /// ```rust diff --git a/tests/help.rs b/tests/help.rs index 863f1221..784e91de 100644 --- a/tests/help.rs +++ b/tests/help.rs @@ -549,60 +549,6 @@ FLAGS: -V, --version Print version information"; -#[cfg(feature = "env")] -static HIDE_ENV: &str = "ctest 0.1 - -USAGE: - ctest [OPTIONS] - -FLAGS: - -h, --help Print help information - -V, --version Print version information - -OPTIONS: - -c, --cafe A coffeehouse, coffee shop, or café."; - -#[cfg(feature = "env")] -static SHOW_ENV: &str = "ctest 0.1 - -USAGE: - ctest [OPTIONS] - -FLAGS: - -h, --help Print help information - -V, --version Print version information - -OPTIONS: - -c, --cafe A coffeehouse, coffee shop, or café. [env: ENVVAR=MYVAL]"; - -#[cfg(feature = "env")] -static HIDE_ENV_VALS: &str = "ctest 0.1 - -USAGE: - ctest [OPTIONS] - -FLAGS: - -h, --help Print help information - -V, --version Print version information - -OPTIONS: - -c, --cafe A coffeehouse, coffee shop, or café. [env: ENVVAR] - -p, --pos Some vals [possible values: fast, slow]"; - -#[cfg(feature = "env")] -static SHOW_ENV_VALS: &str = "ctest 0.1 - -USAGE: - ctest [OPTIONS] - -FLAGS: - -h, --help Print help information - -V, --version Print version information - -OPTIONS: - -c, --cafe A coffeehouse, coffee shop, or café. [env: ENVVAR=MYVAL] - -p, --pos Some vals [possible values: fast, slow]"; - static CUSTOM_HELP_SECTION: &str = "blorp 1.4 Will M. @@ -1733,114 +1679,6 @@ fn issue_1052_require_delim_help() { )); } -#[cfg(feature = "env")] -#[test] -fn hide_env() { - use std::env; - - env::set_var("ENVVAR", "MYVAL"); - let app = App::new("ctest").version("0.1").arg( - Arg::new("cafe") - .short('c') - .long("cafe") - .value_name("FILE") - .hide_env(true) - .env("ENVVAR") - .about("A coffeehouse, coffee shop, or café.") - .takes_value(true), - ); - assert!(utils::compare_output(app, "ctest --help", HIDE_ENV, false)); -} - -#[cfg(feature = "env")] -#[test] -fn show_env() { - use std::env; - - env::set_var("ENVVAR", "MYVAL"); - let app = App::new("ctest").version("0.1").arg( - Arg::new("cafe") - .short('c') - .long("cafe") - .value_name("FILE") - .hide_env(false) - .env("ENVVAR") - .about("A coffeehouse, coffee shop, or café.") - .takes_value(true), - ); - assert!(utils::compare_output(app, "ctest --help", SHOW_ENV, false)); -} - -#[cfg(feature = "env")] -#[test] -fn hide_env_vals() { - use std::env; - - env::set_var("ENVVAR", "MYVAL"); - let app = App::new("ctest") - .version("0.1") - .arg( - Arg::new("pos") - .short('p') - .long("pos") - .value_name("VAL") - .possible_values(&["fast", "slow"]) - .about("Some vals") - .takes_value(true), - ) - .arg( - Arg::new("cafe") - .short('c') - .long("cafe") - .value_name("FILE") - .hide_env_values(true) - .env("ENVVAR") - .about("A coffeehouse, coffee shop, or café.") - .takes_value(true), - ); - assert!(utils::compare_output( - app, - "ctest --help", - HIDE_ENV_VALS, - false - )); -} - -#[cfg(feature = "env")] -#[test] -fn show_env_vals() { - use std::env; - - env::set_var("ENVVAR", "MYVAL"); - let app = App::new("ctest") - .version("0.1") - .arg( - Arg::new("pos") - .short('p') - .long("pos") - .value_name("VAL") - .possible_values(&["fast", "slow"]) - .about("Some vals") - .takes_value(true), - ) - .arg( - Arg::new("cafe") - .short('c') - .long("cafe") - .value_name("FILE") - .hide_possible_values(true) - .env("ENVVAR") - .about("A coffeehouse, coffee shop, or café.") - .takes_value(true), - ); - assert!(utils::compare_output( - app, - "ctest --help", - SHOW_ENV_VALS, - false - )); -} - #[test] fn custom_headers_headers() { let app = App::new("blorp") diff --git a/tests/help_env.rs b/tests/help_env.rs new file mode 100644 index 00000000..adbaea4b --- /dev/null +++ b/tests/help_env.rs @@ -0,0 +1,257 @@ +#![cfg(feature = "env")] + +use std::env; + +use clap::{App, Arg}; + +mod utils; + +static HIDE_ENV: &str = "ctest 0.1 + +USAGE: + ctest [OPTIONS] + +FLAGS: + -h, --help Print help information + -V, --version Print version information + +OPTIONS: + -c, --cafe A coffeehouse, coffee shop, or café."; + +static SHOW_ENV: &str = "ctest 0.1 + +USAGE: + ctest [OPTIONS] + +FLAGS: + -h, --help Print help information + -V, --version Print version information + +OPTIONS: + -c, --cafe A coffeehouse, coffee shop, or café. [env: ENVVAR=MYVAL]"; + +static HIDE_ENV_VALS: &str = "ctest 0.1 + +USAGE: + ctest [OPTIONS] + +FLAGS: + -h, --help Print help information + -V, --version Print version information + +OPTIONS: + -c, --cafe A coffeehouse, coffee shop, or café. [env: ENVVAR]"; + +static SHOW_ENV_VALS: &str = "ctest 0.1 + +USAGE: + ctest [OPTIONS] + +FLAGS: + -h, --help Print help information + -V, --version Print version information + +OPTIONS: + -c, --cafe A coffeehouse, coffee shop, or café. [env: ENVVAR=MYVAL]"; + +static HIDE_ENV_FLAG: &str = "ctest 0.1 + +USAGE: + ctest [FLAGS] + +FLAGS: + -c, --cafe A coffeehouse, coffee shop, or café. + -h, --help Print help information + -V, --version Print version information"; + +static SHOW_ENV_FLAG: &str = "ctest 0.1 + +USAGE: + ctest [FLAGS] + +FLAGS: + -c, --cafe A coffeehouse, coffee shop, or café. [env: ENVVAR=MYVAL] + -h, --help Print help information + -V, --version Print version information"; + +static HIDE_ENV_VALS_FLAG: &str = "ctest 0.1 + +USAGE: + ctest [FLAGS] + +FLAGS: + -c, --cafe A coffeehouse, coffee shop, or café. [env: ENVVAR] + -h, --help Print help information + -V, --version Print version information"; + +static SHOW_ENV_VALS_FLAG: &str = "ctest 0.1 + +USAGE: + ctest [FLAGS] + +FLAGS: + -c, --cafe A coffeehouse, coffee shop, or café. [env: ENVVAR=MYVAL] + -h, --help Print help information + -V, --version Print version information"; + +#[test] +fn hide_env() { + env::set_var("ENVVAR", "MYVAL"); + + let app = App::new("ctest").version("0.1").arg( + Arg::new("cafe") + .short('c') + .long("cafe") + .value_name("FILE") + .hide_env(true) + .env("ENVVAR") + .about("A coffeehouse, coffee shop, or café.") + .takes_value(true), + ); + + assert!(utils::compare_output(app, "ctest --help", HIDE_ENV, false)); +} + +#[test] +fn show_env() { + env::set_var("ENVVAR", "MYVAL"); + + let app = App::new("ctest").version("0.1").arg( + Arg::new("cafe") + .short('c') + .long("cafe") + .value_name("FILE") + .env("ENVVAR") + .about("A coffeehouse, coffee shop, or café.") + .takes_value(true), + ); + + assert!(utils::compare_output(app, "ctest --help", SHOW_ENV, false)); +} + +#[test] +fn hide_env_vals() { + env::set_var("ENVVAR", "MYVAL"); + + let app = App::new("ctest").version("0.1").arg( + Arg::new("cafe") + .short('c') + .long("cafe") + .value_name("FILE") + .hide_env_values(true) + .env("ENVVAR") + .about("A coffeehouse, coffee shop, or café.") + .takes_value(true), + ); + + assert!(utils::compare_output( + app, + "ctest --help", + HIDE_ENV_VALS, + false + )); +} + +#[test] +fn show_env_vals() { + env::set_var("ENVVAR", "MYVAL"); + + let app = App::new("ctest").version("0.1").arg( + Arg::new("cafe") + .short('c') + .long("cafe") + .value_name("FILE") + .env("ENVVAR") + .about("A coffeehouse, coffee shop, or café.") + .takes_value(true), + ); + + assert!(utils::compare_output( + app, + "ctest --help", + SHOW_ENV_VALS, + false + )); +} + +#[test] +fn hide_env_flag() { + env::set_var("ENVVAR", "MYVAL"); + + let app = App::new("ctest").version("0.1").arg( + Arg::new("cafe") + .short('c') + .long("cafe") + .hide_env(true) + .env("ENVVAR") + .about("A coffeehouse, coffee shop, or café."), + ); + + assert!(utils::compare_output( + app, + "ctest --help", + HIDE_ENV_FLAG, + false + )); +} + +#[test] +fn show_env_flag() { + env::set_var("ENVVAR", "MYVAL"); + + let app = App::new("ctest").version("0.1").arg( + Arg::new("cafe") + .short('c') + .long("cafe") + .env("ENVVAR") + .about("A coffeehouse, coffee shop, or café."), + ); + + assert!(utils::compare_output( + app, + "ctest --help", + SHOW_ENV_FLAG, + false + )); +} + +#[test] +fn hide_env_vals_flag() { + env::set_var("ENVVAR", "MYVAL"); + + let app = App::new("ctest").version("0.1").arg( + Arg::new("cafe") + .short('c') + .long("cafe") + .hide_env_values(true) + .env("ENVVAR") + .about("A coffeehouse, coffee shop, or café."), + ); + + assert!(utils::compare_output( + app, + "ctest --help", + HIDE_ENV_VALS_FLAG, + false + )); +} + +#[test] +fn show_env_vals_flag() { + env::set_var("ENVVAR", "MYVAL"); + + let app = App::new("ctest").version("0.1").arg( + Arg::new("cafe") + .short('c') + .long("cafe") + .env("ENVVAR") + .about("A coffeehouse, coffee shop, or café."), + ); + + assert!(utils::compare_output( + app, + "ctest --help", + SHOW_ENV_VALS_FLAG, + false + )); +}