spf13--cobra/flag_groups_test.go
evermake 98c9b4c903 refactor(flag_groups): flag groups implementation changed
This commit changes the flag groups feature logic. New implementation is more clean, readable and extendable (hope it won't be just my opinion).

The following changes have been made:

1. Main change:
Flags annotating by "cobra_annotation_required_if_others_set" and "cobra_annotation_mutually_exclusive" annotations was removed as well as all related and hard-to-understand "hacks" to combine flags back into groups on validation process.
Instead, `flagGroups` field was added to the `Command` struct. `flagGroups` field is a list of (new) structs `flagGroup`, which represents the "relationships" between flags within the command.

2. "Required together" and "mutually exclusive" groups logic was updated by implementing `requiredTogetherFlagGroup` and `mutuallyExclusiveFlagGroup` `flagGroup`s.

3. `enforceFlagGroupsForCompletion` `Command`'s method was renamed to `adjustByFlagGroupsForCompletions`.

4. Groups failed validation error messages were changed:
  - `"if any flags in the group [...] are set they must all be set; missing [...]"` to `"flags [...] must be set together, but [...] were not set"`
  - `"if any flags in the group [...] are set none of the others can be; [...] were all set"` to `"exactly one of the flags [...] can be set, but [...] were set"`

5. Not found flag on group marking error messages were updated from "Failed to find flag %q and mark it as being required in a flag group" and "Failed to find flag %q and mark it as being in a mutually exclusive flag group" to "flag %q is not defined"

6. `TestValidateFlagGroups` test was updated in `flag_groups_test.go`.
  - `getCmd` function was updated and test flag names were changed to improve readability
  - 2 testcases (`Validation of required groups occurs on groups in sorted order` and `Validation of exclusive groups occurs on groups in sorted order`) were removed, because groups validation now occur in the same order those groups were registered
  - other 16 testcases are preserved with updated descriptions, error messages

The completions generation tests that contain flag groups related testcases and updated flag groups tests, as well as all other tests, have been passed.

API was not changed: `MarkFlagsRequiredTogether` and `MarkFlagsMutuallyExclusive` functions have the same signatures.
2022-10-07 12:09:03 +03:00

163 lines
6.3 KiB
Go

// Copyright 2013-2022 The Cobra Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cobra
import (
"strings"
"testing"
)
func TestValidateFlagGroups(t *testing.T) {
getCmd := func() *Command {
cmd := &Command{
Use: "testcmd",
Run: func(cmd *Command, args []string) {},
}
cmd.Flags().String("a", "", "")
cmd.Flags().String("b", "", "")
cmd.Flags().String("c", "", "")
cmd.Flags().String("d", "", "")
cmd.PersistentFlags().String("p-a", "", "")
cmd.PersistentFlags().String("p-b", "", "")
cmd.PersistentFlags().String("p-c", "", "")
subCmd := &Command{
Use: "subcmd",
Run: func(cmd *Command, args []string) {},
}
subCmd.Flags().String("sub-a", "", "")
cmd.AddCommand(subCmd)
return cmd
}
// Each test case uses a unique command from the function above.
testcases := []struct {
desc string
requiredTogether []string
mutuallyExclusive []string
subRequiredTogether []string
subMutuallyExclusive []string
args []string
expectErr string
}{
{
desc: "No flags no problems",
}, {
desc: "No flags no problems even with conflicting groups",
requiredTogether: []string{"a b"},
mutuallyExclusive: []string{"a b"},
}, {
desc: "Required together flag group validation fails",
requiredTogether: []string{"a b c"},
args: []string{"--a=foo"},
expectErr: `flags [a b c] must be set together, but [b c] were not set`,
}, {
desc: "Required together flag group validation passes",
requiredTogether: []string{"a b c"},
args: []string{"--c=bar", "--a=foo", "--b=baz"},
}, {
desc: "Mutually exclusive flag group validation fails",
mutuallyExclusive: []string{"a b c"},
args: []string{"--b=foo", "--c=bar"},
expectErr: `exactly one of the flags [a b c] can be set, but [b c] were set`,
}, {
desc: "Mutually exclusive flag group validation passes",
mutuallyExclusive: []string{"a b c"},
args: []string{"--b=foo"},
}, {
desc: "Multiple required together flag groups failed validation returns first error",
requiredTogether: []string{"a b c", "a d"},
args: []string{"--d=foo", "--c=foo"},
expectErr: `flags [a b c] must be set together, but [a b] were not set`,
}, {
desc: "Multiple mutually exclusive flag groups failed validation returns first error",
mutuallyExclusive: []string{"a b c", "a d"},
args: []string{"--a=foo", "--c=foo", "--d=foo"},
expectErr: `exactly one of the flags [a b c] can be set, but [a c] were set`,
}, {
desc: "Flag and persistent flags being in multiple groups fail required together group",
requiredTogether: []string{"a p-a", "p-a p-b"},
mutuallyExclusive: []string{"p-b p-c"},
args: []string{"--a=foo", "--p-b=foo", "--p-c=foo"},
expectErr: `flags [a p-a] must be set together, but [p-a] were not set`,
}, {
desc: "Flag and persistent flags being in multiple groups fail mutually exclusive group",
requiredTogether: []string{"a p-a", "p-a p-b"},
mutuallyExclusive: []string{"p-b p-c"},
args: []string{"--a=foo", "--p-a=foo", "--p-b=foo", "--p-c=foo"},
expectErr: `exactly one of the flags [p-b p-c] can be set, but [p-b p-c] were set`,
}, {
desc: "Flag and persistent flags pass required together and mutually exclusive groups",
requiredTogether: []string{"a p-a", "p-a p-b"},
mutuallyExclusive: []string{"p-b p-c"},
args: []string{"--a=foo", "--p-a=foo", "--p-b=foo"},
}, {
desc: "Required together flag group validation fails on subcommand with inherited flag",
subRequiredTogether: []string{"p-a sub-a"},
args: []string{"subcmd", "--sub-a=foo"},
expectErr: `flags [p-a sub-a] must be set together, but [p-a] were not set`,
}, {
desc: "Required together flag group validation passes on subcommand with inherited flag",
subRequiredTogether: []string{"p-a sub-a"},
args: []string{"subcmd", "--p-a=foo", "--sub-a=foo"},
}, {
desc: "Mutually exclusive flag group validation fails on subcommand with inherited flag",
subMutuallyExclusive: []string{"p-a sub-a"},
args: []string{"subcmd", "--p-a=foo", "--sub-a=foo"},
expectErr: `exactly one of the flags [p-a sub-a] can be set, but [p-a sub-a] were set`,
}, {
desc: "Mutually exclusive flag group validation passes on subcommand with inherited flag",
subMutuallyExclusive: []string{"p-a sub-a"},
args: []string{"subcmd", "--p-a=foo"},
}, {
desc: "Required together flag group validation is not applied on other command",
subRequiredTogether: []string{"p-a sub-a"},
args: []string{"--p-a=foo"},
},
}
for _, tc := range testcases {
t.Run(tc.desc, func(t *testing.T) {
cmd := getCmd()
subCmd := cmd.Commands()[0]
for _, group := range tc.requiredTogether {
cmd.MarkFlagsRequiredTogether(strings.Split(group, " ")...)
}
for _, group := range tc.mutuallyExclusive {
cmd.MarkFlagsMutuallyExclusive(strings.Split(group, " ")...)
}
for _, group := range tc.subRequiredTogether {
subCmd.MarkFlagsRequiredTogether(strings.Split(group, " ")...)
}
for _, group := range tc.subMutuallyExclusive {
subCmd.MarkFlagsMutuallyExclusive(strings.Split(group, " ")...)
}
cmd.SetArgs(tc.args)
err := cmd.Execute()
switch {
case err == nil && len(tc.expectErr) > 0:
t.Errorf("Expected error %q but got nil", tc.expectErr)
case err != nil && err.Error() != tc.expectErr:
t.Errorf("Expected error %q but got %q", tc.expectErr, err)
}
})
}
}