From e22c786d21bfc6be8d50d6d706839832f3447bfb Mon Sep 17 00:00:00 2001 From: Arthur Paulino Date: Tue, 1 Aug 2023 15:46:00 -0300 Subject: [PATCH] Solve CLI parse order bug (#574) * solve CLI parse order bug * add CLI test to validate fix --- src/cli/mod.rs | 36 ++++++++++++------- ...lurkrs-smoke-test.rs => lurk-cli-tests.rs} | 8 +++++ tests/{lurk-tests.rs => lurk-lib-tests.rs} | 2 +- 3 files changed, 32 insertions(+), 14 deletions(-) rename tests/{lurkrs-smoke-test.rs => lurk-cli-tests.rs} (68%) rename tests/{lurk-tests.rs => lurk-lib-tests.rs} (97%) diff --git a/src/cli/mod.rs b/src/cli/mod.rs index 504c7a533b..d54461a0ff 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -105,7 +105,7 @@ struct LoadCli { } impl LoadArgs { - pub fn into_cli(self) -> LoadCli { + fn into_cli(self) -> LoadCli { LoadCli { lurk_file: self.lurk_file, zstore: self.zstore, @@ -175,7 +175,7 @@ struct ReplCli { } impl ReplArgs { - pub fn into_cli(self) -> ReplCli { + fn into_cli(self) -> ReplCli { ReplCli { load: self.load, zstore: self.zstore, @@ -364,17 +364,9 @@ struct VerifyArgs { proof_id: String, } -/// Parses CLI arguments and continues the program flow accordingly -pub fn parse_and_run() -> Result<()> { - #[cfg(not(target_arch = "wasm32"))] - paths::non_wasm::create_lurk_dirs()?; - - if let Ok(repl_cli) = ReplCli::try_parse() { - repl_cli.run() - } else if let Ok(load_cli) = LoadCli::try_parse() { - load_cli.run() - } else { - match Cli::parse().command { +impl Cli { + fn run(self) -> Result<()> { + match self.command { Command::Repl(repl_args) => repl_args.into_cli().run(), Command::Load(load_args) => load_args.into_cli().run(), #[allow(unused_variables)] @@ -389,3 +381,21 @@ pub fn parse_and_run() -> Result<()> { } } } + +/// Parses CLI arguments and continues the program flow accordingly +pub fn parse_and_run() -> Result<()> { + #[cfg(not(target_arch = "wasm32"))] + paths::non_wasm::create_lurk_dirs()?; + + if let Ok(cli) = Cli::try_parse() { + cli.run() + } else if let Ok(repl_cli) = ReplCli::try_parse() { + repl_cli.run() + } else if let Ok(load_cli) = LoadCli::try_parse() { + load_cli.run() + } else { + // force printing help + Cli::parse(); + Ok(()) + } +} diff --git a/tests/lurkrs-smoke-test.rs b/tests/lurk-cli-tests.rs similarity index 68% rename from tests/lurkrs-smoke-test.rs rename to tests/lurk-cli-tests.rs index 1a3513ee7e..a29d68d32f 100644 --- a/tests/lurkrs-smoke-test.rs +++ b/tests/lurk-cli-tests.rs @@ -12,3 +12,11 @@ fn test_help_command() { cmd.arg("--help"); cmd.assert().success(); } + +#[test] +fn test_repl_command() { + let mut cmd = lurk_cmd(); + + cmd.arg("repl"); + cmd.assert().success(); +} diff --git a/tests/lurk-tests.rs b/tests/lurk-lib-tests.rs similarity index 97% rename from tests/lurk-tests.rs rename to tests/lurk-lib-tests.rs index 279a64e338..660db467b1 100644 --- a/tests/lurk-tests.rs +++ b/tests/lurk-lib-tests.rs @@ -7,7 +7,7 @@ use std::path::Path; #[test] #[ignore] -fn lurk_tests() { +fn lurk_cli_tests() { let test_files = [ "test.lurk", "micro-tests.lurk",