Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only add CoverageOutputGenerator in coverage mode. #8477

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,24 @@ public static LabelLateBoundDefault<TestConfiguration> coverageReportGeneratorAt
TestConfiguration.class, defaultValue, COVERAGE_REPORT_GENERATOR_CONFIGURATION_RESOLVER);
}

public static LabelLateBoundDefault<BuildConfiguration> getCoverageOutputGeneratorLabel() {
return LabelLateBoundDefault.fromTargetConfiguration(
BuildConfiguration.class, null,
COVERAGE_OUTPUT_GENERATOR_RESOLVER
);
}

@AutoCodec
static final Resolver<BuildConfiguration, Label> COVERAGE_OUTPUT_GENERATOR_RESOLVER =
(rule, attributes, configuration) -> {
if (configuration.isCodeCoverageEnabled()) {
return Label.parseAbsoluteUnchecked(
"@bazel_tools//tools/test/CoverageOutputGenerator/java/com/google/devtools/coverageoutputgenerator:Main");
} else {
return null;
}
};

// TODO(b/65746853): provide a way to do this without passing the entire configuration
/** Implementation for the :run_under attribute. */
@AutoCodec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,9 @@ private TestParams createTestAction(int shards) {
extraTestEnv.put(BAZEL_CC_COVERAGE_TOOL, GCOV_TOOL);

// We don't add this attribute to non-supported test target
if (ruleContext.isAttrDefined("$lcov_merger", LABEL)) {
if (ruleContext.isAttrDefined(":lcov_merger", LABEL)) {
TransitiveInfoCollection lcovMerger =
ruleContext.getPrerequisite("$lcov_merger", Mode.TARGET);
ruleContext.getPrerequisite(":lcov_merger", Mode.TARGET);
FilesToRunProvider lcovFilesToRun = lcovMerger.getProvider(FilesToRunProvider.class);
if (lcovFilesToRun != null) {
extraTestEnv.put(LCOV_MERGER, lcovFilesToRun.getExecutable().getExecPathString());
Expand All @@ -285,7 +285,7 @@ private TestParams createTestAction(int shards) {
extraTestEnv.put(LCOV_MERGER, lcovMergerArtifact.getExecPathString());
inputsBuilder.add(lcovMergerArtifact);
} else {
ruleContext.attributeError("$lcov_merger",
ruleContext.attributeError(":lcov_merger",
"the LCOV merger should be either an executable or a single artifact");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,8 @@ public RuleClass build(RuleClass.Builder builder, RuleDefinitionEnvironment env)
.override(attr("linkstatic", BOOLEAN).value(OS.getCurrent() == OS.WINDOWS))
.override(attr("stamp", TRISTATE).value(TriState.NO))
.add(
attr("$lcov_merger", LABEL)
.value(
Label.parseAbsoluteUnchecked(
"@bazel_tools//tools/test/CoverageOutputGenerator/java/com/google/devtools/coverageoutputgenerator:Main")))
attr(":lcov_merger", LABEL)
.value(BaseRuleClasses.getCoverageOutputGeneratorLabel()))
.add(
attr("$collect_cc_coverage", LABEL)
.cfg(HostTransition.createFactory())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,8 @@ public RuleClass build(RuleClass.Builder builder, RuleDefinitionEnvironment env)
.override(attr(":java_launcher", LABEL).value(JavaSemantics.JAVA_LAUNCHER))
// Input files for test actions collecting code coverage
.add(
attr("$lcov_merger", LABEL)
.value(
Label.parseAbsoluteUnchecked(
"@bazel_tools//tools/test/CoverageOutputGenerator/java/com/google/devtools/coverageoutputgenerator:Main")))
attr(":lcov_merger", LABEL)
.value(BaseRuleClasses.getCoverageOutputGeneratorLabel()))
.add(
attr("$jacocorunner", LABEL)
.value(env.getToolsLabel("//tools/jdk:JacocoCoverageRunner")))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,11 @@
public final class BazelShTestRule implements RuleDefinition {
@Override
public RuleClass build(RuleClass.Builder builder, RuleDefinitionEnvironment environment) {
// TODO(bazel-team): Add $lcov_merger to every test rule as opposed to particular rules.
// TODO(bazel-team): Add :lcov_merger to every test rule as opposed to particular rules.
builder
.add(
attr("$lcov_merger", LABEL)
.value(
Label.parseAbsoluteUnchecked(
"@bazel_tools//tools/test/CoverageOutputGenerator/java/com/google/devtools/coverageoutputgenerator:Main")))
attr(":lcov_merger", LABEL)
.value(BaseRuleClasses.getCoverageOutputGeneratorLabel()))
.add(
attr("$launcher", LABEL)
.cfg(HostTransition.createFactory())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.google.devtools.build.lib.analysis.ConfiguredTarget;
import com.google.devtools.build.lib.analysis.OutputGroupInfo;
import com.google.devtools.build.lib.analysis.configuredtargets.RuleConfiguredTarget;
import com.google.devtools.build.lib.analysis.configuredtargets.RuleConfiguredTarget.Mode;
import com.google.devtools.build.lib.analysis.util.AnalysisMock;
import com.google.devtools.build.lib.analysis.util.BuildViewTestCase;
import com.google.devtools.build.lib.cmdline.Label;
Expand Down Expand Up @@ -495,6 +496,30 @@ public void testCcTestDisallowsAlwaysLink() throws Exception {
"//cc/common:testlib: no such attribute 'alwayslink'" + " in 'cc_test' rule");
}

@Test
public void testCcTestCoverageOutputGenerator() throws Exception {
scratch.file(
"cc/common/BUILD",
"cc_test(name = 'foo_test',",
" srcs = ['foo_test.cc'])");
reporter.removeHandler(failFastHandler);
ConfiguredTarget ct = getConfiguredTarget("//cc/common:foo_test");
assertThat(getRuleContext(ct).getPrerequisite(":lcov_merger", Mode.HOST)).isNull();
}

@Test
public void testCcTestCoverageOutputGeneratorCoverageMode() throws Exception {
useConfiguration("--collect_code_coverage");
scratch.file(
"cc/common/BUILD",
"cc_test(name = 'foo_test',",
" srcs = ['foo_test.cc'])");
reporter.removeHandler(failFastHandler);
ConfiguredTarget ct = getConfiguredTarget("//cc/common:foo_test");
assertThat(getRuleContext(ct).getPrerequisite(":lcov_merger", Mode.HOST)
.getLabel().toString()).isEqualTo("@bazel_tools//tools/test/CoverageOutputGenerator/java/com/google/devtools/coverageoutputgenerator:Main");
}

@Test
public void testCcTestBuiltWithFissionHasDwp() throws Exception {
// Tests that cc_tests built statically and with Fission will have the .dwp file
Expand Down
22 changes: 22 additions & 0 deletions src/test/java/com/google/devtools/build/lib/rules/sh/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package(
default_testonly = 1,
default_visibility = ["//src:__subpackages__"],
)

filegroup(
name = "srcs",
testonly = 0,
srcs = glob(["**"]),
visibility = ["//src/test/java/com/google/devtools/build/lib:__pkg__"],
)

java_test(
name = "BazelShTestConfiguredTargetTest",
srcs = ["BazelShTestConfiguredTargetTest.java"],
deps = [
"//src/main/java/com/google/devtools/build/lib:build-base",
"//src/test/java/com/google/devtools/build/lib:analysis_testutil",
"//third_party:junit4",
"//third_party:truth",
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2019 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.devtools.build.lib.rules.sh;

import static com.google.common.truth.Truth.assertThat;

import com.google.devtools.build.lib.analysis.ConfiguredTarget;
import com.google.devtools.build.lib.analysis.configuredtargets.RuleConfiguredTarget.Mode;
import com.google.devtools.build.lib.analysis.util.BuildViewTestCase;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Tests for sh_test configured target. */
@RunWith(JUnit4.class)
public class BazelShTestConfiguredTargetTest extends BuildViewTestCase {
@Test
public void testCcTestCoverageOutputGenerator() throws Exception {
scratch.file(
"sh/test/BUILD",
"sh_test(name = 'foo_test',",
" srcs = ['foo_test.sh'])");
iirina marked this conversation as resolved.
Show resolved Hide resolved
reporter.removeHandler(failFastHandler);
ConfiguredTarget ct = getConfiguredTarget("//sh/test:foo_test");
assertThat(getRuleContext(ct).getPrerequisite(":lcov_merger", Mode.HOST)).isNull();
}

@Test
public void testCcTestCoverageOutputGeneratorCoverageMode() throws Exception {
useConfiguration("--collect_code_coverage");
scratch.file(
"sh/test/BUILD",
"sh_test(name = 'foo_test',",
" srcs = ['foo_test.sh'])");
reporter.removeHandler(failFastHandler);
ConfiguredTarget ct = getConfiguredTarget("//sh/test:foo_test");
assertThat(getRuleContext(ct).getPrerequisite(":lcov_merger", Mode.HOST)
.getLabel().toString()).isEqualTo("@bazel_tools//tools/test/CoverageOutputGenerator/java/com/google/devtools/coverageoutputgenerator:Main");
}
}