fix test error message

This commit is contained in:
faizan-siddiqui 2025-01-16 02:39:38 +00:00
parent 1c44c42794
commit dd7bc42aa8
2 changed files with 11 additions and 11 deletions

View file

@ -23,10 +23,10 @@ import (
) )
const ( const (
requiredAsGroup = "cobra_annotation_required_if_others_set" requiredAsGroupAnnotation = "cobra_annotation_required_if_others_set"
oneRequired = "cobra_annotation_one_required" oneRequiredAnnotation = "cobra_annotation_one_required"
mutuallyExclusive = "cobra_annotation_mutually_exclusive" mutuallyExclusiveAnnotation = "cobra_annotation_mutually_exclusive"
ifPresentThenOthersRequired = "cobra_annotation_if_present_then_others_required" ifPresentThenOthersRequiredAnnotation = "cobra_annotation_if_present_then_others_required"
) )
// MarkFlagsRequiredTogether marks the given flags with annotations so that Cobra errors // MarkFlagsRequiredTogether marks the given flags with annotations so that Cobra errors
@ -90,7 +90,7 @@ func (c *Command) MarkIfFlagPresentThenOthersRequired(flagNames ...string) {
panic(fmt.Sprintf("Failed to find flag %q and mark it as being in an if present then others required flag group", v)) panic(fmt.Sprintf("Failed to find flag %q and mark it as being in an if present then others required flag group", v))
} }
// Each time this is called is a single new entry; this allows it to be a member of multiple groups if needed. // Each time this is called is a single new entry; this allows it to be a member of multiple groups if needed.
if err := c.Flags().SetAnnotation(v, ifPresentThenOthersRequired, append(f.Annotations[ifPresentThenOthersRequired], strings.Join(flagNames, " "))); err != nil { if err := c.Flags().SetAnnotation(v, ifPresentThenOthersRequiredAnnotation, append(f.Annotations[ifPresentThenOthersRequiredAnnotation], strings.Join(flagNames, " "))); err != nil {
panic(err) panic(err)
} }
} }
@ -115,7 +115,7 @@ func (c *Command) ValidateFlagGroups() error {
processFlagForGroupAnnotation(flags, pflag, requiredAsGroupAnnotation, groupStatus) processFlagForGroupAnnotation(flags, pflag, requiredAsGroupAnnotation, groupStatus)
processFlagForGroupAnnotation(flags, pflag, oneRequiredAnnotation, oneRequiredGroupStatus) processFlagForGroupAnnotation(flags, pflag, oneRequiredAnnotation, oneRequiredGroupStatus)
processFlagForGroupAnnotation(flags, pflag, mutuallyExclusiveAnnotation, mutuallyExclusiveGroupStatus) processFlagForGroupAnnotation(flags, pflag, mutuallyExclusiveAnnotation, mutuallyExclusiveGroupStatus)
processFlagForGroupAnnotation(flags, pflag, ifPresentThenOthersRequired, ifPresentThenOthersRequiredGroupStatus) processFlagForGroupAnnotation(flags, pflag, ifPresentThenOthersRequiredAnnotation, ifPresentThenOthersRequiredGroupStatus)
}) })
if err := validateRequiredFlagGroups(groupStatus); err != nil { if err := validateRequiredFlagGroups(groupStatus); err != nil {
@ -255,7 +255,7 @@ func validateIfPresentThenRequiredFlagGroups(data map[string]map[string]bool) er
if len(unset) > 0 { if len(unset) > 0 {
return fmt.Errorf( return fmt.Errorf(
"%v is set, the following flags must be provided: %v", "%v is set, the following flags must be provided: %v",
flagList[0], unset, primaryFlag, unset,
) )
} }
} }
@ -294,7 +294,7 @@ func (c *Command) enforceFlagGroupsForCompletion() {
processFlagForGroupAnnotation(flags, pflag, requiredAsGroupAnnotation, groupStatus) processFlagForGroupAnnotation(flags, pflag, requiredAsGroupAnnotation, groupStatus)
processFlagForGroupAnnotation(flags, pflag, oneRequiredAnnotation, oneRequiredGroupStatus) processFlagForGroupAnnotation(flags, pflag, oneRequiredAnnotation, oneRequiredGroupStatus)
processFlagForGroupAnnotation(flags, pflag, mutuallyExclusiveAnnotation, mutuallyExclusiveGroupStatus) processFlagForGroupAnnotation(flags, pflag, mutuallyExclusiveAnnotation, mutuallyExclusiveGroupStatus)
processFlagForGroupAnnotation(flags, pflag, ifPresentThenOthersRequired, ifPresentThenRequiredGroupStatus) processFlagForGroupAnnotation(flags, pflag, ifPresentThenOthersRequiredAnnotation, ifPresentThenRequiredGroupStatus)
}) })
// If a flag that is part of a group is present, we make all the other flags // If a flag that is part of a group is present, we make all the other flags

View file

@ -81,7 +81,7 @@ func TestValidateFlagGroups(t *testing.T) {
desc: "If present then others required flag group not satisfied", desc: "If present then others required flag group not satisfied",
flagGroupsIfPresentThenRequired: []string{"a b"}, flagGroupsIfPresentThenRequired: []string{"a b"},
args: []string{"--a=foo"}, 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]", expectErr: "a is set, the following flags must be provided: [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"},
@ -102,7 +102,7 @@ func TestValidateFlagGroups(t *testing.T) {
desc: "Multiple if present then others required flag group not satisfied returns first error", desc: "Multiple if present then others required flag group not satisfied returns first error",
flagGroupsIfPresentThenRequired: []string{"a b", "d e"}, flagGroupsIfPresentThenRequired: []string{"a b", "d e"},
args: []string{"--a=foo", "--f=foo"}, 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]`, expectErr: `a is set, the following flags must be provided: [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"},