| 1 |
// Package golden provides a helper function to assert the output of tests. |
| 2 |
package golden |
| 3 |
|
| 4 |
import ( |
| 5 |
"flag" |
| 6 |
"os" |
| 7 |
"path/filepath" |
| 8 |
"runtime" |
| 9 |
"strconv" |
| 10 |
"strings" |
| 11 |
"testing" |
| 12 |
|
| 13 |
"github.com/aymanbagabas/go-udiff" |
| 14 |
) |
| 15 |
|
| 16 |
var update = flag.Bool("update", false, "update .golden files") |
| 17 |
|
| 18 |
// RequireEqual is a helper function to assert the given output is |
| 19 |
// the expected from the golden files, printing its diff in case it is not. |
| 20 |
// |
| 21 |
// Golden files contain the raw expected output of your tests, which can |
| 22 |
// contain control codes and escape sequences. When comparing the output of |
| 23 |
// your tests, [RequireEqual] will escape the control codes and sequences |
| 24 |
// before comparing the output with the golden files. |
| 25 |
// |
| 26 |
// You can update the golden files by running your tests with the -update flag. |
| 27 |
func RequireEqual[T []byte | string](tb testing.TB, out T) { |
| 28 |
tb.Helper() |
| 29 |
|
| 30 |
golden := filepath.Join("testdata", tb.Name()+".golden") |
| 31 |
if *update { |
| 32 |
if err := os.MkdirAll(filepath.Dir(golden), 0o750); err != nil { //nolint: mnd |
| 33 |
tb.Fatal(err) |
| 34 |
} |
| 35 |
if err := os.WriteFile(golden, []byte(out), 0o600); err != nil { //nolint: mnd |
| 36 |
tb.Fatal(err) |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
goldenBts, err := os.ReadFile(golden) |
| 41 |
if err != nil { |
| 42 |
tb.Fatal(err) |
| 43 |
} |
| 44 |
|
| 45 |
goldenStr := normalizeWindowsLineBreaks(string(goldenBts)) |
| 46 |
goldenStr = escapeSeqs(goldenStr) |
| 47 |
outStr := escapeSeqs(string(out)) |
| 48 |
|
| 49 |
diff := udiff.Unified("golden", "run", goldenStr, outStr) |
| 50 |
if diff != "" { |
| 51 |
tb.Fatalf("output does not match, expected:\n\n%s\n\ngot:\n\n%s\n\ndiff:\n\n%s", goldenStr, outStr, diff) |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
// RequireEqualEscape is a helper function to assert the given output is |
| 56 |
// the expected from the golden files, printing its diff in case it is not. |
| 57 |
// |
| 58 |
// Deprecated: Use [RequireEqual] instead. |
| 59 |
func RequireEqualEscape(tb testing.TB, out []byte, escapes bool) { //nolint:revive |
| 60 |
RequireEqual(tb, out) |
| 61 |
} |
| 62 |
|
| 63 |
// escapeSeqs escapes control codes and escape sequences from the given string. |
| 64 |
// The only preserved exception is the newline character. |
| 65 |
func escapeSeqs(in string) string { |
| 66 |
s := strings.Split(in, "\n") |
| 67 |
for i, l := range s { |
| 68 |
q := strconv.Quote(l) |
| 69 |
q = strings.TrimPrefix(q, `"`) |
| 70 |
q = strings.TrimSuffix(q, `"`) |
| 71 |
s[i] = q |
| 72 |
} |
| 73 |
return strings.Join(s, "\n") |
| 74 |
} |
| 75 |
|
| 76 |
// normalizeWindowsLineBreaks replaces all \r\n with \n. |
| 77 |
// This is needed because Git for Windows checks out with \r\n by default. |
| 78 |
func normalizeWindowsLineBreaks(str string) string { |
| 79 |
if runtime.GOOS == "windows" { |
| 80 |
return strings.ReplaceAll(str, "\r\n", "\n") |
| 81 |
} |
| 82 |
return str |
| 83 |
} |