Skip to content

Commit

Permalink
cmd/cover: add //line comment pointing to original file
Browse files Browse the repository at this point in the history
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: Ian Lance Taylor <iant@golang.org>
  • Loading branch information
rsc committed Nov 16, 2017
1 parent a8474c7 commit 2a39d1e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/cmd/cover/cover.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
27 changes: 27 additions & 0 deletions src/cmd/go/go_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions src/cmd/go/testdata/src/coverbad/p.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package p

func f() {
g()
}
5 changes: 5 additions & 0 deletions src/cmd/go/testdata/src/coverbad/p_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package p

import "testing"

func Test(t *testing.T) {}

0 comments on commit 2a39d1e

Please sign in to comment.