test: add test for display help (#1623)

This commit is contained in:
Matthias Seitz
2023-03-04 21:52:12 +01:00
committed by GitHub
parent 7b71af2409
commit 31f62c11d5
2 changed files with 30 additions and 4 deletions

View File

@ -38,7 +38,7 @@ pub fn run() -> eyre::Result<()> {
} }
/// Commands to be executed /// Commands to be executed
#[derive(Subcommand)] #[derive(Debug, Subcommand)]
pub enum Commands { pub enum Commands {
/// Start the node /// Start the node
#[command(name = "node")] #[command(name = "node")]
@ -74,7 +74,7 @@ pub enum Commands {
TestVectors(test_vectors::Command), TestVectors(test_vectors::Command),
} }
#[derive(Parser)] #[derive(Debug, Parser)]
#[command(author, version = "0.1", about = "Reth", long_about = None)] #[command(author, version = "0.1", about = "Reth", long_about = None)]
struct Cli { struct Cli {
/// The command to run /// The command to run
@ -89,7 +89,7 @@ struct Cli {
} }
/// The log configuration. /// The log configuration.
#[derive(Args)] #[derive(Debug, Args)]
#[command(next_help_heading = "Logging")] #[command(next_help_heading = "Logging")]
pub struct Logs { pub struct Logs {
/// The path to put log files in. /// The path to put log files in.
@ -131,7 +131,7 @@ impl Logs {
} }
/// The verbosity settings for the cli. /// The verbosity settings for the cli.
#[derive(Args)] #[derive(Debug, Copy, Clone, Args)]
#[command(next_help_heading = "Display")] #[command(next_help_heading = "Display")]
pub struct Verbosity { pub struct Verbosity {
/// Set the minimum log level. /// 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);
}
}
}

View File

@ -504,6 +504,12 @@ async fn run_network_until_shutdown<C>(
mod tests { mod tests {
use super::*; 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] #[test]
fn parse_common_node_command_chain_args() { fn parse_common_node_command_chain_args() {
for chain in ["mainnet", "sepolia", "goerli"] { for chain in ["mainnet", "sepolia", "goerli"] {