Reputation: 222
We have a git repository that has two go modules because we want to import only the submodule in other projects
Example:
go.mod
go.sum
cmd/api
main.go
internal/foo
foo.go
internal/bar
bar.go
pkg/middleware
go.mod <- another module
go.sum
middleware.go
For both modules, we can calculate the coverage correctly but have no way to merge both reports, which we would like to have calculated in CI.
We've tried the approach that we use to merge code from different suites (unit, integration, e2e)
echo 'mode: atomic' > coverage.cov
tail -q -n +2 coverage.*.suite >> coverage.cov
go tool cover -html=coverage.cov
After we merge all the files, coverage.cov
contains all the line hits for both modules, but go tool cover
only builds the report for the main module, and ignores the submodule lines.
Is there a way to merge the coverage from multiple go modules?
Upvotes: 3
Views: 2702
Reputation: 176
I came across this question today. I am sharing a work around for this problem.
One way to solve this problem is to use gocov
tool
go install github.com/axw/gocov/gocov@latest
go install github.com/AlekSi/gocov-xml@latest
go to the module directory and generate the coverage json using gocov tool
cd pkg/middleware
then generate coverage json file
gocov test ./... > coverage_middleware.json
cd -
generate the coverage json for main module using the gocov
gocov test ./... > coverage_main.json
Now use jq to merge these json and gocov-xml to generate the xml
jq -n '{ Packages: [ inputs.Packages ] | add }' pkg/middleware/coverage_middleware.json coverage_main.json | gocov-xml > coverage.xml
Hope it will help some one. If anyone has better solution, please share.
Upvotes: 3