Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make assert and golden packages compatible with other golden packages #271

Merged
merged 1 commit into from
Jul 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions 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 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
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, 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