Go version
go version go1.22.5 darwin/arm64
Output of go env
in your module/workspace:
-
What did you do?
Go coverage reports seems to be an arbitrary rollercoaster when generics are involved.
Here's a report saying that a function in my package has no coverage:
https://app.codecov.io/github/karalabe/ssz/commit/f4f8d91d4d6d8a8b874f35934265b34b414a8c27/blob/codec.go#L70
And the exact same report calls that function with positive coverage:
https://app.codecov.io/github/karalabe/ssz/commit/f4f8d91d4d6d8a8b874f35934265b34b414a8c27/blob/tests/testtypes/consensus-spec-tests/gen_single_field_test_struct_ssz.go#L14
What did you see happen?
A call to function F is reported covered, but function F itself is reported uncovered.
What did you expect to see?
If the coverage tool reports a call to function F, I expect F to be reported as called.
Comment From: gabyhelp
Related Issues and Documentation
- Coverage incorrectly reported as 100% ( go test cover) #26760 (closed)
- cmd/cover: treats empty lambdas as being executed even when not executed #46825
- cmd/cover: incomplete coverage when using goto #16624 (closed)
- cmd/cover: go test -cover and go tool cover -func don't give the same results #20515 (closed)
- cmd/cover: regression in 1.20, cover tool no longer includes packages without tests #58770 (closed)
(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)
Comment From: karalabe
FWIW, most of the methods in those files are reported as uncovered, or parts of it covered and parts not, but the tests are driven by a very comprehensive suite, so I'm quite certain most - if not 100% even - lines should be reported covered in the package.
Comment From: karalabe
And I can demo it is wonky.
Add a print statement to the function:
diff --git a/codec.go b/codec.go
index 02fcae0..15fa686 100644
--- a/codec.go
+++ b/codec.go
@@ -5,6 +5,7 @@
package ssz
import (
+ "fmt"
"math/big"
"github.com/holiman/uint256"
@@ -68,6 +69,7 @@ func DefineBool[T ~bool](c *Codec, v *T) {
// DefineUint8 defines the next field as a uint8.
func DefineUint8[T ~uint8](c *Codec, n *T) {
+ fmt.Println("YOLO")
if c.enc != nil {
EncodeUint8(c.enc, *n)
return
Run the tests with coverage and see how many times that print is hit.
go test ./... -coverprofile cover.txt --covermode=atomic -v | grep YOLO | wc
128 128 640
Report the coverage for that line (72 based on the diff):
cat cover.txt | grep codec.go:7
github.com/karalabe/ssz/codec.go:71.44,73.18 2 0
github.com/karalabe/ssz/codec.go:73.18,76.3 2 0
github.com/karalabe/ssz/codec.go:77.2,77.18 1 0
github.com/karalabe/ssz/codec.go:77.18,80.3 2 0
Coverage HTML display:
Comment From: seankhliao
Do you have a self contained reproducer for the issue?
Comment From: karalabe
Does a 1.8GB repo count as self contained?
Comment From: karalabe
Figured it out, you need go test -coverpkg=./... ./...
...