|
6 | 6 |
|
7 | 7 | . "github.com/onsi/gomega" |
8 | 8 | "github.com/spf13/cobra" |
| 9 | + "github.com/spf13/pflag" |
| 10 | + "k8s.io/apimachinery/pkg/types" |
9 | 11 | ) |
10 | 12 |
|
11 | 13 | type flagTestCase struct { |
@@ -454,3 +456,128 @@ func TestSleepCmdFlagValidation(t *testing.T) { |
454 | 456 | }) |
455 | 457 | } |
456 | 458 | } |
| 459 | + |
| 460 | +func TestParseFlags(t *testing.T) { |
| 461 | + g := NewWithT(t) |
| 462 | + |
| 463 | + flagSet := pflag.NewFlagSet("flagSet", 0) |
| 464 | + // set SortFlags to false for testing purposes so when parseFlags loops over the flagSet it |
| 465 | + // goes off of primordial order. |
| 466 | + flagSet.SortFlags = false |
| 467 | + |
| 468 | + var boolFlagTrue bool |
| 469 | + flagSet.BoolVar( |
| 470 | + &boolFlagTrue, |
| 471 | + "boolFlagTrue", |
| 472 | + true, |
| 473 | + "boolean true test flag", |
| 474 | + ) |
| 475 | + |
| 476 | + var boolFlagFalse bool |
| 477 | + flagSet.BoolVar( |
| 478 | + &boolFlagFalse, |
| 479 | + "boolFlagFalse", |
| 480 | + false, |
| 481 | + "boolean false test flag", |
| 482 | + ) |
| 483 | + |
| 484 | + customIntFlagDefault := intValidatingValue{ |
| 485 | + validator: validatePort, |
| 486 | + value: 8080, |
| 487 | + } |
| 488 | + flagSet.Var( |
| 489 | + &customIntFlagDefault, |
| 490 | + "customIntFlagDefault", |
| 491 | + "default custom int test flag", |
| 492 | + ) |
| 493 | + |
| 494 | + customIntFlagUserDefined := intValidatingValue{ |
| 495 | + validator: validatePort, |
| 496 | + value: 8080, |
| 497 | + } |
| 498 | + flagSet.Var( |
| 499 | + &customIntFlagUserDefined, |
| 500 | + "customIntFlagUserDefined", |
| 501 | + "user defined custom int test flag", |
| 502 | + ) |
| 503 | + err := flagSet.Set("customIntFlagUserDefined", "8081") |
| 504 | + g.Expect(err).To(Not(HaveOccurred())) |
| 505 | + |
| 506 | + customStringFlagDefault := stringValidatingValue{ |
| 507 | + validator: validateResourceName, |
| 508 | + value: "default-custom-string-test-flag", |
| 509 | + } |
| 510 | + flagSet.Var( |
| 511 | + &customStringFlagDefault, |
| 512 | + "customStringFlagDefault", |
| 513 | + "default custom string test flag", |
| 514 | + ) |
| 515 | + |
| 516 | + customStringFlagUserDefined := stringValidatingValue{ |
| 517 | + validator: validateResourceName, |
| 518 | + value: "user-defined-custom-string-test-flag", |
| 519 | + } |
| 520 | + flagSet.Var( |
| 521 | + &customStringFlagUserDefined, |
| 522 | + "customStringFlagUserDefined", |
| 523 | + "user defined custom string test flag", |
| 524 | + ) |
| 525 | + err = flagSet.Set("customStringFlagUserDefined", "changed-test-flag-value") |
| 526 | + g.Expect(err).To(Not(HaveOccurred())) |
| 527 | + |
| 528 | + customStringFlagNoDefaultValueUnset := namespacedNameValue{ |
| 529 | + value: types.NamespacedName{}, |
| 530 | + } |
| 531 | + flagSet.Var( |
| 532 | + &customStringFlagNoDefaultValueUnset, |
| 533 | + "customStringFlagNoDefaultValueUnset", |
| 534 | + "no default value custom string test flag", |
| 535 | + ) |
| 536 | + |
| 537 | + customStringFlagNoDefaultValueUserDefined := namespacedNameValue{ |
| 538 | + value: types.NamespacedName{}, |
| 539 | + } |
| 540 | + flagSet.Var( |
| 541 | + &customStringFlagNoDefaultValueUserDefined, |
| 542 | + "customStringFlagNoDefaultValueUserDefined", |
| 543 | + "no default value but with user defined namespacedName test flag", |
| 544 | + ) |
| 545 | + userDefinedNamespacedName := types.NamespacedName{ |
| 546 | + Namespace: "changed-namespace", |
| 547 | + Name: "changed-name", |
| 548 | + } |
| 549 | + err = flagSet.Set("customStringFlagNoDefaultValueUserDefined", userDefinedNamespacedName.String()) |
| 550 | + g.Expect(err).To(Not(HaveOccurred())) |
| 551 | + |
| 552 | + expectedKeys := []string{ |
| 553 | + "boolFlagTrue", |
| 554 | + "boolFlagFalse", |
| 555 | + |
| 556 | + "customIntFlagDefault", |
| 557 | + "customIntFlagUserDefined", |
| 558 | + |
| 559 | + "customStringFlagDefault", |
| 560 | + "customStringFlagUserDefined", |
| 561 | + |
| 562 | + "customStringFlagNoDefaultValueUnset", |
| 563 | + "customStringFlagNoDefaultValueUserDefined", |
| 564 | + } |
| 565 | + expectedValues := []string{ |
| 566 | + "true", |
| 567 | + "false", |
| 568 | + |
| 569 | + "default", |
| 570 | + "user-defined", |
| 571 | + |
| 572 | + "default", |
| 573 | + "user-defined", |
| 574 | + |
| 575 | + "default", |
| 576 | + "user-defined", |
| 577 | + } |
| 578 | + |
| 579 | + flagKeys, flagValues := parseFlags(flagSet) |
| 580 | + |
| 581 | + g.Expect(flagKeys).Should(Equal(expectedKeys)) |
| 582 | + g.Expect(flagValues).Should(Equal(expectedValues)) |
| 583 | +} |
0 commit comments