From: David Symonds Date: Thu, 7 Jun 2018 10:25:10 +0000 (+1000) Subject: cmd/cover: add test for HTML output X-Git-Tag: go1.11beta1~180 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=19f3794c00252db3363eb649bc73bad66a2c583a;p=gostls13.git cmd/cover: add test for HTML output This adds a case for what was fixed in 4fe688c 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 TryBot-Result: Gobot Gobot Reviewed-by: Brad Fitzpatrick --- diff --git a/src/cmd/cover/cover_test.go b/src/cmd/cover/cover_test.go index f20fbb4b71..a677ab67e9 100644 --- a/src/cmd/cover/cover_test.go +++ b/src/cmd/cover/cover_test.go @@ -5,6 +5,7 @@ package main_test import ( + "bufio" "bytes" "flag" "fmt" @@ -17,6 +18,7 @@ import ( "os/exec" "path/filepath" "regexp" + "runtime" "strings" "testing" ) @@ -36,6 +38,12 @@ var ( 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") @@ -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) { t.Helper() c.Stdout = os.Stdout diff --git a/src/cmd/cover/testdata/html/html.go b/src/cmd/cover/testdata/html/html.go new file mode 100644 index 0000000000..5c7b81f063 --- /dev/null +++ b/src/cmd/cover/testdata/html/html.go @@ -0,0 +1,18 @@ +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 diff --git a/src/cmd/cover/testdata/html/html.golden b/src/cmd/cover/testdata/html/html.golden new file mode 100644 index 0000000000..2a2abd65a5 --- /dev/null +++ b/src/cmd/cover/testdata/html/html.golden @@ -0,0 +1,10 @@ +// START f +func f() { + ch := make(chan int) + select { + case <-ch: + default: + } +} + +// END f diff --git a/src/cmd/cover/testdata/html/html_test.go b/src/cmd/cover/testdata/html/html_test.go new file mode 100644 index 0000000000..d52cf51149 --- /dev/null +++ b/src/cmd/cover/testdata/html/html_test.go @@ -0,0 +1,7 @@ +package html + +import "testing" + +func TestAll(t *testing.T) { + f() +}