Only announce test summaries in BEP if expected

In some situations, we do not even expect a test summary to appear in
the BEP, e.g., if testing is not asked for. Do not announce a test summary
in this case.

--
Change-Id: Ifd46e3582292b087bb1d37f255a140f631854830
Reviewed-on: https://cr.bazel.build/7373
MOS_MIGRATED_REVID=140589645
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/TargetCompleteEvent.java b/src/main/java/com/google/devtools/build/lib/analysis/TargetCompleteEvent.java
index e2ebbb8..aa2e9b4 100644
--- a/src/main/java/com/google/devtools/build/lib/analysis/TargetCompleteEvent.java
+++ b/src/main/java/com/google/devtools/build/lib/analysis/TargetCompleteEvent.java
@@ -24,7 +24,6 @@
 import com.google.devtools.build.lib.collect.nestedset.NestedSet;
 import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
 import com.google.devtools.build.lib.collect.nestedset.Order;
-import com.google.devtools.build.lib.packages.TargetUtils;
 import com.google.devtools.build.lib.util.Preconditions;
 import com.google.devtools.build.skyframe.SkyValue;
 import java.util.Collection;
@@ -35,8 +34,10 @@
   private final ConfiguredTarget target;
   private final NestedSet<Cause> rootCauses;
   private final Collection<BuildEventId> postedAfter;
+  private final boolean isTest;
 
-  private TargetCompleteEvent(ConfiguredTarget target, NestedSet<Cause> rootCauses) {
+  private TargetCompleteEvent(
+      ConfiguredTarget target, NestedSet<Cause> rootCauses, boolean isTest) {
     this.target = target;
     this.rootCauses =
         (rootCauses == null) ? NestedSetBuilder.<Cause>emptySet(Order.STABLE_ORDER) : rootCauses;
@@ -46,21 +47,26 @@
       postedAfterBuilder.add(BuildEventId.fromCause(cause));
     }
     this.postedAfter = postedAfterBuilder.build();
+    this.isTest = isTest;
   }
 
