(t *testing.T, gotPath, wantGoldenPath string)
| 55 | } |
| 56 | |
| 57 | func cmpGoldenFile(t *testing.T, gotPath, wantGoldenPath string) { |
| 58 | got, err := os.ReadFile(gotPath) |
| 59 | if err != nil { |
| 60 | t.Fatal("got error reading generated file:", err) |
| 61 | } |
| 62 | if *update { |
| 63 | err = os.WriteFile(wantGoldenPath, got, 0o666) |
| 64 | if err != nil { |
| 65 | t.Error("got error updating golden file:", err) |
| 66 | } |
| 67 | } |
| 68 | golden, err := os.ReadFile(wantGoldenPath) |
| 69 | if err != nil { |
| 70 | t.Fatal("got error reading golden file:", err) |
| 71 | } |
| 72 | diff := cmp.Diff(golden, got) |
| 73 | if diff != "" { |
| 74 | t.Errorf(strings.TrimSpace(` |
| 75 | got wrong file contents (-%s +%s): |
| 76 | |
| 77 | %s |
| 78 | If the new file is correct, you can update the golden file with: |
| 79 | |
| 80 | go test -run "^%s$" -update`), |
| 81 | filepath.Base(wantGoldenPath), filepath.Base(gotPath), |
| 82 | diff, t.Name()) |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | var ( |
| 87 | locker = &lockmock{} |
no test coverage detected