From 49443501edb6f459eb4453fc7b7d20c502ec9962 Mon Sep 17 00:00:00 2001 From: faizan-siddiqui Date: Wed, 23 Oct 2024 17:00:26 +1100 Subject: [PATCH] added test cases --- flag_groups_test.go | 49 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 12 deletions(-) diff --git a/flag_groups_test.go b/flag_groups_test.go index cffa8552..a1c79ce3 100644 --- a/flag_groups_test.go +++ b/flag_groups_test.go @@ -43,22 +43,25 @@ func TestValidateFlagGroups(t *testing.T) { // Each test case uses a unique command from the function above. testcases := []struct { - desc string - flagGroupsRequired []string - flagGroupsOneRequired []string - flagGroupsExclusive []string - subCmdFlagGroupsRequired []string - subCmdFlagGroupsOneRequired []string - subCmdFlagGroupsExclusive []string - args []string - expectErr string + desc string + flagGroupsRequired []string + flagGroupsOneRequired []string + flagGroupsExclusive []string + flagGroupsIfPresentThenRequired []string + subCmdFlagGroupsRequired []string + subCmdFlagGroupsOneRequired []string + subCmdFlagGroupsExclusive []string + subCmdFlagGroupsIfPresentThenRequired []string + args []string + expectErr string }{ { desc: "No flags no problem", }, { - desc: "No flags no problem even with conflicting groups", - flagGroupsRequired: []string{"a b"}, - flagGroupsExclusive: []string{"a b"}, + desc: "No flags no problem even with conflicting groups", + flagGroupsRequired: []string{"a b"}, + flagGroupsExclusive: []string{"a b"}, + flagGroupsIfPresentThenRequired: []string{"a b"}, }, { desc: "Required flag group not satisfied", flagGroupsRequired: []string{"a b c"}, @@ -74,6 +77,11 @@ func TestValidateFlagGroups(t *testing.T) { flagGroupsExclusive: []string{"a b c"}, 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", + }, { + 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", flagGroupsRequired: []string{"a b c", "a d"}, @@ -89,6 +97,12 @@ func TestValidateFlagGroups(t *testing.T) { flagGroupsExclusive: []string{"a b c", "a d"}, 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`, + }, + { + 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", 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"}, 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`, + }, { + 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", flagGroupsRequired: []string{"a e", "e f"}, @@ -182,6 +201,12 @@ func TestValidateFlagGroups(t *testing.T) { for _, flagGroup := range tc.subCmdFlagGroupsExclusive { 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) err := c.Execute() switch {