Skip to content

Commit 56c3123

Browse files
committed
Make assert and golden compatible with other golden packages
1 parent a80f057 commit 56c3123

3 files changed

Lines changed: 27 additions & 7 deletions

File tree

golden/golden.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Golden files can be automatically updated to match new values by running
66
`go test pkgname -update`. To ensure the update is correct
77
compare the diff of the old expected value to the new expected value.
88
*/
9-
package golden // import "gotest.tools/v3/golden"
9+
package golden
1010

1111
import (
1212
"bytes"
@@ -44,7 +44,7 @@ var NormalizeCRLFToLF = os.Getenv("GOTESTTOOLS_GOLDEN_NormalizeCRLFToLF") != "fa
4444

4545
// FlagUpdate returns true when the -update flag has been set.
4646
func FlagUpdate() bool {
47-
return source.Update
47+
return source.IsUpdate()
4848
}
4949

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

180180
func update(filename string, actual []byte) error {
181-
if !source.Update {
181+
if !source.IsUpdate() {
182182
return nil
183183
}
184184
if dir := filepath.Dir(Path(filename)); dir != "." {

internal/assert/result.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func RunComparison(
2626
return true
2727
}
2828

29-
if source.Update {
29+
if source.IsUpdate() {
3030
if updater, ok := result.(updateExpected); ok {
3131
const stackIndex = 3 // Assert/Check, assert, RunComparison
3232
err := updater.UpdatedExpected(stackIndex)

internal/source/update.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,32 @@ import (
1414
"strings"
1515
)
1616

17-
// Update is set by the -update flag. It indicates the user running the tests
18-
// would like to update any golden values.
17+
// IsUpdate is returns true if the -update flag is set. It indicates the user
18+
// running the tests would like to update any golden values.
19+
func IsUpdate() bool {
20+
if Update {
21+
return true
22+
}
23+
return flag.Lookup("update").Value.(flag.Getter).Get().(bool)
24+
}
25+
26+
// Update is a shim for testing, and for compatibility with the old -update-golden
27+
// flag.
1928
var Update bool
2029

2130
func init() {
22-
flag.BoolVar(&Update, "update", false, "update golden values")
31+
if f := flag.Lookup("update"); f != nil {
32+
getter, ok := f.Value.(flag.Getter)
33+
msg := "some other package defined an incompatible -update flag, expected a flag.Bool"
34+
if !ok {
35+
panic(msg)
36+
}
37+
if _, ok := getter.Get().(bool); !ok {
38+
panic(msg)
39+
}
40+
return
41+
}
42+
flag.Bool("update", false, "update golden values")
2343
}
2444

2545
// ErrNotFound indicates that UpdateExpectedValue failed to find the

0 commit comments

Comments
 (0)