| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "testing" |
| 6 | "time" |
| 7 | |
| 8 | "github.com/stretchr/testify/require" |
| 9 | ) |
| 10 | |
| 11 | func TestArgNotSet(t *testing.T) { |
| 12 | arg := &StringArg{ |
| 13 | Name: "sa", |
| 14 | Value: "foo", |
| 15 | } |
| 16 | |
| 17 | require.Equal(t, "foo", arg.Get()) |
| 18 | } |
| 19 | |
| 20 | func TestArgsMaxNotSet(t *testing.T) { |
| 21 | arg := &StringArgs{ |
| 22 | Name: "sa", |
| 23 | Value: "foo", |
| 24 | } |
| 25 | |
| 26 | cmd := buildMinimalTestCommand() |
| 27 | cmd.Arguments = []Argument{arg} |
| 28 | |
| 29 | err := cmd.Run(buildTestContext(t), []string{"foo", "bar"}) |
| 30 | require.ErrorContains(t, err, "args sa has max 0, not parsing argument") |
| 31 | } |
| 32 | |
| 33 | func TestArgsMinGtMax(t *testing.T) { |
| 34 | arg := &StringArgs{ |
| 35 | Name: "sa", |
| 36 | Value: "foo", |
| 37 | Min: 2, |
| 38 | Max: 1, |
| 39 | } |
| 40 | |
| 41 | cmd := buildMinimalTestCommand() |
| 42 | cmd.Arguments = []Argument{arg} |
| 43 | |
| 44 | err := cmd.Run(buildTestContext(t), []string{"foo", "bar"}) |
| 45 | require.ErrorContains(t, err, "args sa has min[2] > max[1], not parsing argument") |
| 46 | } |
| 47 | |
| 48 | func TestArgsFloatTypes(t *testing.T) { |
| 49 | cmd := buildMinimalTestCommand() |
| 50 | var fval float64 |
| 51 | cmd.Arguments = []Argument{ |
| 52 | &FloatArg{ |
| 53 | Name: "ia", |
| 54 | Destination: &fval, |
| 55 | }, |
| 56 | } |
| 57 | |
| 58 | err := cmd.Run(buildTestContext(t), []string{"foo", "10"}) |
| 59 | r := require.New(t) |
| 60 | r.NoError(err) |
| 61 | r.Equal(float64(10), fval) |
| 62 | r.Equal(float64(10), cmd.FloatArg("ia")) |
| 63 | r.Equal(float64(10), cmd.Float64Arg("ia")) |
| 64 | r.Equal(float32(0), cmd.Float32Arg("ia")) |
| 65 | r.Equal(float64(0), cmd.FloatArg("iab")) |
| 66 | r.Equal(int8(0), cmd.Int8Arg("ia")) |
| 67 | r.Equal(int16(0), cmd.Int16Arg("ia")) |
| 68 | r.Equal(int32(0), cmd.Int32Arg("ia")) |
| 69 | r.Equal(int64(0), cmd.Int64Arg("ia")) |
| 70 | r.Empty(cmd.StringArg("ia")) |
| 71 | |
| 72 | r.Error(cmd.Run(buildTestContext(t), []string{"foo", "a"})) |
| 73 | } |
| 74 | |
| 75 | func TestArgsIntTypes(t *testing.T) { |
| 76 | cmd := buildMinimalTestCommand() |
| 77 | var ival int |
| 78 | cmd.Arguments = []Argument{ |
| 79 | &IntArg{ |
| 80 | Name: "ia", |
| 81 | Destination: &ival, |
| 82 | }, |
| 83 | } |
| 84 | |
| 85 | err := cmd.Run(buildTestContext(t), []string{"foo", "10"}) |
| 86 | r := require.New(t) |
| 87 | r.NoError(err) |
| 88 | r.Equal(10, ival) |
| 89 | r.Equal(10, cmd.IntArg("ia")) |
| 90 | r.Equal(0, cmd.IntArg("iab")) |
| 91 | r.Equal(int8(0), cmd.Int8Arg("ia")) |
| 92 | r.Equal(int16(0), cmd.Int16Arg("ia")) |
| 93 | r.Equal(int32(0), cmd.Int32Arg("ia")) |
| 94 | r.Equal(int64(0), cmd.Int64Arg("ia")) |
| 95 | r.Equal(float64(0), cmd.FloatArg("ia")) |
| 96 | r.Empty(cmd.StringArg("ia")) |
| 97 | |
| 98 | r.Error(cmd.Run(buildTestContext(t), []string{"foo", "10.0"})) |
| 99 | } |
| 100 | |
| 101 | func TestArgsFloatSliceTypes(t *testing.T) { |
| 102 | cmd := buildMinimalTestCommand() |
| 103 | var fval []float64 |
| 104 | cmd.Arguments = []Argument{ |
| 105 | &FloatArgs{ |
| 106 | Name: "ia", |
| 107 | Min: 1, |
| 108 | Max: -1, |
| 109 | Destination: &fval, |
| 110 | }, |
| 111 | } |
| 112 | |
| 113 | err := cmd.Run(buildTestContext(t), []string{"foo", "10", "20", "30"}) |
| 114 | r := require.New(t) |
| 115 | r.NoError(err) |
| 116 | r.Equal([]float64{10, 20, 30}, fval) |
| 117 | r.Equal([]float64{10, 20, 30}, cmd.FloatArgs("ia")) |
| 118 | r.Equal([]float64{10, 20, 30}, cmd.Float64Args("ia")) |
| 119 | r.Nil(cmd.Float32Args("ia")) |
| 120 | |
| 121 | r.Error(cmd.Run(buildTestContext(t), []string{"foo", "10", "a"})) |
| 122 | } |
| 123 | |
| 124 | func TestArgsIntSliceTypes(t *testing.T) { |
| 125 | cmd := buildMinimalTestCommand() |
| 126 | var ival []int |
| 127 | cmd.Arguments = []Argument{ |
| 128 | &IntArgs{ |
| 129 | Name: "ia", |
| 130 | Min: 1, |
| 131 | Max: -1, |
| 132 | Destination: &ival, |
| 133 | }, |
| 134 | } |
| 135 | |
| 136 | err := cmd.Run(buildTestContext(t), []string{"foo", "10", "20", "30"}) |
| 137 | r := require.New(t) |
| 138 | r.NoError(err) |
| 139 | r.Equal([]int{10, 20, 30}, ival) |
| 140 | r.Equal([]int{10, 20, 30}, cmd.IntArgs("ia")) |
| 141 | r.Nil(cmd.Int8Args("ia")) |
| 142 | r.Nil(cmd.Int16Args("ia")) |
| 143 | r.Nil(cmd.Int32Args("ia")) |
| 144 | r.Nil(cmd.Int64Args("ia")) |
| 145 | |
| 146 | r.Error(cmd.Run(buildTestContext(t), []string{"foo", "10", "20.0"})) |
| 147 | } |
| 148 | |
| 149 | func TestArgsUintTypes(t *testing.T) { |
| 150 | cmd := buildMinimalTestCommand() |
| 151 | var ival uint |
| 152 | cmd.Arguments = []Argument{ |
| 153 | &UintArg{ |
| 154 | Name: "ia", |
| 155 | Destination: &ival, |
| 156 | }, |
| 157 | } |
| 158 | |
| 159 | err := cmd.Run(buildTestContext(t), []string{"foo", "10"}) |
| 160 | r := require.New(t) |
| 161 | r.NoError(err) |
| 162 | r.Equal(uint(10), ival) |
| 163 | r.Equal(uint(10), cmd.UintArg("ia")) |
| 164 | r.Equal(uint(0), cmd.UintArg("iab")) |
| 165 | r.Equal(uint8(0), cmd.Uint8Arg("ia")) |
| 166 | r.Equal(uint16(0), cmd.Uint16Arg("ia")) |
| 167 | r.Equal(uint32(0), cmd.Uint32Arg("ia")) |
| 168 | r.Equal(uint64(0), cmd.Uint64Arg("ia")) |
| 169 | |
| 170 | r.Error(cmd.Run(buildTestContext(t), []string{"foo", "10.0"})) |
| 171 | } |
| 172 | |
| 173 | func TestArgsUintSliceTypes(t *testing.T) { |
| 174 | cmd := buildMinimalTestCommand() |
| 175 | var ival []uint |
| 176 | cmd.Arguments = []Argument{ |
| 177 | &UintArgs{ |
| 178 | Name: "ia", |
| 179 | Min: 1, |
| 180 | Max: -1, |
| 181 | Destination: &ival, |
| 182 | }, |
| 183 | } |
| 184 | |
| 185 | err := cmd.Run(buildTestContext(t), []string{"foo", "10", "20", "30"}) |
| 186 | r := require.New(t) |
| 187 | r.NoError(err) |
| 188 | r.Equal([]uint{10, 20, 30}, ival) |
| 189 | r.Equal([]uint{10, 20, 30}, cmd.UintArgs("ia")) |
| 190 | r.Nil(cmd.Uint8Args("ia")) |
| 191 | r.Nil(cmd.Uint16Args("ia")) |
| 192 | r.Nil(cmd.Uint32Args("ia")) |
| 193 | r.Nil(cmd.Uint64Args("ia")) |
| 194 | |
| 195 | r.Error(cmd.Run(buildTestContext(t), []string{"foo", "10", "20.0"})) |
| 196 | } |
| 197 | |
| 198 | func TestArgumentsRootCommand(t *testing.T) { |
| 199 | tests := []struct { |
| 200 | name string |
| 201 | args []string |
| 202 | expectedIvals []int |
| 203 | expectedUivals []uint |
| 204 | expectedFvals []float64 |
| 205 | errStr string |
| 206 | }{ |
| 207 | { |
| 208 | name: "set ival", |
| 209 | args: []string{"foo", "10"}, |
| 210 | expectedIvals: []int{10}, |
| 211 | expectedUivals: []uint{}, |
| 212 | expectedFvals: []float64{}, |
| 213 | }, |
| 214 | { |
| 215 | name: "set invalid ival", |
| 216 | args: []string{"foo", "10.0"}, |
| 217 | expectedIvals: []int{}, |
| 218 | expectedUivals: []uint{}, |
| 219 | expectedFvals: []float64{}, |
| 220 | errStr: "strconv.ParseInt: parsing \"10.0\": invalid syntax", |
| 221 | }, |
| 222 | { |
| 223 | name: "set ival uival", |
| 224 | args: []string{"foo", "-10", "11"}, |
| 225 | expectedIvals: []int{-10}, |
| 226 | expectedUivals: []uint{11}, |
| 227 | expectedFvals: []float64{}, |
| 228 | }, |
| 229 | { |
| 230 | name: "set ival uival fval", |
| 231 | args: []string{"foo", "-12", "14", "10.1"}, |
| 232 | expectedIvals: []int{-12}, |
| 233 | expectedUivals: []uint{14}, |
| 234 | expectedFvals: []float64{10.1}, |
| 235 | }, |
| 236 | { |
| 237 | name: "set ival uival multu fvals", |
| 238 | args: []string{"foo", "-13", "12", "10.1", "11.09"}, |
| 239 | expectedIvals: []int{-13}, |
| 240 | expectedUivals: []uint{12}, |
| 241 | expectedFvals: []float64{10.1, 11.09}, |
| 242 | }, |
| 243 | { |
| 244 | name: "set fvals beyond max", |
| 245 | args: []string{"foo", "13", "10", "10.1", "11.09", "12.1"}, |
| 246 | expectedIvals: []int{13}, |
| 247 | expectedUivals: []uint{10}, |
| 248 | expectedFvals: []float64{10.1, 11.09}, |
| 249 | errStr: "No help topic for '12.1'", |
| 250 | }, |
| 251 | } |
| 252 | |
| 253 | for _, test := range tests { |
| 254 | t.Run(test.name, func(t *testing.T) { |
| 255 | cmd := buildMinimalTestCommand() |
| 256 | var ivals []int |
| 257 | var uivals []uint |
| 258 | var fvals []float64 |
| 259 | cmd.Arguments = []Argument{ |
| 260 | &IntArgs{ |
| 261 | Name: "ia", |
| 262 | Min: 1, |
| 263 | Max: 1, |
| 264 | Destination: &ivals, |
| 265 | }, |
| 266 | &UintArgs{ |
| 267 | Name: "uia", |
| 268 | Min: 1, |
| 269 | Max: 1, |
| 270 | Destination: &uivals, |
| 271 | }, |
| 272 | &FloatArgs{ |
| 273 | Name: "fa", |
| 274 | Min: 0, |
| 275 | Max: 2, |
| 276 | Destination: &fvals, |
| 277 | }, |
| 278 | } |
| 279 | |
| 280 | err := cmd.Run(buildTestContext(t), test.args) |
| 281 | |
| 282 | r := require.New(t) |
| 283 | |
| 284 | if test.errStr != "" { |
| 285 | r.ErrorContains(err, test.errStr) |
| 286 | } else { |
| 287 | r.Equal(test.expectedIvals, ivals) |
| 288 | } |
| 289 | r.Equal(test.expectedIvals, cmd.IntArgs("ia")) |
| 290 | r.Equal(test.expectedFvals, cmd.FloatArgs("fa")) |
| 291 | r.Equal(test.expectedUivals, cmd.UintArgs("uia")) |
| 292 | /*if test.expectedFvals != nil { |
| 293 | r.Equal(test.expectedFvals, fvals) |
| 294 | }*/ |
| 295 | }) |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | func TestArgumentsInvalidType(t *testing.T) { |
| 300 | cmd := buildMinimalTestCommand() |
| 301 | cmd.Arguments = []Argument{ |
| 302 | &IntArgs{ |
| 303 | Name: "ia", |
| 304 | Min: 1, |
| 305 | Max: 1, |
| 306 | }, |
| 307 | } |
| 308 | r := require.New(t) |
| 309 | r.Nil(cmd.StringArgs("ia")) |
| 310 | r.Nil(cmd.FloatArgs("ia")) |
| 311 | r.Nil(cmd.Int8Args("ia")) |
| 312 | r.Nil(cmd.Int16Args("ia")) |
| 313 | r.Nil(cmd.Int32Args("ia")) |
| 314 | r.Nil(cmd.Int64Args("ia")) |
| 315 | r.Equal(time.Time{}, cmd.TimestampArg("ia")) |
| 316 | r.Nil(cmd.TimestampArgs("ia")) |
| 317 | r.Nil(cmd.UintArgs("ia")) |
| 318 | r.Nil(cmd.Uint8Args("ia")) |
| 319 | r.Nil(cmd.Uint16Args("ia")) |
| 320 | r.Nil(cmd.Uint32Args("ia")) |
| 321 | r.Nil(cmd.Uint64Args("ia")) |
| 322 | } |
| 323 | |
| 324 | func TestArgumentsSubcommand(t *testing.T) { |
| 325 | tests := []struct { |
| 326 | name string |
| 327 | args []string |
| 328 | expectedIval int |
| 329 | expectedSvals []string |
| 330 | expectedTVals []time.Time |
| 331 | errStr string |
| 332 | }{ |
| 333 | { |
| 334 | name: "insuff args", |
| 335 | args: []string{"foo", "subcmd", "2006-01-02T15:04:05Z"}, |
| 336 | errStr: "sufficient count of arg sa not provided, given 0 expected 1", |
| 337 | }, |
| 338 | { |
| 339 | name: "set sval and tval", |
| 340 | args: []string{"foo", "subcmd", "2006-01-02T15:04:05Z", "fubar"}, |
| 341 | expectedIval: 10, |
| 342 | expectedTVals: []time.Time{time.Date(2006, time.January, 2, 15, 4, 5, 0, time.UTC)}, |
| 343 | expectedSvals: []string{"fubar"}, |
| 344 | }, |
| 345 | { |
| 346 | name: "set sval, tval and ival", |
| 347 | args: []string{"foo", "subcmd", "--foo", "100", "2006-01-02T15:04:05Z", "fubar", "some"}, |
| 348 | expectedIval: 100, |
| 349 | expectedTVals: []time.Time{time.Date(2006, time.January, 2, 15, 4, 5, 0, time.UTC)}, |
| 350 | expectedSvals: []string{"fubar", "some"}, |
| 351 | }, |
| 352 | } |
| 353 | |
| 354 | for _, test := range tests { |
| 355 | t.Run(test.name, func(t *testing.T) { |
| 356 | cmd := buildMinimalTestCommand() |
| 357 | var ival int |
| 358 | var svals []string |
| 359 | var tvals []time.Time |
| 360 | cmd.Commands = []*Command{ |
| 361 | { |
| 362 | Name: "subcmd", |
| 363 | Flags: []Flag{ |
| 364 | &IntFlag{ |
| 365 | Name: "foo", |
| 366 | Value: 10, |
| 367 | Destination: &ival, |
| 368 | }, |
| 369 | }, |
| 370 | Arguments: []Argument{ |
| 371 | &TimestampArgs{ |
| 372 | Name: "ta", |
| 373 | Min: 1, |
| 374 | Max: 1, |
| 375 | Destination: &tvals, |
| 376 | Config: TimestampConfig{ |
| 377 | Layouts: []string{time.RFC3339}, |
| 378 | }, |
| 379 | }, |
| 380 | &StringArgs{ |
| 381 | Name: "sa", |
| 382 | Min: 1, |
| 383 | Max: 3, |
| 384 | Destination: &svals, |
| 385 | }, |
| 386 | }, |
| 387 | }, |
| 388 | } |
| 389 | |
| 390 | numUsageErrors := 0 |
| 391 | cmd.Commands[0].OnUsageError = func(ctx context.Context, cmd *Command, err error, isSubcommand bool) error { |
| 392 | numUsageErrors++ |
| 393 | return err |
| 394 | } |
| 395 | |
| 396 | err := cmd.Run(buildTestContext(t), test.args) |
| 397 | |
| 398 | r := require.New(t) |
| 399 | |
| 400 | if test.errStr != "" { |
| 401 | r.ErrorContains(err, test.errStr) |
| 402 | r.Equal(1, numUsageErrors) |
| 403 | } else { |
| 404 | if test.expectedSvals != nil { |
| 405 | r.Equal(test.expectedSvals, svals) |
| 406 | r.Equal(test.expectedSvals, cmd.Commands[0].StringArgs("sa")) |
| 407 | } |
| 408 | if test.expectedTVals != nil { |
| 409 | r.Equal(test.expectedTVals, tvals) |
| 410 | r.Equal(test.expectedTVals, cmd.Commands[0].TimestampArgs("ta")) |
| 411 | } |
| 412 | r.Equal(test.expectedIval, ival) |
| 413 | } |
| 414 | }) |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | func TestArgUsage(t *testing.T) { |
| 419 | arg := &IntArg{ |
| 420 | Name: "ia", |
| 421 | } |
| 422 | tests := []struct { |
| 423 | name string |
| 424 | usage string |
| 425 | expected string |
| 426 | }{ |
| 427 | { |
| 428 | name: "default", |
| 429 | expected: "ia", |
| 430 | }, |
| 431 | { |
| 432 | name: "usage", |
| 433 | usage: "foo-usage", |
| 434 | expected: "foo-usage", |
| 435 | }, |
| 436 | } |
| 437 | for _, test := range tests { |
| 438 | t.Run(test.name, func(t *testing.T) { |
| 439 | arg.UsageText = test.usage |
| 440 | require.Equal(t, test.expected, arg.Usage()) |
| 441 | }) |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | func TestArgsUsage(t *testing.T) { |
| 446 | arg := &IntArgs{ |
| 447 | Name: "ia", |
| 448 | Min: 0, |
| 449 | Max: 1, |
| 450 | } |
| 451 | tests := []struct { |
| 452 | name string |
| 453 | min int |
| 454 | max int |
| 455 | usage string |
| 456 | expected string |
| 457 | }{ |
| 458 | { |
| 459 | name: "optional", |
| 460 | min: 0, |
| 461 | max: 1, |
| 462 | expected: "[ia]", |
| 463 | }, |
| 464 | { |
| 465 | name: "optional", |
| 466 | min: 0, |
| 467 | max: 1, |
| 468 | usage: "[my optional usage]", |
| 469 | expected: "[my optional usage]", |
| 470 | }, |
| 471 | { |
| 472 | name: "zero or more", |
| 473 | min: 0, |
| 474 | max: 2, |
| 475 | expected: "[ia ...]", |
| 476 | }, |
| 477 | { |
| 478 | name: "one", |
| 479 | min: 1, |
| 480 | max: 1, |
| 481 | expected: "ia [ia ...]", |
| 482 | }, |
| 483 | { |
| 484 | name: "many", |
| 485 | min: 2, |
| 486 | max: 1, |
| 487 | expected: "ia [ia ...]", |
| 488 | }, |
| 489 | { |
| 490 | name: "many2", |
| 491 | min: 2, |
| 492 | max: 0, |
| 493 | expected: "ia [ia ...]", |
| 494 | }, |
| 495 | { |
| 496 | name: "unlimited", |
| 497 | min: 2, |
| 498 | max: -1, |
| 499 | expected: "ia [ia ...]", |
| 500 | }, |
| 501 | } |
| 502 | for _, test := range tests { |
| 503 | t.Run(test.name, func(t *testing.T) { |
| 504 | arg.Min, arg.Max, arg.UsageText = test.min, test.max, test.usage |
| 505 | require.Equal(t, test.expected, arg.Usage()) |
| 506 | }) |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | func TestSingleOptionalArg(t *testing.T) { |
| 511 | tests := []struct { |
| 512 | name string |
| 513 | args []string |
| 514 | argValue string |
| 515 | exp string |
| 516 | }{ |
| 517 | { |
| 518 | name: "no args", |
| 519 | args: []string{"foo"}, |
| 520 | exp: "", |
| 521 | }, |
| 522 | { |
| 523 | name: "no arg with def value", |
| 524 | args: []string{"foo"}, |
| 525 | argValue: "bar", |
| 526 | exp: "bar", |
| 527 | }, |
| 528 | { |
| 529 | name: "one arg", |
| 530 | args: []string{"foo", "zbar"}, |
| 531 | exp: "zbar", |
| 532 | }, |
| 533 | } |
| 534 | |
| 535 | for _, test := range tests { |
| 536 | t.Run(test.name, func(t *testing.T) { |
| 537 | cmd := buildMinimalTestCommand() |
| 538 | var s1 string |
| 539 | arg := &StringArg{ |
| 540 | Value: test.argValue, |
| 541 | Destination: &s1, |
| 542 | } |
| 543 | cmd.Arguments = []Argument{ |
| 544 | arg, |
| 545 | } |
| 546 | |
| 547 | err := cmd.Run(buildTestContext(t), test.args) // |
| 548 | r := require.New(t) |
| 549 | r.NoError(err) |
| 550 | r.Equal(test.exp, s1) |
| 551 | }) |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | func TestUnboundedArgs(t *testing.T) { |
| 556 | arg := &StringArgs{ |
| 557 | Min: 0, |
| 558 | Max: -1, |
| 559 | } |
| 560 | tests := []struct { |
| 561 | name string |
| 562 | args []string |
| 563 | defValues []string |
| 564 | values []string |
| 565 | expected []string |
| 566 | }{ |
| 567 | { |
| 568 | name: "cmd accepts no args", |
| 569 | args: []string{"foo"}, |
| 570 | expected: []string{}, |
| 571 | }, |
| 572 | { |
| 573 | name: "cmd uses given args", |
| 574 | args: []string{"foo", "bar", "baz"}, |
| 575 | expected: []string{"bar", "baz"}, |
| 576 | }, |
| 577 | { |
| 578 | name: "cmd uses default values", |
| 579 | args: []string{"foo"}, |
| 580 | expected: []string{}, |
| 581 | }, |
| 582 | { |
| 583 | name: "given args override default values", |
| 584 | args: []string{"foo", "bar", "baz"}, |
| 585 | values: []string{"zbar", "zbaz"}, |
| 586 | expected: []string{"bar", "baz"}, |
| 587 | }, |
| 588 | } |
| 589 | |
| 590 | for _, test := range tests { |
| 591 | t.Run(test.name, func(t *testing.T) { |
| 592 | cmd := buildMinimalTestCommand() |
| 593 | cmd.Arguments = []Argument{arg} |
| 594 | arg.Destination = &test.values |
| 595 | require.NoError(t, cmd.Run(context.Background(), test.args)) |
| 596 | require.Equal(t, test.expected, *arg.Destination) |
| 597 | }) |
| 598 | } |
| 599 | } |