Commit 2a39d1e9 authored by Russ Cox's avatar Russ Cox

cmd/cover: add //line comment pointing to original file

Now that cover does not modify the formatting of the original file
or add any newline characters, we can make it print a //line comment
pointing back at the original, and compiler errors and panics will
report accurate line numbers.

Fixes #6329.
Fixes #15757.

Change-Id: I7b0e386112c69beafe69e0d47c5f9e9abc87c0f5
Reviewed-on: https://go-review.googlesource.com/77151
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
parent a8474c79
......@@ -340,6 +340,7 @@ func annotate(name string) {
}
}
fmt.Fprintf(fd, "//line %s:1\n", name)
fd.Write(newContent)
// After printing the source tree, add some declarations for the counters etc.
......
......@@ -2430,6 +2430,33 @@ func TestCoveragePattern(t *testing.T) {
tg.run("test", "-coverprofile="+filepath.Join(tg.tempdir, "cover.out"), "-coverpkg=sleepy...", "-run=^$", "sleepy1")
}
func TestCoverageErrorLine(t *testing.T) {
tg := testgo(t)
defer tg.cleanup()
tg.parallel()
tg.makeTempdir()
tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata"))
tg.setenv("GOTMPDIR", tg.tempdir)
tg.runFail("test", "coverbad")
tg.grepStderr(`coverbad[\\/]p.go:4`, "did not find correct line number for error")
tg.grepStderrNot(regexp.QuoteMeta(tg.tempdir), "found temporary directory in error")
stderr := tg.getStderr()
tg.runFail("test", "-cover", "coverbad")
tg.grepStderr(`coverbad[\\/]p.go:4`, "did not find correct line number for error")
stderr2 := tg.getStderr()
// It's OK that stderr2 drops the character position in the error,
// because of the //line directive.
stderr = strings.Replace(stderr, "p.go:4:2:", "p.go:4:", -1)
if stderr != stderr2 {
t.Logf("test -cover changed error messages:\nbefore:\n%s\n\nafter:\n%s", stderr, stderr2)
t.Skip("golang.org/issue/22660")
t.FailNow()
}
}
func TestPluginNonMain(t *testing.T) {
wd, err := os.Getwd()
if err != nil {
......
package p
func f() {
g()
}
package p
import "testing"
func Test(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