Skip to content

Commit

Permalink
test: add Command::debug_assert tests to CLIs (#7024)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes authored Feb 6, 2024
1 parent 865a0d1 commit 2cf84d9
Show file tree
Hide file tree
Showing 6 changed files with 195 additions and 159 deletions.
34 changes: 21 additions & 13 deletions crates/anvil/src/anvil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ use anvil::cmd::NodeArgs;
use clap::{CommandFactory, Parser, Subcommand};

/// A fast local Ethereum development node.
#[derive(Debug, Parser)]
#[derive(Parser)]
#[clap(name = "anvil", version = anvil::VERSION_MESSAGE, next_display_order = None)]
pub struct App {
pub struct Anvil {
#[clap(flatten)]
pub node: NodeArgs,

#[clap(subcommand)]
pub cmd: Option<Commands>,
pub cmd: Option<AnvilSubcommand>,
}

#[derive(Clone, Debug, PartialEq, Eq, Subcommand)]
pub enum Commands {
#[derive(Subcommand)]
pub enum AnvilSubcommand {
/// Generate shell completions script.
#[clap(visible_alias = "com")]
Completions {
Expand All @@ -29,22 +29,22 @@ pub enum Commands {

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut app = App::parse();
let mut app = Anvil::parse();
app.node.evm_opts.resolve_rpc_alias();

if let Some(ref cmd) = app.cmd {
match cmd {
Commands::Completions { shell } => {
AnvilSubcommand::Completions { shell } => {
clap_complete::generate(
*shell,
&mut App::command(),
&mut Anvil::command(),
"anvil",
&mut std::io::stdout(),
);
}
Commands::GenerateFigSpec => clap_complete::generate(
AnvilSubcommand::GenerateFigSpec => clap_complete::generate(
clap_complete_fig::Fig,
&mut App::command(),
&mut Anvil::command(),
"anvil",
&mut std::io::stdout(),
),
Expand All @@ -62,14 +62,22 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
mod tests {
use super::*;

#[test]
fn verify_cli() {
Anvil::command().debug_assert();
}

#[test]
fn can_parse_help() {
let _: App = App::parse_from(["anvil", "--help"]);
let _: Anvil = Anvil::parse_from(["anvil", "--help"]);
}

#[test]
fn can_parse_completions() {
let args: App = App::parse_from(["anvil", "completions", "bash"]);
assert_eq!(args.cmd, Some(Commands::Completions { shell: clap_complete::Shell::Bash }));
let args: Anvil = Anvil::parse_from(["anvil", "completions", "bash"]);
assert!(matches!(
args.cmd,
Some(AnvilSubcommand::Completions { shell: clap_complete::Shell::Bash })
));
}
}
Loading

0 comments on commit 2cf84d9

Please sign in to comment.