MCPcopy
hub / github.com/spf13/cobra / TestValidateFlagGroups

Function TestValidateFlagGroups

flag_groups_test.go:22–195  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

20)
21
22func TestValidateFlagGroups(t *testing.T) {
23 getCmd := func() *Command {
24 c := &Command{
25 Use: "testcmd",
26 Run: func(cmd *Command, args []string) {
27 }}
28 // Define lots of flags to utilize for testing.
29 for _, v := range []string{"a", "b", "c", "d"} {
30 c.Flags().String(v, "", "")
31 }
32 for _, v := range []string{"e", "f", "g"} {
33 c.PersistentFlags().String(v, "", "")
34 }
35 subC := &Command{
36 Use: "subcmd",
37 Run: func(cmd *Command, args []string) {
38 }}
39 subC.Flags().String("subonly", "", "")
40 c.AddCommand(subC)
41 return c
42 }
43
44 // Each test case uses a unique command from the function above.
45 testcases := []struct {
46 desc string
47 flagGroupsRequired []string
48 flagGroupsOneRequired []string
49 flagGroupsExclusive []string
50 subCmdFlagGroupsRequired []string
51 subCmdFlagGroupsOneRequired []string
52 subCmdFlagGroupsExclusive []string
53 args []string
54 expectErr string
55 }{
56 {
57 desc: "No flags no problem",
58 }, {
59 desc: "No flags no problem even with conflicting groups",
60 flagGroupsRequired: []string{"a b"},
61 flagGroupsExclusive: []string{"a b"},
62 }, {
63 desc: "Required flag group not satisfied",
64 flagGroupsRequired: []string{"a b c"},
65 args: []string{"--a=foo"},
66 expectErr: "if any flags in the group [a b c] are set they must all be set; missing [b c]",
67 }, {
68 desc: "One-required flag group not satisfied",
69 flagGroupsOneRequired: []string{"a b"},
70 args: []string{"--c=foo"},
71 expectErr: "at least one of the flags in the group [a b] is required",
72 }, {
73 desc: "Exclusive flag group not satisfied",
74 flagGroupsExclusive: []string{"a b c"},
75 args: []string{"--a=foo", "--b=foo"},
76 expectErr: "if any flags in the group [a b c] are set none of the others can be; [a b] were all set",
77 }, {
78 desc: "Multiple required flag group not satisfied returns first error",
79 flagGroupsRequired: []string{"a b c", "a d"},

Callers

nothing calls this directly

Calls 11

FlagsMethod · 0.95
PersistentFlagsMethod · 0.95
AddCommandMethod · 0.95
CommandsMethod · 0.95
MarkFlagsOneRequiredMethod · 0.95
SetArgsMethod · 0.95
ExecuteMethod · 0.95
StringMethod · 0.80
ErrorMethod · 0.80

Tested by

no test coverage detected