Skip to content

Commit c0fc7ba

Browse files
bazel-iofmeum
andauthored
[8.3.0] Fix and document coverage support for test rules (#26122)
The LCOV merger needs to be able to run on the test action's execution platform. Closes #25996. PiperOrigin-RevId: 7616358 Change-Id: Idfd35f3fdb389e57066ad22487432a6571c3eba1 Commit 13f1646 Co-authored-by: Fabian Meumertzheim <[email protected]>
1 parent f59f5a4 commit c0fc7ba

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

site/en/extending/rules.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -968,6 +968,43 @@ like `srcs` in `dependency_attributes` instead of `source_attributes`, but it
968968
avoids the need for explicit coverage configuration for all rules in the
969969
dependency chain.)
970970

971+
#### Test rules
972+
973+
Test rules require additional setup to generate coverage reports. The rule
974+
itself has to add the following implicit attributes:
975+
976+
```python
977+
my_test = rule(
978+
...,
979+
attrs = {
980+
...,
981+
# Implicit dependencies used by Bazel to generate coverage reports.
982+
"_lcov_merger": attr.label(
983+
default = configuration_field(fragment = "coverage", name = "output_generator"),
984+
executable = True,
985+
cfg = config.exec(exec_group = "test"),
986+
),
987+
"_collect_cc_coverage": attr.label(
988+
default = "@bazel_tools//tools/test:collect_cc_coverage",
989+
executable = True,
990+
cfg = config.exec(exec_group = "test"),
991+
)
992+
},
993+
test = True,
994+
)
995+
```
996+
997+
By using `configuration_field`, the dependency on the Java LCOV merger tool can
998+
be avoided as long as coverage is not requested.
999+
1000+
When the test is run, it should emit coverage information in the form of one or
1001+
more [LCOV files]
1002+
(https://manpages.debian.org/unstable/lcov/geninfo.1.en.html#TRACEFILE_FORMAT)
1003+
with unique names into the directory specified by the `COVERAGE_DIR` environment
1004+
variable. Bazel will then merge these files into a single LCOV file using the
1005+
`_lcov_merger` tool. If present, it will also collect C/C++ coverage using the
1006+
`_collect_cc_coverage` tool.
1007+
9711008
### Validation Actions
9721009

9731010
Sometimes you need to validate something about the build, and the

src/main/starlark/builtins_bzl/common/cc/semantics.bzl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
"""Semantics for Bazel cc rules"""
1616

17+
_config = _builtins.toplevel.config
18+
1719
# Point virtual includes symlinks to the source root for better IDE integration.
1820
# See https://github.com/bazelbuild/bazel/pull/20540.
1921
# TODO: b/320980684 - Add a test that fails if this is flipped to True.
@@ -78,12 +80,12 @@ def _get_coverage_attrs():
7880
"_lcov_merger": attr.label(
7981
default = configuration_field(fragment = "coverage", name = "output_generator"),
8082
executable = True,
81-
cfg = "exec",
83+
cfg = _config.exec(exec_group = "test"),
8284
),
8385
"_collect_cc_coverage": attr.label(
8486
default = "@bazel_tools//tools/test:collect_cc_coverage",
8587
executable = True,
86-
cfg = "exec",
88+
cfg = _config.exec(exec_group = "test"),
8789
),
8890
}
8991

0 commit comments

Comments
 (0)