Skip to content

Commit 1502549

Browse files
committed
Fix and document coverage support for test rules
The LCOV merger needs to be able to run on the test action's execution platform.
1 parent 90f9bb1 commit 1502549

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

site/en/extending/rules.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -968,6 +968,42 @@ 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](https://manpages.debian.org/unstable/lcov/geninfo.1.en.html#TRACEFILE_FORMAT)
1002+
with unique names into the directory specified by the `COVERAGE_DIR` environment
1003+
variable. Bazel will then merge these files into a single LCOV file using the
1004+
`_lcov_merger` tool. If present, it will also collect C/C++ coverage using the
1005+
`_collect_cc_coverage` tool.
1006+
9711007
### Validation Actions
9721008

9731009
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.internal.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.
@@ -75,12 +77,12 @@ def _get_coverage_attrs():
7577
"_lcov_merger": attr.label(
7678
default = configuration_field(fragment = "coverage", name = "output_generator"),
7779
executable = True,
78-
cfg = "exec",
80+
cfg = _config.exec(exec_group = "test"),
7981
),
8082
"_collect_cc_coverage": attr.label(
8183
default = "@bazel_tools//tools/test:collect_cc_coverage",
8284
executable = True,
83-
cfg = "exec",
85+
cfg = _config.exec(exec_group = "test"),
8486
),
8587
}
8688

0 commit comments

Comments
 (0)