Skip to content

Commit

Permalink
fix(golden): always show escaped golden files when comparing the outputs
Browse files Browse the repository at this point in the history
This change makes `RequireEqual` always escape the control codes and
escape sequences from the diff output when comparing the golden file
with the output.

It also deprecates the `RequireEqualEscape` function, which was
previously used to escape the control codes and escape sequences from
the output before comparing it with the golden file.
  • Loading branch information
aymanbagabas committed Nov 18, 2024
1 parent 0af7d04 commit cc5c35c
Showing 1 changed file with 16 additions and 19 deletions.
35 changes: 16 additions & 19 deletions exp/golden/golden.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package golden

import (
"bytes"
"flag"
"os"
"path/filepath"
Expand All @@ -17,18 +16,15 @@ var update = flag.Bool("update", false, "update .golden files")
// RequireEqual is a helper function to assert the given output is
// the expected from the golden files, printing its diff in case it is not.
//
// Golden files contain the raw expected output of your tests, which can
// contain control codes and escape sequences. When comparing the output of
// your tests, [RequireEqual] will escape the control codes and sequences
// before comparing the output with the golden files.
//
// You can update the golden files by running your tests with the -update flag.
func RequireEqual(tb testing.TB, out []byte) {
RequireEqualEscape(tb, out, false)
}

// RequireEqualEscape is a helper function to assert the given output is
// the expected from the golden files, printing its diff in case it is not.
func RequireEqualEscape(tb testing.TB, out []byte, escapes bool) {
tb.Helper()

out = fixLineEndings(out)

golden := filepath.Join("testdata", tb.Name()+".golden")
if *update {
if err := os.MkdirAll(filepath.Dir(golden), 0o755); err != nil { //nolint: gomnd
Expand All @@ -44,25 +40,26 @@ func RequireEqualEscape(tb testing.TB, out []byte, escapes bool) {
tb.Fatal(err)
}

goldenBts = fixLineEndings(goldenBts)
goldenStr := string(goldenBts)
outStr := string(out)
if escapes {
goldenStr = escapesSeqs(goldenStr)
outStr = escapesSeqs(outStr)
}
goldenStr := escapeSeqs(string(goldenBts))
outStr := escapeSeqs(string(out))

diff := udiff.Unified("golden", "run", goldenStr, outStr)
if diff != "" {
tb.Fatalf("output does not match, expected:\n\n%s\n\ngot:\n\n%s\n\ndiff:\n\n%s", goldenStr, outStr, diff)
}
}

func fixLineEndings(in []byte) []byte {
return bytes.ReplaceAll(in, []byte("\r\n"), []byte{'\n'})
// RequireEqualEscape is a helper function to assert the given output is
// the expected from the golden files, printing its diff in case it is not.
//
// Deprecated: Use [RequireEqual] instead.
func RequireEqualEscape(tb testing.TB, out []byte, escapes bool) {
RequireEqual(tb, out)
}

func escapesSeqs(in string) string {
// escapeSeqs escapes control codes and escape sequences from the given string.
// The only preserved exception is the newline character.
func escapeSeqs(in string) string {
s := strings.Split(in, "\n")
for i, l := range s {
q := strconv.Quote(l)
Expand Down

0 comments on commit cc5c35c

Please sign in to comment.