The validation functions to test for the number of passed arguments did not
pluralize `argument(s)`, and used `argument(s)` in all cases.
This patch adds a simple `pluralize()` helper to improve this.
Before this change, `argument(s)` was used in all cases:
$ docker container ls foobar
"docker container ls" accepts no argument(s).
$ docker network create one two
"docker network create" requires exactly 1 argument(s).
$ docker network connect
"docker network connect" requires exactly 2 argument(s).
$ docker volume create one two
"docker volume create" requires at most 1 argument(s).
After this change, `argument(s)` is properly singularized or plurarized:
$ docker container ls foobar
"docker container ls" accepts no arguments.
$ docker network create one two
"docker network create" requires exactly 1 argument.
$ docker network connect
"docker network connect" requires exactly 2 arguments.
$ docker volume create one two
"docker volume create" requires at most 1 argument.
Test cases were updated accordingly.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
142 lines
2.9 KiB
Go
142 lines
2.9 KiB
Go
package cli
|
|
|
|
import (
|
|
"errors"
|
|
"io/ioutil"
|
|
"testing"
|
|
|
|
"github.com/spf13/cobra"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestRequiresNoArgs(t *testing.T) {
|
|
testCases := []testCase{
|
|
{
|
|
validateFunc: NoArgs,
|
|
expectedError: "no error",
|
|
},
|
|
{
|
|
args: []string{"foo"},
|
|
validateFunc: NoArgs,
|
|
expectedError: "accepts no arguments.",
|
|
},
|
|
}
|
|
runTestCases(t, testCases)
|
|
}
|
|
|
|
func TestRequiresMinArgs(t *testing.T) {
|
|
testCases := []testCase{
|
|
{
|
|
validateFunc: RequiresMinArgs(0),
|
|
expectedError: "no error",
|
|
},
|
|
{
|
|
validateFunc: RequiresMinArgs(1),
|
|
expectedError: "at least 1 argument.",
|
|
},
|
|
{
|
|
args: []string{"foo"},
|
|
validateFunc: RequiresMinArgs(2),
|
|
expectedError: "at least 2 arguments.",
|
|
},
|
|
}
|
|
runTestCases(t, testCases)
|
|
}
|
|
|
|
func TestRequiresMaxArgs(t *testing.T) {
|
|
testCases := []testCase{
|
|
{
|
|
validateFunc: RequiresMaxArgs(0),
|
|
expectedError: "no error",
|
|
},
|
|
{
|
|
args: []string{"foo", "bar"},
|
|
validateFunc: RequiresMaxArgs(1),
|
|
expectedError: "at most 1 argument.",
|
|
},
|
|
{
|
|
args: []string{"foo", "bar", "baz"},
|
|
validateFunc: RequiresMaxArgs(2),
|
|
expectedError: "at most 2 arguments.",
|
|
},
|
|
}
|
|
runTestCases(t, testCases)
|
|
}
|
|
|
|
func TestRequiresRangeArgs(t *testing.T) {
|
|
testCases := []testCase{
|
|
{
|
|
validateFunc: RequiresRangeArgs(0, 0),
|
|
expectedError: "no error",
|
|
},
|
|
{
|
|
validateFunc: RequiresRangeArgs(0, 1),
|
|
expectedError: "no error",
|
|
},
|
|
{
|
|
args: []string{"foo", "bar"},
|
|
validateFunc: RequiresRangeArgs(0, 1),
|
|
expectedError: "at most 1 argument.",
|
|
},
|
|
{
|
|
args: []string{"foo", "bar", "baz"},
|
|
validateFunc: RequiresRangeArgs(0, 2),
|
|
expectedError: "at most 2 arguments.",
|
|
},
|
|
{
|
|
validateFunc: RequiresRangeArgs(1, 2),
|
|
expectedError: "at least 1 ",
|
|
},
|
|
}
|
|
runTestCases(t, testCases)
|
|
}
|
|
|
|
func TestExactArgs(t *testing.T) {
|
|
testCases := []testCase{
|
|
{
|
|
validateFunc: ExactArgs(0),
|
|
expectedError: "no error",
|
|
},
|
|
{
|
|
validateFunc: ExactArgs(1),
|
|
expectedError: "exactly 1 argument.",
|
|
},
|
|
{
|
|
validateFunc: ExactArgs(2),
|
|
expectedError: "exactly 2 arguments.",
|
|
},
|
|
}
|
|
runTestCases(t, testCases)
|
|
}
|
|
|
|
type testCase struct {
|
|
args []string
|
|
validateFunc cobra.PositionalArgs
|
|
expectedError string
|
|
}
|
|
|
|
func runTestCases(t *testing.T, testCases []testCase) {
|
|
for _, tc := range testCases {
|
|
cmd := newDummyCommand(tc.validateFunc)
|
|
cmd.SetArgs(tc.args)
|
|
cmd.SetOutput(ioutil.Discard)
|
|
|
|
err := cmd.Execute()
|
|
|
|
require.Error(t, err, "Expected an error: %s", tc.expectedError)
|
|
assert.Contains(t, err.Error(), tc.expectedError)
|
|
}
|
|
}
|
|
|
|
func newDummyCommand(validationFunc cobra.PositionalArgs) *cobra.Command {
|
|
cmd := &cobra.Command{
|
|
Use: "dummy",
|
|
Args: validationFunc,
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
return errors.New("no error")
|
|
},
|
|
}
|
|
return cmd
|
|
}
|