Skip to content
Myr

args_test.go

This file

Path

args_test.go

323 lines · 8.0 KiB · Go

Last changed by

Add backwards-compatibility tests for legacyArgs() (#1547)Marc Khouzambefore adoption94e552d

History of this file

Take it

The walk stopped after 200 commits: lines older than 0ea93dd point at it.

Fixed linter issuesJuan Lenibefore adoption0ea93dd

lines 1–3why, one step back
1package cobra
23import (

enable composing PositionalArgs (#896)Nelzbefore adoption45e521e

line 4why, one step back
4 "fmt"

Fixed linter issuesJuan Lenibefore adoption0ea93dd

lines 5–8why, one step back
5 "strings"
6 "testing"7)8

args_test: add helper functions (#1426)Unai Martinez-Corralbefore adoption9e1d6f1

lines 9–19why, one step back
9func getCommand(args PositionalArgs, withValid bool) *Command {
10 c := &Command{11 Use: "c",12 Args: args,13 Run: emptyRun,14 }15 if withValid {16 c.ValidArgs = []string{"one", "two", "three"}17 }18 return c19}

Fixed linter issuesJuan Lenibefore adoption0ea93dd

line 20why, one step back
20

args_test: add helper functions (#1426)Unai Martinez-Corralbefore adoption9e1d6f1

line 21why, one step back
21func expectSuccess(output string, err error, t *testing.T) {

Fixed linter issuesJuan Lenibefore adoption0ea93dd

line 22why, one step back
22 if output != "" {

args_test: add helper functions (#1426)Unai Martinez-Corralbefore adoption9e1d6f1

line 23why, one step back
23 t.Errorf("Unexpected output: %v", output)

Fixed linter issuesJuan Lenibefore adoption0ea93dd

lines 24–29why, one step back
24 }
25 if err != nil {26 t.Fatalf("Unexpected error: %v", err)27 }28}29

args_test: add helper functions (#1426)Unai Martinez-Corralbefore adoption9e1d6f1

line 30why, one step back
30func validWithInvalidArgs(err error, t *testing.T) {

Fixed linter issuesJuan Lenibefore adoption0ea93dd

lines 31–33why, one step back
31 if err == nil {
32 t.Fatal("Expected an error")33 }

args_test: add helper functions (#1426)Unai Martinez-Corralbefore adoption9e1d6f1

lines 34–39why, one step back
34 got := err.Error()
35 expected := `invalid argument "a" for "c"`36 if got != expected {37 t.Errorf("Expected: %q, got: %q", expected, got)38 }39}

Fixed linter issuesJuan Lenibefore adoption0ea93dd

line 40why, one step back
40

args_test: add helper functions (#1426)Unai Martinez-Corralbefore adoption9e1d6f1

lines 41–44why, one step back
41func noArgsWithArgs(err error, t *testing.T) {
42 if err == nil {43 t.Fatal("Expected an error")44 }

Fixed linter issuesJuan Lenibefore adoption0ea93dd

lines 45–51why, one step back
45 got := err.Error()
46 expected := `unknown command "illegal" for "c"`47 if got != expected {48 t.Errorf("Expected: %q, got: %q", expected, got)49 }50}51

args_test: add helper functions (#1426)Unai Martinez-Corralbefore adoption9e1d6f1

lines 52–54why, one step back
52func minimumNArgsWithLessArgs(err error, t *testing.T) {
53 if err == nil {54 t.Fatal("Expected an error")

Fixed linter issuesJuan Lenibefore adoption0ea93dd

line 55why, one step back
55 }

args_test: add helper functions (#1426)Unai Martinez-Corralbefore adoption9e1d6f1

lines 56–61why, one step back
56 got := err.Error()
57 expected := "requires at least 2 arg(s), only received 1"58 if got != expected {59 t.Fatalf("Expected %q, got %q", expected, got)60 }61}

Fixed linter issuesJuan Lenibefore adoption0ea93dd

line 62why, one step back
62

args_test: add helper functions (#1426)Unai Martinez-Corralbefore adoption9e1d6f1

lines 63–65why, one step back
63func maximumNArgsWithMoreArgs(err error, t *testing.T) {
64 if err == nil {65 t.Fatal("Expected an error")

Fixed linter issuesJuan Lenibefore adoption0ea93dd

line 66why, one step back
66 }

args_test: add helper functions (#1426)Unai Martinez-Corralbefore adoption9e1d6f1

lines 67–70why, one step back
67 got := err.Error()
68 expected := "accepts at most 2 arg(s), received 3"69 if got != expected {70 t.Fatalf("Expected %q, got %q", expected, got)

Fixed linter issuesJuan Lenibefore adoption0ea93dd

lines 71–73why, one step back
71 }
72}73

args_test: add helper functions (#1426)Unai Martinez-Corralbefore adoption9e1d6f1

lines 74–76why, one step back
74func exactArgsWithInvalidCount(err error, t *testing.T) {
75 if err == nil {76 t.Fatal("Expected an error")

Fixed linter issuesJuan Lenibefore adoption0ea93dd

line 77why, one step back
77 }

args_test: add helper functions (#1426)Unai Martinez-Corralbefore adoption9e1d6f1

lines 78–83why, one step back
78 got := err.Error()
79 expected := "accepts 2 arg(s), received 3"80 if got != expected {81 t.Fatalf("Expected %q, got %q", expected, got)82 }83}

Fixed linter issuesJuan Lenibefore adoption0ea93dd

line 84why, one step back
84

args_test: add helper functions (#1426)Unai Martinez-Corralbefore adoption9e1d6f1

line 85why, one step back
85func rangeArgsWithInvalidCount(err error, t *testing.T) {

Fixed linter issuesJuan Lenibefore adoption0ea93dd

lines 86–89why, one step back
86 if err == nil {
87 t.Fatal("Expected an error")88 }89 got := err.Error()

args_test: add helper functions (#1426)Unai Martinez-Corralbefore adoption9e1d6f1

line 90why, one step back
90 expected := "accepts between 2 and 4 arg(s), received 1"

Fixed linter issuesJuan Lenibefore adoption0ea93dd

line 91why, one step back
91 if got != expected {

args_test: add helper functions (#1426)Unai Martinez-Corralbefore adoption9e1d6f1

line 92why, one step back
92 t.Fatalf("Expected %q, got %q", expected, got)

Fixed linter issuesJuan Lenibefore adoption0ea93dd

lines 93–95why, one step back
93 }
94}95

args_test: add helper functions (#1426)Unai Martinez-Corralbefore adoption9e1d6f1

lines 96–119why, one step back
96func TestNoArgs(t *testing.T) {
97 c := getCommand(NoArgs, false)98 output, err := executeCommand(c)99 expectSuccess(output, err, t)100}101102func TestNoArgsWithArgs(t *testing.T) {103 c := getCommand(NoArgs, false)104 _, err := executeCommand(c, "illegal")105 noArgsWithArgs(err, t)106}107108func TestOnlyValidArgs(t *testing.T) {109 c := getCommand(OnlyValidArgs, true)110 output, err := executeCommand(c, "one", "two")111 expectSuccess(output, err, t)112}113114func TestOnlyValidArgsWithInvalidArgs(t *testing.T) {115 c := getCommand(OnlyValidArgs, true)116 _, err := executeCommand(c, "a")117 validWithInvalidArgs(err, t)118}119

Fixed linter issuesJuan Lenibefore adoption0ea93dd

line 120why, one step back
120func TestArbitraryArgs(t *testing.T) {

args_test: add helper functions (#1426)Unai Martinez-Corralbefore adoption9e1d6f1

line 121why, one step back
121 c := getCommand(ArbitraryArgs, false)

Fixed linter issuesJuan Lenibefore adoption0ea93dd

line 122why, one step back
122 output, err := executeCommand(c, "a", "b")

args_test: add helper functions (#1426)Unai Martinez-Corralbefore adoption9e1d6f1

line 123why, one step back
123 expectSuccess(output, err, t)

Fixed linter issuesJuan Lenibefore adoption0ea93dd

lines 124–126why, one step back
124}
125126func TestMinimumNArgs(t *testing.T) {

args_test: add helper functions (#1426)Unai Martinez-Corralbefore adoption9e1d6f1

line 127why, one step back
127 c := getCommand(MinimumNArgs(2), false)

Fixed linter issuesJuan Lenibefore adoption0ea93dd

line 128why, one step back
128 output, err := executeCommand(c, "a", "b", "c")

args_test: add helper functions (#1426)Unai Martinez-Corralbefore adoption9e1d6f1

line 129why, one step back
129 expectSuccess(output, err, t)

Fixed linter issuesJuan Lenibefore adoption0ea93dd

lines 130–132why, one step back
130}
131132func TestMinimumNArgsWithLessArgs(t *testing.T) {

args_test: add helper functions (#1426)Unai Martinez-Corralbefore adoption9e1d6f1

line 133why, one step back
133 c := getCommand(MinimumNArgs(2), false)

Fixed linter issuesJuan Lenibefore adoption0ea93dd

line 134why, one step back
134 _, err := executeCommand(c, "a")

args_test: add helper functions (#1426)Unai Martinez-Corralbefore adoption9e1d6f1

line 135why, one step back
135 minimumNArgsWithLessArgs(err, t)

Fixed linter issuesJuan Lenibefore adoption0ea93dd

lines 136–138why, one step back
136}
137138func TestMaximumNArgs(t *testing.T) {

args_test: add helper functions (#1426)Unai Martinez-Corralbefore adoption9e1d6f1

line 139why, one step back
139 c := getCommand(MaximumNArgs(3), false)

Fixed linter issuesJuan Lenibefore adoption0ea93dd

line 140why, one step back
140 output, err := executeCommand(c, "a", "b")

args_test: add helper functions (#1426)Unai Martinez-Corralbefore adoption9e1d6f1

line 141why, one step back
141 expectSuccess(output, err, t)

Fixed linter issuesJuan Lenibefore adoption0ea93dd

lines 142–144why, one step back
142}
143144func TestMaximumNArgsWithMoreArgs(t *testing.T) {

args_test: add helper functions (#1426)Unai Martinez-Corralbefore adoption9e1d6f1

line 145why, one step back
145 c := getCommand(MaximumNArgs(2), false)

Fixed linter issuesJuan Lenibefore adoption0ea93dd

line 146why, one step back
146 _, err := executeCommand(c, "a", "b", "c")

args_test: add helper functions (#1426)Unai Martinez-Corralbefore adoption9e1d6f1

line 147why, one step back
147 maximumNArgsWithMoreArgs(err, t)

Fixed linter issuesJuan Lenibefore adoption0ea93dd

lines 148–150why, one step back
148}
149150func TestExactArgs(t *testing.T) {

args_test: add helper functions (#1426)Unai Martinez-Corralbefore adoption9e1d6f1

line 151why, one step back
151 c := getCommand(ExactArgs(3), false)

Fixed linter issuesJuan Lenibefore adoption0ea93dd

line 152why, one step back
152 output, err := executeCommand(c, "a", "b", "c")

args_test: add helper functions (#1426)Unai Martinez-Corralbefore adoption9e1d6f1

line 153why, one step back
153 expectSuccess(output, err, t)

Fixed linter issuesJuan Lenibefore adoption0ea93dd

lines 154–156why, one step back
154}
155156func TestExactArgsWithInvalidCount(t *testing.T) {

args_test: add helper functions (#1426)Unai Martinez-Corralbefore adoption9e1d6f1

line 157why, one step back
157 c := getCommand(ExactArgs(2), false)

Fixed linter issuesJuan Lenibefore adoption0ea93dd

line 158why, one step back
158 _, err := executeCommand(c, "a", "b", "c")

args_test: add helper functions (#1426)Unai Martinez-Corralbefore adoption9e1d6f1

line 159why, one step back
159 exactArgsWithInvalidCount(err, t)

Fixed linter issuesJuan Lenibefore adoption0ea93dd

lines 160–162why, one step back
160}
161162func TestExactValidArgs(t *testing.T) {

args_test: add helper functions (#1426)Unai Martinez-Corralbefore adoption9e1d6f1

lines 163–165why, one step back
163 c := getCommand(ExactValidArgs(3), true)
164 output, err := executeCommand(c, "three", "one", "two")165 expectSuccess(output, err, t)

Fixed linter issuesJuan Lenibefore adoption0ea93dd

lines 166–168why, one step back
166}
167168func TestExactValidArgsWithInvalidCount(t *testing.T) {

args_test: add helper functions (#1426)Unai Martinez-Corralbefore adoption9e1d6f1

lines 169–171why, one step back
169 c := getCommand(ExactValidArgs(2), false)
170 _, err := executeCommand(c, "three", "one", "two")171 exactArgsWithInvalidCount(err, t)

Fixed linter issuesJuan Lenibefore adoption0ea93dd

lines 172–174why, one step back
172}
173174func TestExactValidArgsWithInvalidArgs(t *testing.T) {

args_test: add helper functions (#1426)Unai Martinez-Corralbefore adoption9e1d6f1

lines 175–177why, one step back
175 c := getCommand(ExactValidArgs(3), true)
176 _, err := executeCommand(c, "three", "a", "two")177 validWithInvalidArgs(err, t)

Fixed linter issuesJuan Lenibefore adoption0ea93dd

lines 178–180why, one step back
178}
179180func TestRangeArgs(t *testing.T) {

args_test: add helper functions (#1426)Unai Martinez-Corralbefore adoption9e1d6f1

line 181why, one step back
181 c := getCommand(RangeArgs(2, 4), false)

Fixed linter issuesJuan Lenibefore adoption0ea93dd

line 182why, one step back
182 output, err := executeCommand(c, "a", "b", "c")

args_test: add helper functions (#1426)Unai Martinez-Corralbefore adoption9e1d6f1

line 183why, one step back
183 expectSuccess(output, err, t)

Fixed linter issuesJuan Lenibefore adoption0ea93dd

lines 184–186why, one step back
184}
185186func TestRangeArgsWithInvalidCount(t *testing.T) {

args_test: add helper functions (#1426)Unai Martinez-Corralbefore adoption9e1d6f1

line 187why, one step back
187 c := getCommand(RangeArgs(2, 4), false)

Fixed linter issuesJuan Lenibefore adoption0ea93dd

line 188why, one step back
188 _, err := executeCommand(c, "a")

args_test: add helper functions (#1426)Unai Martinez-Corralbefore adoption9e1d6f1

line 189why, one step back
189 rangeArgsWithInvalidCount(err, t)

Fixed linter issuesJuan Lenibefore adoption0ea93dd

lines 190–244why, one step back
190}
191192func TestRootTakesNoArgs(t *testing.T) {193 rootCmd := &Command{Use: "root", Run: emptyRun}194 childCmd := &Command{Use: "child", Run: emptyRun}195 rootCmd.AddCommand(childCmd)196197 _, err := executeCommand(rootCmd, "illegal", "args")198 if err == nil {199 t.Fatal("Expected an error")200 }201202 got := err.Error()203 expected := `unknown command "illegal" for "root"`204 if !strings.Contains(got, expected) {205 t.Errorf("expected %q, got %q", expected, got)206 }207}208209func TestRootTakesArgs(t *testing.T) {210 rootCmd := &Command{Use: "root", Args: ArbitraryArgs, Run: emptyRun}211 childCmd := &Command{Use: "child", Run: emptyRun}212 rootCmd.AddCommand(childCmd)213214 _, err := executeCommand(rootCmd, "legal", "args")215 if err != nil {216 t.Errorf("Unexpected error: %v", err)217 }218}219220func TestChildTakesNoArgs(t *testing.T) {221 rootCmd := &Command{Use: "root", Run: emptyRun}222 childCmd := &Command{Use: "child", Args: NoArgs, Run: emptyRun}223 rootCmd.AddCommand(childCmd)224225 _, err := executeCommand(rootCmd, "child", "illegal", "args")226 if err == nil {227 t.Fatal("Expected an error")228 }229230 got := err.Error()231 expected := `unknown command "illegal" for "root child"`232 if !strings.Contains(got, expected) {233 t.Errorf("expected %q, got %q", expected, got)234 }235}236237func TestChildTakesArgs(t *testing.T) {238 rootCmd := &Command{Use: "root", Run: emptyRun}239 childCmd := &Command{Use: "child", Args: ArbitraryArgs, Run: emptyRun}240 rootCmd.AddCommand(childCmd)241242 _, err := executeCommand(rootCmd, "child", "legal", "args")243 if err != nil {244 t.Fatalf("Unexpected error: %v", err)

enable composing PositionalArgs (#896)Nelzbefore adoption45e521e

lines 245–292why, one step back
245 }
246}247248func TestMatchAll(t *testing.T) {249 // Somewhat contrived example check that ensures there are exactly 3250 // arguments, and each argument is exactly 2 bytes long.251 pargs := MatchAll(252 ExactArgs(3),253 func(cmd *Command, args []string) error {254 for _, arg := range args {255 if len([]byte(arg)) != 2 {256 return fmt.Errorf("expected to be exactly 2 bytes long")257 }258 }259 return nil260 },261 )262263 testCases := map[string]struct {264 args []string265 fail bool266 }{267 "happy path": {268 []string{"aa", "bb", "cc"},269 false,270 },271 "incorrect number of args": {272 []string{"aa", "bb", "cc", "dd"},273 true,274 },275 "incorrect number of bytes in one arg": {276 []string{"aa", "bb", "abc"},277 true,278 },279 }280281 rootCmd := &Command{Use: "root", Args: pargs, Run: emptyRun}282283 for name, tc := range testCases {284 t.Run(name, func(t *testing.T) {285 _, err := executeCommand(rootCmd, tc.args...)286 if err != nil && !tc.fail {287 t.Errorf("unexpected: %v\n", err)288 }289 if err == nil && tc.fail {290 t.Errorf("expected error")291 }292 })

Fixed linter issuesJuan Lenibefore adoption0ea93dd

lines 293–294why, one step back
293 }
294}

Add backwards-compatibility tests for legacyArgs() (#1547)Marc Khouzambefore adoption94e552d

lines 295–323why, one step back
295
296// This test make sure we keep backwards-compatibility with respect297// to the legacyArgs() function.298// It makes sure the root command accepts arguments if it does not have299// sub-commands.300func TestLegacyArgsRootAcceptsArgs(t *testing.T) {301 rootCmd := &Command{Use: "root", Args: nil, Run: emptyRun}302303 _, err := executeCommand(rootCmd, "somearg")304 if err != nil {305 t.Fatalf("Unexpected error: %v", err)306 }307}308309// This test make sure we keep backwards-compatibility with respect310// to the legacyArgs() function.311// It makes sure a sub-command accepts arguments and further sub-commands312func TestLegacyArgsSubcmdAcceptsArgs(t *testing.T) {313 rootCmd := &Command{Use: "root", Args: nil, Run: emptyRun}314 childCmd := &Command{Use: "child", Args: nil, Run: emptyRun}315 grandchildCmd := &Command{Use: "grandchild", Args: nil, Run: emptyRun}316 rootCmd.AddCommand(childCmd)317 childCmd.AddCommand(grandchildCmd)318319 _, err := executeCommand(rootCmd, "child", "somearg")320 if err != nil {321 t.Fatalf("Unexpected error: %v", err)322 }323}