-
Notifications
You must be signed in to change notification settings - Fork 574
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rpk/cli: Add matchAll to all positional arguments definition
- Loading branch information
Showing
28 changed files
with
149 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright 2020 Vectorized, Inc. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.md | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0 | ||
|
||
package cli | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func newRootCmd(pargs cobra.PositionalArgs) *cobra.Command { | ||
return &cobra.Command{Use: "test", Args: pargs, Run: func(cmd *cobra.Command, args []string) { | ||
fmt.Fprintf(cmd.OutOrStdout(), "ok") | ||
}} | ||
} | ||
|
||
func executeCommand(cmd *cobra.Command, args []string) (out string, err error) { | ||
b := new(bytes.Buffer) | ||
cmd.SetOut(b) | ||
cmd.SetErr(b) | ||
cmd.SetArgs(args) | ||
_, e := cmd.ExecuteC() | ||
|
||
return b.String(), e | ||
} | ||
|
||
func TestMatchAll(t *testing.T) { | ||
// Arranged example to show the capability of MatchAll function, any kind of positional arguments | ||
// validation can be used in the function. | ||
positionalArgs := MatchAll(cobra.MinimumNArgs(1), cobra.MaximumNArgs(2)) | ||
cmd := newRootCmd(positionalArgs) | ||
|
||
tests := []struct { | ||
name string | ||
args []string | ||
output string | ||
fail bool | ||
}{ | ||
{ | ||
name: "Should validate with 1 argument", | ||
args: []string{"foo"}, | ||
output: "ok", | ||
fail: false, | ||
}, | ||
{ | ||
name: "Should validate with 2 argument", | ||
args: []string{"foo", "bar"}, | ||
output: "ok", | ||
fail: false, | ||
}, | ||
{ | ||
name: "Should show validation error and display usage when positional arguments are incorrect", | ||
args: []string{"foo", "bar", "baz"}, | ||
output: "Error: accepts at most 2 arg(s), received 3\n\nUsage:", | ||
fail: true, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
out, err := executeCommand(cmd, test.args) | ||
|
||
if err != nil && !test.fail { | ||
t.Errorf("Unexpected error: %v\n", err) | ||
} | ||
|
||
require.Contains(t, out, test.output) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.