assertDOTGraph requires that the graph's DOT representation matches the golden file
(t *testing.T, graph *testGraph, goldenName string)
| 61 | |
| 62 | // assertDOTGraph requires that the graph's DOT representation matches the golden file |
| 63 | func assertDOTGraph(t *testing.T, graph *testGraph, goldenName string) { |
| 64 | t.Helper() |
| 65 | |
| 66 | dot, err := graph.ToDOT(goldenName) |
| 67 | require.NoError(t, err) |
| 68 | |
| 69 | goldenFile := filepath.Join("testdata", goldenName+".golden") |
| 70 | if *UpdateGoldenFiles { |
| 71 | t.Logf("update golden file for: %q: %s", goldenName, goldenFile) |
| 72 | err := os.MkdirAll(filepath.Dir(goldenFile), 0o755) |
| 73 | require.NoError(t, err, "want no error creating golden file directory") |
| 74 | err = os.WriteFile(goldenFile, []byte(dot), 0o600) |
| 75 | require.NoError(t, err, "update golden file") |
| 76 | } |
| 77 | |
| 78 | expected, err := os.ReadFile(goldenFile) |
| 79 | require.NoError(t, err, "read golden file, run \"make gen/golden-files\" and commit the changes") |
| 80 | |
| 81 | // Normalize line endings for cross-platform compatibility |
| 82 | expected = normalizeLineEndings(expected) |
| 83 | normalizedDot := normalizeLineEndings([]byte(dot)) |
| 84 | |
| 85 | assert.Empty(t, cmp.Diff(string(expected), string(normalizedDot)), "golden file mismatch (-want +got): %s, run \"make gen/golden-files\", verify and commit the changes", goldenFile) |
| 86 | } |
| 87 | |
| 88 | // normalizeLineEndings ensures that all line endings are normalized to \n. |
| 89 | // Required for Windows compatibility. |