From 31f62c11d5544ac8aaa78a10f6f5a4201c64cd61 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Sat, 4 Mar 2023 21:52:12 +0100 Subject: [PATCH] test: add test for display help (#1623) --- bin/reth/src/cli.rs | 28 ++++++++++++++++++++++++---- bin/reth/src/node/mod.rs | 6 ++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/bin/reth/src/cli.rs b/bin/reth/src/cli.rs index 9f6a5e4633..4afa909392 100644 --- a/bin/reth/src/cli.rs +++ b/bin/reth/src/cli.rs @@ -38,7 +38,7 @@ pub fn run() -> eyre::Result<()> { } /// Commands to be executed -#[derive(Subcommand)] +#[derive(Debug, Subcommand)] pub enum Commands { /// Start the node #[command(name = "node")] @@ -74,7 +74,7 @@ pub enum Commands { TestVectors(test_vectors::Command), } -#[derive(Parser)] +#[derive(Debug, Parser)] #[command(author, version = "0.1", about = "Reth", long_about = None)] struct Cli { /// The command to run @@ -89,7 +89,7 @@ struct Cli { } /// The log configuration. -#[derive(Args)] +#[derive(Debug, Args)] #[command(next_help_heading = "Logging")] pub struct Logs { /// The path to put log files in. @@ -131,7 +131,7 @@ impl Logs { } /// The verbosity settings for the cli. -#[derive(Args)] +#[derive(Debug, Copy, Clone, Args)] #[command(next_help_heading = "Display")] pub struct Verbosity { /// Set the minimum log level. @@ -168,3 +168,23 @@ impl Verbosity { } } } + +#[cfg(test)] +mod tests { + use super::*; + use clap::CommandFactory; + + /// Tests that the help message is parsed correctly. This ensures that clap args are configured + /// correctly and no conflicts are introduced via attributes that would result in a panic at + /// runtime + #[test] + fn test_parse_help_all_subcommands() { + let reth = Cli::command(); + for sub_command in reth.get_subcommands() { + let err = Cli::try_parse_from(["reth", sub_command.get_name(), "--help"]).unwrap_err(); + // --help is treated as error, but + // > Not a true "error" as it means --help or similar was used. The help message will be sent to stdout. + assert_eq!(err.kind(), clap::error::ErrorKind::DisplayHelp); + } + } +} diff --git a/bin/reth/src/node/mod.rs b/bin/reth/src/node/mod.rs index 1c8753b608..5852caec68 100644 --- a/bin/reth/src/node/mod.rs +++ b/bin/reth/src/node/mod.rs @@ -504,6 +504,12 @@ async fn run_network_until_shutdown( mod tests { use super::*; + #[test] + fn parse_help_node_command() { + let err = Command::try_parse_from(["reth", "--help"]).unwrap_err(); + assert_eq!(err.kind(), clap::error::ErrorKind::DisplayHelp); + } + #[test] fn parse_common_node_command_chain_args() { for chain in ["mainnet", "sepolia", "goerli"] {