-
Notifications
You must be signed in to change notification settings - Fork 18.1k
cmd/go: -test.gocoverdir cannot be used for non-curdir packages #73842
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
Comments
Updated since it turns out that even explicitly naming the subpackages or doing readarray -t pkgs < <(go list -f "{{ .Dir }}" ./...)
[[ "$GOCOVERDIR" =~ ^/ ]] || GOCOVERDIR="$PWD/$GOCOVERDIR"
for dir in "${pkgs[@]}"
do
pushd "$dir"
go test -cover -test.gocoverdir="$GOCOVERDIR" .
popd
done |
as I understand, -test.coverdir is an argument understood only by test binaries, rather than go test itself. Once go test sees a flag that it doesn't recognize, any remaining flags and arguments are passed directly to the test binary, so your package list goes to the test binary as arguments, rather than to go test as packages to test. Since this is using undocumented internal details, I don't think we're going to make any change, especially now that the new behavior has been around for a few years. |
Wait, is this really undocumented? There is no other way of using FWIW I found another workaround in that thread though, |
I guess there should be a feature request opened to support this then, given that the utility of |
Uh oh!
There was an error while loading. Please reload this page.
Go version
go version go1.24.3 linux/amd64
Output of
go env
in your module/workspace:What did you do?
I'm trying to migrate an existing project that uses a bunch of hacks to get coverage from integration and unit tests to using
go build -cover
/GOCOVERDIR=foo
andgo test -cover -test.gocoverdir=foo
instead. However, it turns out thatgo test ./...
behaviour changes with-test.gocoverdir=foo
.You can verify this in any project by just observing the output difference between
go test -cover ./...
andgo test -cover -test.gocoverdir=foo ./...
, but here's an example:As far as I can tell,
-test.gocoverdir
has had this behaviour since at least Go 1.14 (though I believe-test.gocoverdir
was an implementation detail that wasn't widely used untilGOCOVERDIR
andgo build -cover
support were added in Go 1.20).EDIT: Upon some further testing, it turns out that even naming subpackages doesn't work:
Even if you are only running a single test package:
What did you see happen?
Only the current directory had its tests run:
-coverpkg=./...
doesn't affect the output.What did you expect to see?
You would expect
./...
to recursively run the tests like regulargo test
does:... or at least return an error if the behaviour is intentional. And named test packages should absolutely have their tests run.
Silently not running tests is quite concerning behaviour, and I only noticed this because I have a minimum coverage percentage for my project that was broken by switching to
-test.gocoverdir=...
. I wouldn't have noticed otherwise that my unit tests weren't running. I wonder how many people have switched and haven't noticed that they are running fewer tests than they did before.The obvious workaround is to manually list the package names withgo list ./...
.EDIT:
go test -cover -test.gocoverdir=foo $(go list ./...)
doesn't work. You need to run the test for each subpackage in a subshell in that directory.The text was updated successfully, but these errors were encountered: