Skip to content

Commit

Permalink
Make assert and golden compatible with other golden packages
Browse files Browse the repository at this point in the history
  • Loading branch information
dnephin committed Jul 14, 2023
1 parent a80f057 commit 03626a8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
2 changes: 1 addition & 1 deletion golden/golden.go
Original file line number Diff line number Diff line change
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
2 changes: 1 addition & 1 deletion internal/assert/result.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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, _ := f.Value.(flag.Getter)
msg := "some other package defined an incompatible -update flag, expected a flag.Bool"
if getter == nil {
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 03626a8

Please sign in to comment.