Skip to content

Commit f1add18

Browse files
iwdgogopherbot
authored andcommitted
testing: replace CRLF by LF on windows before comparing to the expected output
Fixes #51269 Change-Id: I06747db18ca078c1f1bda9b7bc60006f53191f4d Reviewed-on: https://go-review.googlesource.com/c/go/+/627035 LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> Reviewed-by: Michael Matloob <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent d311cc9 commit f1add18

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Tests that crlf in the output of examples are handled.
2+
# Verifies golang.org/issue/51269
3+
go test x_test.go
4+
5+
-- x_test.go --
6+
package x
7+
8+
import (
9+
"io"
10+
"fmt"
11+
"os"
12+
"runtime"
13+
)
14+
15+
func Example_lf() {
16+
fmt.Print("foo", "\n", "bar")
17+
// Output:
18+
// foo
19+
// bar
20+
}
21+
22+
func Example_println() {
23+
fmt.Println("foo")
24+
fmt.Println("bar")
25+
// Output:
26+
// foo
27+
// bar
28+
}
29+
30+
func Example_crlf() {
31+
if runtime.GOOS == "windows" {
32+
io.WriteString(os.Stdout, "foo\r\nbar\r\n")
33+
} else {
34+
io.WriteString(os.Stdout, "foo\nbar\n")
35+
}
36+
// Output:
37+
// foo
38+
// bar
39+
}

src/testing/example.go

+5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package testing
66

77
import (
88
"fmt"
9+
"runtime"
910
"slices"
1011
"strings"
1112
"time"
@@ -66,6 +67,10 @@ func (eg *InternalExample) processRunResult(stdout string, timeSpent time.Durati
6667
var fail string
6768
got := strings.TrimSpace(stdout)
6869
want := strings.TrimSpace(eg.Output)
70+
if runtime.GOOS == "windows" {
71+
got = strings.ReplaceAll(got, "\r\n", "\n")
72+
want = strings.ReplaceAll(want, "\r\n", "\n")
73+
}
6974
if eg.Unordered {
7075
if sortLines(got) != sortLines(want) && recovered == nil {
7176
fail = fmt.Sprintf("got:\n%s\nwant (unordered):\n%s\n", stdout, eg.Output)

0 commit comments

Comments
 (0)