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/go: go test -cover reports 0% coverage on pure struct packages #25492

Closed
egonk opened this issue May 22, 2018 · 11 comments
Closed

cmd/go: go test -cover reports 0% coverage on pure struct packages #25492

egonk opened this issue May 22, 2018 · 11 comments
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@egonk
Copy link

egonk commented May 22, 2018

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

go version go1.10.2 windows/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\egon\AppData\Local\go-build
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=C:\aa
set GORACE=
set GOROOT=C:\Go
set GOTMPDIR=
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\egon\AppData\Local\Temp\go-build861229170=/tmp/go-build -gno-record-gcc-switches

What did you do?

cvr.go:

package cvr

type X struct {
	Y int
}

cvr_test.go:

package cvr

import (
	"encoding/xml"
	"testing"
)

func TestX(t *testing.T) {
	xml.Marshal(&X{})
// check output of Marshal
}

go test -cover:

cvr    0.032s  coverage: 0.0% of statements

What did you expect to see?

Anything but "0.0% of statements", it's misleading, it looks like there are no tests. I have a pure struct package like this that describes a network protocol and contains no other code. Adding a single line of code suddenly makes the coverage jump to 100%:

cvr.go:

package cvr

type X struct {
	Y int
}

func Z() int {
	return 5
}

cvr_test.go:

package cvr

import (
	"encoding/xml"
	"testing"
)

func TestX(t *testing.T) {
	xml.Marshal(&X{})
	Z()
}

go test -cover:

cvr    0.021s  coverage: 100.0% of statements
@ysmolski
Copy link
Member

ysmolski commented May 22, 2018

This is an expected behavior of the cover tool. Type struct is not a statement.

https://golang.org/ref/spec#Statements

@egonk
Copy link
Author

egonk commented May 22, 2018

Yes, but cover could print something else than coverage: 0.0% of statements (-> division by zero?) if there are no statements. I think this is a valid scenario: pure protocol description packages.

@davecheney
Copy link
Contributor

davecheney commented May 22, 2018 via email

@egonk
Copy link
Author

egonk commented May 22, 2018

coverage: 0.0% of 0 statements

Yes, this looks ok to me. Or maybe [no statements]? It's similar to already reported [no test files]

@bcmills
Copy link
Contributor

bcmills commented May 22, 2018

Strictly speaking, we should not print 0.0% or 100.0%: 0 ∕ 0 is not a well-defined fraction. (https://play.golang.org/p/hODi5MeDkWp)

[no statements] would probably be the most accurate fix.

@bcmills bcmills changed the title go test -cover reports 0% coverage on pure struct packages cmd/go: go test -cover reports 0% coverage on pure struct packages May 22, 2018
@mvdan mvdan added the NeedsFix The path to resolution is known, but the work has not been done. label May 22, 2018
@mvdan
Copy link
Member

mvdan commented May 22, 2018

This looks like a simple fix, so I'll optimistically milestone it for 1.11. If noone has sent a fix by tomorrow, I'll likely send one myself.

@mvdan mvdan added this to the Go1.11 milestone May 22, 2018
@kyroy
Copy link
Contributor

kyroy commented May 30, 2018

Note: #24570 adds coverage to [no test files]

@mvdan
Copy link
Member

mvdan commented May 30, 2018

@kyroy thanks - I've mentioned it there, to make sure a decision is made before a fix for either issue is merged.

@mvdan mvdan modified the milestones: Go1.11, Unplanned May 30, 2018
@ramirezag
Copy link

I stumbled in this issue as well using go 1.11.2. Tried 1.9.7 and I cannot replicate this.

@ramirezag
Copy link

Surprisingly, using go 1.11.4, I can no longer replicate this issue.

@gopherbot
Copy link

Change https://golang.org/cl/155777 mentions this issue: testing/cover: improve message when a package has no statements

@golang golang locked and limited conversation to collaborators Mar 12, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

8 participants