From 26861688bfadaa58112f7a949d196ec09c1eaee5 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Sun, 1 May 2022 14:33:27 -0400 Subject: [PATCH] Move update flag to internal/source package So it can be used by other packages. --- golden/golden.go | 10 ++++------ golden/golden_test.go | 7 ++++--- internal/source/update.go | 11 +++++++++++ 3 files changed, 19 insertions(+), 9 deletions(-) create mode 100644 internal/source/update.go diff --git a/golden/golden.go b/golden/golden.go index 72ca05f..47ea85f 100644 --- a/golden/golden.go +++ b/golden/golden.go @@ -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 { @@ -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 @@ -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 != "." { diff --git a/golden/golden_test.go b/golden/golden_test.go index 54e807c..3b0bd02 100644 --- a/golden/golden_test.go +++ b/golden/golden_test.go @@ -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 { @@ -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 diff --git a/internal/source/update.go b/internal/source/update.go new file mode 100644 index 0000000..f66fff9 --- /dev/null +++ b/internal/source/update.go @@ -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") +}