Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add Command::debug_assert tests to CLIs #7024

Merged
merged 1 commit into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading