Commit 19f3794c authored by David Symonds's avatar David Symonds

cmd/cover: add test for HTML output

This adds a case for what was fixed in 4fe688c6 to prevent regression;
a follow-on change will address #25767.

Change-Id: Iced8cc10e2993ef7caf7e9c59ffbc7147d78ddd7
Reviewed-on: https://go-review.googlesource.com/116975
Run-TryBot: David Symonds <dsymonds@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
parent 17f54231
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
package main_test package main_test
import ( import (
"bufio"
"bytes" "bytes"
"flag" "flag"
"fmt" "fmt"
...@@ -17,6 +18,7 @@ import ( ...@@ -17,6 +18,7 @@ import (
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"regexp" "regexp"
"runtime"
"strings" "strings"
"testing" "testing"
) )
...@@ -36,6 +38,12 @@ var ( ...@@ -36,6 +38,12 @@ var (
coverInput = filepath.Join(testdata, "test_line.go") coverInput = filepath.Join(testdata, "test_line.go")
coverOutput = filepath.Join(testdata, "test_cover.go") coverOutput = filepath.Join(testdata, "test_cover.go")
coverProfile = filepath.Join(testdata, "profile.cov") coverProfile = filepath.Join(testdata, "profile.cov")
// The HTML test files are in a separate directory
// so they are a complete package.
htmlProfile = filepath.Join(testdata, "html", "html.cov")
htmlHTML = filepath.Join(testdata, "html", "html.html")
htmlGolden = filepath.Join(testdata, "html", "html.golden")
) )
var debug = flag.Bool("debug", false, "keep rewritten files for debugging") var debug = flag.Bool("debug", false, "keep rewritten files for debugging")
...@@ -256,6 +264,57 @@ func TestCoverFunc(t *testing.T) { ...@@ -256,6 +264,57 @@ func TestCoverFunc(t *testing.T) {
} }
} }
// 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)
defer os.Remove(htmlProfile)
defer os.Remove(htmlHTML)
}
// go build -o testcover
cmd := exec.Command(testenv.GoToolPath(t), "build", "-o", testcover)
run(cmd, t)
// go test -coverprofile testdata/html/html.cov cmd/cover/testdata/html
cmd = exec.Command(testenv.GoToolPath(t), "test", "-coverprofile", htmlProfile, "cmd/cover/testdata/html")
run(cmd, t)
// ./testcover -html testdata/html/html.cov -o testdata/html/html.html
cmd = exec.Command(testcover, "-html", htmlProfile, "-o", htmlHTML)
run(cmd, t)
// Extract the parts of the HTML with comment markers,
// and compare against a golden file.
entireHTML, err := ioutil.ReadFile(htmlHTML)
if err != nil {
t.Fatal(err)
}
var out bytes.Buffer
scan := bufio.NewScanner(bytes.NewReader(entireHTML))
in := false
for scan.Scan() {
line := scan.Text()
if strings.Contains(line, "// START") {
in = true
}
if in {
fmt.Fprintln(&out, line)
}
if strings.Contains(line, "// END") {
in = false
}
}
if err := ioutil.WriteFile(htmlHTML, out.Bytes(), 0644); err != nil {
t.Fatal(err)
}
// diff -ud testdata/html/html.html testdata/html/html.golden
cmd = exec.Command("diff", "-udw", htmlHTML, htmlGolden)
run(cmd, t)
}
func run(c *exec.Cmd, t *testing.T) { func run(c *exec.Cmd, t *testing.T) {
t.Helper() t.Helper()
c.Stdout = os.Stdout c.Stdout = os.Stdout
......
package html
// This file is tested by html_test.go.
// The comments below are markers for extracting the annotated source
// from the HTML output.
// This is a regression test for incorrect sorting of boundaries
// that coincide, specifically for empty select clauses.
// START f
func f() {
ch := make(chan int)
select {
case <-ch:
default:
}
}
// END f
// START f
func f() <span class="cov8" title="1">{
ch := make(chan int)
select </span>{
case &lt;-ch:<span class="cov0" title="0"></span>
default:<span class="cov8" title="1"></span>
}
}
// END f
package html
import "testing"
func TestAll(t *testing.T) {
f()
}
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