Skip to content
This repository was archived by the owner on Nov 17, 2021. It is now read-only.

Commit 1ac5ce3

Browse files
author
Steve Harris
committed
Reset flags set on RootCmd in "show" test
1 parent 7e5c15e commit 1ac5ce3

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

cmd/show_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,21 @@ import (
2323
"reflect"
2424
"testing"
2525

26+
"github.com/spf13/cobra"
27+
"github.com/spf13/pflag"
2628
"gopkg.in/yaml.v2"
2729
)
2830

31+
func resetFlagsOf(cmd *cobra.Command) {
32+
cmd.Flags().VisitAll(func(f *pflag.Flag) {
33+
if sv, ok := f.Value.(pflag.SliceValue); ok {
34+
sv.Replace(nil)
35+
} else {
36+
f.Value.Set(f.DefValue)
37+
}
38+
})
39+
}
40+
2941
func cmdOutput(t *testing.T, args []string) string {
3042
var buf bytes.Buffer
3143
RootCmd.SetOutput(&buf)
@@ -86,6 +98,7 @@ func TestShow(t *testing.T) {
8698
"-V", "anVar",
8799
"--ext-str-file", "filevar=" + filepath.FromSlash("../testdata/extvar.file"),
88100
})
101+
defer resetFlagsOf(RootCmd)
89102

90103
t.Log("output is", output)
91104
actual, err := parser(output)
@@ -136,6 +149,7 @@ func TestShowUsingExtVarFiles(t *testing.T) {
136149
"--tla-code-file", "sink=sink.jsonnet",
137150
"--ext-str-file", "filevar=var.txt",
138151
})
152+
defer resetFlagsOf(RootCmd)
139153

140154
t.Log("output is", output)
141155
var actual interface{}

0 commit comments

Comments
 (0)