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

Comments

Projects
None yet
8 participants
@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
@ysmolsky

This comment has been minimized.

Copy link
Member

ysmolsky 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

This comment has been minimized.

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

This comment has been minimized.

Copy link
Contributor

davecheney commented May 22, 2018

@egonk

This comment has been minimized.

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

This comment has been minimized.

Copy link
Member

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 label May 22, 2018

@mvdan

This comment has been minimized.

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

This comment has been minimized.

Copy link
Contributor

kyroy commented May 30, 2018

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

@mvdan

This comment has been minimized.

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

This comment has been minimized.

Copy link

ramirezag commented Dec 12, 2018

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

@ramirezag

This comment has been minimized.

Copy link

ramirezag commented Dec 19, 2018

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

@gopherbot

This comment has been minimized.

Copy link

gopherbot commented Dec 23, 2018

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

@gopherbot gopherbot closed this in b6544a2 Mar 13, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.