Skip to content

Commit

Permalink
Collect coverage from cc_binary data deps of py_test
Browse files Browse the repository at this point in the history
Before this commit, if a py_test executed a cc_binary from its data
deps, coverage for this cc_binary would not be collected. This is fixed
by adding the implicit `$collect_cc_coverage` attribute to
BazelPyTestRule, similar to how this is already done for
BazelShTestRule.

Fixes #15098.

Closes #15097.

PiperOrigin-RevId: 442800790
  • Loading branch information
fmeum committed May 10, 2022
1 parent 4dca905 commit d5f5d17
Show file tree
Hide file tree
Showing 4 changed files with 534 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ java_library(
"//src/main/java/com/google/devtools/build/lib/analysis:analysis_cluster",
"//src/main/java/com/google/devtools/build/lib/analysis:config/build_options",
"//src/main/java/com/google/devtools/build/lib/analysis:config/core_option_converters",
"//src/main/java/com/google/devtools/build/lib/analysis:config/execution_transition_factory",
"//src/main/java/com/google/devtools/build/lib/analysis:config/fragment",
"//src/main/java/com/google/devtools/build/lib/analysis:config/fragment_options",
"//src/main/java/com/google/devtools/build/lib/analysis:config/host_transition",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.google.devtools.build.lib.analysis.BaseRuleClasses;
import com.google.devtools.build.lib.analysis.RuleDefinition;
import com.google.devtools.build.lib.analysis.RuleDefinitionEnvironment;
import com.google.devtools.build.lib.analysis.config.ExecutionTransitionFactory;
import com.google.devtools.build.lib.analysis.config.HostTransition;
import com.google.devtools.build.lib.bazel.rules.python.BazelPyRuleClasses.PyBinaryBaseRule;
import com.google.devtools.build.lib.packages.RuleClass;
Expand Down Expand Up @@ -58,6 +59,13 @@ public RuleClass build(RuleClass.Builder builder, RuleDefinitionEnvironment env)
.cfg(HostTransition.createFactory())
.value(env.getToolsLabel("//tools/launcher:launcher")))
.add(attr(":lcov_merger", LABEL).value(BaseRuleClasses.getCoverageOutputGeneratorLabel()))
// Add the script as an attribute in order for py_test to output code coverage results for
// code covered by CC binaries invocations.
.add(
attr("$collect_cc_coverage", LABEL)
.cfg(ExecutionTransitionFactory.create())
.singleArtifact()
.value(env.getToolsLabel("//tools/test:collect_cc_coverage")))
.build();
}

Expand Down
9 changes: 9 additions & 0 deletions src/test/shell/bazel/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,15 @@ sh_test(
for java_version in JAVA_VERSIONS_COVERAGE
]

sh_test(
name = "bazel_coverage_py_test",
srcs = ["bazel_coverage_py_test.sh"],
data = [":test-deps"],
tags = [
"no_windows",
],
)

sh_test(
name = "bazel_coverage_sh_test",
srcs = ["bazel_coverage_sh_test.sh"],
Expand Down
Loading

0 comments on commit d5f5d17

Please sign in to comment.