Skip to content

Commit

Permalink
allow usage together with other golden packages (#45)
Browse files Browse the repository at this point in the history
* allow usage together with other golden packages

Other packages can also define an -update flag. In general we can't be
compatible with every possible conflicting flag, but for this specific
flag we assume that it's a boolean flag with the same meaning as ours.

My motivation for this change is usage of autogold along with
gotest.tools/v3/assert, which among other things, has its own golden
tests functionality.

---------

Co-authored-by: Stephen Gutekanst <stephen.gutekanst@gmail.com>
  • Loading branch information
tadeokondrak and slimsag authored Jul 14, 2023
1 parent 7741e9e commit f28e503
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
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

0 comments on commit f28e503

Please sign in to comment.