Rules_go: Supporting Code Coverage

Created on 18 Dec 2017  路  10Comments  路  Source: bazelbuild/rules_go

I noticed that coverage is not supported , but there seems to be some experimental support. How can I contribute to the progression of this feature?

bug coverage go

Most helpful comment

An update: Bazel folks are working on coverage support. See the design doc. They are planning to migrate the internal coverage format to Cobertura XML, and they'll generate reports based on that. There are already tools to convert Go coverage data to this format, so once Bazel supports coverage, completing the support in rules_go won't take much work.

All 10 comments

All the things needed to correctly run coverage are done, the issues are how to collect the results and how to display them.
We either need to massage the results into a form that Bazel can understand, or provide an easy way to use the normal go tooling. Maybe even both.

Is there a way to make a profile go test -coverprofile=c.out currently in bazel.

You can pass arguments to tests on the command line with --test_arg=arg.

Note that tests run in a sandbox by default, and Bazel will clean up their output files after they run. You can disable the sandbox with --test_strategy=standalone.

the --test_arg method doesn't work is there another way to get this coverage profile besides using the go_path method? This profile is needed to do anything related to code coverage.

The way to do this is with --test_arg -test.coverprofile=out. However, I just tried this and got this message:

==================== Test output for //:go_default_test:
testing: cannot use -test.coverprofile because test binary was not built with coverage enabled

I think that used to work, but it doesn't anymore. It looks like we need to set the Mode field in Cover objects.

Hello has any progress been made with this?

I just checked this again with rules_go on master and Bazel 0.10.0rc6, and I'm getting a different error. Not sure if something changed, or I was running it wrong before.

$ bazel coverage :go_default_test --test_arg -test.coverprofile=$PWD/cover.out
...
testing: open /usr/local/google/home/jayconrod/go/src/example.com/repo/cover.out: read-only file system

I think this fails because of sandboxing; the test can't write anything outside of the sandbox. Running with --test_strategy=standalone did not work. You can write to /tmp though.

$ bazel coverage :go_default_test --test_arg -test.coverprofile=/tmp/cover.out
...
//:go_default_test                                                       PASSED in 0.1s

$ go test -coverprofile=/tmp/cover.out
go test -coverprofile=/tmp/cover.out
PASS
coverage: 100.0% of statements
ok      example.com/repo    0.011s

Sorry I know this isn't a great solution. There's a long-term plan for Bazel rules in various languages to all produce coverage data in the same format, then produce unified reports for multiple tests across multiple languages. We'll provide Go support for that as soon as the Bazel backend for it is ready.

An update: Bazel folks are working on coverage support. See the design doc. They are planning to migrate the internal coverage format to Cobertura XML, and they'll generate reports based on that. There are already tools to convert Go coverage data to this format, so once Bazel supports coverage, completing the support in rules_go won't take much work.

Duplicate of #140?

Sure, no reason to have two tracking issues for this.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

justinsb picture justinsb  路  3Comments

prestonvanloon picture prestonvanloon  路  3Comments

twpayne picture twpayne  路  5Comments

jayconrod picture jayconrod  路  6Comments

jarreds picture jarreds  路  7Comments