internal/report: fix bug in lint test update
Fixes a bug in which the "-update" flag caused all golden
files' copyright year to be updated (even if none of the
content had changed).
This is fixed by short-circuiting the update step if all
validation checks pass without an update.
Change-Id: I5d8b8a5b6af339faf0774a39442e76deff178afc
Reviewed-on: https://go-review.googlesource.com/c/vulndb/+/569617
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Damien Neil <dneil@google.com>
diff --git a/internal/report/lint_test.go b/internal/report/lint_test.go
index 8392a35..9c8bfd1 100644
--- a/internal/report/lint_test.go
+++ b/internal/report/lint_test.go
@@ -717,11 +717,16 @@
func updateAndCheckGolden(t *testing.T, test *lintTC, lints []string) {
if *updateGolden {
+ if errs := checkGoldenFile(t, test, lints); len(errs) == 0 {
+ return
+ }
if err := updateGoldenFile(t, test, lints); err != nil {
t.Error(err)
}
}
- checkGoldenFile(t, test, lints)
+ for _, err := range checkGoldenFile(t, test, lints) {
+ t.Error(err)
+ }
}
func updateGoldenFile(t *testing.T, tc *lintTC, lints []string) error {
@@ -752,25 +757,25 @@
}, newComment(t, tc))
}
-func checkGoldenFile(t *testing.T, tc *lintTC, lints []string) {
+func checkGoldenFile(t *testing.T, tc *lintTC, lints []string) []error {
t.Helper()
fpath := goldenFilename(t)
if _, err := os.Stat(fpath); err != nil {
- t.Errorf("golden file %s does not exist (re-run test with -update flag)", fpath)
- return
+ return []error{fmt.Errorf("golden file %s does not exist (re-run test with -update flag)", fpath)}
}
ar, err := txtar.ParseFile(fpath)
if err != nil {
- t.Error(err)
- return
+ return []error{err}
}
+ var errs []error
+
wantComment, gotComment := newComment(t, tc), string(ar.Comment)
if err := test.CheckComment(wantComment, gotComment); err != nil {
- t.Error(err)
+ errs = append(errs, err)
}
// Check that all expected files are present and have the correct contents.
@@ -782,7 +787,7 @@
want := f.Data
got := lintsToBytes(lints)
if diff := cmp.Diff(want, got); diff != "" {
- t.Errorf("%s: %s: mismatch (-want, +got):\n%s", fpath, af, diff)
+ errs = append(errs, fmt.Errorf("%s: %s: mismatch (-want, +got):\n%s", fpath, af, diff))
}
foundGolden = true
case af == reportFile:
@@ -793,19 +798,20 @@
continue
}
if diff := cmp.Diff(want, got); diff != "" {
- t.Errorf("%s: %s: mismatch (-want, +got):\n%s", fpath, af, diff)
+ errs = append(errs, fmt.Errorf("%s: %s: mismatch (-want, +got):\n%s", fpath, af, diff))
}
foundReport = true
default:
- t.Errorf("%s: unexpected archive file %s, expected one of (%q, %q)", fpath, af, reportFile, golden)
+ errs = append(errs, fmt.Errorf("%s: unexpected archive file %s, expected one of (%q, %q)", fpath, af, reportFile, golden))
}
}
if !foundReport {
- t.Errorf("%s: no report found (want archive file %q)", fpath, reportFile)
+ errs = append(errs, fmt.Errorf("%s: no report found (want archive file %q)", fpath, reportFile))
}
if !foundGolden {
- t.Errorf("%s: no golden found (want archive file %q)", fpath, golden)
+ errs = append(errs, fmt.Errorf("%s: no golden found (want archive file %q)", fpath, golden))
}
+ return errs
}
func testYAMLFilename(r *Report) string {