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

Function normalizeGoldenFile

cli/clitest/golden.go:198–234  ·  view source on GitHub ↗

normalizeGoldenFile replaces any strings that are system or timing dependent with a placeholder so that the golden files can be compared with a simple equality check.

(t *testing.T, byt []byte)

Source from the content-addressed store, hash-verified

196// with a placeholder so that the golden files can be compared with a simple
197// equality check.
198func normalizeGoldenFile(t *testing.T, byt []byte) []byte {
199 // Replace any timestamps with a placeholder.
200 byt = timestampRegex.ReplaceAll(byt, []byte(pad("[timestamp]", 20)))
201
202 homeDir, err := os.UserHomeDir()
203 require.NoError(t, err)
204
205 configDir := config.DefaultDir()
206 byt = bytes.ReplaceAll(byt, []byte(configDir), []byte("~/.config/coderv2"))
207
208 byt = bytes.ReplaceAll(byt, []byte(codersdk.DefaultCacheDir()), []byte("[cache dir]"))
209
210 // The home directory changes depending on the test environment.
211 byt = bytes.ReplaceAll(byt, []byte(homeDir), []byte("~"))
212
213 // Normalize the temp directory. os.TempDir() may include a trailing slash
214 // (macOS) or not (Linux/Windows), and the temp directory may be followed by
215 // more filepath elements with an OS-specific separator. We handle all cases
216 // by replacing tempdir+separator first, then tempdir alone.
217 tempDir := filepath.Clean(os.TempDir())
218 byt = bytes.ReplaceAll(byt, []byte(tempDir+string(filepath.Separator)), []byte("/tmp/"))
219 byt = bytes.ReplaceAll(byt, []byte(tempDir), []byte("/tmp"))
220 // Clean up trailing slash when temp dir is used standalone (e.g., "/tmp/)" -> "/tmp)").
221 byt = bytes.ReplaceAll(byt, []byte("/tmp/)"), []byte("/tmp)"))
222
223 for _, r := range []struct {
224 old string
225 new string
226 }{
227 {"\r\n", "\n"},
228 {`~\.cache\coder`, "~/.cache/coder"},
229 {`C:\Users\RUNNER~1\AppData\Local\Temp`, "/tmp"},
230 } {
231 byt = bytes.ReplaceAll(byt, []byte(r.old), []byte(r.new))
232 }
233 return byt
234}
235
236func extractVisibleCommandPaths(cmdPath []string, cmds []*serpent.Command) [][]string {
237 var cmdPaths [][]string

Callers 1

TestGoldenFileFunction · 0.85

Calls 4

DefaultDirFunction · 0.92
DefaultCacheDirFunction · 0.92
padFunction · 0.70
TempDirMethod · 0.65

Tested by 1

TestGoldenFileFunction · 0.68