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

allow usage together with other golden packages #45

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions autogold.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
)

var (
update = flag.Bool("update", false, "update .golden files, leaving unused)")
clean = flag.Bool("clean", false, "remove unused .golden files (slightly slower)")
failOnUpdate = flag.Bool("fail-on-update", false, "If a .golden file is updated, fail the test")

Expand All @@ -26,9 +25,19 @@ var (
)

func init() {
// For compatibility with other packages that also define an -update parameter, only define the
// flag if it's not already defined.
if updateFlag := flag.Lookup("update"); updateFlag == nil {
flag.Bool("update", false, "update .golden files, leaving unused)")
}

color.NoColor = false
}

func update() bool {
return flag.Lookup("update").Value.(flag.Getter).Get().(bool)
}

// ExpectFile checks if got is equal to the saved `testdata/<test name>.golden` test file. If it is
// not, the test is failed.
//
Expand Down Expand Up @@ -131,7 +140,7 @@ func ExpectFile(t *testing.T, got interface{}, opts ...Option) {
os.Remove(outFile)
}
if diff != "" {
if *update {
if update() {
grabLock()
if _, err := os.Stat(dir); os.IsNotExist(err) {
if err := os.MkdirAll(dir, 0o700); err != nil {
Expand All @@ -142,7 +151,7 @@ func ExpectFile(t *testing.T, got interface{}, opts ...Option) {
t.Fatal(err)
}
}
if *failOnUpdate || !*update {
if *failOnUpdate || !update() {
t.Log(fmt.Errorf("mismatch (-want +got):\n%s", colorDiff(diff)))
t.FailNow()
}
Expand Down
4 changes: 2 additions & 2 deletions expect.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func Expect(want interface{}) Value {
}

// Update the test file if so desired.
if *update {
if update() {
// Acquire a file-level lock to prevent concurrent mutations to the _test.go file
// by parallel tests (whether in-process, or not.)
start = time.Now()
Expand All @@ -181,7 +181,7 @@ func Expect(want interface{}) Value {
t.Fatal(fmt.Errorf("autogold: %v", err))
}
}
if *failOnUpdate || !*update {
if *failOnUpdate || !update() {
writeProfile()
t.Log(fmt.Errorf("mismatch (-want +got):\n%s", colorDiff(diff)))
t.FailNow()
Expand Down
Loading