-  /**
-   * Construct a successful target completion event.
-   */
-  public static TargetCompleteEvent createSuccessful(ConfiguredTarget ct) {
-    return new TargetCompleteEvent(ct, null);
+  /** Construct a successful target completion event. */
+  public static TargetCompleteEvent createSuccessfulTarget(ConfiguredTarget ct) {
+    return new TargetCompleteEvent(ct, null, false);
   }
 
+  /** Construct a successful target completion event for a target that will be tested. */
+  public static TargetCompleteEvent createSuccessfulTestTarget(ConfiguredTarget ct) {
+    return new TargetCompleteEvent(ct, null, true);
+  }
+
+
   /**
    * Construct a target completion event for a failed target, with the given non-empty root causes.
    */
   public static TargetCompleteEvent createFailed(ConfiguredTarget ct, NestedSet<Cause> rootCauses) {
     Preconditions.checkArgument(!Iterables.isEmpty(rootCauses));
-    return new TargetCompleteEvent(ct, rootCauses);
+    return new TargetCompleteEvent(ct, rootCauses, false);
   }
 
   /**
@@ -93,7 +99,7 @@
     for (Cause cause : getRootCauses()) {
       childrenBuilder.add(BuildEventId.fromCause(cause));
     }
-    if (TargetUtils.isTestRule(target.getTarget())) {
+    if (isTest) {
       childrenBuilder.add(BuildEventId.testSummary(target.getTarget().getLabel()));
     }
     return childrenBuilder.build();
diff --git a/src/main/java/com/google/devtools/build/lib/buildtool/ExecutionProgressReceiver.java b/src/main/java/com/google/devtools/build/lib/buildtool/ExecutionProgressReceiver.java
index da0480d..cb18260 100644
--- a/src/main/java/com/google/devtools/build/lib/buildtool/ExecutionProgressReceiver.java
+++ b/src/main/java/com/google/devtools/build/lib/buildtool/ExecutionProgressReceiver.java
@@ -32,7 +32,6 @@
 import com.google.devtools.build.skyframe.SkyFunctionName;
 import com.google.devtools.build.skyframe.SkyKey;
 import com.google.devtools.build.skyframe.SkyValue;
-
 import java.text.NumberFormat;
 import java.util.Collections;
 import java.util.Locale;
@@ -55,6 +54,7 @@
   private final Object activityIndicator = new Object();
   /** Number of exclusive tests. To be accounted for in progress messages. */
   private final int exclusiveTestsCount;
+  private final Set<ConfiguredTarget> testedTargets;
   private final EventBus eventBus;
 
   static {
@@ -63,14 +63,18 @@
   }
 
   /**
-   * {@code builtTargets} is accessed through a synchronized set, and so no other access to it
-   * is permitted while this receiver is active.
+   * {@code builtTargets} is accessed through a synchronized set, and so no other access to it is
+   * permitted while this receiver is active.
    */
   ExecutionProgressReceiver(
-      Set<ConfiguredTarget> builtTargets, int exclusiveTestsCount, EventBus eventBus) {
+      Set<ConfiguredTarget> builtTargets,
+      int exclusiveTestsCount,
+      Set<ConfiguredTarget> testedTargets,
+      EventBus eventBus) {
     this.builtTargets = Collections.synchronizedSet(builtTargets);
     this.exclusiveTestsCount = exclusiveTestsCount;
     this.eventBus = eventBus;
+    this.testedTargets = testedTargets;
   }
 
   @Override
@@ -98,7 +102,11 @@
       if (value != null) {
         ConfiguredTarget target = value.getConfiguredTarget();
         builtTargets.add(target);
-        eventBus.post(TargetCompleteEvent.createSuccessful(target));
+        if (testedTargets.contains(target)) {
+          eventBus.post(TargetCompleteEvent.createSuccessfulTestTarget(target));
+        } else {
+          eventBus.post(TargetCompleteEvent.createSuccessfulTarget(target));
+        }
       }
     } else if (type.equals(SkyFunctions.ASPECT_COMPLETION)) {
       AspectCompletionValue value = (AspectCompletionValue) skyValueSupplier.get();
diff --git a/src/main/java/com/google/devtools/build/lib/buildtool/SkyframeBuilder.java b/src/main/java/com/google/devtools/build/lib/buildtool/SkyframeBuilder.java
index ba0fbe6..c74cfc80 100644
--- a/src/main/java/com/google/devtools/build/lib/buildtool/SkyframeBuilder.java
+++ b/src/main/java/com/google/devtools/build/lib/buildtool/SkyframeBuilder.java
@@ -48,7 +48,6 @@
 import com.google.devtools.build.skyframe.ErrorInfo;
 import com.google.devtools.build.skyframe.EvaluationResult;
 import com.google.devtools.build.skyframe.SkyKey;
-
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Comparator;
@@ -58,7 +57,6 @@
 import java.util.Set;
 import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.logging.Level;
-
 import javax.annotation.Nullable;
 
 /**
@@ -110,8 +108,14 @@
     // Note that executionProgressReceiver accesses builtTargets concurrently (after wrapping in a
     // synchronized collection), so unsynchronized access to this variable is unsafe while it runs.
     ExecutionProgressReceiver executionProgressReceiver =
-        new ExecutionProgressReceiver(Preconditions.checkNotNull(builtTargets),
-            countTestActions(exclusiveTests), skyframeExecutor.getEventBus());
+        new ExecutionProgressReceiver(
+            Preconditions.checkNotNull(builtTargets),
+            countTestActions(exclusiveTests),
+            ImmutableSet.<ConfiguredTarget>builder()
+                .addAll(parallelTests)
+                .addAll(exclusiveTests)
+                .build(),
+            skyframeExecutor.getEventBus());
     skyframeExecutor
         .getEventBus()
         .post(new ExecutionProgressReceiverAvailableEvent(executionProgressReceiver));
diff --git a/src/test/shell/integration/build_event_stream_test.sh b/src/test/shell/integration/build_event_stream_test.sh
index 9d16b69..c625604 100755
--- a/src/test/shell/integration/build_event_stream_test.sh
+++ b/src/test/shell/integration/build_event_stream_test.sh
@@ -81,6 +81,17 @@
   expect_not_log 'aborted'
 }
 
+function test_build_only() {
+  # When building but not testing a test, there won't be a test summary
+  # (as nothing was tested), so it should not be announced.
+  # Still, no event should only be chained in by progress events.
+  bazel build --experimental_build_event_text_file=$TEST_log pkg:true \
+    || fail "bazel build failed"
+  expect_not_log 'aborted'
+  expect_not_log 'test_summary '
+  expect_log_once '^progress'
+}
+
 function test_multiple_transports() {
   # Verifies usage of multiple build event transports at the same time
     outdir=$(mktemp -d ${TEST_TMPDIR}/bazel.XXXXXXXX)