2023-03-06 05:28:31 +03:00
|
|
|
// Copyright 2013-2023 The Cobra Authors
|
2022-09-16 14:55:56 +03:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2017-10-31 21:57:17 +03:00
|
|
|
package cobra
|
|
|
|
|
|
|
|
import (
|
2021-12-08 01:38:00 +03:00
|
|
|
"fmt"
|
2017-10-31 21:57:17 +03:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2021-11-17 01:20:18 +03:00
|
|
|
func getCommand(args PositionalArgs, withValid bool) *Command {
|
|
|
|
c := &Command{
|
|
|
|
Use: "c",
|
|
|
|
Args: args,
|
|
|
|
Run: emptyRun,
|
|
|
|
}
|
|
|
|
if withValid {
|
|
|
|
c.ValidArgs = []string{"one", "two", "three"}
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
2017-10-31 21:57:17 +03:00
|
|
|
|
2021-11-17 01:20:18 +03:00
|
|
|
func expectSuccess(output string, err error, t *testing.T) {
|
2017-10-31 21:57:17 +03:00
|
|
|
if output != "" {
|
2021-11-17 01:20:18 +03:00
|
|
|
t.Errorf("Unexpected output: %v", output)
|
2017-10-31 21:57:17 +03:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-10 16:33:34 +03:00
|
|
|
func validOnlyWithInvalidArgs(err error, t *testing.T) {
|
2017-10-31 21:57:17 +03:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected an error")
|
|
|
|
}
|
2021-11-17 01:20:18 +03:00
|
|
|
got := err.Error()
|
|
|
|
expected := `invalid argument "a" for "c"`
|
|
|
|
if got != expected {
|
|
|
|
t.Errorf("Expected: %q, got: %q", expected, got)
|
|
|
|
}
|
|
|
|
}
|
2017-10-31 21:57:17 +03:00
|
|
|
|
2022-09-10 16:33:34 +03:00
|
|
|
func noArgsWithArgs(err error, t *testing.T, arg string) {
|
2021-11-17 01:20:18 +03:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected an error")
|
|
|
|
}
|
2017-10-31 21:57:17 +03:00
|
|
|
got := err.Error()
|
2022-09-10 16:33:34 +03:00
|
|
|
expected := `unknown command "` + arg + `" for "c"`
|
2017-10-31 21:57:17 +03:00
|
|
|
if got != expected {
|
|
|
|
t.Errorf("Expected: %q, got: %q", expected, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-17 01:20:18 +03:00
|
|
|
func minimumNArgsWithLessArgs(err error, t *testing.T) {
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected an error")
|
|
|
|
}
|
|
|
|
got := err.Error()
|
|
|
|
expected := "requires at least 2 arg(s), only received 1"
|
|
|
|
if got != expected {
|
|
|
|
t.Fatalf("Expected %q, got %q", expected, got)
|
2017-10-31 21:57:17 +03:00
|
|
|
}
|
2021-11-17 01:20:18 +03:00
|
|
|
}
|
2017-10-31 21:57:17 +03:00
|
|
|
|
2021-11-17 01:20:18 +03:00
|
|
|
func maximumNArgsWithMoreArgs(err error, t *testing.T) {
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected an error")
|
2017-10-31 21:57:17 +03:00
|
|
|
}
|
2021-11-17 01:20:18 +03:00
|
|
|
got := err.Error()
|
|
|
|
expected := "accepts at most 2 arg(s), received 3"
|
|
|
|
if got != expected {
|
|
|
|
t.Fatalf("Expected %q, got %q", expected, got)
|
2017-10-31 21:57:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-17 01:20:18 +03:00
|
|
|
func exactArgsWithInvalidCount(err error, t *testing.T) {
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected an error")
|
|
|
|
}
|
|
|
|
got := err.Error()
|
|
|
|
expected := "accepts 2 arg(s), received 3"
|
|
|
|
if got != expected {
|
|
|
|
t.Fatalf("Expected %q, got %q", expected, got)
|
2017-10-31 21:57:17 +03:00
|
|
|
}
|
2021-11-17 01:20:18 +03:00
|
|
|
}
|
2017-10-31 21:57:17 +03:00
|
|
|
|
2021-11-17 01:20:18 +03:00
|
|
|
func rangeArgsWithInvalidCount(err error, t *testing.T) {
|
2017-10-31 21:57:17 +03:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected an error")
|
|
|
|
}
|
|
|
|
got := err.Error()
|
2021-11-17 01:20:18 +03:00
|
|
|
expected := "accepts between 2 and 4 arg(s), received 1"
|
2017-10-31 21:57:17 +03:00
|
|
|
if got != expected {
|
2021-11-17 01:20:18 +03:00
|
|
|
t.Fatalf("Expected %q, got %q", expected, got)
|
2017-10-31 21:57:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-10 16:33:34 +03:00
|
|
|
// NoArgs
|
|
|
|
|
2021-11-17 01:20:18 +03:00
|
|
|
func TestNoArgs(t *testing.T) {
|
|
|
|
c := getCommand(NoArgs, false)
|
|
|
|
output, err := executeCommand(c)
|
|
|
|
expectSuccess(output, err, t)
|
|
|
|
}
|
|
|
|
|
2022-09-10 16:33:34 +03:00
|
|
|
func TestNoArgs_WithArgs(t *testing.T) {
|
2021-11-17 01:20:18 +03:00
|
|
|
c := getCommand(NoArgs, false)
|
2022-09-10 16:33:34 +03:00
|
|
|
_, err := executeCommand(c, "one")
|
|
|
|
noArgsWithArgs(err, t, "one")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNoArgs_WithValid_WithArgs(t *testing.T) {
|
|
|
|
c := getCommand(NoArgs, true)
|
|
|
|
_, err := executeCommand(c, "one")
|
|
|
|
noArgsWithArgs(err, t, "one")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNoArgs_WithValid_WithInvalidArgs(t *testing.T) {
|
|
|
|
c := getCommand(NoArgs, true)
|
|
|
|
_, err := executeCommand(c, "a")
|
|
|
|
noArgsWithArgs(err, t, "a")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNoArgs_WithValidOnly_WithInvalidArgs(t *testing.T) {
|
|
|
|
c := getCommand(MatchAll(OnlyValidArgs, NoArgs), true)
|
|
|
|
_, err := executeCommand(c, "a")
|
|
|
|
validOnlyWithInvalidArgs(err, t)
|
2021-11-17 01:20:18 +03:00
|
|
|
}
|
|
|
|
|
2022-09-10 16:33:34 +03:00
|
|
|
// OnlyValidArgs
|
|
|
|
|
2021-11-17 01:20:18 +03:00
|
|
|
func TestOnlyValidArgs(t *testing.T) {
|
|
|
|
c := getCommand(OnlyValidArgs, true)
|
|
|
|
output, err := executeCommand(c, "one", "two")
|
|
|
|
expectSuccess(output, err, t)
|
|
|
|
}
|
|
|
|
|
2022-09-10 16:33:34 +03:00
|
|
|
func TestOnlyValidArgs_WithInvalidArgs(t *testing.T) {
|
2021-11-17 01:20:18 +03:00
|
|
|
c := getCommand(OnlyValidArgs, true)
|
|
|
|
_, err := executeCommand(c, "a")
|
2022-09-10 16:33:34 +03:00
|
|
|
validOnlyWithInvalidArgs(err, t)
|
2021-11-17 01:20:18 +03:00
|
|
|
}
|
|
|
|
|
2022-09-10 16:33:34 +03:00
|
|
|
// ArbitraryArgs
|
|
|
|
|
2017-10-31 21:57:17 +03:00
|
|
|
func TestArbitraryArgs(t *testing.T) {
|
2021-11-17 01:20:18 +03:00
|
|
|
c := getCommand(ArbitraryArgs, false)
|
2017-10-31 21:57:17 +03:00
|
|
|
output, err := executeCommand(c, "a", "b")
|
2021-11-17 01:20:18 +03:00
|
|
|
expectSuccess(output, err, t)
|
2017-10-31 21:57:17 +03:00
|
|
|
}
|
|
|
|
|
2022-09-10 16:33:34 +03:00
|
|
|
func TestArbitraryArgs_WithValid(t *testing.T) {
|
|
|
|
c := getCommand(ArbitraryArgs, true)
|
|
|
|
output, err := executeCommand(c, "one", "two")
|
|
|
|
expectSuccess(output, err, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestArbitraryArgs_WithValid_WithInvalidArgs(t *testing.T) {
|
|
|
|
c := getCommand(ArbitraryArgs, true)
|
|
|
|
output, err := executeCommand(c, "a")
|
|
|
|
expectSuccess(output, err, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestArbitraryArgs_WithValidOnly_WithInvalidArgs(t *testing.T) {
|
|
|
|
c := getCommand(MatchAll(OnlyValidArgs, ArbitraryArgs), true)
|
|
|
|
_, err := executeCommand(c, "a")
|
|
|
|
validOnlyWithInvalidArgs(err, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MinimumNArgs
|
|
|
|
|
2017-10-31 21:57:17 +03:00
|
|
|
func TestMinimumNArgs(t *testing.T) {
|
2021-11-17 01:20:18 +03:00
|
|
|
c := getCommand(MinimumNArgs(2), false)
|
2017-10-31 21:57:17 +03:00
|
|
|
output, err := executeCommand(c, "a", "b", "c")
|
2021-11-17 01:20:18 +03:00
|
|
|
expectSuccess(output, err, t)
|
2017-10-31 21:57:17 +03:00
|
|
|
}
|
|
|
|
|
2022-09-10 16:33:34 +03:00
|
|
|
func TestMinimumNArgs_WithValid(t *testing.T) {
|
|
|
|
c := getCommand(MinimumNArgs(2), true)
|
|
|
|
output, err := executeCommand(c, "one", "three")
|
|
|
|
expectSuccess(output, err, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMinimumNArgs_WithValid__WithInvalidArgs(t *testing.T) {
|
|
|
|
c := getCommand(MinimumNArgs(2), true)
|
|
|
|
output, err := executeCommand(c, "a", "b")
|
|
|
|
expectSuccess(output, err, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMinimumNArgs_WithValidOnly_WithInvalidArgs(t *testing.T) {
|
|
|
|
c := getCommand(MatchAll(OnlyValidArgs, MinimumNArgs(2)), true)
|
|
|
|
_, err := executeCommand(c, "a", "b")
|
|
|
|
validOnlyWithInvalidArgs(err, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMinimumNArgs_WithLessArgs(t *testing.T) {
|
2021-11-17 01:20:18 +03:00
|
|
|
c := getCommand(MinimumNArgs(2), false)
|
2017-10-31 21:57:17 +03:00
|
|
|
_, err := executeCommand(c, "a")
|
2021-11-17 01:20:18 +03:00
|
|
|
minimumNArgsWithLessArgs(err, t)
|
2017-10-31 21:57:17 +03:00
|
|
|
}
|
|
|
|
|
2022-09-10 16:33:34 +03:00
|
|
|
func TestMinimumNArgs_WithLessArgs_WithValid(t *testing.T) {
|
|
|
|
c := getCommand(MinimumNArgs(2), true)
|
|
|
|
_, err := executeCommand(c, "one")
|
|
|
|
minimumNArgsWithLessArgs(err, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMinimumNArgs_WithLessArgs_WithValid_WithInvalidArgs(t *testing.T) {
|
|
|
|
c := getCommand(MinimumNArgs(2), true)
|
|
|
|
_, err := executeCommand(c, "a")
|
|
|
|
minimumNArgsWithLessArgs(err, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMinimumNArgs_WithLessArgs_WithValidOnly_WithInvalidArgs(t *testing.T) {
|
|
|
|
c := getCommand(MatchAll(OnlyValidArgs, MinimumNArgs(2)), true)
|
|
|
|
_, err := executeCommand(c, "a")
|
|
|
|
validOnlyWithInvalidArgs(err, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MaximumNArgs
|
|
|
|
|
2017-10-31 21:57:17 +03:00
|
|
|
func TestMaximumNArgs(t *testing.T) {
|
2021-11-17 01:20:18 +03:00
|
|
|
c := getCommand(MaximumNArgs(3), false)
|
2017-10-31 21:57:17 +03:00
|
|
|
output, err := executeCommand(c, "a", "b")
|
2021-11-17 01:20:18 +03:00
|
|
|
expectSuccess(output, err, t)
|
2017-10-31 21:57:17 +03:00
|
|
|
}
|
|
|
|
|
2022-09-10 16:33:34 +03:00
|
|
|
func TestMaximumNArgs_WithValid(t *testing.T) {
|
|
|
|
c := getCommand(MaximumNArgs(2), true)
|
|
|
|
output, err := executeCommand(c, "one", "three")
|
|
|
|
expectSuccess(output, err, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMaximumNArgs_WithValid_WithInvalidArgs(t *testing.T) {
|
|
|
|
c := getCommand(MaximumNArgs(2), true)
|
|
|
|
output, err := executeCommand(c, "a", "b")
|
|
|
|
expectSuccess(output, err, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMaximumNArgs_WithValidOnly_WithInvalidArgs(t *testing.T) {
|
|
|
|
c := getCommand(MatchAll(OnlyValidArgs, MaximumNArgs(2)), true)
|
|
|
|
_, err := executeCommand(c, "a", "b")
|
|
|
|
validOnlyWithInvalidArgs(err, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMaximumNArgs_WithMoreArgs(t *testing.T) {
|
2021-11-17 01:20:18 +03:00
|
|
|
c := getCommand(MaximumNArgs(2), false)
|
2017-10-31 21:57:17 +03:00
|
|
|
_, err := executeCommand(c, "a", "b", "c")
|
2021-11-17 01:20:18 +03:00
|
|
|
maximumNArgsWithMoreArgs(err, t)
|
2017-10-31 21:57:17 +03:00
|
|
|
}
|
|
|
|
|
2022-09-10 16:33:34 +03:00
|
|
|
func TestMaximumNArgs_WithMoreArgs_WithValid(t *testing.T) {
|
|
|
|
c := getCommand(MaximumNArgs(2), true)
|
|
|
|
_, err := executeCommand(c, "one", "three", "two")
|
|
|
|
maximumNArgsWithMoreArgs(err, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMaximumNArgs_WithMoreArgs_WithValid_WithInvalidArgs(t *testing.T) {
|
|
|
|
c := getCommand(MaximumNArgs(2), true)
|
|
|
|
_, err := executeCommand(c, "a", "b", "c")
|
|
|
|
maximumNArgsWithMoreArgs(err, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMaximumNArgs_WithMoreArgs_WithValidOnly_WithInvalidArgs(t *testing.T) {
|
|
|
|
c := getCommand(MatchAll(OnlyValidArgs, MaximumNArgs(2)), true)
|
|
|
|
_, err := executeCommand(c, "a", "b", "c")
|
|
|
|
validOnlyWithInvalidArgs(err, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExactArgs
|
|
|
|
|
2017-10-31 21:57:17 +03:00
|
|
|
func TestExactArgs(t *testing.T) {
|
2021-11-17 01:20:18 +03:00
|
|
|
c := getCommand(ExactArgs(3), false)
|
2017-10-31 21:57:17 +03:00
|
|
|
output, err := executeCommand(c, "a", "b", "c")
|
2021-11-17 01:20:18 +03:00
|
|
|
expectSuccess(output, err, t)
|
2017-10-31 21:57:17 +03:00
|
|
|
}
|
|
|
|
|
2022-09-10 16:33:34 +03:00
|
|
|
func TestExactArgs_WithValid(t *testing.T) {
|
|
|
|
c := getCommand(ExactArgs(3), true)
|
|
|
|
output, err := executeCommand(c, "three", "one", "two")
|
|
|
|
expectSuccess(output, err, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExactArgs_WithValid_WithInvalidArgs(t *testing.T) {
|
|
|
|
c := getCommand(ExactArgs(3), true)
|
|
|
|
output, err := executeCommand(c, "three", "a", "two")
|
|
|
|
expectSuccess(output, err, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExactArgs_WithValidOnly_WithInvalidArgs(t *testing.T) {
|
|
|
|
c := getCommand(MatchAll(OnlyValidArgs, ExactArgs(3)), true)
|
|
|
|
_, err := executeCommand(c, "three", "a", "two")
|
|
|
|
validOnlyWithInvalidArgs(err, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExactArgs_WithInvalidCount(t *testing.T) {
|
2021-11-17 01:20:18 +03:00
|
|
|
c := getCommand(ExactArgs(2), false)
|
2017-10-31 21:57:17 +03:00
|
|
|
_, err := executeCommand(c, "a", "b", "c")
|
2021-11-17 01:20:18 +03:00
|
|
|
exactArgsWithInvalidCount(err, t)
|
2017-10-31 21:57:17 +03:00
|
|
|
}
|
|
|
|
|
2022-09-10 16:33:34 +03:00
|
|
|
func TestExactArgs_WithInvalidCount_WithValid(t *testing.T) {
|
|
|
|
c := getCommand(ExactArgs(2), true)
|
|
|
|
_, err := executeCommand(c, "three", "one", "two")
|
|
|
|
exactArgsWithInvalidCount(err, t)
|
2018-10-21 17:01:21 +03:00
|
|
|
}
|
|
|
|
|
2022-09-10 16:33:34 +03:00
|
|
|
func TestExactArgs_WithInvalidCount_WithValid_WithInvalidArgs(t *testing.T) {
|
|
|
|
c := getCommand(ExactArgs(2), true)
|
|
|
|
_, err := executeCommand(c, "three", "a", "two")
|
2021-11-17 01:20:18 +03:00
|
|
|
exactArgsWithInvalidCount(err, t)
|
2018-10-21 17:01:21 +03:00
|
|
|
}
|
|
|
|
|
2022-09-10 16:33:34 +03:00
|
|
|
func TestExactArgs_WithInvalidCount_WithValidOnly_WithInvalidArgs(t *testing.T) {
|
|
|
|
c := getCommand(MatchAll(OnlyValidArgs, ExactArgs(2)), true)
|
2021-11-17 01:20:18 +03:00
|
|
|
_, err := executeCommand(c, "three", "a", "two")
|
2022-09-10 16:33:34 +03:00
|
|
|
validOnlyWithInvalidArgs(err, t)
|
2018-10-21 17:01:21 +03:00
|
|
|
}
|
|
|
|
|
2022-09-10 16:33:34 +03:00
|
|
|
// RangeArgs
|
|
|
|
|
2017-10-31 21:57:17 +03:00
|
|
|
func TestRangeArgs(t *testing.T) {
|
2021-11-17 01:20:18 +03:00
|
|
|
c := getCommand(RangeArgs(2, 4), false)
|
2017-10-31 21:57:17 +03:00
|
|
|
output, err := executeCommand(c, "a", "b", "c")
|
2021-11-17 01:20:18 +03:00
|
|
|
expectSuccess(output, err, t)
|
2017-10-31 21:57:17 +03:00
|
|
|
}
|
|
|
|
|
2022-09-10 16:33:34 +03:00
|
|
|
func TestRangeArgs_WithValid(t *testing.T) {
|
|
|
|
c := getCommand(RangeArgs(2, 4), true)
|
|
|
|
output, err := executeCommand(c, "three", "one", "two")
|
|
|
|
expectSuccess(output, err, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRangeArgs_WithValid_WithInvalidArgs(t *testing.T) {
|
|
|
|
c := getCommand(RangeArgs(2, 4), true)
|
|
|
|
output, err := executeCommand(c, "three", "a", "two")
|
|
|
|
expectSuccess(output, err, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRangeArgs_WithValidOnly_WithInvalidArgs(t *testing.T) {
|
|
|
|
c := getCommand(MatchAll(OnlyValidArgs, RangeArgs(2, 4)), true)
|
|
|
|
_, err := executeCommand(c, "three", "a", "two")
|
|
|
|
validOnlyWithInvalidArgs(err, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRangeArgs_WithInvalidCount(t *testing.T) {
|
2021-11-17 01:20:18 +03:00
|
|
|
c := getCommand(RangeArgs(2, 4), false)
|
2017-10-31 21:57:17 +03:00
|
|
|
_, err := executeCommand(c, "a")
|
2021-11-17 01:20:18 +03:00
|
|
|
rangeArgsWithInvalidCount(err, t)
|
2017-10-31 21:57:17 +03:00
|
|
|
}
|
|
|
|
|
2022-09-10 16:33:34 +03:00
|
|
|
func TestRangeArgs_WithInvalidCount_WithValid(t *testing.T) {
|
|
|
|
c := getCommand(RangeArgs(2, 4), true)
|
|
|
|
_, err := executeCommand(c, "two")
|
|
|
|
rangeArgsWithInvalidCount(err, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRangeArgs_WithInvalidCount_WithValid_WithInvalidArgs(t *testing.T) {
|
|
|
|
c := getCommand(RangeArgs(2, 4), true)
|
|
|
|
_, err := executeCommand(c, "a")
|
|
|
|
rangeArgsWithInvalidCount(err, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRangeArgs_WithInvalidCount_WithValidOnly_WithInvalidArgs(t *testing.T) {
|
|
|
|
c := getCommand(MatchAll(OnlyValidArgs, RangeArgs(2, 4)), true)
|
|
|
|
_, err := executeCommand(c, "a")
|
|
|
|
validOnlyWithInvalidArgs(err, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Takes(No)Args
|
|
|
|
|
2017-10-31 21:57:17 +03:00
|
|
|
func TestRootTakesNoArgs(t *testing.T) {
|
|
|
|
rootCmd := &Command{Use: "root", Run: emptyRun}
|
|
|
|
childCmd := &Command{Use: "child", Run: emptyRun}
|
|
|
|
rootCmd.AddCommand(childCmd)
|
|
|
|
|
|
|
|
_, err := executeCommand(rootCmd, "illegal", "args")
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected an error")
|
|
|
|
}
|
|
|
|
|
|
|
|
got := err.Error()
|
|
|
|
expected := `unknown command "illegal" for "root"`
|
|
|
|
if !strings.Contains(got, expected) {
|
|
|
|
t.Errorf("expected %q, got %q", expected, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRootTakesArgs(t *testing.T) {
|
|
|
|
rootCmd := &Command{Use: "root", Args: ArbitraryArgs, Run: emptyRun}
|
|
|
|
childCmd := &Command{Use: "child", Run: emptyRun}
|
|
|
|
rootCmd.AddCommand(childCmd)
|
|
|
|
|
|
|
|
_, err := executeCommand(rootCmd, "legal", "args")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestChildTakesNoArgs(t *testing.T) {
|
|
|
|
rootCmd := &Command{Use: "root", Run: emptyRun}
|
|
|
|
childCmd := &Command{Use: "child", Args: NoArgs, Run: emptyRun}
|
|
|
|
rootCmd.AddCommand(childCmd)
|
|
|
|
|
|
|
|
_, err := executeCommand(rootCmd, "child", "illegal", "args")
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected an error")
|
|
|
|
}
|
|
|
|
|
|
|
|
got := err.Error()
|
|
|
|
expected := `unknown command "illegal" for "root child"`
|
|
|
|
if !strings.Contains(got, expected) {
|
|
|
|
t.Errorf("expected %q, got %q", expected, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestChildTakesArgs(t *testing.T) {
|
|
|
|
rootCmd := &Command{Use: "root", Run: emptyRun}
|
|
|
|
childCmd := &Command{Use: "child", Args: ArbitraryArgs, Run: emptyRun}
|
|
|
|
rootCmd.AddCommand(childCmd)
|
|
|
|
|
|
|
|
_, err := executeCommand(rootCmd, "child", "legal", "args")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2021-12-08 01:38:00 +03:00
|
|
|
|
|
|
|
func TestMatchAll(t *testing.T) {
|
|
|
|
// Somewhat contrived example check that ensures there are exactly 3
|
|
|
|
// arguments, and each argument is exactly 2 bytes long.
|
|
|
|
pargs := MatchAll(
|
|
|
|
ExactArgs(3),
|
|
|
|
func(cmd *Command, args []string) error {
|
|
|
|
for _, arg := range args {
|
|
|
|
if len([]byte(arg)) != 2 {
|
|
|
|
return fmt.Errorf("expected to be exactly 2 bytes long")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
|
|
|
args []string
|
|
|
|
fail bool
|
|
|
|
}{
|
|
|
|
"happy path": {
|
|
|
|
[]string{"aa", "bb", "cc"},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
"incorrect number of args": {
|
|
|
|
[]string{"aa", "bb", "cc", "dd"},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
"incorrect number of bytes in one arg": {
|
|
|
|
[]string{"aa", "bb", "abc"},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
rootCmd := &Command{Use: "root", Args: pargs, Run: emptyRun}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
_, err := executeCommand(rootCmd, tc.args...)
|
|
|
|
if err != nil && !tc.fail {
|
|
|
|
t.Errorf("unexpected: %v\n", err)
|
|
|
|
}
|
|
|
|
if err == nil && tc.fail {
|
|
|
|
t.Errorf("expected error")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-03-18 04:36:47 +03:00
|
|
|
|
2022-09-10 16:33:34 +03:00
|
|
|
// DEPRECATED
|
|
|
|
|
|
|
|
func TestExactValidArgs(t *testing.T) {
|
|
|
|
c := getCommand(ExactValidArgs(3), true)
|
|
|
|
output, err := executeCommand(c, "three", "one", "two")
|
|
|
|
expectSuccess(output, err, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExactValidArgs_WithInvalidCount(t *testing.T) {
|
|
|
|
c := getCommand(ExactValidArgs(2), false)
|
|
|
|
_, err := executeCommand(c, "three", "one", "two")
|
|
|
|
exactArgsWithInvalidCount(err, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExactValidArgs_WithInvalidCount_WithInvalidArgs(t *testing.T) {
|
|
|
|
c := getCommand(ExactValidArgs(2), true)
|
|
|
|
_, err := executeCommand(c, "three", "a", "two")
|
|
|
|
exactArgsWithInvalidCount(err, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExactValidArgs_WithInvalidArgs(t *testing.T) {
|
|
|
|
c := getCommand(ExactValidArgs(2), true)
|
|
|
|
_, err := executeCommand(c, "three", "a")
|
|
|
|
validOnlyWithInvalidArgs(err, t)
|
|
|
|
}
|
|
|
|
|
2022-03-18 04:36:47 +03:00
|
|
|
// This test make sure we keep backwards-compatibility with respect
|
|
|
|
// to the legacyArgs() function.
|
|
|
|
// It makes sure the root command accepts arguments if it does not have
|
|
|
|
// sub-commands.
|
|
|
|
func TestLegacyArgsRootAcceptsArgs(t *testing.T) {
|
|
|
|
rootCmd := &Command{Use: "root", Args: nil, Run: emptyRun}
|
|
|
|
|
|
|
|
_, err := executeCommand(rootCmd, "somearg")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This test make sure we keep backwards-compatibility with respect
|
|
|
|
// to the legacyArgs() function.
|
|
|
|
// It makes sure a sub-command accepts arguments and further sub-commands
|
|
|
|
func TestLegacyArgsSubcmdAcceptsArgs(t *testing.T) {
|
|
|
|
rootCmd := &Command{Use: "root", Args: nil, Run: emptyRun}
|
|
|
|
childCmd := &Command{Use: "child", Args: nil, Run: emptyRun}
|
|
|
|
grandchildCmd := &Command{Use: "grandchild", Args: nil, Run: emptyRun}
|
|
|
|
rootCmd.AddCommand(childCmd)
|
|
|
|
childCmd.AddCommand(grandchildCmd)
|
|
|
|
|
|
|
|
_, err := executeCommand(rootCmd, "child", "somearg")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
}
|