mirror of
https://github.com/spf13/cobra
synced 2024-11-24 22:57:12 +00:00
Add validation args test whith PostTerminatorArgs
This commit is contained in:
parent
c18bddc73d
commit
94b8e4d5df
1 changed files with 122 additions and 3 deletions
125
args_test.go
125
args_test.go
|
@ -89,6 +89,22 @@ func TestNoArgs(t *testing.T) {
|
||||||
output, err := executeCommand(c)
|
output, err := executeCommand(c)
|
||||||
expectSuccess(output, err, t)
|
expectSuccess(output, err, t)
|
||||||
}
|
}
|
||||||
|
func TestNoArgs_WithPostTerminatorArgs(t *testing.T) {
|
||||||
|
c := getCommand(NoArgs, false)
|
||||||
|
_, err := executeCommand(c, "--", "post", "args")
|
||||||
|
noArgsWithArgs(err, t, "post")
|
||||||
|
// got := c.PostTerminatorArgs()
|
||||||
|
// expected := []string{"post", "args"}
|
||||||
|
// if strings.Join(got, ",") != strings.Join(expected, ",") {
|
||||||
|
// t.Fatalf("Expected %q, got %q", expected, got)
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
func TestNoArgs_WithIgnoredPostTerminatorArgs(t *testing.T) {
|
||||||
|
c := getCommand(NoArgs, false)
|
||||||
|
c.IgnorePostTerminatorArgs = true
|
||||||
|
output, err := executeCommand(c, "--", "post", "args")
|
||||||
|
expectSuccess(output, err, t)
|
||||||
|
}
|
||||||
|
|
||||||
func TestNoArgs_WithArgs(t *testing.T) {
|
func TestNoArgs_WithArgs(t *testing.T) {
|
||||||
c := getCommand(NoArgs, false)
|
c := getCommand(NoArgs, false)
|
||||||
|
@ -122,6 +138,19 @@ func TestOnlyValidArgs(t *testing.T) {
|
||||||
expectSuccess(output, err, t)
|
expectSuccess(output, err, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestOnlyValidArgs_WithPostTerminatorArgs(t *testing.T) {
|
||||||
|
c := getCommand(OnlyValidArgs, true)
|
||||||
|
_, err := executeCommand(c, "one", "two", "--", "post", "args")
|
||||||
|
expectError("invalid argument \"post\" for \"c\"", err, t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestOnlyValidArgs_WithIgnoredPostTerminatorArgs(t *testing.T) {
|
||||||
|
c := getCommand(OnlyValidArgs, true)
|
||||||
|
c.IgnorePostTerminatorArgs = true
|
||||||
|
output, err := executeCommand(c, "one", "two", "--", "post", "args")
|
||||||
|
expectSuccess(output, err, t)
|
||||||
|
}
|
||||||
|
|
||||||
func TestOnlyValidArgs_WithInvalidArgs(t *testing.T) {
|
func TestOnlyValidArgs_WithInvalidArgs(t *testing.T) {
|
||||||
c := getCommand(OnlyValidArgs, true)
|
c := getCommand(OnlyValidArgs, true)
|
||||||
_, err := executeCommand(c, "a")
|
_, err := executeCommand(c, "a")
|
||||||
|
@ -185,10 +214,21 @@ func TestMinimumNArgs_WithLessArgs(t *testing.T) {
|
||||||
_, err := executeCommand(c, "a")
|
_, err := executeCommand(c, "a")
|
||||||
minimumNArgsWithLessArgs(err, t)
|
minimumNArgsWithLessArgs(err, t)
|
||||||
}
|
}
|
||||||
|
func TestMinimumNArgs_WithLessArgs_WithPostTerminatorArgs(t *testing.T) {
|
||||||
|
c := getCommand(MinimumNArgs(2), false)
|
||||||
|
output, err := executeCommand(c, "a", "--", "post", "args")
|
||||||
|
expectSuccess(output, err, t)
|
||||||
|
}
|
||||||
|
func TestMinimumNArgs_WithLessArgs_WithIgnoredPostTerminatorArgs(t *testing.T) {
|
||||||
|
c := getCommand(MinimumNArgs(2), false)
|
||||||
|
c.IgnorePostTerminatorArgs = true
|
||||||
|
_, err := executeCommand(c, "a", "--", "post", "args")
|
||||||
|
minimumNArgsWithLessArgs(err, t)
|
||||||
|
}
|
||||||
|
|
||||||
func TestMinimumNArgs_WithLessArgs_WithValid(t *testing.T) {
|
func TestMinimumNArgs_WithLessArgs_WithValid(t *testing.T) {
|
||||||
c := getCommand(MinimumNArgs(2), true)
|
c := getCommand(MinimumNArgs(2), true)
|
||||||
_, err := executeCommand(c, "one")
|
_, err := executeCommand(c, "one") // @todo check
|
||||||
minimumNArgsWithLessArgs(err, t)
|
minimumNArgsWithLessArgs(err, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -212,12 +252,37 @@ func TestMaximumNArgs(t *testing.T) {
|
||||||
expectSuccess(output, err, t)
|
expectSuccess(output, err, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMaximumNArgs_WithPostTerminatorArgs(t *testing.T) {
|
||||||
|
c := getCommand(MaximumNArgs(3), false)
|
||||||
|
_, err := executeCommand(c, "a", "b", "--", "post", "args")
|
||||||
|
expectError("accepts at most 3 arg(s), received 4", err, t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMaximumNArgs_WithIgnoredPostTerminatorArgs(t *testing.T) {
|
||||||
|
c := getCommand(MaximumNArgs(3), false)
|
||||||
|
c.IgnorePostTerminatorArgs = true
|
||||||
|
output, err := executeCommand(c, "a", "b", "--", "post", "args")
|
||||||
|
expectSuccess(output, err, t)
|
||||||
|
}
|
||||||
|
|
||||||
func TestMaximumNArgs_WithValid(t *testing.T) {
|
func TestMaximumNArgs_WithValid(t *testing.T) {
|
||||||
c := getCommand(MaximumNArgs(2), true)
|
c := getCommand(MaximumNArgs(2), true)
|
||||||
output, err := executeCommand(c, "one", "three")
|
output, err := executeCommand(c, "one", "three")
|
||||||
expectSuccess(output, err, t)
|
expectSuccess(output, err, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMaximumNArgs_WithValid_WithPostTerminatorArgs(t *testing.T) {
|
||||||
|
c := getCommand(MaximumNArgs(2), true)
|
||||||
|
_, err := executeCommand(c, "one", "three", "--", "post", "args")
|
||||||
|
expectError("accepts at most 2 arg(s), received 4", err, t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMaximumNArgs_WithValid_WithIgnoredPostTermintatorArgs(t *testing.T) {
|
||||||
|
c := getCommand(MaximumNArgs(2), true)
|
||||||
|
c.IgnorePostTerminatorArgs = true
|
||||||
|
output, err := executeCommand(c, "one", "three", "--", "post", "args")
|
||||||
|
expectSuccess(output, err, t)
|
||||||
|
}
|
||||||
func TestMaximumNArgs_WithValid_WithInvalidArgs(t *testing.T) {
|
func TestMaximumNArgs_WithValid_WithInvalidArgs(t *testing.T) {
|
||||||
c := getCommand(MaximumNArgs(2), true)
|
c := getCommand(MaximumNArgs(2), true)
|
||||||
output, err := executeCommand(c, "a", "b")
|
output, err := executeCommand(c, "a", "b")
|
||||||
|
@ -236,6 +301,19 @@ func TestMaximumNArgs_WithMoreArgs(t *testing.T) {
|
||||||
maximumNArgsWithMoreArgs(err, t)
|
maximumNArgsWithMoreArgs(err, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMaximumNArgs_WithMoreArgs_WithPostTerminatorArgs(t *testing.T) {
|
||||||
|
c := getCommand(MaximumNArgs(2), false)
|
||||||
|
_, err := executeCommand(c, "a", "b", "c", "--", "post", "args")
|
||||||
|
expectError("accepts at most 2 arg(s), received 5", err, t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMaximumNArgs_WithMoreArgs_WithIgnoredPostTerminatorArgs(t *testing.T) {
|
||||||
|
c := getCommand(MaximumNArgs(2), false)
|
||||||
|
c.IgnorePostTerminatorArgs = true
|
||||||
|
_, err := executeCommand(c, "a", "b", "c", "--", "post", "args")
|
||||||
|
maximumNArgsWithMoreArgs(err, t)
|
||||||
|
}
|
||||||
|
|
||||||
func TestMaximumNArgs_WithMoreArgs_WithValid(t *testing.T) {
|
func TestMaximumNArgs_WithMoreArgs_WithValid(t *testing.T) {
|
||||||
c := getCommand(MaximumNArgs(2), true)
|
c := getCommand(MaximumNArgs(2), true)
|
||||||
_, err := executeCommand(c, "one", "three", "two")
|
_, err := executeCommand(c, "one", "three", "two")
|
||||||
|
@ -262,6 +340,19 @@ func TestExactArgs(t *testing.T) {
|
||||||
expectSuccess(output, err, t)
|
expectSuccess(output, err, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestExactArgs_WithPostTerminatorArgs(t *testing.T) {
|
||||||
|
c := getCommand(ExactArgs(3), false)
|
||||||
|
_, err := executeCommand(c, "a", "b", "c", "--", "post", "args")
|
||||||
|
expectError("accepts 3 arg(s), received 5", err, t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestExactArgs_WithIgnoredPostTerminatorArgs(t *testing.T) {
|
||||||
|
c := getCommand(ExactArgs(3), false)
|
||||||
|
c.IgnorePostTerminatorArgs = true
|
||||||
|
output, err := executeCommand(c, "a", "b", "c", "--", "post", "args")
|
||||||
|
expectSuccess(output, err, t)
|
||||||
|
}
|
||||||
|
|
||||||
func TestExactArgs_WithValid(t *testing.T) {
|
func TestExactArgs_WithValid(t *testing.T) {
|
||||||
c := getCommand(ExactArgs(3), true)
|
c := getCommand(ExactArgs(3), true)
|
||||||
output, err := executeCommand(c, "three", "one", "two")
|
output, err := executeCommand(c, "three", "one", "two")
|
||||||
|
@ -312,6 +403,19 @@ func TestRangeArgs(t *testing.T) {
|
||||||
expectSuccess(output, err, t)
|
expectSuccess(output, err, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRangeArgs_WithPostTerminatorArgs(t *testing.T) {
|
||||||
|
c := getCommand(RangeArgs(2, 4), false)
|
||||||
|
_, err := executeCommand(c, "a", "b", "c", "--", "post", "args")
|
||||||
|
expectError("accepts between 2 and 4 arg(s), received 5", err, t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRangeArgs_WithIgnoredPostTerminatorArgs(t *testing.T) {
|
||||||
|
c := getCommand(RangeArgs(2, 4), false)
|
||||||
|
c.IgnorePostTerminatorArgs = true
|
||||||
|
output, err := executeCommand(c, "a", "b", "c", "--", "post", "args")
|
||||||
|
expectSuccess(output, err, t)
|
||||||
|
}
|
||||||
|
|
||||||
func TestRangeArgs_WithValid(t *testing.T) {
|
func TestRangeArgs_WithValid(t *testing.T) {
|
||||||
c := getCommand(RangeArgs(2, 4), true)
|
c := getCommand(RangeArgs(2, 4), true)
|
||||||
output, err := executeCommand(c, "three", "one", "two")
|
output, err := executeCommand(c, "three", "one", "two")
|
||||||
|
@ -428,27 +532,42 @@ func TestMatchAll(t *testing.T) {
|
||||||
)
|
)
|
||||||
|
|
||||||
testCases := map[string]struct {
|
testCases := map[string]struct {
|
||||||
args []string
|
args []string
|
||||||
fail bool
|
ignorePostArgs bool
|
||||||
|
fail bool
|
||||||
}{
|
}{
|
||||||
"happy path": {
|
"happy path": {
|
||||||
[]string{"aa", "bb", "cc"},
|
[]string{"aa", "bb", "cc"},
|
||||||
false,
|
false,
|
||||||
|
false,
|
||||||
},
|
},
|
||||||
"incorrect number of args": {
|
"incorrect number of args": {
|
||||||
[]string{"aa", "bb", "cc", "dd"},
|
[]string{"aa", "bb", "cc", "dd"},
|
||||||
|
false,
|
||||||
true,
|
true,
|
||||||
},
|
},
|
||||||
"incorrect number of bytes in one arg": {
|
"incorrect number of bytes in one arg": {
|
||||||
[]string{"aa", "bb", "abc"},
|
[]string{"aa", "bb", "abc"},
|
||||||
|
false,
|
||||||
true,
|
true,
|
||||||
},
|
},
|
||||||
|
"happy path with post args makes unhappy": {
|
||||||
|
[]string{"aa", "bb", "cc", "--", "post", "args"},
|
||||||
|
false,
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
"happy path with ignored post args stay happy": {
|
||||||
|
[]string{"aa", "bb", "cc", "--", "post", "args"},
|
||||||
|
true,
|
||||||
|
false,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
rootCmd := &Command{Use: "root", Args: pargs, Run: emptyRun}
|
rootCmd := &Command{Use: "root", Args: pargs, Run: emptyRun}
|
||||||
|
|
||||||
for name, tc := range testCases {
|
for name, tc := range testCases {
|
||||||
t.Run(name, func(t *testing.T) {
|
t.Run(name, func(t *testing.T) {
|
||||||
|
rootCmd.IgnorePostTerminatorArgs = tc.ignorePostArgs
|
||||||
_, err := executeCommand(rootCmd, tc.args...)
|
_, err := executeCommand(rootCmd, tc.args...)
|
||||||
if err != nil && !tc.fail {
|
if err != nil && !tc.fail {
|
||||||
t.Errorf("unexpected: %v\n", err)
|
t.Errorf("unexpected: %v\n", err)
|
||||||
|
|
Loading…
Reference in a new issue