aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/java/com/google/devtools/build/lib/skylark
diff options
context:
space:
mode:
authorGravatar Googler <noreply@google.com>2016-11-17 16:01:26 +0000
committerGravatar Kristina Chodorow <kchodorow@google.com>2016-11-17 18:18:39 +0000
commite4f5c82209dc939857815cefb52933bf249cc52d (patch)
tree362989d76a2bc6f8388cd3b4871fe8af250001dc /src/test/java/com/google/devtools/build/lib/skylark
parenta2bbe67ecf5a95777e13820c165f2955037a14fd (diff)
Add ctx.coverage_instrumented function to Skylark
Skylark already has ctx.configuration.coverage_enabled to determine if coverage data collection is on for an entire run. But that does not reveal which targets specifically are supposed to be instrumented (based on the values of --instrumentation_filer and --instrument_test_targets). This is inefficient for languages which add coverage instrumentation at compile-time, though correct coverage output can still be produced by instrumenting everything and filtering later. By default, this function returns whether the rule represented by ctx should be instrumented. If a Skylark Target (e.g. from a label or label_list attribute in ctx.attr) is passed to the function, it instead returns whether that Target is a rule whose sources should be instrumented. Rules that directly incorporate source-files from their dependencies before compilation (e.g. header files) may need to know if those source files need to be instrumented when compiled. Expanded the documentation of instrumented_files to be a more general section on implementing code coverage instrumentation in Skylark. Also tweaked the code comment and variable names for the version of shouldIncludeLocalSources that takes a TransitiveInfoCollection. RELNOTES: Add ctx.coverage_instrumented function to Skylark, to indicate whether a specific targets should be instrumented for code coverage data collection. -- MOS_MIGRATED_REVID=139460989
Diffstat (limited to 'src/test/java/com/google/devtools/build/lib/skylark')
-rw-r--r--src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleContextTest.java72
1 files changed, 72 insertions, 0 deletions
diff --git a/src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleContextTest.java b/src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleContextTest.java
index 8f200c340a..15277ff181 100644
--- a/src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleContextTest.java
+++ b/src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleContextTest.java
@@ -1296,4 +1296,76 @@ public class SkylarkRuleContextTest extends SkylarkTestCase {
assertThat(substitutionsUnchecked).isInstanceOf(SkylarkDict.class);
assertThat(substitutionsUnchecked).isEqualTo(SkylarkDict.of(null, "a", "b"));
}
+
+ private void setUpCoverageInstrumentedTest() throws Exception {
+ scratch.file("test/BUILD",
+ "cc_library(",
+ " name = 'foo',",
+ " srcs = ['foo.cc'],",
+ " deps = [':bar'],",
+ ")",
+ "cc_library(",
+ " name = 'bar',",
+ " srcs = ['bar.cc'],",
+ ")");
+ }
+
+ @Test
+ public void testCoverageInstrumentedCoverageDisabled() throws Exception {
+ setUpCoverageInstrumentedTest();
+ useConfiguration("--nocollect_code_coverage", "--instrumentation_filter=.");
+ SkylarkRuleContext ruleContext = createRuleContext("//test:foo");
+ Object result = evalRuleContextCode(ruleContext, "ruleContext.coverage_instrumented()");
+ assertThat((Boolean) result).isFalse();
+ }
+
+ @Test
+ public void testCoverageInstrumentedFalseForSourceFileLabel() throws Exception {
+ setUpCoverageInstrumentedTest();
+ useConfiguration("--collect_code_coverage", "--instrumentation_filter=.");
+ SkylarkRuleContext ruleContext = createRuleContext("//test:foo");
+ Object result = evalRuleContextCode(ruleContext,
+ "ruleContext.coverage_instrumented(ruleContext.attr.srcs[0])");
+ assertThat((Boolean) result).isFalse();
+ }
+
+ @Test
+ public void testCoverageInstrumentedDoesNotMatchFilter() throws Exception {
+ setUpCoverageInstrumentedTest();
+ useConfiguration("--collect_code_coverage", "--instrumentation_filter=:foo");
+ SkylarkRuleContext ruleContext = createRuleContext("//test:bar");
+ Object result = evalRuleContextCode(ruleContext, "ruleContext.coverage_instrumented()");
+ assertThat((Boolean) result).isFalse();
+ }
+
+ @Test
+ public void testCoverageInstrumentedMatchesFilter() throws Exception {
+ setUpCoverageInstrumentedTest();
+ useConfiguration("--collect_code_coverage", "--instrumentation_filter=:foo");
+ SkylarkRuleContext ruleContext = createRuleContext("//test:foo");
+ Object result = evalRuleContextCode(ruleContext, "ruleContext.coverage_instrumented()");
+ assertThat((Boolean) result).isTrue();
+ }
+
+ @Test
+ public void testCoverageInstrumentedDoesNotMatchFilterNonDefaultLabel() throws Exception {
+ setUpCoverageInstrumentedTest();
+ useConfiguration("--collect_code_coverage", "--instrumentation_filter=:foo");
+ SkylarkRuleContext ruleContext = createRuleContext("//test:foo");
+ // //test:bar does not match :foo, though //test:foo would.
+ Object result = evalRuleContextCode(ruleContext,
+ "ruleContext.coverage_instrumented(ruleContext.attr.deps[0])");
+ assertThat((Boolean) result).isFalse();
+ }
+
+ @Test
+ public void testCoverageInstrumentedMatchesFilterNonDefaultLabel() throws Exception {
+ setUpCoverageInstrumentedTest();
+ useConfiguration("--collect_code_coverage", "--instrumentation_filter=:bar");
+ SkylarkRuleContext ruleContext = createRuleContext("//test:foo");
+ // //test:bar does match :bar, though //test:foo would not.
+ Object result = evalRuleContextCode(ruleContext,
+ "ruleContext.coverage_instrumented(ruleContext.attr.deps[0])");
+ assertThat((Boolean) result).isTrue();
+ }
}