coverage seems to show code as not covered, but code is actually executed

79 views
Skip to first unread message

TheDiveO

unread,
Feb 23, 2024, 8:39:20 AM2/23/24
to golang-nuts
I've noticed what looks at its surface that some code that is covered doesn't show up in the cover profile data. Unfortunately, I don't have a minimal example as I have no idea how to drill down. So please bear with my explanations and hopefully it's a problem between VT100 and chair, I just don't see it yet.

I see the same behavior with go 1.22.0 as well as 1.21.7.

This is the repository where I see the seemingly odd profile: https://github.com/thediveo/notwork (sic!)

I run all tests as follows with coverage, see also https://github.com/thediveo/notwork/blob/master/scripts/cov.sh, but this is the gist:

GOCOVERTMPDIR="$(mktemp -d)"
trap 'rm -rf -- "$GOCOVERTMPDIR"' EXIT
go test -cover -v -race -exec sudo -tags ${BUILDTAGS} -p=1 \
  -count=1 ./... -args -test.gocoverdir="$GOCOVERTMPDIR"
go tool covdata textfmt -i="$GOCOVERTMPDIR" -o=coverage.out
go tool cover -html=coverage.out -o=coverage.html

The problem now is in https://github.com/thediveo/notwork/blob/14cb6bc6ffc51704df84f95b88fdd0097c62bad6/link/link.go#L81, where the if branch is shown as uncovered:

if veth, ok := link.(*netlink.Veth); ok {
    Expect(rand.Read(randbytes)).Error().NotTo(HaveOccurred())
    peername := prefix + hex.EncodeToString(randbytes)
    veth.PeerName = peername
}


This coverage behavior should be hopefully reprodroducable by cloning the repository and running "make coverage" in its top-level directory.

So what am I doing wrong here?
Reply all
Reply to author
Forward
0 new messages