"os/exec"
"path/filepath"
"regexp"
- "runtime"
"strings"
"testing"
)
// Check that cover produces correct HTML.
// Issue #25767.
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)
if !*debug {
defer os.Remove(testcover)
in = false
}
}
- if err := ioutil.WriteFile(htmlHTML, out.Bytes(), 0644); err != nil {
- t.Fatal(err)
+ golden, err := ioutil.ReadFile(htmlGolden)
+ if err != nil {
+ t.Fatalf("reading golden file: %v", err)
}
- diff := "diff"
- if runtime.GOOS == "plan9" {
- diff = "/bin/ape/diff"
+ // Ignore white space differences.
+ // Break into lines, then compare by breaking into words.
+ 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) {