Skip to content

Commit

Permalink
Merge pull request #271 from dnephin/gate-update-flag
Browse files Browse the repository at this point in the history
Make assert and golden packages compatible with other golden packages
  • Loading branch information
dnephin committed Jul 30, 2023
2 parents a80f057 + 56c3123 commit e15fa27
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
6 changes: 3 additions & 3 deletions golden/golden.go
Expand Up @@ -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"
Expand Down Expand Up @@ -44,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 source.Update
return source.IsUpdate()
}

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

func update(filename string, actual []byte) error {
if !source.Update {
if !source.IsUpdate() {
return nil
}
if dir := filepath.Dir(Path(filename)); dir != "." {
Expand Down
2 changes: 1 addition & 1 deletion internal/assert/result.go
Expand Up @@ -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)
Expand Down
26 changes: 23 additions & 3 deletions internal/source/update.go
Expand Up @@ -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
Expand Down

0 comments on commit e15fa27

Please sign in to comment.