include test-case resolution at the end of the build log

Added test-case resolution in all log modes.
https://github.com/bazelbuild/bazel/issues/1506

Closes #5429.

PiperOrigin-RevId: 210362746
diff --git a/src/test/java/com/google/devtools/build/lib/runtime/TerminalTestResultNotifierTest.java b/src/test/java/com/google/devtools/build/lib/runtime/TerminalTestResultNotifierTest.java
new file mode 100644
index 0000000..af2e211
--- /dev/null
+++ b/src/test/java/com/google/devtools/build/lib/runtime/TerminalTestResultNotifierTest.java
@@ -0,0 +1,129 @@
+// Copyright 2015 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.runtime;
+
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import com.google.common.collect.ImmutableMap;
+import com.google.devtools.build.lib.cmdline.Label;
+import com.google.devtools.build.lib.exec.ExecutionOptions;
+import com.google.devtools.build.lib.exec.TestStrategy.TestSummaryFormat;
+import com.google.devtools.build.lib.runtime.TerminalTestResultNotifier.TestSummaryOptions;
+import com.google.devtools.build.lib.util.io.AnsiTerminalPrinter;
+import com.google.devtools.build.lib.view.test.TestStatus.BlazeTestStatus;
+import com.google.devtools.build.lib.view.test.TestStatus.TestCase;
+import com.google.devtools.build.lib.view.test.TestStatus.TestCase.Status;
+import com.google.devtools.common.options.OptionsParsingResult;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Random;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+import org.mockito.Mockito;
+
+/** Tests {@link TerminalTestResultNotifier}. */
+@RunWith(JUnit4.class)
+public class TerminalTestResultNotifierTest {
+
+  private final OptionsParsingResult optionsParsingResult =
+      Mockito.mock(OptionsParsingResult.class);
+  private final AnsiTerminalPrinter ansiTerminalPrinter = Mockito.mock(AnsiTerminalPrinter.class);
+  private final Random random = new Random();
+
+  private void givenExecutionOption(TestSummaryFormat format) {
+    ExecutionOptions executionOptions = ExecutionOptions.DEFAULTS;
+    executionOptions.testSummary = format;
+    when(optionsParsingResult.getOptions(ExecutionOptions.class)).thenReturn(executionOptions);
+
+    TestSummaryOptions testSummaryOptions = new TestSummaryOptions();
+    testSummaryOptions.verboseSummary = true;
+    when(optionsParsingResult.getOptions(TestSummaryOptions.class)).thenReturn(testSummaryOptions);
+  }
+
+  private void runTest(Boolean shouldPrintTestCaseSummary) throws Exception {
+    int numOfTotalTestCases = random.nextInt(10) + 1;
+    int numOfFailedCases = random.nextInt(numOfTotalTestCases);
+    int numOfSuccessfulTestCases = numOfTotalTestCases - numOfFailedCases;
+
+    TerminalTestResultNotifier terminalTestResultNotifier =
+        new TerminalTestResultNotifier(ansiTerminalPrinter, optionsParsingResult);
+
+    TestSummary testSummary = Mockito.mock(TestSummary.class);
+    when(testSummary.getTotalTestCases()).thenReturn(numOfTotalTestCases);
+    TestCase failedTestCase = TestCase.newBuilder().setStatus(Status.FAILED).build();
+    ArrayList<TestCase> testCases =
+        new ArrayList<>(Collections.nCopies(numOfFailedCases, failedTestCase));
+
+    Label labelA = Label.parseAbsolute("//foo/bar:baz", ImmutableMap.of());
+    when(testSummary.getFailedTestCases()).thenReturn(testCases);
+    when(testSummary.getStatus()).thenReturn(BlazeTestStatus.FAILED);
+    when(testSummary.getLabel()).thenReturn(labelA);
+
+    HashSet<TestSummary> testSummaries = new HashSet<>();
+    testSummaries.add(testSummary);
+    terminalTestResultNotifier.notify(testSummaries, 1);
+
+    String summaryMessage =
+        String.format(
+            "Test cases: finished with %s%d passing%s and %s%d failing%s out of %d test cases",
+            numOfSuccessfulTestCases > 0 ? AnsiTerminalPrinter.Mode.INFO : "",
+            numOfSuccessfulTestCases,
+            AnsiTerminalPrinter.Mode.DEFAULT,
+            numOfFailedCases > 0 ? AnsiTerminalPrinter.Mode.ERROR : "",
+            numOfFailedCases,
+            AnsiTerminalPrinter.Mode.DEFAULT,
+            numOfTotalTestCases);
+
+    if (shouldPrintTestCaseSummary) {
+      verify(ansiTerminalPrinter).printLn(summaryMessage);
+    } else {
+      verify(ansiTerminalPrinter, never()).printLn(summaryMessage);
+    }
+  }
+
+  @Test
+  public void testCasesDataVisibleInTestCaseOption() throws Exception {
+    givenExecutionOption(TestSummaryFormat.TESTCASE);
+    runTest(true);
+  }
+
+  @Test
+  public void testCasesDataVisibleInDetailedOption() throws Exception {
+    givenExecutionOption(TestSummaryFormat.DETAILED);
+    runTest(true);
+  }
+
+  @Test
+  public void testCasesDataInVisibleInShortOption() throws Exception {
+    givenExecutionOption(TestSummaryFormat.SHORT);
+    runTest(false);
+  }
+
+  @Test
+  public void testCasesDataInVisibleInTerseOption() throws Exception {
+    givenExecutionOption(TestSummaryFormat.TERSE);
+    runTest(false);
+  }
+
+  @Test
+  public void testCasesDataInVisibleInNoneOption() throws Exception {
+    givenExecutionOption(TestSummaryFormat.NONE);
+    runTest(false);
+  }
+}
diff --git a/src/test/java/com/google/devtools/build/lib/runtime/TestSummaryTest.java b/src/test/java/com/google/devtools/build/lib/runtime/TestSummaryTest.java
index ee0f08c..c48ebbc 100644
--- a/src/test/java/com/google/devtools/build/lib/runtime/TestSummaryTest.java
+++ b/src/test/java/com/google/devtools/build/lib/runtime/TestSummaryTest.java
@@ -108,7 +108,7 @@
     TestSummary summary = createTestSummary(stubTarget, BlazeTestStatus.PASSED, NOT_CACHED);
     TestSummaryPrinter.print(summary, terminalPrinter, true, false);
 
