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
The walk stopped after 200 commits: lines older than 89d7b83 point at it.
update copyright year (#1927)Unai Martinez-Corralbefore adoption9e6b58a
line 1why, one step back1// Copyright 2013-2023 The Cobra Authorsadd missing license headers (#1809)Unai Martinez-Corralbefore adoption6d978a9
lines 2–14why, one step back2//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.14Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83
lines 15–43why, one step back15package 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}43Deprecate ExactValidArgs() and test combinations of args validators (#1643)Unai Martinez-Corralbefore adoption70e53f6
line 44why, one step back44func validOnlyWithInvalidArgs(err error, t *testing.T) {Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83
lines 45–54why, one step back45 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}54Deprecate ExactValidArgs() and test combinations of args validators (#1643)Unai Martinez-Corralbefore adoption70e53f6
line 55why, one step back55func noArgsWithArgs(err error, t *testing.T, arg string) {Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83
lines 56–59why, one step back56 if err == nil {57 t.Fatal("Expected an error")58 }59 got := err.Error()Deprecate ExactValidArgs() and test combinations of args validators (#1643)Unai Martinez-Corralbefore adoption70e53f6
line 60why, one step back60 expected := `unknown command "` + arg + `" for "c"`Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83
lines 61–109why, one step back61 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}109Add cobra unique args validator (#2397)takanuva15before adoptionad460ea
lines 110–120why, one step back110func 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}120Deprecate ExactValidArgs() and test combinations of args validators (#1643)Unai Martinez-Corralbefore adoption70e53f6
lines 121–122why, one step back121// NoArgs122Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83
lines 123–128why, one step back123func TestNoArgs(t *testing.T) {124 c := getCommand(NoArgs, false)125 output, err := executeCommand(c)126 expectSuccess(output, err, t)127}128Deprecate ExactValidArgs() and test combinations of args validators (#1643)Unai Martinez-Corralbefore adoption70e53f6
line 129why, one step back129func TestNoArgs_WithArgs(t *testing.T) {Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83
line 130why, one step back130 c := getCommand(NoArgs, false)Deprecate ExactValidArgs() and test combinations of args validators (#1643)Unai Martinez-Corralbefore adoption70e53f6
lines 131–132why, one step back131 _, err := executeCommand(c, "one")132 noArgsWithArgs(err, t, "one")Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83
lines 133–134why, one step back133}134Deprecate ExactValidArgs() and test combinations of args validators (#1643)Unai Martinez-Corralbefore adoption70e53f6
lines 135–154why, one step back135func 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// OnlyValidArgs154Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83
lines 155–160why, one step back155func TestOnlyValidArgs(t *testing.T) {156 c := getCommand(OnlyValidArgs, true)157 output, err := executeCommand(c, "one", "two")158 expectSuccess(output, err, t)159}160Deprecate ExactValidArgs() and test combinations of args validators (#1643)Unai Martinez-Corralbefore adoption70e53f6
line 161why, one step back161func TestOnlyValidArgs_WithInvalidArgs(t *testing.T) {Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83
lines 162–163why, one step back162 c := getCommand(OnlyValidArgs, true)163 _, err := executeCommand(c, "a")Add cobra unique args validator (#2397)takanuva15before adoptionad460ea
lines 164–189why, one step back164 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")Deprecate ExactValidArgs() and test combinations of args validators (#1643)Unai Martinez-Corralbefore adoption70e53f6
line 190why, one step back190 validOnlyWithInvalidArgs(err, t)Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83
lines 191–192why, one step back191}192Deprecate ExactValidArgs() and test combinations of args validators (#1643)Unai Martinez-Corralbefore adoption70e53f6
lines 193–194why, one step back193// ArbitraryArgs194Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83
lines 195–200why, one step back195func TestArbitraryArgs(t *testing.T) {196 c := getCommand(ArbitraryArgs, false)197 output, err := executeCommand(c, "a", "b")198 expectSuccess(output, err, t)199}200Deprecate ExactValidArgs() and test combinations of args validators (#1643)Unai Martinez-Corralbefore adoption70e53f6
lines 201–220why, one step back201func 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// MinimumNArgs220Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83
lines 221–226why, one step back221func TestMinimumNArgs(t *testing.T) {222 c := getCommand(MinimumNArgs(2), false)223 output, err := executeCommand(c, "a", "b", "c")224 expectSuccess(output, err, t)225}226Deprecate ExactValidArgs() and test combinations of args validators (#1643)Unai Martinez-Corralbefore adoption70e53f6
lines 227–245why, one step back227func 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) {Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83
lines 246–250why, one step back246 c := getCommand(MinimumNArgs(2), false)247 _, err := executeCommand(c, "a")248 minimumNArgsWithLessArgs(err, t)249}250Deprecate ExactValidArgs() and test combinations of args validators (#1643)Unai Martinez-Corralbefore adoption70e53f6
lines 251–270why, one step back251func 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// MaximumNArgs270Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83
lines 271–276why, one step back271func TestMaximumNArgs(t *testing.T) {272 c := getCommand(MaximumNArgs(3), false)273 output, err := executeCommand(c, "a", "b")274 expectSuccess(output, err, t)275}276Deprecate ExactValidArgs() and test combinations of args validators (#1643)Unai Martinez-Corralbefore adoption70e53f6
lines 277–295why, one step back277func 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) {Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83
lines 296–300why, one step back296 c := getCommand(MaximumNArgs(2), false)297 _, err := executeCommand(c, "a", "b", "c")298 maximumNArgsWithMoreArgs(err, t)299}300Deprecate ExactValidArgs() and test combinations of args validators (#1643)Unai Martinez-Corralbefore adoption70e53f6
lines 301–320why, one step back301func 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// ExactArgs320Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83
lines 321–326why, one step back321func TestExactArgs(t *testing.T) {322 c := getCommand(ExactArgs(3), false)323 output, err := executeCommand(c, "a", "b", "c")324 expectSuccess(output, err, t)325}326Deprecate ExactValidArgs() and test combinations of args validators (#1643)Unai Martinez-Corralbefore adoption70e53f6
lines 327–345why, one step back327func 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) {Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83
lines 346–350why, one step back346 c := getCommand(ExactArgs(2), false)347 _, err := executeCommand(c, "a", "b", "c")348 exactArgsWithInvalidCount(err, t)349}350Deprecate ExactValidArgs() and test combinations of args validators (#1643)Unai Martinez-Corralbefore adoption70e53f6
lines 351–354why, one step back351func TestExactArgs_WithInvalidCount_WithValid(t *testing.T) {352 c := getCommand(ExactArgs(2), true)353 _, err := executeCommand(c, "three", "one", "two")354 exactArgsWithInvalidCount(err, t)Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83
lines 355–356why, one step back355}356Deprecate ExactValidArgs() and test combinations of args validators (#1643)Unai Martinez-Corralbefore adoption70e53f6
lines 357–359why, one step back357func TestExactArgs_WithInvalidCount_WithValid_WithInvalidArgs(t *testing.T) {358 c := getCommand(ExactArgs(2), true)359 _, err := executeCommand(c, "three", "a", "two")Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83
lines 360–362why, one step back360 exactArgsWithInvalidCount(err, t)361}362Deprecate ExactValidArgs() and test combinations of args validators (#1643)Unai Martinez-Corralbefore adoption70e53f6
lines 363–364why, one step back363func TestExactArgs_WithInvalidCount_WithValidOnly_WithInvalidArgs(t *testing.T) {364 c := getCommand(MatchAll(OnlyValidArgs, ExactArgs(2)), true)Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83
line 365why, one step back365 _, err := executeCommand(c, "three", "a", "two")Deprecate ExactValidArgs() and test combinations of args validators (#1643)Unai Martinez-Corralbefore adoption70e53f6
line 366why, one step back366 validOnlyWithInvalidArgs(err, t)Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83
lines 367–368why, one step back367}368Deprecate ExactValidArgs() and test combinations of args validators (#1643)Unai Martinez-Corralbefore adoption70e53f6
lines 369–370why, one step back369// RangeArgs370Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83
lines 371–376why, one step back371func 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}376Deprecate ExactValidArgs() and test combinations of args validators (#1643)Unai Martinez-Corralbefore adoption70e53f6
lines 377–395why, one step back377func 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) {Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83
lines 396–400why, one step back396 c := getCommand(RangeArgs(2, 4), false)397 _, err := executeCommand(c, "a")398 rangeArgsWithInvalidCount(err, t)399}400Deprecate ExactValidArgs() and test combinations of args validators (#1643)Unai Martinez-Corralbefore adoption70e53f6
lines 401–420why, one step back401func 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)Args420Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83
lines 421–522why, one step back421func 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 }Deprecate ExactValidArgs() and test combinations of args validators (#1643)Unai Martinez-Corralbefore adoption70e53f6
lines 523–548why, one step back523}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)Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83
lines 549–578why, one step back549}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}