Skip to content

Commit a7bc37e

Browse files
committed
fix: prevent data race
1 parent 23356f6 commit a7bc37e

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

cli/errors_test.go

+14-6
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ type commandErrorCase struct {
2424
func TestErrorExamples(t *testing.T) {
2525
t.Parallel()
2626

27-
var root cli.RootCmd
28-
rootCmd, err := root.Command(root.AGPL())
29-
require.NoError(t, err)
27+
rootCmd := getRoot(t)
3028

3129
var cases []commandErrorCase
3230

@@ -52,8 +50,7 @@ ExtractCommandPathsLoop:
5250

5351
var outBuf bytes.Buffer
5452

55-
rootCmd, err := root.Command(root.AGPL())
56-
require.NoError(t, err)
53+
rootCmd := getRoot(t)
5754

5855
inv, _ := clitest.NewWithCommand(t, rootCmd, tt.Cmd...)
5956
inv.Stderr = &outBuf
@@ -65,7 +62,7 @@ ExtractCommandPathsLoop:
6562
inv.Stdin = os.Stdin
6663
}
6764

68-
err = inv.Run()
65+
err := inv.Run()
6966

7067
errFormatter := cli.ExportNewPrettyErrorFormatter(&outBuf, false)
7168
cli.ExportFormat(errFormatter, err)
@@ -84,3 +81,14 @@ func extractCommandPaths(cmdPath []string, cmds []*serpent.Command) [][]string {
8481
}
8582
return cmdPaths
8683
}
84+
85+
// Must return a fresh instance of cmds each time.
86+
func getRoot(t *testing.T) *serpent.Command {
87+
t.Helper()
88+
89+
var root cli.RootCmd
90+
rootCmd, err := root.Command(root.AGPL())
91+
require.NoError(t, err)
92+
93+
return rootCmd
94+
}

0 commit comments

Comments
 (0)