Skip to content

Commit a8ee25d

Browse files
committed
Add WithUpdateEnv option to protect fixtures from blanket updates
1 parent 7fc86f4 commit a8ee25d

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

testutil/golden.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ func CompareWithFixture(t *testing.T, output interface{}, opts ...option) string
2929
t.Helper()
3030
options := &options{
3131
Extension: ".yaml",
32+
UpdateEnv: "UPDATE",
3233
}
3334
for _, opt := range opts {
3435
opt(options)
@@ -52,7 +53,7 @@ func CompareWithFixture(t *testing.T, output interface{}, opts ...option) string
5253
if err != nil {
5354
t.Fatalf("failed to get absolute path to testdata file: %v", err)
5455
}
55-
if os.Getenv("UPDATE") != "" {
56+
if os.Getenv(options.UpdateEnv) != "" {
5657
if err := os.MkdirAll(filepath.Dir(golden), 0755); err != nil {
5758
t.Fatalf("failed to create fixture directory: %v", err)
5859
}
@@ -77,7 +78,8 @@ type options struct {
7778
Suffix string
7879
Extension string
7980

80-
SubDir string
81+
SubDir string
82+
UpdateEnv string
8183
}
8284

8385
type option func(*options)
@@ -100,6 +102,12 @@ func WithSubDir(subDir string) option {
100102
}
101103
}
102104

105+
func WithUpdateEnv(env string) option {
106+
return func(opts *options) {
107+
opts.UpdateEnv = env
108+
}
109+
}
110+
103111
// golden determines the golden file to use
104112
func golden(t *testing.T, opts *options) (string, error) {
105113
if opts.Extension == "" {

0 commit comments

Comments
 (0)