Skip to content

Commit c4dbb36

Browse files
committed
test: refactor InitializeGlobalFlags to table-driven test with flag details
1 parent e2e0ed5 commit c4dbb36

1 file changed

Lines changed: 59 additions & 12 deletions

File tree

internal/config/flags_test.go

Lines changed: 59 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,67 @@ func Test_InitializeGlobalFlags(t *testing.T) {
4646

4747
config.InitializeGlobalFlags(cmd)
4848

49-
// Verify that key persistent flags were registered
50-
flagNames := []string{
51-
"apihost", "app", "config-dir", "experiment",
52-
"force", "no-color", "skip-update", "slackdev",
53-
"runtime", "team", "token", "verbose",
49+
tests := map[string]struct {
50+
longform string
51+
shorthand string
52+
hidden bool
53+
}{
54+
"apihost": {
55+
longform: "apihost",
56+
hidden: true,
57+
},
58+
"app": {
59+
longform: "app",
60+
shorthand: "a",
61+
},
62+
"config-dir": {
63+
longform: "config-dir",
64+
},
65+
"experiment": {
66+
longform: "experiment",
67+
},
68+
"force": {
69+
longform: "force",
70+
shorthand: "f",
71+
},
72+
"no-color": {
73+
longform: "no-color",
74+
},
75+
"runtime": {
76+
longform: "runtime",
77+
shorthand: "r",
78+
hidden: true,
79+
},
80+
"skip-update": {
81+
longform: "skip-update",
82+
shorthand: "s",
83+
},
84+
"slackdev": {
85+
longform: "slackdev",
86+
hidden: true,
87+
},
88+
"team": {
89+
longform: "team",
90+
shorthand: "w",
91+
},
92+
"token": {
93+
longform: "token",
94+
},
95+
"verbose": {
96+
longform: "verbose",
97+
shorthand: "v",
98+
},
5499
}
55-
for _, name := range flagNames {
56-
f := cmd.PersistentFlags().Lookup(name)
57-
assert.NotNil(t, f, "flag %s should be registered", name)
100+
for name, tc := range tests {
101+
t.Run(name, func(t *testing.T) {
102+
f := cmd.PersistentFlags().Lookup(tc.longform)
103+
assert.NotNil(t, f, "flag %s should be registered", tc.longform)
104+
if tc.shorthand != "" {
105+
assert.Equal(t, tc.shorthand, f.Shorthand, "flag %s shorthand mismatch", tc.longform)
106+
}
107+
assert.Equal(t, tc.hidden, f.Hidden, "flag %s hidden mismatch", tc.longform)
108+
})
58109
}
59-
60-
// Verify hidden flags
61-
assert.True(t, cmd.PersistentFlags().Lookup("apihost").Hidden)
62-
assert.True(t, cmd.PersistentFlags().Lookup("slackdev").Hidden)
63110
}
64111

65112
func TestDeprecatedFlagSubstitutions(t *testing.T) {

0 commit comments

Comments
 (0)