Skip to content

Commit 0a9d455

Browse files
committed
fix: reset shared rootCmd flag state between tests
`rootCmd`` is a package-level variable in interal/cli/root.go, shared across all tests in the cli package. Cobra does not reset flag values or their Changed state between `Execute()` calls, so a flag set by one test leaks into the next. This commit adds a `resetCmd` helper that recursively walks the command tree and restores each flag to its default value and `Changed=false`. Let's call it at the start of each test that invokes `rootCmd.Execute()`.
1 parent d70af89 commit 0a9d455

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

internal/cli/auth_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,24 @@ import (
88
"testing"
99

1010
"github.com/git-pkgs/forge/internal/config"
11+
"github.com/spf13/cobra"
12+
"github.com/spf13/pflag"
1113
)
1214

15+
// resetCmd resets flag values and Changed state across the command tree so
16+
// tests using the shared rootCmd don't leak flag state into each other.
17+
func resetCmd(cmd *cobra.Command) {
18+
reset := func(f *pflag.Flag) {
19+
f.Changed = false
20+
_ = f.Value.Set(f.DefValue)
21+
}
22+
cmd.Flags().VisitAll(reset)
23+
cmd.PersistentFlags().VisitAll(reset)
24+
for _, sub := range cmd.Commands() {
25+
resetCmd(sub)
26+
}
27+
}
28+
1329
func TestAuthCmd(t *testing.T) {
1430
cmd := authCmd
1531
if cmd.Use != "auth" {
@@ -36,6 +52,7 @@ func TestAuthCmd(t *testing.T) {
3652
}
3753

3854
func TestAuthLoginRequiresDomainNonInteractive(t *testing.T) {
55+
resetCmd(rootCmd)
3956
// Replace stdin with a pipe so term.IsTerminal returns false
4057
origStdin := os.Stdin
4158
r, w, _ := os.Pipe()
@@ -58,6 +75,7 @@ func TestAuthLoginRequiresDomainNonInteractive(t *testing.T) {
5875
}
5976

6077
func TestAuthLoginNonInteractive(t *testing.T) {
78+
resetCmd(rootCmd)
6179
dir := t.TempDir()
6280
t.Setenv("XDG_CONFIG_HOME", dir)
6381
config.ResetCache()
@@ -96,6 +114,7 @@ func TestAuthLoginNonInteractive(t *testing.T) {
96114
}
97115

98116
func TestAuthLoginTokenCmd(t *testing.T) {
117+
resetCmd(rootCmd)
99118
dir := t.TempDir()
100119
t.Setenv("XDG_CONFIG_HOME", dir)
101120
config.ResetCache()
@@ -125,6 +144,7 @@ func TestAuthLoginTokenCmd(t *testing.T) {
125144
}
126145

127146
func TestAuthLoginTokenAndTokenCmdMutuallyExclusive(t *testing.T) {
147+
resetCmd(rootCmd)
128148
var buf bytes.Buffer
129149
rootCmd.SetOut(&buf)
130150
rootCmd.SetErr(&buf)
@@ -141,6 +161,7 @@ func TestAuthLoginTokenAndTokenCmdMutuallyExclusive(t *testing.T) {
141161
}
142162

143163
func TestAuthStatus(t *testing.T) {
164+
resetCmd(rootCmd)
144165
dir := t.TempDir()
145166
t.Setenv("XDG_CONFIG_HOME", dir)
146167
config.ResetCache()
@@ -172,6 +193,7 @@ token = some_token
172193
}
173194

174195
func TestAuthStatusWithTokenCmd(t *testing.T) {
196+
resetCmd(rootCmd)
175197
dir := t.TempDir()
176198
t.Setenv("XDG_CONFIG_HOME", dir)
177199
config.ResetCache()

0 commit comments

Comments
 (0)