From 560152b074e017753517f1d3c6e4431410ebed4b Mon Sep 17 00:00:00 2001 From: Daniel Mueller Date: Thu, 22 Aug 2019 08:20:22 -0700 Subject: Fix incomplete help text for encrypted & unencrypted subcommands Subcommands of the encrypted and unencrypted commands were found to have a wrong help text displayed. The reason for that behavior was that the subargs were are constructing as part of the argument parsing process were missing the command being requested and instead containing only the subcommand. This change fixes this deficiency. It also adds a test ensuring that the "Usage" string displayed in the help text of each command and subcommand contains the proper arguments. --- nitrocli/src/args.rs | 7 +++++-- nitrocli/src/tests/mod.rs | 6 +++--- nitrocli/src/tests/run.rs | 49 +++++++++++++++++++++++++++++++++++++++++------ 3 files changed, 51 insertions(+), 11 deletions(-) diff --git a/nitrocli/src/args.rs b/nitrocli/src/args.rs index b83c495..dfa42eb 100644 --- a/nitrocli/src/args.rs +++ b/nitrocli/src/args.rs @@ -286,7 +286,10 @@ fn unencrypted(ctx: &mut ExecCtx<'_>, args: Vec) -> Result<()> { parser.stop_on_first_argument(true); parse(ctx, parser, args)?; - subargs.insert(0, format!("nitrocli {}", subcommand)); + subargs.insert( + 0, + format!("nitrocli {} {}", Command::Unencrypted, subcommand), + ); subcommand.execute(ctx, subargs) } @@ -331,7 +334,7 @@ fn encrypted(ctx: &mut ExecCtx<'_>, args: Vec) -> Result<()> { parser.stop_on_first_argument(true); parse(ctx, parser, args)?; - subargs.insert(0, format!("nitrocli {}", subcommand)); + subargs.insert(0, format!("nitrocli {} {}", Command::Encrypted, subcommand)); subcommand.execute(ctx, subargs) } diff --git a/nitrocli/src/tests/mod.rs b/nitrocli/src/tests/mod.rs index 2f2926f..c823f52 100644 --- a/nitrocli/src/tests/mod.rs +++ b/nitrocli/src/tests/mod.rs @@ -136,7 +136,7 @@ impl Nitrocli { } } - fn do_run(&mut self, args: &[&'static str], f: F) -> (R, Vec, Vec) + fn do_run(&mut self, args: &[&str], f: F) -> (R, Vec, Vec) where F: FnOnce(&mut crate::RunCtx<'_>, Vec) -> R, { @@ -166,12 +166,12 @@ impl Nitrocli { } /// Run `nitrocli`'s `run` function. - pub fn run(&mut self, args: &[&'static str]) -> (i32, Vec, Vec) { + pub fn run(&mut self, args: &[&str]) -> (i32, Vec, Vec) { self.do_run(args, |c, a| crate::run(c, a)) } /// Run `nitrocli`'s `handle_arguments` function. - pub fn handle(&mut self, args: &[&'static str]) -> crate::Result { + pub fn handle(&mut self, args: &[&str]) -> crate::Result { let (res, out, _) = self.do_run(args, |c, a| crate::args::handle_arguments(c, a)); res.map(|_| String::from_utf8_lossy(&out).into_owned()) } diff --git a/nitrocli/src/tests/run.rs b/nitrocli/src/tests/run.rs index dda7473..c59c660 100644 --- a/nitrocli/src/tests/run.rs +++ b/nitrocli/src/tests/run.rs @@ -31,19 +31,56 @@ fn no_command_or_option() { } #[test] -fn help_option() { - fn test(opt: &'static str) { - let (rc, out, err) = Nitrocli::new().run(&[opt]); +fn help_options() { + fn test_run(args: &[&str], help: &str) { + let mut all = args.to_vec(); + all.push(help); + + let (rc, out, err) = Nitrocli::new().run(&all); assert_eq!(rc, 0); assert_eq!(err, b""); let s = String::from_utf8_lossy(&out).into_owned(); - assert!(s.starts_with("Usage:\n"), s); + let expected = format!("Usage:\n nitrocli {}", args.join(" ")); + assert!(s.starts_with(&expected), s); + } + + fn test(args: &[&str]) { + test_run(args, "--help"); + test_run(args, "-h"); } - test("--help"); - test("-h") + test(&[]); + test(&["config"]); + test(&["config", "get"]); + test(&["config", "set"]); + test(&["encrypted"]); + test(&["encrypted", "open"]); + test(&["encrypted", "close"]); + test(&["hidden"]); + test(&["hidden", "close"]); + test(&["hidden", "create"]); + test(&["hidden", "open"]); + test(&["lock"]); + test(&["otp"]); + test(&["otp", "clear"]); + test(&["otp", "get"]); + test(&["otp", "set"]); + test(&["otp", "status"]); + test(&["pin"]); + test(&["pin", "clear"]); + test(&["pin", "set"]); + test(&["pin", "unblock"]); + test(&["pws"]); + test(&["pws", "clear"]); + test(&["pws", "get"]); + test(&["pws", "set"]); + test(&["pws", "status"]); + test(&["reset"]); + test(&["status"]); + test(&["unencrypted"]); + test(&["unencrypted", "set"]); } #[test] -- cgit v1.2.3