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

testing: -coverprofile includes comment-only lines as covered or not covered #61403

Closed
shanebishop opened this issue Jul 17, 2023 · 1 comment

Comments

@shanebishop
Copy link

shanebishop commented Jul 17, 2023

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

$ go version
go version go1.20.6 linux/amd64

Does this issue reproduce with the latest release?

Yes.

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

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/shane/.cache/go-build"
GOENV="/home/shane/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/shane/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/shane/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.20.6"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
CGO_CFLAGS="-O2 -g"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-O2 -g"
CGO_FFLAGS="-O2 -g"
CGO_LDFLAGS="-O2 -g"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build2538044363=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I created the following three files:

src.go:

package main

// Comment outside a function

func foo() int {
	// Comment inside a function

	if false {
		// Comment inside branch that is not executed
		return 0
	} else {
		// Comment inside branch that is executed
		/*
			General comment rather than a line comment
			See https://go.dev/ref/spec#Comments for the difference
		*/
		return 1
	}
}

src_test.go:

package main

import "testing"

func TestFoo(t *testing.T) {
	foo()
}

go.mod:

module main

go 1.20

Next, I run the test, and generate and view an HTML coverage report:

$ go test -cover -coverprofile cp.out *.go
ok  	command-line-arguments	0.003s	coverage: 66.7% of statements
$ go tool cover -html=cp.out

In my browser (Mozilla Firefox), this is the report I see:
image

Note that the comment inside the if block is considered not covered, and the comments inside the else block are considered covered. Also note the comment outside the foo function is considered not tracked.

The generated cp.out file has these contents:

mode: set
/home/shane/src/tmp/src.go:5.16,8.11 1 1
/home/shane/src/tmp/src.go:8.11,11.3 1 0
/home/shane/src/tmp/src.go:11.8,18.3 1 1

What did you expect to see?

I expect all lines that consist of only comments, or of only comments and whitespace to be not tracked.

I expect lines that only consist of comments or only consist of comments and whitespace (i.e., lines with no code) to not be tracked, as, according to the Go language specification, comments are documentation:

Comments serve as program documentation

I expect lines like

x = 0 // Sets x to 0

to of course count towards coverage, since it has code, but lines without code should not count towards coverage.

It does not make sense to be concerned about coverage of lines that consist of only comments, in the same way that it does not make sense to be concerned about coverage of lines that consist of only whitespace. Therefore, go test -coverprofile should consider comment-only lines as untracked.

What did you see instead?

I see comments within function bodies are covered or not covered, i.e., comments within function bodies are always tracked.

@seankhliao
Copy link
Member

Duplicate of #22545

@seankhliao seankhliao marked this as a duplicate of #22545 Jul 18, 2023
@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Jul 18, 2023
@golang golang locked and limited conversation to collaborators Jul 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants