Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/cover: treats empty lambdas as being executed even when not executed #46825

Open
krader1961 opened this issue Jun 19, 2021 · 0 comments
Open

cmd/cover: treats empty lambdas as being executed even when not executed #46825

krader1961 opened this issue Jun 19, 2021 · 0 comments
Assignees
Labels
NeedsInvestigation

Comments

@krader1961
Copy link

@krader1961 krader1961 commented Jun 19, 2021

What version of Go are you using (go version)?

$ go version
go version go1.16.5 darwin/amd64

Does this issue reproduce with the latest release?

Yes

What did you do?

This problem came to my attention because I was surprised the coverage reported by go test -coverprofile=cover was significantly higher than that reported by the Codecov service. After carefully looking at a couple of files in the project which should have had identical coverage I came to the conclusion there appears to be an off by one bug, or something akin to it, in the go coverage calculations.

Create a directory with the following two files, run go mod example.com/coverage then go test -coverprofile=cover ./....

File x.go:

package coverage

type covered struct {
        f func()
}

var c covered

func BadCoverage() {
        c.f = func() {}
}

func GoodCoverage1() {
        c.f = func() { c = covered{} }
}

func GoodCoverage2() {
        c.f = func() {}
        c.f()
}

File x_test.go:

package coverage

import "testing"

func TestCoverage(t *testing.T) {
        BadCoverage()
        GoodCoverage1()
        GoodCoverage2()
}

What did you expect to see?

Correct coverage of 66.7%.

What did you see instead?

Incorrect coverage of 80% due to treating the lambda in the BadCoverage function as if it had been executed. Changing the body of the lambda to do anything at all "fixes" the coverage. Interestingly, the HTML produced by go tool cover -html=cover highlights the right-paren in red in the BadCoverage case. Which suggests a disconnect between the code highlighting and coverage calculation. Also, the incorrect value of 80% is also wrong, it seems to me, since it is treating the code as if 5/6 of the lines were executed which is 83.3%. It looks like it is treating the incorrect case as if 4/5 of the lines were executed. Which is another clue how the coverage data is being misinterpreted.

For the record, the cover data is (compare lines 3 and 5):

mode: set
example.com/t/x.go:9.20,10.15 1 1
example.com/t/x.go:10.16,10.17 0 0
example.com/t/x.go:13.22,14.15 1 1
example.com/t/x.go:14.15,14.32 1 0
example.com/t/x.go:17.22,18.15 1 1
example.com/t/x.go:19.2,19.7 1 1
example.com/t/x.go:18.16,18.17 0 1
@seankhliao seankhliao changed the title Test coverage of empty lambdas treats them as being executed even when not executed cmd/cover: treats empty lambdas as being executed even when not executed Jun 19, 2021
@seankhliao seankhliao added the NeedsInvestigation label Jun 19, 2021
@thanm thanm self-assigned this Sep 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation
Projects
None yet
Development

No branches or pull requests

3 participants