go test -coverpkg=$LIST ... failing with exit code

320 views
Skip to first unread message

TheDiveO

unread,
Mar 12, 2024, 4:29:55 PM3/12/24
to golang-nuts
Dear Gophers,

I'm struggling with "go test -coverpkg=XXX ... ./... -args -test.gocoverdir" returning non-zero exit codes, albeit all tests are passing. It might well be that I'm not really yet understanding how "go test -coverpkg=" is supposed to work. As illustrated below, I don't want to -coverpkg all packages "./..." in my module, but instead all but one.

First, I'm using go version go1.22.1 linux/amd64. The same happens for go1.22.0, too.

Now, my repo in a minimized example might look like the following -- unfortunately, I don't see how I can make a useful playground example with the required specific "go test" invocation.
Following is preparing the list of packages to cover, with the exclusion(s):

GOCOVERTMPDIR="$(mktemp -d)"
trap 'rm -rf -- "$GOCOVERTMPDIR"' EXIT
IGNORE_PKGS=("github.com/example/foobaz")
FILTER_PATTERN="$(printf '^%s$|' "${IGNORE_PKGS[@]}" | sed 's/|$//')"
PACKAGE_LIST=$(go list ./... | grep -v -E "${FILTER_PATTERN}" | tr '\n' ',' | sed 's/,$//')
go test -coverpkg="${PACKAGE_LIST}" -v -tags=matchers -p=1 -count=1 -race \
    ./... -args -test.gocoverdir="$GOCOVERTMPDIR"

This runs all available tests including testable examples with success, but exits with code 1.

Running the same tests with -coverpkg=./... succeeds.

What am I doing wrong? Or might this be an issue?

Nota bene: why do I want to exclude a package from the coverage? Because that is a trimmed-down package (as permitted by its license) following CalVer(!) and having no go.mod, with otherwise large number of unnecessary dependencies in my particular case. It changes the coverage considerably, distracting from the coverage of my own code.

Jeremy French

unread,
Mar 13, 2024, 1:17:24 PM3/13/24
to golang-nuts
I don't have any insight in trouble-shooting your exact issue, but I too would like a simple, reliable way to exclude packages from -coverpkg=./...

Feels like relying on shell scripts (which is what a google search recommends) is a) fragile, and b) not particularly cross-platform.

TheDiveO

unread,
Mar 14, 2024, 10:29:07 AM3/14/24
to golang-nuts
Looking and looking again, at least I spotted a lonely "open /tmp/go-build..../covmeta....: no such file or directory" error message. This now finally matches https://github.com/golang/go/issues/65653 and using gotip finally succeeds correctly.
Reply all
Reply to author
Forward
0 new messages