bazel coverage results are incomplete - bazel

The problem I'm running into is that my bazel coverage output coverage data feels incomplete.
For example, I have a project with 10 source files, and 1 test file (which tests only 1 source file).
Run bazel clean && bazel coverage --combined_report=lcov -- //src/...:all
Verify coverage data generated with find -L -type f -name 'coverage.dat'
Notice that:
There exists 1 coverage file for each test run
(Problem!) There is no coverage data for the untested source files
Does anyone know how to configure bazel to generate a complete coverage report, which includes the untested source files as well?

Related

Bazel's Java lcov report uses package instead of file paths

I am collecting code coverage for a Java-based repository using the following command:
bazel coverage --jobs=$PARALLEL_JOBS --keep_going --flaky_test_attempts=3 --cache_test_results=no --combined_report=lcov --coverage_report_generator="#bazel_tools//tools/test/CoverageOutputGenerator/java/com/google/devtools/coverageoutputgenerator:Main" -- //$TARGET/...
This creates a coverage report in this directory:
$(bazel info output_path)/_coverage/_coverage_report.dat
Problem: All of the files reported in this coverage report correspond to Java packages instead of actual files (e.g. com/my_package/path/my_class.java)
Question: How do I configure bazel coverage to report coverage data by original source files instead of Java packages?
I would like to post process these coverage files, but I don't have a clear way to map all of these Java packages into original source files.
Thank you!

How to include all targets in bazel coverage

How do I include non-test targets in bazel coverage? Currently I use the following bazel command to get code coverage:
bazel coverage \
--instrument_test_targets \
--experimental_cc_coverage \
--combined_report=lcov \
//... --test_arg=--logtostderr
The project is written in C++. The command works fine. However, the output lcov trace file only includes the files that have coverage. If a C file does not have a test, it is not in the lcov trace file.
Does bazel coverage only executes the test targets? Is there a way to include all targets (the non-test targets)? So that even if a file has no test, I can still see it in the report (the report will show zero coverage). The intention for this is that if someone adds new files and doesn't write unit test, the file can be shown in coverage report.
Can you reproduce using --incompatible_cc_coverage?

Bazel: How to exclude path from code coverage for Scala / Java?

I am using Bazel with rules_scala. My problem now is how to exclude files from code coverage. So far this is how I am running coverage:
rm -rf coverage
bazel coverage --combined_report=lcov --coverage_report_generator="#bazel_tools//tools/test/CoverageOutputGenerator/java/com/google/devtools/coverageoutputgenerator:Main" ...
genhtml -o coverage --ignore-errors source bazel-out/_coverage/_coverage_report.dat
But there are some folders I would like to exclude from code coverage. I tried using the --instrumentation_filter flag, but no matter what I tried putting there Bazel still collect coverage for this folder.
Are there any examples how I should use this flag?
Thanks!
Use
bazel coverage --combined_report=lcov --coverage_report_generator="#bazel_tools//tools/test/CoverageOutputGenerator/java/com/google/devtools/coverageoutputgenerator:Main" -- //... -//package/to/ignore/...
This appears to be a bug with rules_scala. See this issue for more details.

why gcov total files count is varying?

We are using gcov and lcov for code coverage. After installing a instrumented build we see 5060 gcno files. when we run a test we see around 4687 gcda files. Each test run shows different number of gcda files. Though our build has 5060 files why each test run shows different number of total files as shown below?:
Test1% = 2808(covered files)/4883(total files)
Test2% = 2501(covered files)/4811(total files)
Why my code coverage output varying total files for each test run? Can we make it constant so our coverage calculations are meaningful/proper.

Generating empty .gcda files

I use gcov for doing code coverage analysis with lcov for generating graphical output of coverage. This works well for code file where atleast some part of object file has been executed. I want to be able to track files which have not been executed at all. I suspect this has to do with .gcda files not being generated for these files. Is there a way to force the generation of .gcda file for all object files irrespective of execution?
The procedure to do this is outlined here:
http://linux.die.net/man/1/lcov
Recommended procedure when capturing data for a test case:
create baseline coverage data file
lcov -c -i -d appdir -o app_base.info
perform test
appdir/test
create test coverage data file
lcov -c -d appdir -o app_test.info
combine baseline and test coverage data
lcov -a app_base.info -a app_test.info -o app_total.info
For all of your files that are correctly compiled and linked, there will be a .gcda file. If you see that there's a missing *.gcda file check to see if the *.gcno file exists. If it doesn't check to see if all of you Makefiles are correctly build with:
-ftest-coverage : The .gcno notes file is generated when the source file is compiled with this
-fprofile-arcs : .gcda count data file is generated when a program containing object files built with the GCC -fprofile-arcs option is executed.
More info on: https://gcc.gnu.org/onlinedocs/gcc/Gcov.html#Gcov

Resources