Move test goldens to testdata/ directory to be more idiomatic, and add assertion that they're all actively being used by the tests (#154)

* Move golden files to testdata dir

* Add assertion that requires that all goldens are used

* Add map init so the map is non-nil

* Add print to confirm that tests finished running

* Remove colon so that this doesn't match against gotestsum looking for the substring "panic:", which can happen depending on how stdout is buffer

* Revert "Remove colon so that this doesn't match against gotestsum looking for the substring "panic:", which can happen depending on how stdout is buffer"

This reverts commit b44e53a17b.
This commit is contained in:
David Dworken
2023-12-21 17:06:46 -08:00
committed by GitHub
parent 516ee59791
commit 88f1c0168e
103 changed files with 26 additions and 2 deletions

View File

@ -27,9 +27,11 @@ const (
)
var initialWd string
var usedGoldens map[string]bool
func init() {
initialWd = getInitialWd()
usedGoldens = make(map[string]bool)
}
func getInitialWd() string {
@ -358,9 +360,28 @@ func persistLog() {
checkError(err)
}
func AssertAllGoldensUsed() {
if os.Getenv("HISHTORY_FILTERED_TEST") != "" {
return
}
goldensDir := path.Join(initialWd, "client/testdata/")
files, err := os.ReadDir(goldensDir)
if err != nil {
panic(fmt.Errorf("failed to list files in %s: %w", goldensDir, err))
}
for _, f := range files {
_, present := usedGoldens[path.Base(f.Name())]
if !present && !strings.Contains(f.Name(), "unittestTable-truncatedTable") {
panic(fmt.Errorf("golden file %v was never used", path.Base(f.Name())))
}
}
fmt.Println("Validated that all goldens in testdata/ were referenced!")
}
func CompareGoldens(t testing.TB, out, goldenName string) {
usedGoldens[goldenName] = true
out = normalizeHostnames(out)
goldenPath := path.Join(initialWd, "client/lib/goldens/", goldenName)
goldenPath := path.Join(initialWd, "client/testdata/", goldenName)
expected, err := os.ReadFile(goldenPath)
expected = []byte(normalizeHostnames(string(expected)))
if err != nil {