Skip to content

Commit 2f1ff6f

Browse files
philsccopybara-github
authored andcommitted
Make coverage --combined_report=lcov skip incompatible tests
Before this patch we see coverage trying to execute incompatible tests. This results in `Can't build this. This target is incompatible` messages. Instead, those tests should just be skipped. This patch makes it so the coverage code only tries to collect coverage artifacts for compatible tests. Artifacts for incompatible tests are ignored. Fixes #15385 Closes #15419. PiperOrigin-RevId: 447710011
1 parent 830d464 commit 2f1ff6f

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed

src/main/java/com/google/devtools/build/lib/bazel/coverage/BUILD

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ java_library(
2424
"//src/main/java/com/google/devtools/build/lib/analysis:analysis_cluster",
2525
"//src/main/java/com/google/devtools/build/lib/analysis:blaze_directories",
2626
"//src/main/java/com/google/devtools/build/lib/analysis:configured_target",
27+
"//src/main/java/com/google/devtools/build/lib/analysis:incompatible_platform_provider",
2728
"//src/main/java/com/google/devtools/build/lib/analysis:test/coverage_report_action_factory",
2829
"//src/main/java/com/google/devtools/build/lib/collect/nestedset",
2930
"//src/main/java/com/google/devtools/build/lib/concurrent",

src/main/java/com/google/devtools/build/lib/bazel/coverage/CoverageReportActionBuilder.java

+5
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import com.google.devtools.build.lib.analysis.BlazeDirectories;
4242
import com.google.devtools.build.lib.analysis.ConfiguredTarget;
4343
import com.google.devtools.build.lib.analysis.FilesToRunProvider;
44+
import com.google.devtools.build.lib.analysis.IncompatiblePlatformProvider;
4445
import com.google.devtools.build.lib.analysis.RunfilesSupport;
4546
import com.google.devtools.build.lib.analysis.actions.Compression;
4647
import com.google.devtools.build.lib.analysis.actions.FileWriteAction;
@@ -201,6 +202,10 @@ public CoverageReportActionsWrapper createCoverageActionsWrapper(
201202
ImmutableList.Builder<Artifact> builder = ImmutableList.<Artifact>builder();
202203
FilesToRunProvider reportGenerator = null;
203204
for (ConfiguredTarget target : targetsToTest) {
205+
// Skip incompatible tests.
206+
if (target.get(IncompatiblePlatformProvider.PROVIDER) != null) {
207+
continue;
208+
}
204209
TestParams testParams = target.getProvider(TestProvider.class).getTestParams();
205210
builder.addAll(testParams.getCoverageArtifacts());
206211
if (reportGenerator == null) {

src/test/shell/bazel/BUILD

+9
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,15 @@ sh_test(
532532
],
533533
)
534534

535+
sh_test(
536+
name = "bazel_coverage_compatibility_test",
537+
srcs = ["bazel_coverage_compatibility_test.sh"],
538+
data = [":test-deps"],
539+
tags = [
540+
"no_windows",
541+
],
542+
)
543+
535544
sh_test(
536545
name = "bazel_cc_code_coverage_test",
537546
srcs = ["bazel_cc_code_coverage_test.sh"],
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/bin/bash
2+
#
3+
# Copyright 2022 The Bazel Authors. All rights reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
set -eu
18+
19+
# Load the test setup defined in the parent directory
20+
CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
21+
source "${CURRENT_DIR}/../integration_test_setup.sh" \
22+
|| { echo "integration_test_setup.sh not found!" >&2; exit 1; }
23+
24+
function set_up_sh_test_coverage() {
25+
cat <<EOF > BUILD
26+
constraint_setting(name = "incompatible_setting")
27+
28+
constraint_value(
29+
name = "incompatible",
30+
constraint_setting = ":incompatible_setting",
31+
)
32+
33+
sh_test(
34+
name = "compatible_test",
35+
srcs = ["compatible_test.sh"],
36+
)
37+
38+
sh_test(
39+
name = "incompatible_test",
40+
srcs = ["incompatible_test.sh"],
41+
target_compatible_with = [":incompatible"],
42+
)
43+
EOF
44+
cat <<EOF > compatible_test.sh
45+
#!/bin/bash
46+
exit 0
47+
EOF
48+
cat <<EOF > incompatible_test.sh
49+
#!/bin/bash
50+
exit 1
51+
EOF
52+
chmod +x compatible_test.sh
53+
chmod +x incompatible_test.sh
54+
}
55+
56+
# Validates that coverage skips incompatible tests. This is a regression test for
57+
# https://github.com/bazelbuild/bazel/issues/15385.
58+
function test_sh_test_coverage() {
59+
set_up_sh_test_coverage
60+
bazel coverage --test_output=all --combined_report=lcov //:all &>$TEST_log \
61+
|| fail "Coverage for //:all failed"
62+
expect_log "INFO: Build completed successfully"
63+
expect_log "//:compatible_test .* PASSED"
64+
expect_log "//:incompatible_test .* SKIPPED"
65+
}
66+
67+
run_suite "test tests"

0 commit comments

Comments
 (0)