added test cases

This commit is contained in:
faizan-siddiqui 2024-10-23 17:00:26 +11:00 committed by GitHub
parent ca3972ed08
commit 49443501ed
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -43,22 +43,25 @@ func TestValidateFlagGroups(t *testing.T) {
// Each test case uses a unique command from the function above. // Each test case uses a unique command from the function above.
testcases := []struct { testcases := []struct {
desc string desc string
flagGroupsRequired []string flagGroupsRequired []string
flagGroupsOneRequired []string flagGroupsOneRequired []string
flagGroupsExclusive []string flagGroupsExclusive []string
subCmdFlagGroupsRequired []string flagGroupsIfPresentThenRequired []string
subCmdFlagGroupsOneRequired []string subCmdFlagGroupsRequired []string
subCmdFlagGroupsExclusive []string subCmdFlagGroupsOneRequired []string
args []string subCmdFlagGroupsExclusive []string
expectErr string subCmdFlagGroupsIfPresentThenRequired []string
args []string
expectErr string
}{ }{
{ {
desc: "No flags no problem", desc: "No flags no problem",
}, { }, {
desc: "No flags no problem even with conflicting groups", desc: "No flags no problem even with conflicting groups",
flagGroupsRequired: []string{"a b"}, flagGroupsRequired: []string{"a b"},
flagGroupsExclusive: []string{"a b"}, flagGroupsExclusive: []string{"a b"},
flagGroupsIfPresentThenRequired: []string{"a b"},
}, { }, {
desc: "Required flag group not satisfied", desc: "Required flag group not satisfied",
flagGroupsRequired: []string{"a b c"}, flagGroupsRequired: []string{"a b c"},
@ -74,6 +77,11 @@ func TestValidateFlagGroups(t *testing.T) {
flagGroupsExclusive: []string{"a b c"}, flagGroupsExclusive: []string{"a b c"},
args: []string{"--a=foo", "--b=foo"}, args: []string{"--a=foo", "--b=foo"},
expectErr: "if any flags in the group [a b c] are set none of the others can be; [a b] were all set", expectErr: "if any flags in the group [a b c] are set none of the others can be; [a b] were all set",
}, {
desc: "If present then others required flag group not satisfied",
flagGroupsIfPresentThenRequired: []string{"a b"},
args: []string{"--a=foo"},
expectErr: "if the first flag in the group [a b] is set, all other flags must be set; the following flags are not set: [b]",
}, { }, {
desc: "Multiple required flag group not satisfied returns first error", desc: "Multiple required flag group not satisfied returns first error",
flagGroupsRequired: []string{"a b c", "a d"}, flagGroupsRequired: []string{"a b c", "a d"},
@ -89,6 +97,12 @@ func TestValidateFlagGroups(t *testing.T) {
flagGroupsExclusive: []string{"a b c", "a d"}, flagGroupsExclusive: []string{"a b c", "a d"},
args: []string{"--a=foo", "--c=foo", "--d=foo"}, args: []string{"--a=foo", "--c=foo", "--d=foo"},
expectErr: `if any flags in the group [a b c] are set none of the others can be; [a c] were all set`, expectErr: `if any flags in the group [a b c] are set none of the others can be; [a c] were all set`,
},
{
desc: "Multiple if present then others required flag group not satisfied returns first error",
flagGroupsIfPresentThenRequired: []string{"a b", "d e"},
args: []string{"--a=foo", "--f=foo"},
expectErr: `if the first flag in the group [a b] is set, all other flags must be set; the following flags are not set: [b]`,
}, { }, {
desc: "Validation of required groups occurs on groups in sorted order", desc: "Validation of required groups occurs on groups in sorted order",
flagGroupsRequired: []string{"a d", "a b", "a c"}, flagGroupsRequired: []string{"a d", "a b", "a c"},
@ -104,6 +118,11 @@ func TestValidateFlagGroups(t *testing.T) {
flagGroupsExclusive: []string{"a d", "a b", "a c"}, flagGroupsExclusive: []string{"a d", "a b", "a c"},
args: []string{"--a=foo", "--b=foo", "--c=foo"}, args: []string{"--a=foo", "--b=foo", "--c=foo"},
expectErr: `if any flags in the group [a b] are set none of the others can be; [a b] were all set`, expectErr: `if any flags in the group [a b] are set none of the others can be; [a b] were all set`,
}, {
desc: "Validation of if present then others required groups occurs on groups in sorted order",
flagGroupsIfPresentThenRequired: []string{"a d", "a b", "a c"},
args: []string{"--a=foo"},
expectErr: `if the first flag in the group [a b] is set, all other flags must be set; the following flags are not set: [b]`,
}, { }, {
desc: "Persistent flags utilize required and exclusive groups and can fail required groups", desc: "Persistent flags utilize required and exclusive groups and can fail required groups",
flagGroupsRequired: []string{"a e", "e f"}, flagGroupsRequired: []string{"a e", "e f"},
@ -182,6 +201,12 @@ func TestValidateFlagGroups(t *testing.T) {
for _, flagGroup := range tc.subCmdFlagGroupsExclusive { for _, flagGroup := range tc.subCmdFlagGroupsExclusive {
sub.MarkFlagsMutuallyExclusive(strings.Split(flagGroup, " ")...) sub.MarkFlagsMutuallyExclusive(strings.Split(flagGroup, " ")...)
} }
for _, flagGroup := range tc.flagGroupsIfPresentThenRequired {
c.MarkIfFlagPresentThenOthersRequired(strings.Split(flagGroup, " ")...)
}
for _, flagGroup := range tc.subCmdFlagGroupsIfPresentThenRequired {
sub.MarkIfFlagPresentThenOthersRequired(strings.Split(flagGroup, " ")...)
}
c.SetArgs(tc.args) c.SetArgs(tc.args)
err := c.Execute() err := c.Execute()
switch { switch {