[#10] Perform early validation of completion cmd

In the completion command validation was being done at runtime to
determine if too many or too few args were being passed in.  This
approach required extra unit tests to validate the behavior.

This patch seeks to streamline the code by specifying the number of
args expected when initializing the cobra command, and allowing cobra
to throw an error before doing any work in go if the number of args is
incorrect.

As a result the unit tests for too many and too few args are no longer
needed for this package and have been removed.

Change-Id: If0cf043713ef08333f17b010210352205d74c4ee
This commit is contained in:
Alexander Hughes 2020-02-10 16:09:47 -05:00
parent dae73126e9
commit c7f42bfcf4
2 changed files with 1 additions and 18 deletions

View File

@ -36,6 +36,7 @@ func NewCompletionCommand() *cobra.Command {
Use: "completion SHELL",
Short: "Generate autocompletions script for the specified shell (bash or zsh)",
Long: completionDesc,
Args: cobra.ExactArgs(1),
RunE: runCompletion,
ValidArgs: shells,
}
@ -44,12 +45,6 @@ func NewCompletionCommand() *cobra.Command {
}
func runCompletion(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return fmt.Errorf("shell not specified")
}
if len(args) > 1 {
return fmt.Errorf("too many arguments, expected only the shell type")
}
run, found := completionShells[args[0]]
if !found {
return fmt.Errorf("unsupported shell type %q", args[0])

View File

@ -22,18 +22,6 @@ func TestCompletion(t *testing.T) {
CmdLine: "zsh",
Cmd: cmd,
},
{
Name: "completion-no-args",
CmdLine: "",
Cmd: cmd,
Error: errors.New("shell not specified"),
},
{
Name: "completion-too-many-args",
CmdLine: "bash zsh",
Cmd: cmd,
Error: errors.New("too many arguments, expected only the shell type"),
},
{
Name: "completion-unknown-shell",
CmdLine: "fish",