package main_test
import (
+ "bufio"
"bytes"
"flag"
"fmt"
"os/exec"
"path/filepath"
"regexp"
+ "runtime"
"strings"
"testing"
)
coverInput = filepath.Join(testdata, "test_line.go")
coverOutput = filepath.Join(testdata, "test_cover.go")
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")
}
}
+// 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) {
t.Helper()
c.Stdout = os.Stdout