Skip to content

Commit 5fdacfa

Browse files
committed
cmd/cover: remove use of diff in cover_test.go
It's non-portable, and the test isn't hard to write without diff. It still produces helpful output in case of trouble: --- FAIL: TestCoverHTML (0.75s) cover_test.go:325: line 4 differs: got: case &lt;-ch:<span class="cov0" title="0"></span> want: case &lt;-ch:<span class="cov0" xitle="0"></span> This makes the test operating-system independent. Change-Id: Iff35f00cb76ba89bc1b93db01c6f994e74341f4a Reviewed-on: https://go-review.googlesource.com/118795 Reviewed-by: Ian Lance Taylor <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 26727b8 commit 5fdacfa

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

src/cmd/cover/cover_test.go

+22-12
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"os/exec"
1919
"path/filepath"
2020
"regexp"
21-
"runtime"
2221
"strings"
2322
"testing"
2423
)
@@ -267,9 +266,6 @@ func TestCoverFunc(t *testing.T) {
267266
// Check that cover produces correct HTML.
268267
// Issue #25767.
269268
func TestCoverHTML(t *testing.T) {
270-
if _, err := exec.LookPath("diff"); err != nil {
271-
t.Skipf("skip test on %s: diff command is required", runtime.GOOS)
272-
}
273269
testenv.MustHaveGoBuild(t)
274270
if !*debug {
275271
defer os.Remove(testcover)
@@ -307,16 +303,30 @@ func TestCoverHTML(t *testing.T) {
307303
in = false
308304
}
309305
}
310-
if err := ioutil.WriteFile(htmlHTML, out.Bytes(), 0644); err != nil {
311-
t.Fatal(err)
306+
golden, err := ioutil.ReadFile(htmlGolden)
307+
if err != nil {
308+
t.Fatalf("reading golden file: %v", err)
312309
}
313-
diff := "diff"
314-
if runtime.GOOS == "plan9" {
315-
diff = "/bin/ape/diff"
310+
// Ignore white space differences.
311+
// Break into lines, then compare by breaking into words.
312+
goldenLines := strings.Split(string(golden), "\n")
313+
outLines := strings.Split(out.String(), "\n")
314+
// Compare at the line level, stopping at first different line so
315+
// we don't generate tons of output if there's an inserted or deleted line.
316+
for i, goldenLine := range goldenLines {
317+
if i > len(outLines) {
318+
t.Fatalf("output shorter than golden; stops before line %d: %s\n", i+1, goldenLine)
319+
}
320+
// Convert all white space to simple spaces, for easy comparison.
321+
goldenLine = strings.Join(strings.Fields(goldenLine), " ")
322+
outLine := strings.Join(strings.Fields(outLines[i]), " ")
323+
if outLine != goldenLine {
324+
t.Fatalf("line %d differs: got:\n\t%s\nwant:\n\t%s", i+1, outLine, goldenLine)
325+
}
326+
}
327+
if len(goldenLines) != len(outLines) {
328+
t.Fatalf("output longer than golden; first extra output line %d: %q\n", len(goldenLines), outLines[len(goldenLines)])
316329
}
317-
// diff -uw testdata/html/html.html testdata/html/html.golden
318-
cmd = exec.Command(diff, "-u", "-w", htmlHTML, htmlGolden)
319-
run(cmd, t)
320330
}
321331

322332
func run(c *exec.Cmd, t *testing.T) {

0 commit comments

Comments
 (0)