From e37a8b7521a4a91e90e79e4fd87a37efc5a258a6 Mon Sep 17 00:00:00 2001 From: Zanie Blue Date: Mon, 8 Jul 2024 17:15:54 -0500 Subject: [PATCH] test: add coverage for help flag hints --- tests/builder/help.rs | 160 ++++++++++++++++++++++++++++++++++- tests/builder/subcommands.rs | 21 +++-- 2 files changed, 171 insertions(+), 10 deletions(-) diff --git a/tests/builder/help.rs b/tests/builder/help.rs index a66f5f0d8d5..d1f53de4583 100644 --- a/tests/builder/help.rs +++ b/tests/builder/help.rs @@ -81,14 +81,17 @@ fn help_multi_subcommand_error() { .try_get_matches_from(["ctest", "help", "subcmd", "multi", "foo"]) .unwrap_err(); - assert_data_eq!(err.to_string(), str![[r#" + assert_data_eq!( + err.to_string(), + str![[r#" error: unrecognized subcommand 'foo' Usage: ctest subcmd multi [OPTIONS] For more information, try '--help'. -"#]]); +"#]] + ); } #[test] @@ -351,6 +354,152 @@ Options: utils::assert_output(cmd, "ctest --help", DEFAULT_HELP, false); } +#[test] +fn try_help_default() { + static DEFAULT_HELP: &str = "\ +error: unexpected argument 'bar' found + +Usage: ctest + +For more information, try '--help'. +"; + + let cmd = Command::new("ctest").version("1.0").term_width(0); + utils::assert_output(cmd, "ctest bar", DEFAULT_HELP, true); +} + +#[test] +fn try_help_custom_flag() { + static EXPECTED_HELP: &str = "\ +error: unexpected argument 'bar' found + +Usage: ctest +"; + + let cmd = Command::new("ctest") + .version("1.0") + .disable_help_flag(true) + .arg( + Arg::new("help") + .long("help") + .short('h') + .action(ArgAction::Help), + ) + .term_width(0); + utils::assert_output(cmd, "ctest bar", EXPECTED_HELP, true); +} + +#[test] +fn try_help_custom_flag_short() { + static EXPECTED_HELP: &str = "\ +error: unexpected argument 'bar' found + +Usage: ctest +"; + + let cmd = Command::new("ctest") + .version("1.0") + .disable_help_flag(true) + .arg(Arg::new("help").short('h').action(ArgAction::HelpShort)) + .term_width(0); + utils::assert_output(cmd, "ctest bar", EXPECTED_HELP, true); +} + +#[test] +fn try_help_custom_flag_long() { + static EXPECTED_HELP: &str = "\ +error: unexpected argument 'bar' found + +Usage: ctest +"; + + let cmd = Command::new("ctest") + .version("1.0") + .disable_help_flag(true) + .arg(Arg::new("help").long("help").action(ArgAction::HelpShort)) + .term_width(0); + utils::assert_output(cmd, "ctest bar", EXPECTED_HELP, true); +} + +#[test] +fn try_help_custom_flag_no_action() { + static EXPECTED_HELP: &str = "\ +error: unexpected argument 'bar' found + +Usage: ctest +"; + + let cmd = Command::new("ctest") + .version("1.0") + .disable_help_flag(true) + // Note `ArgAction::Help` is excluded + .arg(Arg::new("help").long("help").global(true)) + .term_width(0); + utils::assert_output(cmd, "ctest bar", EXPECTED_HELP, true); +} + +#[test] +fn try_help_subcommand_default() { + static DEFAULT_HELP: &str = "\ +error: unrecognized subcommand 'bar' + +Usage: ctest [COMMAND] + +For more information, try '--help'. +"; + + let cmd = Command::new("ctest") + .version("1.0") + .subcommand(Command::new("foo")) + .term_width(0); + utils::assert_output(cmd, "ctest bar", DEFAULT_HELP, true); +} + +#[test] +fn try_help_subcommand_custom_flag() { + static EXPECTED_HELP: &str = "\ +error: unrecognized subcommand 'bar' + +Usage: ctest [COMMAND] + +For more information, try 'help'. +"; + + let cmd = Command::new("ctest") + .version("1.0") + .disable_help_flag(true) + .arg( + Arg::new("help") + .long("help") + .short('h') + .action(ArgAction::Help) + .global(true), + ) + .subcommand(Command::new("foo")) + .term_width(0); + utils::assert_output(cmd, "ctest bar", EXPECTED_HELP, true); +} + +#[test] +fn try_help_subcommand_custom_flag_no_action() { + static EXPECTED_HELP: &str = "\ +error: unrecognized subcommand 'bar' + +Usage: ctest [COMMAND] + +For more information, try 'help'. +"; + + let cmd = Command::new("ctest") + .version("1.0") + .disable_help_flag(true) + // Note `ArgAction::Help` is excluded + .arg(Arg::new("help").long("help").global(true)) + .subcommand(Command::new("foo")) + .term_width(0); + utils::assert_output(cmd, "ctest bar", EXPECTED_HELP, true); +} + #[test] #[cfg(feature = "wrap_help")] fn wrapped_help() { @@ -2861,7 +3010,9 @@ fn parent_cmd_req_in_usage_with_render_help() { let subcmd = cmd.find_subcommand_mut("test").unwrap(); let help = subcmd.render_help().to_string(); - assert_data_eq!(help, str![[r#" + assert_data_eq!( + help, + str![[r#" some Usage: parent test @@ -2869,7 +3020,8 @@ Usage: parent test Options: -h, --help Print help -"#]]); +"#]] + ); } #[test] diff --git a/tests/builder/subcommands.rs b/tests/builder/subcommands.rs index 1cf2f6db12a..0925d9f07cb 100644 --- a/tests/builder/subcommands.rs +++ b/tests/builder/subcommands.rs @@ -487,20 +487,25 @@ fn bad_multicall_command_error() { let err = cmd.clone().try_get_matches_from(["world"]).unwrap_err(); assert_eq!(err.kind(), ErrorKind::InvalidSubcommand); - assert_data_eq!(err.to_string(), str![[r#" + assert_data_eq!( + err.to_string(), + str![[r#" error: unrecognized subcommand 'world' Usage: For more information, try 'help'. -"#]]); +"#]] + ); #[cfg(feature = "suggestions")] { let err = cmd.clone().try_get_matches_from(["baz"]).unwrap_err(); assert_eq!(err.kind(), ErrorKind::InvalidSubcommand); - assert_data_eq!(err.to_string(), str![[r#" + assert_data_eq!( + err.to_string(), + str![[r#" error: unrecognized subcommand 'baz' tip: a similar subcommand exists: 'bar' @@ -509,7 +514,8 @@ Usage: For more information, try 'help'. -"#]]); +"#]] + ); } // Verify whatever we did to get the above to work didn't disable `--help` and `--version`. @@ -600,7 +606,9 @@ fn multicall_render_help() { let subcmd = subcmd.find_subcommand_mut("bar").unwrap(); let help = subcmd.render_help().to_string(); - assert_data_eq!(help, str![[r#" + assert_data_eq!( + help, + str![[r#" Usage: foo bar [value] Arguments: @@ -610,7 +618,8 @@ Options: -h, --help Print help -V, --version Print version -"#]]); +"#]] + ); } #[test]