Skip to content

Commit

Permalink
Only add CoverageOutputGenerator in coverage mode.
Browse files Browse the repository at this point in the history
Fixes #8355.

Closes #8477.

PiperOrigin-RevId: 250489150
  • Loading branch information
iirina authored and copybara-github committed May 29, 2019
1 parent f69d02d commit 384e1cb
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ public static LabelLateBoundDefault<TestConfiguration> coverageSupportAttribute(
public static final String DEFAULT_COVERAGE_REPORT_GENERATOR_VALUE =
"//tools/test:coverage_report_generator";

private static final String DEFAULT_COVERAGE_OUTPUT_GENERATOR_VALUE =
"@bazel_tools//tools/test/CoverageOutputGenerator/java/com/google/devtools/coverageoutputgenerator:Main";

@AutoCodec
static final Resolver<TestConfiguration, Label> COVERAGE_REPORT_GENERATOR_CONFIGURATION_RESOLVER =
(rule, attributes, configuration) -> configuration.getCoverageReportGenerator();
Expand All @@ -109,6 +112,21 @@ 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(DEFAULT_COVERAGE_OUTPUT_GENERATOR_VALUE);
} 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,8 @@ 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 @@ -24,7 +24,6 @@
import com.google.devtools.build.lib.analysis.RuleDefinitionEnvironment;
import com.google.devtools.build.lib.analysis.config.HostTransition;
import com.google.devtools.build.lib.bazel.rules.cpp.BazelCppRuleClasses.CcBinaryBaseRule;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.packages.RuleClass;
import com.google.devtools.build.lib.packages.RuleClass.Builder.RuleClassType;
import com.google.devtools.build.lib.packages.TriState;
Expand All @@ -45,11 +44,7 @@ public RuleClass build(RuleClass.Builder builder, RuleDefinitionEnvironment env)
// to decorate data symbols imported from DLL.
.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")))
.add(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 @@ -26,7 +26,6 @@
import com.google.devtools.build.lib.analysis.RuleDefinitionEnvironment;
import com.google.devtools.build.lib.analysis.config.HostTransition;
import com.google.devtools.build.lib.bazel.rules.java.BazelJavaRuleClasses.BaseJavaBinaryRule;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.packages.RuleClass;
import com.google.devtools.build.lib.packages.RuleClass.Builder.RuleClassType;
import com.google.devtools.build.lib.packages.TriState;
Expand Down Expand Up @@ -66,11 +65,7 @@ public RuleClass build(RuleClass.Builder builder, RuleDefinitionEnvironment env)
.override(attr("use_testrunner", BOOLEAN).value(true))
.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")))
.add(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 @@ -21,7 +21,6 @@
import com.google.devtools.build.lib.analysis.RuleDefinitionEnvironment;
import com.google.devtools.build.lib.analysis.config.HostTransition;
import com.google.devtools.build.lib.bazel.rules.sh.BazelShRuleClasses.ShRule;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.packages.RuleClass;
import com.google.devtools.build.lib.packages.RuleClass.Builder.RuleClassType;

Expand All @@ -31,13 +30,9 @@
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")))
.add(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
@@ -0,0 +1,46 @@
// 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.bazel.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 testCoverageOutputGenerator() throws Exception {
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)).isNull();
}

@Test
public void testCoverageOutputGeneratorCoverageMode() 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");
}
}

0 comments on commit 384e1cb

Please sign in to comment.