Skip to content
Myr

Create unit test illustrating unknown flag bug (#1854)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Created a unit test that tests the unknown flag error message when the unknown flag is located in different arg positions.

authored by Brian Pursley · committed by GitHub · parent 6b0bd30 · Browse files · 1 file+43 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+43 added−0 removed

  • other:+43 added−0 removed

1 file+43 added−0 removed

modified:command_test.go+43 added
2690 unmodified lines
2691 }2692 })2693 }2694}26952696func TestUnknownFlagShouldReturnSameErrorRegardlessOfArgPosition(t *testing.T) {2697 testCases := [][]string{2698 //{"--unknown", "--namespace", "foo", "child", "--bar"}, // FIXME: This test case fails, returning the error `unknown command "foo" for "root"` instead of the expected error `unknown flag: --unknown`2699 {"--namespace", "foo", "--unknown", "child", "--bar"},2700 {"--namespace", "foo", "child", "--unknown", "--bar"},2701 {"--namespace", "foo", "child", "--bar", "--unknown"},27022703 {"--unknown", "--namespace=foo", "child", "--bar"},2704 {"--namespace=foo", "--unknown", "child", "--bar"},2705 {"--namespace=foo", "child", "--unknown", "--bar"},2706 {"--namespace=foo", "child", "--bar", "--unknown"},27072708 {"--unknown", "--namespace=foo", "child", "--bar=true"},2709 {"--namespace=foo", "--unknown", "child", "--bar=true"},2710 {"--namespace=foo", "child", "--unknown", "--bar=true"},2711 {"--namespace=foo", "child", "--bar=true", "--unknown"},2712 }27132714 root := &Command{2715 Use: "root",2716 Run: emptyRun,2717 }2718 root.PersistentFlags().String("namespace", "", "a string flag")27192720 c := &Command{2721 Use: "child",2722 Run: emptyRun,2723 }2724 c.Flags().Bool("bar", false, "a boolean flag")27252726 root.AddCommand(c)27272728 for _, tc := range testCases {2729 t.Run(strings.Join(tc, " "), func(t *testing.T) {2730 output, err := executeCommand(root, tc...)2731 if err == nil {2732 t.Error("expected unknown flag error")2733 }2734 checkStringContains(t, output, "unknown flag: --unknown")2735 })2736 }2737}