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

golden: accept -update for updating files #235

Merged
merged 1 commit into from May 3, 2022
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
11 changes: 8 additions & 3 deletions golden/golden.go
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
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