Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Bazel to generate coverage report

I am using genhtml command to generate html coverage report from Bazel generated coverage.dat file:

genhtml bazel-testlogs/path/to/TestTarget/coverage.dat --output-directory coverage

The problem with using genhtml is that I have to provide the paths to the coverage.dat files (which are generated in bazel-testlogs/..) Is it possible to fetch those coverage.dat files as an output from another rule?

I would like to not have to call genthml command directly, but have Bazel handle everything.

like image 970
Zeitgeist Avatar asked Sep 27 '17 11:09

Zeitgeist


2 Answers

I was not able to find a way to get coverage.dat files as an output of a bazel rule. However, I was able to wrap all the locations of all the .dat files as srcs to a filegroup in WORKSPACE directory:

filegroup(
    name = "coverage_files",
    srcs = glob(["bazel-out/**/coverage.dat"]),
)

and then use that filegroup in a custom .bzl rule that wraps the genthml command to generate html coverage report. So now I only have to call

bazel coverage //path/... --instrumentation_filter=/path[/:]

command to generate the coverage.dat files, generate html report and zip it up. Thus, bazel handles everything.

like image 64
Zeitgeist Avatar answered Nov 15 '22 08:11

Zeitgeist


Bazel added support for C++ coverage (though I couldn't find much documentation for it).

I was able to generate a combined coverage.dat file with

bazel coverage -s \
  --instrument_test_targets \
  --experimental_cc_coverage \
  --combined_report=lcov \
  --coverage_report_generator=@bazel_tools//tools/test/CoverageOutputGenerator/java/com/google/devtools/coverageoutputgenerator:Main \
  //...

The coverage file gets added to bazel-out/_coverage/_coverage_report.dat

like image 26
Ryan Burn Avatar answered Nov 15 '22 07:11

Ryan Burn