Skip to content

Commit

Permalink
-update_golden creating empty files (#277)
Browse files Browse the repository at this point in the history
* confgenerator: -update_golden creating empty files

`updateOrCompareGolden` did not detect the case where a diff is empty
because the file doesn't exist yet. Added a check for the edge case that
creates the empty golden file if it doesn't exist yet.

* confgenerator: reword comment

The other comments don't use self-referring words like "we".

* confgenerator: combine file-writing logic branches

* confgenerator: handle ErrNotExist specifically

The previous implementation assumed any error occurs because the file
does not exist. Instead, check that the returned error is specifically
os.ErrNotExist.

* confgenerator: always write file if empty

To simplify the logic when detecting a golden update, treat an empty
`actual` as something that always needs to be written.

* confgenerator: clarify update golden condition

Added clarifying to the condition that decides whether to (over)write
the golden file.
  • Loading branch information
braydonk committed Dec 21, 2021
1 parent e36bc03 commit 6857f5c
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions confgenerator/confgenerator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package confgenerator_test

import (
"errors"
"flag"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -166,7 +167,7 @@ func readFileContent(t *testing.T, testName string, goos string, filePathFormat
filePath := fmt.Sprintf(filePathFormat, goos, testName)
rawExpectedConfig, err := ioutil.ReadFile(filePath)
if err != nil {
if *updateGolden && respectGolden {
if *updateGolden && respectGolden && errors.Is(err, os.ErrNotExist) {
// Tolerate the file not found error because we will overwrite it later anyway.
return []byte("")
} else {
Expand All @@ -181,16 +182,19 @@ func updateOrCompareGolden(t *testing.T, testName string, goos string, expectedB
expected := strings.ReplaceAll(string(expectedBytes), "\r\n", "\n")
actual = strings.ReplaceAll(actual, "\r\n", "\n")
goldenPath := fmt.Sprintf(path, goos, testName)
if diff := cmp.Diff(expected, actual); diff != "" {
if *updateGolden {
diff := cmp.Diff(expected, actual)
if *updateGolden {
// If there is a diff, or if the actual is empty (it may be due to the file
// not existing), write the golden file with the expected content.
if diff != "" || actual == "" {
// Update the expected to match the actual.
t.Logf("Detected -update_golden flag. Rewriting the %q golden file to apply the following diff\n%s.", goldenPath, cmp.Diff(actual, expected))
if err := ioutil.WriteFile(goldenPath, []byte(actual), 0644); err != nil {
t.Fatalf("error updating golden file at %q : %s", goldenPath, err)
}
} else {
t.Errorf("test %q: golden file at %s mismatch (-want +got):\n%s", testName, goldenPath, diff)
}
} else if diff != "" {
t.Errorf("test %q: golden file at %s mismatch (-want +got):\n%s", testName, goldenPath, diff)
}
}

Expand Down

0 comments on commit 6857f5c

Please sign in to comment.