Skip to content

Commit 2a4aaee

Browse files
committed
test(cli): add unit tests for verbose and version shorthands
1 parent 660c7c8 commit 2a4aaee

1 file changed

Lines changed: 36 additions & 8 deletions

File tree

cmd/root_test.go

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,27 +58,55 @@ func TestRootCommand(t *testing.T) {
5858
// TestGlobalFlags tests that global flags are properly registered
5959
func TestGlobalFlags(t *testing.T) {
6060
tests := []struct {
61-
name string
62-
flagName string
61+
name string
62+
flagName string
63+
shorthand string
6364
}{
6465
{
65-
name: "config flag",
66-
flagName: "config",
66+
name: "config flag",
67+
flagName: "config",
68+
shorthand: "c",
6769
},
6870
{
69-
name: "verbose flag",
70-
flagName: "verbose",
71+
name: "verbose flag",
72+
flagName: "verbose",
73+
shorthand: "V", // Ensures verbose uses uppercase V
7174
},
7275
{
73-
name: "quiet flag",
74-
flagName: "quiet",
76+
name: "quiet flag",
77+
flagName: "quiet",
78+
shorthand: "q",
7579
},
7680
}
7781

7882
for _, tt := range tests {
7983
t.Run(tt.name, func(t *testing.T) {
8084
flag := rootCmd.PersistentFlags().Lookup(tt.flagName)
8185
require.NotNil(t, flag, "flag %s should be registered", tt.flagName)
86+
assert.Equal(t, tt.shorthand, flag.Shorthand, "flag %s should have shorthand %s", tt.flagName, tt.shorthand)
87+
})
88+
}
89+
}
90+
91+
// TestLocalFlags tests that root command specific (non-persistent) flags are properly registered
92+
func TestLocalFlags(t *testing.T) {
93+
tests := []struct {
94+
name string
95+
flagName string
96+
shorthand string
97+
}{
98+
{
99+
name: "version flag",
100+
flagName: "version",
101+
shorthand: "v", // Ensures version uses lowercase v
102+
},
103+
}
104+
105+
for _, tt := range tests {
106+
t.Run(tt.name, func(t *testing.T) {
107+
flag := rootCmd.Flags().Lookup(tt.flagName)
108+
require.NotNil(t, flag, "flag %s should be registered", tt.flagName)
109+
assert.Equal(t, tt.shorthand, flag.Shorthand, "flag %s should have shorthand %s", tt.flagName, tt.shorthand)
82110
})
83111
}
84112
}

0 commit comments

Comments
 (0)