Skip to content

Commit

Permalink
Merge pull request #235 from dnephin/golden-flag
Browse files Browse the repository at this point in the history
golden: accept -update for updating files
  • Loading branch information
dnephin authored May 3, 2022
2 parents 7beca78 + 6e64c83 commit 13888f7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
11 changes: 8 additions & 3 deletions golden/golden.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ import (
"gotest.tools/v3/internal/format"
)

var flagUpdate = flag.Bool("test.update-golden", false, "update golden file")
var flagUpdate bool

func init() {
flag.BoolVar(&flagUpdate, "update", false, "update golden files")
flag.BoolVar(&flagUpdate, "test.update-golden", false, "deprecated flag")
}

type helperT interface {
Helper()
Expand All @@ -41,7 +46,7 @@ var NormalizeCRLFToLF = os.Getenv("GOTESTTOOLS_GOLDEN_NormalizeCRLFToLF") != "fa

// FlagUpdate returns true when the -test.update-golden flag has been set.
func FlagUpdate() bool {
return *flagUpdate
return flagUpdate
}

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

func update(filename string, actual []byte) error {
if !*flagUpdate {
if !flagUpdate {
return nil
}
if dir := filepath.Dir(Path(filename)); dir != "." {
Expand Down
27 changes: 13 additions & 14 deletions golden/golden_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ func TestGoldenAssertInvalidContent(t *testing.T) {
}

func TestGoldenAssertInvalidContentUpdate(t *testing.T) {
undo := setUpdateFlag()
defer undo()
setUpdateFlag(t)
filename, clean := setupGoldenFile(t, "content")
defer clean()

Expand Down Expand Up @@ -132,8 +131,7 @@ func TestGoldenAssertInDir(t *testing.T) {
func TestGoldenAssertInDir_UpdateGolden(t *testing.T) {
filename, clean := setupGoldenFileWithDir(t, "testdatasubdir", "foo")
defer clean()
unsetUpdateFlag := setUpdateFlag()
defer unsetUpdateFlag()
setUpdateFlag(t)

fakeT := new(fakeT)

Expand Down Expand Up @@ -167,8 +165,7 @@ func TestAssert_WithCarriageReturnInActual(t *testing.T) {
func TestAssert_WithCarriageReturnInActual_UpdateGolden(t *testing.T) {
filename, clean := setupGoldenFile(t, "")
defer clean()
unsetUpdateFlag := setUpdateFlag()
defer unsetUpdateFlag()
unsetUpdateFlag := setUpdateFlag(t)

fakeT := new(fakeT)
Assert(fakeT, "a\rfoo\r\nbar\r\n", filename)
Expand All @@ -192,10 +189,14 @@ func TestGoldenAssertBytes(t *testing.T) {
assert.Assert(t, !fakeT.Failed)
}

func setUpdateFlag() func() {
oldFlagUpdate := *flagUpdate
*flagUpdate = true
return func() { *flagUpdate = oldFlagUpdate }
func setUpdateFlag(t *testing.T) func() {
orig := flagUpdate
flagUpdate = true
undo := func() {
flagUpdate = orig
}
t.Cleanup(undo)
return undo
}

func setupGoldenFileWithDir(t *testing.T, dirname, content string) (string, func()) {
Expand Down Expand Up @@ -260,14 +261,12 @@ func TestBytesFailure(t *testing.T) {

func TestFlagUpdate(t *testing.T) {
assert.Assert(t, !FlagUpdate())
undo := setUpdateFlag()
defer undo()
setUpdateFlag(t)
assert.Assert(t, FlagUpdate())
}

func TestUpdate_CreatesPathsAndFile(t *testing.T) {
undo := setUpdateFlag()
defer undo()
setUpdateFlag(t)

dir := fs.NewDir(t, t.Name())

Expand Down

0 comments on commit 13888f7

Please sign in to comment.