Skip to content

Commit

Permalink
test: add test for display help (paradigmxyz#1623)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Mar 4, 2023
1 parent 7b71af2 commit 31f62c1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
28 changes: 24 additions & 4 deletions bin/reth/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}
}
}
6 changes: 6 additions & 0 deletions bin/reth/src/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,12 @@ async fn run_network_until_shutdown<C>(
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"] {
Expand Down

0 comments on commit 31f62c1

Please sign in to comment.