MCPcopy Index your code
hub / github.com/coder/coder / TestGoldenFile

Function TestGoldenFile

cli/clitest/golden.go:171–193  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

169// TestGoldenFile will test the given bytes slice input against the
170// golden file with the given file name, optionally using the given replacements.
171func 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

Callers 7

TestServerYAMLConfigFunction · 0.92
TestProvisioners_GoldenFunction · 0.92
Test_TaskLogs_GoldenFunction · 0.92
TestSyncCommands_GoldenFunction · 0.92
TestErrorExamplesFunction · 0.92
TestCommandHelpFunction · 0.85

Calls 6

normalizeGoldenFileFunction · 0.85
FatalMethod · 0.80
LogfMethod · 0.65
WriteFileMethod · 0.65
ReadFileMethod · 0.65
EmptyMethod · 0.45

Tested by

no test coverage detected