From 16c4393f6a1a2a7a0fdddae9bfdbff036d6a5805 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Thu, 13 Jul 2023 21:55:39 -0400 Subject: [PATCH] Make assert and golden compatible with other golden packages --- golden/golden.go | 2 +- internal/assert/result.go | 2 +- internal/source/update.go | 26 +++++++++++++++++++++++--- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/golden/golden.go b/golden/golden.go index ce96ba1..4dd97fe 100644 --- a/golden/golden.go +++ b/golden/golden.go @@ -6,7 +6,7 @@ Golden files can be automatically updated to match new values by running `go test pkgname -update`. To ensure the update is correct compare the diff of the old expected value to the new expected value. */ -package golden // import "gotest.tools/v3/golden" +package golden import ( "bytes" diff --git a/internal/assert/result.go b/internal/assert/result.go index 3603206..bb8741e 100644 --- a/internal/assert/result.go +++ b/internal/assert/result.go @@ -26,7 +26,7 @@ func RunComparison( return true } - if source.Update { + if source.IsUpdate() { if updater, ok := result.(updateExpected); ok { const stackIndex = 3 // Assert/Check, assert, RunComparison err := updater.UpdatedExpected(stackIndex) diff --git a/internal/source/update.go b/internal/source/update.go index f2006aa..5591bff 100644 --- a/internal/source/update.go +++ b/internal/source/update.go @@ -14,12 +14,32 @@ import ( "strings" ) -// Update is set by the -update flag. It indicates the user running the tests -// would like to update any golden values. +// IsUpdate is returns true if the -update flag is set. It indicates the user +// running the tests would like to update any golden values. +func IsUpdate() bool { + if Update { + return true + } + return flag.Lookup("update").Value.(flag.Getter).Get().(bool) +} + +// Update is a shim for testing, and for compatibility with the old -update-golden +// flag. var Update bool func init() { - flag.BoolVar(&Update, "update", false, "update golden values") + if f := flag.Lookup("update"); f != nil { + getter, ok := f.Value.(flag.Getter) + msg := "some other package defined an incompatible -update flag, expected a flag.Bool" + if !ok { + panic(msg) + } + if _, ok := getter.Get().(bool); !ok { + panic(msg) + } + return + } + flag.Bool("update", false, "update golden values") } // ErrNotFound indicates that UpdateExpectedValue failed to find the