TestGoldenFile will test the given bytes slice input against the golden file with the given file name, optionally using the given replacements.
(t *testing.T, fileName string, actual []byte, replacements map[string]string)
| 169 | // TestGoldenFile will test the given bytes slice input against the |
| 170 | // golden file with the given file name, optionally using the given replacements. |
| 171 | func TestGoldenFile(t *testing.T, fileName string, actual []byte, replacements map[string]string) { |
| 172 | if len(actual) == 0 { |
| 173 | t.Fatal("no output") |
| 174 | } |
| 175 | |
| 176 | for k, v := range replacements { |
| 177 | actual = bytes.ReplaceAll(actual, []byte(k), []byte(v)) |
| 178 | } |
| 179 | |
| 180 | actual = normalizeGoldenFile(t, actual) |
| 181 | goldenPath := filepath.Join("testdata", strings.ReplaceAll(fileName, " ", "_")+".golden") |
| 182 | if *UpdateGoldenFiles { |
| 183 | t.Logf("update golden file for: %q: %s", fileName, goldenPath) |
| 184 | err := os.WriteFile(goldenPath, actual, 0o600) |
| 185 | require.NoError(t, err, "update golden file") |
| 186 | } |
| 187 | |
| 188 | expected, err := os.ReadFile(goldenPath) |
| 189 | require.NoError(t, err, "read golden file, run \"make gen/golden-files\" and commit the changes") |
| 190 | |
| 191 | expected = normalizeGoldenFile(t, expected) |
| 192 | assert.Empty(t, cmp.Diff(string(expected), string(actual)), "golden file mismatch (-want +got): %s, run \"make gen/golden-files\", verify and commit the changes", goldenPath) |
| 193 | } |
| 194 | |
| 195 | // normalizeGoldenFile replaces any strings that are system or timing dependent |
| 196 | // with a placeholder so that the golden files can be compared with a simple |
no test coverage detected