-    terminalPrinter.print(find(expectedString));
+    verify(terminalPrinter).print(find(expectedString));
   }
 
   @Test
@@ -224,6 +224,7 @@
         .build();
     assertThat(failedCacheTemplate.numCached()).isEqualTo(50);
     assertThat(failedCacheTemplate.getStatus()).isEqualTo(BlazeTestStatus.FAILED);
+    assertThat(failedCacheTemplate.getTotalTestCases()).isEqualTo(fiftyCached.getTotalTestCases());
   }
 
   @Test
@@ -437,6 +438,7 @@
   public void testCollectingFailedDetails() throws Exception {
     TestCase rootCase = TestCase.newBuilder()
         .setName("tests")
+        .setClassName("testclass")
         .setRunDurationMillis(5000L)
         .addChild(newDetail("apple", TestCase.Status.FAILED, 1000L))
         .addChild(newDetail("banana", TestCase.Status.PASSED, 1000L))
@@ -455,6 +457,57 @@
     verify(printer).print(find("ERROR.*cherry"));
   }
 
+  @Test
+  public void countTotalTestCases() throws Exception {
+    TestCase rootCase =
+        TestCase.newBuilder()
+            .setName("tests")
+            .setRunDurationMillis(5000L)
+            .addChild(newDetail("apple", TestCase.Status.FAILED, 1000L))
+            .addChild(newDetail("banana", TestCase.Status.PASSED, 1000L))
+            .addChild(newDetail("cherry", TestCase.Status.ERROR, 1000L))
+            .build();
+
+    TestSummary summary =
+        getTemplateBuilder()
+            .countTotalTestCases(rootCase)
+            .setStatus(BlazeTestStatus.FAILED)
+            .build();
+
+    assertThat(summary.getTotalTestCases()).isEqualTo(3);
+  }
+
+  @Test
+  public void countTotalTestCasesInNestedTree() throws Exception {
+    TestCase aCase =
+        TestCase.newBuilder()
+            .setName("tests-1")
+            .setRunDurationMillis(5000L)
+            .addChild(newDetail("apple", TestCase.Status.FAILED, 1000L))
+            .addChild(newDetail("banana", TestCase.Status.PASSED, 1000L))
+            .addChild(newDetail("cherry", TestCase.Status.ERROR, 1000L))
+            .build();
+    TestCase anotherCase =
+        TestCase.newBuilder()
+            .setName("tests-2")
+            .setRunDurationMillis(5000L)
+            .addChild(newDetail("apple", TestCase.Status.FAILED, 1000L))
+            .addChild(newDetail("banana", TestCase.Status.PASSED, 1000L))
+            .addChild(newDetail("cherry", TestCase.Status.ERROR, 1000L))
+            .build();
+
+    TestCase rootCase =
+        TestCase.newBuilder().setName("tests").addChild(aCase).addChild(anotherCase).build();
+
+    TestSummary summary =
+        getTemplateBuilder()
+            .countTotalTestCases(rootCase)
+            .setStatus(BlazeTestStatus.FAILED)
+            .build();
+
+    assertThat(summary.getTotalTestCases()).isEqualTo(6);
+  }
+
   private ConfiguredTarget target(String path, String targetName) throws Exception {
     ConfiguredTarget target = Mockito.mock(ConfiguredTarget.class);
     when(target.getLabel()).thenReturn(Label.create(path, targetName));