| 379 | } |
| 380 | |
| 381 | func CompareGoldens(t testing.TB, out, goldenName string) { |
| 382 | recordUsingGolden(t, goldenName) |
| 383 | out = normalizeHostnames(out) |
| 384 | goldenPath := path.Join(initialWd, "client/testdata/", goldenName) |
| 385 | expected, err := os.ReadFile(goldenPath) |
| 386 | expected = []byte(normalizeHostnames(string(expected))) |
| 387 | if err != nil { |
| 388 | if os.IsNotExist(err) { |
| 389 | expected = []byte("ERR_FILE_NOT_FOUND:" + goldenPath) |
| 390 | } else { |
| 391 | require.NoError(t, err) |
| 392 | } |
| 393 | } |
| 394 | if diff := cmp.Diff(string(expected), out); diff != "" { |
| 395 | if err := os.Mkdir("/tmp/test-goldens", os.ModePerm); err != nil && !os.IsExist(err) { |
| 396 | log.Fatal(err) |
| 397 | } |
| 398 | require.NoError(t, os.WriteFile(path.Join("/tmp/test-goldens", goldenName), []byte(out), 0o644)) |
| 399 | if os.Getenv("HISHTORY_UPDATE_GOLDENS") == "" { |
| 400 | _, filename, line, _ := runtime.Caller(1) |
| 401 | t.Fatalf("hishtory golden mismatch for %s at %s:%d (-expected +got):\n%s\nactual=\n%s", goldenName, filename, line, diff, out) |
| 402 | } else { |
| 403 | require.NoError(t, os.WriteFile(goldenPath, []byte(out), 0o644)) |
| 404 | } |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | func normalizeHostnames(data string) string { |
| 409 | hostnames := []string{"Davids-MacBook-Air", "Davids-MacBook-Air.local", "ghaction-runner-hostname", "Davids-Air"} |