Skip to content

Commit

Permalink
Publish test suite expansions to the BEP.
Browse files Browse the repository at this point in the history
Adds a mapping from suite to tests to the existing `PatternExpanded` message.

RELNOTES: BEP includes test suite expansions.
PiperOrigin-RevId: 353008912
  • Loading branch information
justinhorvitz authored and copybara-github committed Jan 21, 2021
1 parent cb5245b commit 7fb0c16
Show file tree
Hide file tree
Showing 6 changed files with 356 additions and 195 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,23 @@ message Configuration {
// The main information is in the chaining part: the id will contain the
// target pattern that was expanded and the children id will contain the
// target or target pattern it was expanded to.
message PatternExpanded {}
message PatternExpanded {
// Represents a test_suite target and the tests that it expanded to. Nested
// test suites are recursively expanded. The test labels only contain the
// final test targets, not any nested suites.
message TestSuiteExpansion {
// The label of the test_suite rule.
string suite_label = 1;
// Labels of the test targets included in the suite. Includes all tests in
// the suite regardless of any filters or negative patterns which may result
// in the test not actually being run.
repeated string test_labels = 2;
}

// All test suites requested via top-level target patterns. Does not include
// test suites whose label matched a negative pattern.
repeated TestSuiteExpansion test_suite_expansions = 1;
}

// Enumeration type characterizing the size of a test, as specified by the
// test rule.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,18 @@
// limitations under the License.
package com.google.devtools.build.lib.pkgcache;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.collect.Collections2;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSetMultimap;
import com.google.common.collect.Iterables;
import com.google.devtools.build.lib.buildeventstream.BuildEventContext;
import com.google.devtools.build.lib.buildeventstream.BuildEventIdUtil;
import com.google.devtools.build.lib.buildeventstream.BuildEventStreamProtos;
import com.google.devtools.build.lib.buildeventstream.BuildEventStreamProtos.BuildEventId;
import com.google.devtools.build.lib.buildeventstream.BuildEventStreamProtos.PatternExpanded;
import com.google.devtools.build.lib.buildeventstream.BuildEventWithOrderConstraint;
import com.google.devtools.build.lib.buildeventstream.GenericBuildEvent;
import com.google.devtools.build.lib.cmdline.Label;
Expand Down Expand Up @@ -79,39 +81,25 @@ public boolean isNotATestOrTestSuite() {
private final ImmutableSet<ThinTarget> testFilteredTargets;
private final ImmutableSet<ThinTarget> expandedTargets;
private final ImmutableSetMultimap<String, Label> originalPatternsToLabels;
private final ImmutableMap<Label, ImmutableSet<Label>> testSuiteExpansions;

/**
* Construct the event.
*
* @param targets The targets that were parsed from the command-line pattern.
*/
public TargetParsingCompleteEvent(
Collection<Target> targets,
Collection<Target> filteredTargets,
Collection<Target> testFilteredTargets,
ImmutableList<String> originalTargetPattern,
Collection<Target> expandedTargets,
ImmutableList<String> failedTargetPatterns,
ImmutableSetMultimap<String, Label> originalPatternsToLabels) {
ImmutableSetMultimap<String, Label> originalPatternsToLabels,
ImmutableMap<Label, ImmutableSet<Label>> testSuiteExpansions) {
this.targets = asThinTargets(targets);
this.filteredTargets = asThinTargets(filteredTargets);
this.testFilteredTargets = asThinTargets(testFilteredTargets);
this.originalTargetPattern = Preconditions.checkNotNull(originalTargetPattern);
this.expandedTargets = asThinTargets(expandedTargets);
this.failedTargetPatterns = Preconditions.checkNotNull(failedTargetPatterns);
this.originalPatternsToLabels = originalPatternsToLabels;
}

@VisibleForTesting
public TargetParsingCompleteEvent(Collection<Target> targets) {
this(
targets,
ImmutableSet.of(),
ImmutableSet.of(),
ImmutableList.of(),
targets,
ImmutableList.of(),
ImmutableSetMultimap.of());
this.originalPatternsToLabels = Preconditions.checkNotNull(originalPatternsToLabels);
this.testSuiteExpansions = Preconditions.checkNotNull(testSuiteExpansions);
}

public ImmutableList<String> getOriginalTargetPattern() {
Expand Down Expand Up @@ -181,7 +169,7 @@ public Collection<BuildEventId> getChildrenEvents() {
BuildEventIdUtil.targetPatternExpanded(ImmutableList.of(failedTargetPattern)));
}
for (ThinTarget target : expandedTargets) {
// Test suits won't produce target configuration and target-complete events, so do not
// Test suites won't produce target configuration and target-complete events, so do not
// announce here completion as children.
if (!target.isTestSuiteRule()) {
childrenBuilder.add(BuildEventIdUtil.targetConfigured(target.getLabel()));
Expand All @@ -192,9 +180,15 @@ public Collection<BuildEventId> getChildrenEvents() {

@Override
public BuildEventStreamProtos.BuildEvent asStreamProto(BuildEventContext converters) {
return GenericBuildEvent.protoChaining(this)
.setExpanded(BuildEventStreamProtos.PatternExpanded.newBuilder().build())
.build();
PatternExpanded.Builder expanded = PatternExpanded.newBuilder();
testSuiteExpansions.forEach(
(suite, tests) ->
expanded
.addTestSuiteExpansionsBuilder()
.setSuiteLabel(suite.toString())
.addAllTestLabels(Collections2.transform(tests, Label::toString)));

return GenericBuildEvent.protoChaining(this).setExpanded(expanded).build();
}

private static ImmutableSet<ThinTarget> asThinTargets(Collection<Target> targets) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,15 +188,18 @@ public TargetPatternPhaseValue compute(SkyKey key, Environment env) throws Inter
maybeReportDeprecation(env.getListener(), targets.getTargets());

ResolvedTargets.Builder<Label> expandedLabelsBuilder = ResolvedTargets.builder();
ImmutableMap.Builder<Label, ImmutableSet<Label>> testSuiteExpansions =
ImmutableMap.builderWithExpectedSize(expandedTests.size());
for (Target target : targets.getTargets()) {
Label label = target.getLabel();
if (TargetUtils.isTestSuiteRule(target) && options.isExpandTestSuites()) {
SkyKey expansionKey =
Preconditions.checkNotNull(testExpansionKeys.get(target.getLabel()));
TestsForTargetPatternValue testExpansion =
(TestsForTargetPatternValue) expandedTests.get(expansionKey);
expandedLabelsBuilder.merge(testExpansion.getLabels());
SkyKey expansionKey = Preconditions.checkNotNull(testExpansionKeys.get(label));
ResolvedTargets<Label> testExpansion =
((TestsForTargetPatternValue) expandedTests.get(expansionKey)).getLabels();
expandedLabelsBuilder.merge(testExpansion);
testSuiteExpansions.put(label, testExpansion.getTargets());
} else {
expandedLabelsBuilder.add(target.getLabel());
expandedLabelsBuilder.add(label);
}
}
ResolvedTargets<Label> targetLabels = expandedLabelsBuilder.build();
Expand Down Expand Up @@ -231,7 +234,8 @@ public TargetPatternPhaseValue compute(SkyKey key, Environment env) throws Inter
options.getTargetPatterns(),
expandedTargets.getTargets(),
ImmutableList.copyOf(failedPatterns),
mapOriginalPatternsToLabels(expandedPatterns, targets.getTargets())));
mapOriginalPatternsToLabels(expandedPatterns, targets.getTargets()),
testSuiteExpansions.build()));
env.getListener()
.post(new LoadingPhaseCompleteEvent(result.getTargetLabels(), removedTargetLabels));
return result;
Expand Down
1 change: 1 addition & 0 deletions src/test/java/com/google/devtools/build/lib/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ java_test(
"//src/main/java/com/google/devtools/build/lib/analysis:config/build_options",
"//src/main/java/com/google/devtools/build/lib/analysis:server_directories",
"//src/main/java/com/google/devtools/build/lib/bazel/rules",
"//src/main/java/com/google/devtools/build/lib/buildeventstream/proto:build_event_stream_java_proto",
"//src/main/java/com/google/devtools/build/lib/clock",
"//src/main/java/com/google/devtools/build/lib/cmdline",
"//src/main/java/com/google/devtools/build/lib/events",
Expand Down
Loading

0 comments on commit 7fb0c16

Please sign in to comment.