Commit 5fdacfa8 authored by Rob Pike's avatar Rob Pike

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/118795Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 26727b84
...@@ -18,7 +18,6 @@ import ( ...@@ -18,7 +18,6 @@ import (
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"regexp" "regexp"
"runtime"
"strings" "strings"
"testing" "testing"
) )
...@@ -267,9 +266,6 @@ func TestCoverFunc(t *testing.T) { ...@@ -267,9 +266,6 @@ func TestCoverFunc(t *testing.T) {
// Check that cover produces correct HTML. // Check that cover produces correct HTML.
// Issue #25767. // Issue #25767.
func TestCoverHTML(t *testing.T) { func TestCoverHTML(t *testing.T) {
if _, err := exec.LookPath("diff"); err != nil {
t.Skipf("skip test on %s: diff command is required", runtime.GOOS)
}
testenv.MustHaveGoBuild(t) testenv.MustHaveGoBuild(t)
if !*debug { if !*debug {
defer os.Remove(testcover) defer os.Remove(testcover)
...@@ -307,16 +303,30 @@ func TestCoverHTML(t *testing.T) { ...@@ -307,16 +303,30 @@ func TestCoverHTML(t *testing.T) {
in = false in = false
} }
} }
if err := ioutil.WriteFile(htmlHTML, out.Bytes(), 0644); err != nil { golden, err := ioutil.ReadFile(htmlGolden)
t.Fatal(err) if err != nil {
t.Fatalf("reading golden file: %v", err)
} }
diff := "diff" // Ignore white space differences.
if runtime.GOOS == "plan9" { // Break into lines, then compare by breaking into words.
diff = "/bin/ape/diff" goldenLines := strings.Split(string(golden), "\n")
outLines := strings.Split(out.String(), "\n")
// Compare at the line level, stopping at first different line so
// we don't generate tons of output if there's an inserted or deleted line.
for i, goldenLine := range goldenLines {
if i > len(outLines) {
t.Fatalf("output shorter than golden; stops before line %d: %s\n", i+1, goldenLine)
}
// Convert all white space to simple spaces, for easy comparison.
goldenLine = strings.Join(strings.Fields(goldenLine), " ")
outLine := strings.Join(strings.Fields(outLines[i]), " ")
if outLine != goldenLine {
t.Fatalf("line %d differs: got:\n\t%s\nwant:\n\t%s", i+1, outLine, goldenLine)
}
}
if len(goldenLines) != len(outLines) {
t.Fatalf("output longer than golden; first extra output line %d: %q\n", len(goldenLines), outLines[len(goldenLines)])
} }
// diff -uw testdata/html/html.html testdata/html/html.golden
cmd = exec.Command(diff, "-u", "-w", htmlHTML, htmlGolden)
run(cmd, t)
} }
func run(c *exec.Cmd, t *testing.T) { func run(c *exec.Cmd, t *testing.T) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment