Skip to content

Commit

Permalink
Move update flag to internal/source package
Browse files Browse the repository at this point in the history
So it can be used by other packages.
  • Loading branch information
dnephin committed May 29, 2022
1 parent 814f970 commit 2686168
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
10 changes: 4 additions & 6 deletions golden/golden.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@ import (
"gotest.tools/v3/assert"
"gotest.tools/v3/assert/cmp"
"gotest.tools/v3/internal/format"
"gotest.tools/v3/internal/source"
)

var flagUpdate bool

func init() {
flag.BoolVar(&flagUpdate, "update", false, "update golden files")
flag.BoolVar(&flagUpdate, "test.update-golden", false, "deprecated flag")
flag.BoolVar(&source.Update, "test.update-golden", false, "deprecated flag")
}

type helperT interface {
Expand All @@ -46,7 +44,7 @@ var NormalizeCRLFToLF = os.Getenv("GOTESTTOOLS_GOLDEN_NormalizeCRLFToLF") != "fa

// FlagUpdate returns true when the -update flag has been set.
func FlagUpdate() bool {
return flagUpdate
return source.Update
}

// Open opens the file in ./testdata
Expand Down Expand Up @@ -180,7 +178,7 @@ func compare(actual []byte, filename string) (cmp.Result, []byte) {
}

func update(filename string, actual []byte) error {
if !flagUpdate {
if !source.Update {
return nil
}
if dir := filepath.Dir(Path(filename)); dir != "." {
Expand Down
7 changes: 4 additions & 3 deletions golden/golden_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"gotest.tools/v3/assert"
"gotest.tools/v3/assert/cmp"
"gotest.tools/v3/fs"
"gotest.tools/v3/internal/source"
)

type fakeT struct {
Expand Down Expand Up @@ -190,10 +191,10 @@ func TestGoldenAssertBytes(t *testing.T) {
}

func setUpdateFlag(t *testing.T) func() {
orig := flagUpdate
flagUpdate = true
orig := source.Update
source.Update = true
undo := func() {
flagUpdate = orig
source.Update = orig
}
t.Cleanup(undo)
return undo
Expand Down
11 changes: 11 additions & 0 deletions internal/source/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package source

import "flag"

// Update is set by the -update flag. It indicates the user running the tests
// would like to update any golden values.
var Update bool

func init() {
flag.BoolVar(&Update, "update", false, "update golden files")
}

0 comments on commit 2686168

Please sign in to comment.