From 7f9263f93109861b70f5447a912ebf99f4ab0b45 Mon Sep 17 00:00:00 2001 From: David Dworken Date: Wed, 20 Dec 2023 14:31:08 -0800 Subject: [PATCH] Add assertion that requires that all goldens are used --- Makefile | 2 +- client/client_test.go | 3 +++ shared/testutils/testutils.go | 21 ++++++++++++++++++++- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index c23739e..b372783 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ test: ftest: go clean -testcache - TZ='America/Los_Angeles' HISHTORY_TEST=1 HISHTORY_SKIP_INIT_IMPORT=1 gotestsum --packages ./... --rerun-fails=0 --format testname -- -p 1 -run "$(FILTER)" -timeout 60m + HISHTORY_FILTERED_TEST=1 TZ='America/Los_Angeles' HISHTORY_TEST=1 HISHTORY_SKIP_INIT_IMPORT=1 gotestsum --packages ./... --rerun-fails=0 --format testname -- -p 1 -run "$(FILTER)" -timeout 60m acttest: act push -j test -e .github/push_event.json --reuse --container-architecture linux/amd64 diff --git a/client/client_test.go b/client/client_test.go index a0117a8..48ef78a 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -53,6 +53,9 @@ func TestMain(m *testing.M) { // Start the tests m.Run() + + // Teardown + testutils.AssertAllGoldensUsed() } var shellTesters []shellTester = []shellTester{bashTester{}, zshTester{}} diff --git a/shared/testutils/testutils.go b/shared/testutils/testutils.go index 261d1a5..5168b86 100644 --- a/shared/testutils/testutils.go +++ b/shared/testutils/testutils.go @@ -27,6 +27,7 @@ const ( ) var initialWd string +var usedGoldens map[string]bool func init() { initialWd = getInitialWd() @@ -358,9 +359,27 @@ 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()))) + } + } +} + 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 {