args_test.go
This file
Path
args_test.go
578 lines · 15.6 KiB · Go
Last changed by
Add cobra unique args validator (#2397)takanuva15before adoptionad460ea
History of this fileTake it
1// Copyright 2013-2023 The Cobra Authors2//3// Licensed under the Apache License, Version 2.0 (the "License");4// you may not use this file except in compliance with the License.5// You may obtain a copy of the License at6//7// http://www.apache.org/licenses/LICENSE-2.08//9// Unless required by applicable law or agreed to in writing, software10// distributed under the License is distributed on an "AS IS" BASIS,11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12// See the License for the specific language governing permissions and13// limitations under the License.1415package cobra1617import (18 "fmt"19 "strings"20 "testing"21)2223func getCommand(args PositionalArgs, withValid bool) *Command {24 c := &Command{25 Use: "c",26 Args: args,27 Run: emptyRun,28 }29 if withValid {30 c.ValidArgs = []string{"one", "two", "three"}31 }32 return c33}3435func expectSuccess(output string, err error, t *testing.T) {36 if output != "" {37 t.Errorf("Unexpected output: %v", output)38 }39 if err != nil {40 t.Fatalf("Unexpected error: %v", err)41 }42}4344func validOnlyWithInvalidArgs(err error, t *testing.T) {45 if err == nil {46 t.Fatal("Expected an error")47 }48 got := err.Error()49 expected := `invalid argument "a" for "c"`50 if got != expected {51 t.Errorf("Expected: %q, got: %q", expected, got)52 }53}5455func noArgsWithArgs(err error, t *testing.T, arg string) {56 if err == nil {57 t.Fatal("Expected an error")58 }59 got := err.Error()60 expected := `unknown command "` + arg + `" for "c"`61 if got != expected {62 t.Errorf("Expected: %q, got: %q", expected, got)63 }64}6566func minimumNArgsWithLessArgs(err error, t *testing.T) {67 if err == nil {68 t.Fatal("Expected an error")69 }70 got := err.Error()71 expected := "requires at least 2 arg(s), only received 1"72 if got != expected {73 t.Fatalf("Expected %q, got %q", expected, got)74 }75}7677func maximumNArgsWithMoreArgs(err error, t *testing.T) {78 if err == nil {79 t.Fatal("Expected an error")80 }81 got := err.Error()82 expected := "accepts at most 2 arg(s), received 3"83 if got != expected {84 t.Fatalf("Expected %q, got %q", expected, got)85 }86}8788func exactArgsWithInvalidCount(err error, t *testing.T) {89 if err == nil {90 t.Fatal("Expected an error")91 }92 got := err.Error()93 expected := "accepts 2 arg(s), received 3"94 if got != expected {95 t.Fatalf("Expected %q, got %q", expected, got)96 }97}9899func rangeArgsWithInvalidCount(err error, t *testing.T) {100 if err == nil {101 t.Fatal("Expected an error")102 }103 got := err.Error()104 expected := "accepts between 2 and 4 arg(s), received 1"105 if got != expected {106 t.Fatalf("Expected %q, got %q", expected, got)107 }108}109110func noDuplicateArgsWithDuplicate(err error, t *testing.T, arg string) {111 if err == nil {112 t.Fatal("Expected an error")113 }114 got := err.Error()115 expected := `duplicate argument "` + arg + `" for "c"`116 if got != expected {117 t.Errorf("Expected: %q, got: %q", expected, got)118 }119}120121// NoArgs122123func TestNoArgs(t *testing.T) {124 c := getCommand(NoArgs, false)125 output, err := executeCommand(c)126 expectSuccess(output, err, t)127}128129func TestNoArgs_WithArgs(t *testing.T) {130 c := getCommand(NoArgs, false)131 _, err := executeCommand(c, "one")132 noArgsWithArgs(err, t, "one")133}134135func TestNoArgs_WithValid_WithArgs(t *testing.T) {136 c := getCommand(NoArgs, true)137 _, err := executeCommand(c, "one")138 noArgsWithArgs(err, t, "one")139}140141func TestNoArgs_WithValid_WithInvalidArgs(t *testing.T) {142 c := getCommand(NoArgs, true)143 _, err := executeCommand(c, "a")144 noArgsWithArgs(err, t, "a")145}146147func TestNoArgs_WithValidOnly_WithInvalidArgs(t *testing.T) {148 c := getCommand(MatchAll(OnlyValidArgs, NoArgs), true)149 _, err := executeCommand(c, "a")150 validOnlyWithInvalidArgs(err, t)151}152153// OnlyValidArgs154155func TestOnlyValidArgs(t *testing.T) {156 c := getCommand(OnlyValidArgs, true)157 output, err := executeCommand(c, "one", "two")158 expectSuccess(output, err, t)159}160161func TestOnlyValidArgs_WithInvalidArgs(t *testing.T) {162 c := getCommand(OnlyValidArgs, true)163 _, err := executeCommand(c, "a")164 validOnlyWithInvalidArgs(err, t)165}166167// NoDuplicateArgs168169func TestNoDuplicateArgs(t *testing.T) {170 c := getCommand(NoDuplicateArgs, false)171 output, err := executeCommand(c, "one", "two")172 expectSuccess(output, err, t)173}174175func TestNoDuplicateArgs_WithDuplicateArgs(t *testing.T) {176 c := getCommand(NoDuplicateArgs, false)177 _, err := executeCommand(c, "one", "one")178 noDuplicateArgsWithDuplicate(err, t, "one")179}180181func TestNoDuplicateArgs_WithValid_WithDuplicateArgs(t *testing.T) {182 c := getCommand(NoDuplicateArgs, true)183 _, err := executeCommand(c, "one", "one")184 noDuplicateArgsWithDuplicate(err, t, "one")185}186187func TestNoDuplicateArgs_WithValidOnly_WithInvalidArgs(t *testing.T) {188 c := getCommand(MatchAll(OnlyValidArgs, NoDuplicateArgs), true)189 _, err := executeCommand(c, "a", "a")190 validOnlyWithInvalidArgs(err, t)191}192193// ArbitraryArgs194195func TestArbitraryArgs(t *testing.T) {196 c := getCommand(ArbitraryArgs, false)197 output, err := executeCommand(c, "a", "b")198 expectSuccess(output, err, t)199}200201func TestArbitraryArgs_WithValid(t *testing.T) {202 c := getCommand(ArbitraryArgs, true)203 output, err := executeCommand(c, "one", "two")204 expectSuccess(output, err, t)205}206207func TestArbitraryArgs_WithValid_WithInvalidArgs(t *testing.T) {208 c := getCommand(ArbitraryArgs, true)209 output, err := executeCommand(c, "a")210 expectSuccess(output, err, t)211}212213func TestArbitraryArgs_WithValidOnly_WithInvalidArgs(t *testing.T) {214 c := getCommand(MatchAll(OnlyValidArgs, ArbitraryArgs), true)215 _, err := executeCommand(c, "a")216 validOnlyWithInvalidArgs(err, t)217}218219// MinimumNArgs220221func TestMinimumNArgs(t *testing.T) {222 c := getCommand(MinimumNArgs(2), false)223 output, err := executeCommand(c, "a", "b", "c")224 expectSuccess(output, err, t)225}226227func TestMinimumNArgs_WithValid(t *testing.T) {228 c := getCommand(MinimumNArgs(2), true)229 output, err := executeCommand(c, "one", "three")230 expectSuccess(output, err, t)231}232233func TestMinimumNArgs_WithValid__WithInvalidArgs(t *testing.T) {234 c := getCommand(MinimumNArgs(2), true)235 output, err := executeCommand(c, "a", "b")236 expectSuccess(output, err, t)237}238239func TestMinimumNArgs_WithValidOnly_WithInvalidArgs(t *testing.T) {240 c := getCommand(MatchAll(OnlyValidArgs, MinimumNArgs(2)), true)241 _, err := executeCommand(c, "a", "b")242 validOnlyWithInvalidArgs(err, t)243}244245func TestMinimumNArgs_WithLessArgs(t *testing.T) {246 c := getCommand(MinimumNArgs(2), false)247 _, err := executeCommand(c, "a")248 minimumNArgsWithLessArgs(err, t)249}250251func TestMinimumNArgs_WithLessArgs_WithValid(t *testing.T) {252 c := getCommand(MinimumNArgs(2), true)253 _, err := executeCommand(c, "one")254 minimumNArgsWithLessArgs(err, t)255}256257func TestMinimumNArgs_WithLessArgs_WithValid_WithInvalidArgs(t *testing.T) {258 c := getCommand(MinimumNArgs(2), true)259 _, err := executeCommand(c, "a")260 minimumNArgsWithLessArgs(err, t)261}262263func TestMinimumNArgs_WithLessArgs_WithValidOnly_WithInvalidArgs(t *testing.T) {264 c := getCommand(MatchAll(OnlyValidArgs, MinimumNArgs(2)), true)265 _, err := executeCommand(c, "a")266 validOnlyWithInvalidArgs(err, t)267}268269// MaximumNArgs270271func TestMaximumNArgs(t *testing.T) {272 c := getCommand(MaximumNArgs(3), false)273 output, err := executeCommand(c, "a", "b")274 expectSuccess(output, err, t)275}276277func TestMaximumNArgs_WithValid(t *testing.T) {278 c := getCommand(MaximumNArgs(2), true)279 output, err := executeCommand(c, "one", "three")280 expectSuccess(output, err, t)281}282283func TestMaximumNArgs_WithValid_WithInvalidArgs(t *testing.T) {284 c := getCommand(MaximumNArgs(2), true)285 output, err := executeCommand(c, "a", "b")286 expectSuccess(output, err, t)287}288289func TestMaximumNArgs_WithValidOnly_WithInvalidArgs(t *testing.T) {290 c := getCommand(MatchAll(OnlyValidArgs, MaximumNArgs(2)), true)291 _, err := executeCommand(c, "a", "b")292 validOnlyWithInvalidArgs(err, t)293}294295func TestMaximumNArgs_WithMoreArgs(t *testing.T) {296 c := getCommand(MaximumNArgs(2), false)297 _, err := executeCommand(c, "a", "b", "c")298 maximumNArgsWithMoreArgs(err, t)299}300301func TestMaximumNArgs_WithMoreArgs_WithValid(t *testing.T) {302 c := getCommand(MaximumNArgs(2), true)303 _, err := executeCommand(c, "one", "three", "two")304 maximumNArgsWithMoreArgs(err, t)305}306307func TestMaximumNArgs_WithMoreArgs_WithValid_WithInvalidArgs(t *testing.T) {308 c := getCommand(MaximumNArgs(2), true)309 _, err := executeCommand(c, "a", "b", "c")310 maximumNArgsWithMoreArgs(err, t)311}312313func TestMaximumNArgs_WithMoreArgs_WithValidOnly_WithInvalidArgs(t *testing.T) {314 c := getCommand(MatchAll(OnlyValidArgs, MaximumNArgs(2)), true)315 _, err := executeCommand(c, "a", "b", "c")316 validOnlyWithInvalidArgs(err, t)317}318319// ExactArgs320321func TestExactArgs(t *testing.T) {322 c := getCommand(ExactArgs(3), false)323 output, err := executeCommand(c, "a", "b", "c")324 expectSuccess(output, err, t)325}326327func TestExactArgs_WithValid(t *testing.T) {328 c := getCommand(ExactArgs(3), true)329 output, err := executeCommand(c, "three", "one", "two")330 expectSuccess(output, err, t)331}332333func TestExactArgs_WithValid_WithInvalidArgs(t *testing.T) {334 c := getCommand(ExactArgs(3), true)335 output, err := executeCommand(c, "three", "a", "two")336 expectSuccess(output, err, t)337}338339func TestExactArgs_WithValidOnly_WithInvalidArgs(t *testing.T) {340 c := getCommand(MatchAll(OnlyValidArgs, ExactArgs(3)), true)341 _, err := executeCommand(c, "three", "a", "two")342 validOnlyWithInvalidArgs(err, t)343}344345func TestExactArgs_WithInvalidCount(t *testing.T) {346 c := getCommand(ExactArgs(2), false)347 _, err := executeCommand(c, "a", "b", "c")348 exactArgsWithInvalidCount(err, t)349}350351func TestExactArgs_WithInvalidCount_WithValid(t *testing.T) {352 c := getCommand(ExactArgs(2), true)353 _, err := executeCommand(c, "three", "one", "two")354 exactArgsWithInvalidCount(err, t)355}356357func TestExactArgs_WithInvalidCount_WithValid_WithInvalidArgs(t *testing.T) {358 c := getCommand(ExactArgs(2), true)359 _, err := executeCommand(c, "three", "a", "two")360 exactArgsWithInvalidCount(err, t)361}362363func TestExactArgs_WithInvalidCount_WithValidOnly_WithInvalidArgs(t *testing.T) {364 c := getCommand(MatchAll(OnlyValidArgs, ExactArgs(2)), true)365 _, err := executeCommand(c, "three", "a", "two")366 validOnlyWithInvalidArgs(err, t)367}368369// RangeArgs370371func TestRangeArgs(t *testing.T) {372 c := getCommand(RangeArgs(2, 4), false)373 output, err := executeCommand(c, "a", "b", "c")374 expectSuccess(output, err, t)375}376377func TestRangeArgs_WithValid(t *testing.T) {378 c := getCommand(RangeArgs(2, 4), true)379 output, err := executeCommand(c, "three", "one", "two")380 expectSuccess(output, err, t)381}382383func TestRangeArgs_WithValid_WithInvalidArgs(t *testing.T) {384 c := getCommand(RangeArgs(2, 4), true)385 output, err := executeCommand(c, "three", "a", "two")386 expectSuccess(output, err, t)387}388389func TestRangeArgs_WithValidOnly_WithInvalidArgs(t *testing.T) {390 c := getCommand(MatchAll(OnlyValidArgs, RangeArgs(2, 4)), true)391 _, err := executeCommand(c, "three", "a", "two")392 validOnlyWithInvalidArgs(err, t)393}394395func TestRangeArgs_WithInvalidCount(t *testing.T) {396 c := getCommand(RangeArgs(2, 4), false)397 _, err := executeCommand(c, "a")398 rangeArgsWithInvalidCount(err, t)399}400401func TestRangeArgs_WithInvalidCount_WithValid(t *testing.T) {402 c := getCommand(RangeArgs(2, 4), true)403 _, err := executeCommand(c, "two")404 rangeArgsWithInvalidCount(err, t)405}406407func TestRangeArgs_WithInvalidCount_WithValid_WithInvalidArgs(t *testing.T) {408 c := getCommand(RangeArgs(2, 4), true)409 _, err := executeCommand(c, "a")410 rangeArgsWithInvalidCount(err, t)411}412413func TestRangeArgs_WithInvalidCount_WithValidOnly_WithInvalidArgs(t *testing.T) {414 c := getCommand(MatchAll(OnlyValidArgs, RangeArgs(2, 4)), true)415 _, err := executeCommand(c, "a")416 validOnlyWithInvalidArgs(err, t)417}418419// Takes(No)Args420421func TestRootTakesNoArgs(t *testing.T) {422 rootCmd := &Command{Use: "root", Run: emptyRun}423 childCmd := &Command{Use: "child", Run: emptyRun}424 rootCmd.AddCommand(childCmd)425426 _, err := executeCommand(rootCmd, "illegal", "args")427 if err == nil {428 t.Fatal("Expected an error")429 }430431 got := err.Error()432 expected := `unknown command "illegal" for "root"`433 if !strings.Contains(got, expected) {434 t.Errorf("expected %q, got %q", expected, got)435 }436}437438func TestRootTakesArgs(t *testing.T) {439 rootCmd := &Command{Use: "root", Args: ArbitraryArgs, Run: emptyRun}440 childCmd := &Command{Use: "child", Run: emptyRun}441 rootCmd.AddCommand(childCmd)442443 _, err := executeCommand(rootCmd, "legal", "args")444 if err != nil {445 t.Errorf("Unexpected error: %v", err)446 }447}448449func TestChildTakesNoArgs(t *testing.T) {450 rootCmd := &Command{Use: "root", Run: emptyRun}451 childCmd := &Command{Use: "child", Args: NoArgs, Run: emptyRun}452 rootCmd.AddCommand(childCmd)453454 _, err := executeCommand(rootCmd, "child", "illegal", "args")455 if err == nil {456 t.Fatal("Expected an error")457 }458459 got := err.Error()460 expected := `unknown command "illegal" for "root child"`461 if !strings.Contains(got, expected) {462 t.Errorf("expected %q, got %q", expected, got)463 }464}465466func TestChildTakesArgs(t *testing.T) {467 rootCmd := &Command{Use: "root", Run: emptyRun}468 childCmd := &Command{Use: "child", Args: ArbitraryArgs, Run: emptyRun}469 rootCmd.AddCommand(childCmd)470471 _, err := executeCommand(rootCmd, "child", "legal", "args")472 if err != nil {473 t.Fatalf("Unexpected error: %v", err)474 }475}476477func TestMatchAll(t *testing.T) {478 // Somewhat contrived example check that ensures there are exactly 3479 // arguments, and each argument is exactly 2 bytes long.480 pargs := MatchAll(481 ExactArgs(3),482 func(cmd *Command, args []string) error {483 for _, arg := range args {484 if len([]byte(arg)) != 2 {485 return fmt.Errorf("expected to be exactly 2 bytes long")486 }487 }488 return nil489 },490 )491492 testCases := map[string]struct {493 args []string494 fail bool495 }{496 "happy path": {497 []string{"aa", "bb", "cc"},498 false,499 },500 "incorrect number of args": {501 []string{"aa", "bb", "cc", "dd"},502 true,503 },504 "incorrect number of bytes in one arg": {505 []string{"aa", "bb", "abc"},506 true,507 },508 }509510 rootCmd := &Command{Use: "root", Args: pargs, Run: emptyRun}511512 for name, tc := range testCases {513 t.Run(name, func(t *testing.T) {514 _, err := executeCommand(rootCmd, tc.args...)515 if err != nil && !tc.fail {516 t.Errorf("unexpected: %v\n", err)517 }518 if err == nil && tc.fail {519 t.Errorf("expected error")520 }521 })522 }523}524525// DEPRECATED526527func TestExactValidArgs(t *testing.T) {528 c := getCommand(ExactValidArgs(3), true)529 output, err := executeCommand(c, "three", "one", "two")530 expectSuccess(output, err, t)531}532533func TestExactValidArgs_WithInvalidCount(t *testing.T) {534 c := getCommand(ExactValidArgs(2), false)535 _, err := executeCommand(c, "three", "one", "two")536 exactArgsWithInvalidCount(err, t)537}538539func TestExactValidArgs_WithInvalidCount_WithInvalidArgs(t *testing.T) {540 c := getCommand(ExactValidArgs(2), true)541 _, err := executeCommand(c, "three", "a", "two")542 exactArgsWithInvalidCount(err, t)543}544545func TestExactValidArgs_WithInvalidArgs(t *testing.T) {546 c := getCommand(ExactValidArgs(2), true)547 _, err := executeCommand(c, "three", "a")548 validOnlyWithInvalidArgs(err, t)549}550551// This test make sure we keep backwards-compatibility with respect552// to the legacyArgs() function.553// It makes sure the root command accepts arguments if it does not have554// sub-commands.555func TestLegacyArgsRootAcceptsArgs(t *testing.T) {556 rootCmd := &Command{Use: "root", Args: nil, Run: emptyRun}557558 _, err := executeCommand(rootCmd, "somearg")559 if err != nil {560 t.Fatalf("Unexpected error: %v", err)561 }562}563564// This test make sure we keep backwards-compatibility with respect565// to the legacyArgs() function.566// It makes sure a sub-command accepts arguments and further sub-commands567func TestLegacyArgsSubcmdAcceptsArgs(t *testing.T) {568 rootCmd := &Command{Use: "root", Args: nil, Run: emptyRun}569 childCmd := &Command{Use: "child", Args: nil, Run: emptyRun}570 grandchildCmd := &Command{Use: "grandchild", Args: nil, Run: emptyRun}571 rootCmd.AddCommand(childCmd)572 childCmd.AddCommand(grandchildCmd)573574 _, err := executeCommand(rootCmd, "child", "somearg")575 if err != nil {576 t.Fatalf("Unexpected error: %v", err)577 }578}