rogpeppe / go-internal

Selected Go-internal packages factored out from the standard library
BSD 3-Clause "New" or "Revised" License
858 stars 70 forks source link

go test -coverprofile fails under Go 1.20 #199

Closed prymitive closed 1 year ago

prymitive commented 1 year ago

I've upgraded go to 1.20 and suddenly I'm getting this error from testscript:

cannot merge cover profiles: cannot merge coverage profile from ...: no lines found in profile: <nil>

Seems like it might be related to coverage changes documented here: https://go.dev/testing/coverage/

Removing -coverprofile-... flag allows my tests to pass.

mvdan commented 1 year ago

You're absolutely right, and I've been noticing these failures with Go tip for some weeks now.

I wrote our code to collect and merge coverage profiles years ago, before upstream planned on doing the same, but properly :) So our code is not just obsolete, but also apparently broken now. I need to delete it, and check that the new go test -cover works for testscript just like our hacky stuff did.

mvdan commented 1 year ago

Release done: https://github.com/rogpeppe/go-internal/releases/tag/v1.10.0

nbgraham commented 9 months ago

I was also getting this error. Updating to latest (v1.12.0) fixed the error, but I'm not getting the total coverage.

In this walkthrough, it shows that running go test -coverprofile=cover.out will output something like

PASS
coverage: 0.0% of statements
total coverage: 100.0% of statements

But mine shows

PASS
coverage: 100.0% of statements

with no total coverage line. The cover.out file is created, but only with coverage on main.go, not the other files in other packages that actually contain the functionality of the CLI tool.

Everything else works. I have multiple test passing and changing the expected values fails the tests as expected. It seems that everything is being run correctly.

Do I need to enable something to get the total coverage?

I'm using Go 1.21.

nbgraham commented 9 months ago

I found the same in the example repo I updated to Go 1.20 and https://github.com/rogpeppe/go-internal/releases/tag/v1.10.0 and did not see the total coverage: 100.0% of statements line

I set up a test repo. I can't get deep coverage, only one package down, it seems. https://github.com/nbgraham/go-calculator-cli

mvdan commented 9 months ago

Ignore the old output saying "total coverage"; the new output simply outputs that percentage directly.

nbgraham commented 9 months ago

Ok, I can see that in my test repo.

But the cover.out file still does not include the line coverage information for the other packages. I need the cover profile for all the packages to publish to my coverage reporter (SonarQube).

nbgraham commented 9 months ago

But using my custom bash script it outputs the coverage profile correctly

mode: set
calculator/calculator.go:11.17,12.26 1 1
calculator/calculator.go:12.26,15.3 2 1
calculator/calculator.go:17.2,20.16 3 1
calculator/calculator.go:20.16,23.3 2 1
calculator/calculator.go:25.2,26.16 2 1
calculator/calculator.go:26.16,29.3 2 1
calculator/calculator.go:31.2,31.19 1 1
calculator/calculator.go:32.13,33.41 1 1
calculator/calculator.go:34.18,35.55 1 1
calculator/calculator.go:36.10,38.11 2 1
calculator/calculator.go:41.2,41.10 1 1
calculator/cmd/calculator/main.go:9.13,12.2 2 1
calculator/internal/add/add.go:3.24,5.2 1 1
calculator/internal/multiply/multiply.go:3.29,5.2 1 1
mvdan commented 9 months ago

But the cover.out file still does not include the line coverage information for the other packages. I need the cover profile for all the packages to publish to my coverage reporter (SonarQube).

See -coverpkg in go help testflag. I regularly use -coverpkg=./.... I don't think any of this is related to testscript, as the coverage is now done entirely by go test.

nbgraham commented 9 months ago

-coverpkg=./... worked! Thank you!