Skip to content

Commit

Permalink
fix(exp/golden): normalize line breaks to ensure tests pass on windows (
Browse files Browse the repository at this point in the history
  • Loading branch information
andreynering authored Feb 7, 2025
1 parent ac5dd4e commit 21c0278
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
13 changes: 12 additions & 1 deletion exp/golden/golden.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"flag"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"testing"
Expand Down Expand Up @@ -40,7 +41,8 @@ func RequireEqual(tb testing.TB, out []byte) {
tb.Fatal(err)
}

goldenStr := escapeSeqs(string(goldenBts))
goldenStr := normalizeWindowsLineBreaks(string(goldenBts))
goldenStr = escapeSeqs(goldenStr)
outStr := escapeSeqs(string(out))

diff := udiff.Unified("golden", "run", goldenStr, outStr)
Expand Down Expand Up @@ -69,3 +71,12 @@ func escapeSeqs(in string) string {
}
return strings.Join(s, "\n")
}

// normalizeWindowsLineBreaks replaces all \r\n with \n.
// This is needed because Git for Windows checks out with \r\n by default.
func normalizeWindowsLineBreaks(str string) string {
if runtime.GOOS == "windows" {
return strings.ReplaceAll(str, "\r\n", "\n")
}
return str
}
13 changes: 5 additions & 8 deletions exp/golden/golden_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,16 @@ package golden
import "testing"

func TestRequireEqualUpdate(t *testing.T) {
enableUpdate(t)
*update = true
RequireEqual(t, []byte("test"))
}

func TestRequireEqualNoUpdate(t *testing.T) {
*update = false
RequireEqual(t, []byte("test"))
}

func enableUpdate(tb testing.TB) {
tb.Helper()
previous := update
*update = true
tb.Cleanup(func() {
update = previous
})
func TestRequireWithLineBreaks(t *testing.T) {
*update = false
RequireEqual(t, []byte("foo\nbar\nbaz\n"))
}
3 changes: 3 additions & 0 deletions exp/golden/testdata/TestRequireWithLineBreaks.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
foo
bar
baz

0 comments on commit 21c0278

Please sign in to comment.