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: when a panic occurs in a function call, cover still marks the whole basic block covered #16325

Closed
wadey opened this issue Jul 11, 2016 · 1 comment

Comments

@wadey
Copy link
Contributor

@wadey wadey commented Jul 11, 2016

go version go1.6.2 darwin/amd64
This is similar to #10185, but the issue still occurs if the panic happens inside of a function call.

A contrived example:

rand.go:

package covertest

import (
    "log"
    "math/rand"
)

func Rand(i int64) int64 {
    v := rand.Int63n(i)
    log.Println(v)
    return v
}

func MaybeRand(i int64) int64 {
    defer func() {
        recover()
    }()
    return Rand(i)
}

rand_test.go:

package covertest

import "testing"

func TestMaybeRand(t *testing.T) {
    if MaybeRand(0) != 0 {
        t.Error("expected zero")
    }
}

Coverage should be less than 100% because the log.Println inside of Rand is never called. But executing the tests with coverage shows:

coverage: 100.0% of statements

The issue is that the coverage blocks are not fine grained enough if a panic occurs in a function call.

@robpike
Copy link
Contributor

@robpike robpike commented Jul 11, 2016

This is a reasonable request but is impractical to fix given the way the cover tool works. It works line-by-line, file-by-file. To solve it would require a much more global analysis of the program or be much more expensive. In your example it would either need to examine the rand package to see that Int63 can panic, or put a counter on every line, which could be too expensive in time and memory (and would require a major redesign).

@robpike robpike closed this Jul 11, 2016
@golang golang locked and limited conversation to collaborators Jul 11, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.