blob: c48ebbc03ab18ee172e0d089a6b96edc5038242b [file] [log] [blame]
Kristina Chodorowaa8b1522015-10-12 14:43:05 +00001// Copyright 2015 The Bazel Authors. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package com.google.devtools.build.lib.runtime;
16
lberkiaea56b32017-05-30 12:35:33 +020017import static com.google.common.truth.Truth.assertThat;
Kristina Chodorowaa8b1522015-10-12 14:43:05 +000018import static org.mockito.AdditionalMatchers.find;
19import static org.mockito.AdditionalMatchers.not;
Janak Ramakrishnana9493152016-03-18 13:32:07 +000020import static org.mockito.Matchers.anyString;
Kristina Chodorowaa8b1522015-10-12 14:43:05 +000021import static org.mockito.Matchers.contains;
Janak Ramakrishnana9493152016-03-18 13:32:07 +000022import static org.mockito.Mockito.never;
Florian Weikerte89ee4e2015-12-03 15:38:30 +000023import static org.mockito.Mockito.verify;
Kristina Chodorowaa8b1522015-10-12 14:43:05 +000024import static org.mockito.Mockito.when;
25
26import com.google.common.collect.ImmutableList;
27import com.google.devtools.build.lib.analysis.ConfiguredTarget;
lberkib08ed812017-11-02 18:05:56 -040028import com.google.devtools.build.lib.analysis.config.BuildConfiguration;
philwo3bcb9f62017-09-06 12:52:21 +020029import com.google.devtools.build.lib.clock.BlazeClock;
Kristina Chodorowaa8b1522015-10-12 14:43:05 +000030import com.google.devtools.build.lib.cmdline.Label;
Kristina Chodorowaa8b1522015-10-12 14:43:05 +000031import com.google.devtools.build.lib.util.io.AnsiTerminalPrinter;
32import com.google.devtools.build.lib.vfs.FileSystem;
33import com.google.devtools.build.lib.vfs.FileSystemUtils;
34import com.google.devtools.build.lib.vfs.Path;
35import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem;
36import com.google.devtools.build.lib.view.test.TestStatus.BlazeTestStatus;
37import com.google.devtools.build.lib.view.test.TestStatus.FailedTestCasesStatus;
38import com.google.devtools.build.lib.view.test.TestStatus.TestCase;
Googler7807b6c2017-03-14 10:57:43 +000039import java.util.ArrayList;
40import java.util.Arrays;
41import java.util.List;
Kristina Chodorowaa8b1522015-10-12 14:43:05 +000042import org.junit.Before;
43import org.junit.Test;
44import org.junit.runner.RunWith;
45import org.junit.runners.JUnit4;
46import org.mockito.InOrder;
47import org.mockito.Mockito;
48
Kristina Chodorowaa8b1522015-10-12 14:43:05 +000049@RunWith(JUnit4.class)
50public class TestSummaryTest {
51
52 private static final String ANY_STRING = ".*?";
53 private static final String PATH = "package";
54 private static final String TARGET_NAME = "name";
55 private ConfiguredTarget stubTarget;
Googler7807b6c2017-03-14 10:57:43 +000056 private static final ImmutableList<Long> SMALL_TIMING = ImmutableList.of(1L, 2L, 3L, 4L);
Kristina Chodorowaa8b1522015-10-12 14:43:05 +000057
58 private static final int CACHED = SMALL_TIMING.size();
59 private static final int NOT_CACHED = 0;
60
61 private FileSystem fs;
62 private TestSummary.Builder basicBuilder;
63
64 @Before
Florian Weikerte89ee4e2015-12-03 15:38:30 +000065 public final void createFileSystem() throws Exception {
ccalvarinc9efd062018-07-27 12:46:46 -070066 fs = new InMemoryFileSystem(BlazeClock.instance());
Kristina Chodorowaa8b1522015-10-12 14:43:05 +000067 stubTarget = stubTarget();
68 basicBuilder = getTemplateBuilder();
69 }
70
71 private TestSummary.Builder getTemplateBuilder() {
janakr73088a82018-03-27 18:01:46 -070072 BuildConfiguration configuration = Mockito.mock(BuildConfiguration.class);
73 when(configuration.checksum()).thenReturn("abcdef");
Kristina Chodorowaa8b1522015-10-12 14:43:05 +000074 return TestSummary.newBuilder()
75 .setTarget(stubTarget)
janakr73088a82018-03-27 18:01:46 -070076 .setConfiguration(configuration)
Kristina Chodorowaa8b1522015-10-12 14:43:05 +000077 .setStatus(BlazeTestStatus.PASSED)
78 .setNumCached(NOT_CACHED)
79 .setActionRan(true)
80 .setRanRemotely(false)
81 .setWasUnreportedWrongSize(false);
82 }
83
84 private List<Path> getPathList(String... names) {
85 List<Path> list = new ArrayList<>();
86 for (String name : names) {
87 list.add(fs.getPath(name));
88 }
89 return list;
90 }
91
92 @Test
93 public void testShouldProperlyTestLabels() throws Exception {
94 ConfiguredTarget target = target("somepath", "MyTarget");
95 String expectedString = ANY_STRING + "//somepath:MyTarget" + ANY_STRING;
96 AnsiTerminalPrinter terminalPrinter = Mockito.mock(AnsiTerminalPrinter.class);
97
98 TestSummary summaryStatus = createTestSummary(target, BlazeTestStatus.PASSED, CACHED);
99 TestSummaryPrinter.print(summaryStatus, terminalPrinter, true, false);
100 terminalPrinter.print(find(expectedString));
101 }
102
103 @Test
104 public void testShouldPrintPassedStatus() throws Exception {
105 String expectedString = ANY_STRING + "INFO" + ANY_STRING + BlazeTestStatus.PASSED + ANY_STRING;
106 AnsiTerminalPrinter terminalPrinter = Mockito.mock(AnsiTerminalPrinter.class);
107
108 TestSummary summary = createTestSummary(stubTarget, BlazeTestStatus.PASSED, NOT_CACHED);
109 TestSummaryPrinter.print(summary, terminalPrinter, true, false);
110
Eran Shalomf1db75c2018-08-27 07:09:19 -0700111 verify(terminalPrinter).print(find(expectedString));
Kristina Chodorowaa8b1522015-10-12 14:43:05 +0000112 }
113
114 @Test
115 public void testShouldPrintFailedStatus() throws Exception {
116 String expectedString = ANY_STRING + "ERROR" + ANY_STRING + BlazeTestStatus.FAILED + ANY_STRING;
117 AnsiTerminalPrinter terminalPrinter = Mockito.mock(AnsiTerminalPrinter.class);
118
119 TestSummary summary = createTestSummary(stubTarget, BlazeTestStatus.FAILED, NOT_CACHED);
120
121 TestSummaryPrinter.print(summary, terminalPrinter, true, false);
122
123 terminalPrinter.print(find(expectedString));
124 }
125
Janak Ramakrishnana9493152016-03-18 13:32:07 +0000126 private void assertShouldNotPrint(BlazeTestStatus status) throws Exception {
127 AnsiTerminalPrinter terminalPrinter = Mockito.mock(AnsiTerminalPrinter.class);
128 TestSummaryPrinter.print(
129 createTestSummary(stubTarget, status, NOT_CACHED), terminalPrinter, true, false);
130 verify(terminalPrinter, never()).print(anyString());
131 }
132
133 @Test
134 public void testShouldNotPrintFailedToBuildStatus() throws Exception {
135 assertShouldNotPrint(BlazeTestStatus.FAILED_TO_BUILD);
136 }
137
138 @Test
139 public void testShouldNotPrintHaltedStatus() throws Exception {
140 assertShouldNotPrint(BlazeTestStatus.BLAZE_HALTED_BEFORE_TESTING);
141 }
142
Kristina Chodorowaa8b1522015-10-12 14:43:05 +0000143 @Test
144 public void testShouldPrintCachedStatus() throws Exception {
145 String expectedString = ANY_STRING + "\\(cached" + ANY_STRING;
146 AnsiTerminalPrinter terminalPrinter = Mockito.mock(AnsiTerminalPrinter.class);
147
148 TestSummary summary = createTestSummary(stubTarget, BlazeTestStatus.PASSED, CACHED);
149
150 TestSummaryPrinter.print(summary, terminalPrinter, true, false);
151
152 terminalPrinter.print(find(expectedString));
153 }
154
155 @Test
156 public void testPartialCachedStatus() throws Exception {
157 String expectedString = ANY_STRING + "\\(3/4 cached" + ANY_STRING;
158 AnsiTerminalPrinter terminalPrinter = Mockito.mock(AnsiTerminalPrinter.class);
159
160 TestSummary summary = createTestSummary(stubTarget, BlazeTestStatus.PASSED, CACHED - 1);
161 TestSummaryPrinter.print(summary, terminalPrinter, true, false);
162 terminalPrinter.print(find(expectedString));
163 }
164
165 @Test
166 public void testIncompleteCached() throws Exception {
167 AnsiTerminalPrinter terminalPrinter = Mockito.mock(AnsiTerminalPrinter.class);
168 TestSummary summary = createTestSummary(stubTarget, BlazeTestStatus.INCOMPLETE, CACHED - 1);
169 TestSummaryPrinter.print(summary, terminalPrinter, true, false);
170 verify(terminalPrinter).print(not(contains("cached")));
171 }
172
173 @Test
174 public void testShouldPrintUncachedStatus() throws Exception {
175 AnsiTerminalPrinter terminalPrinter = Mockito.mock(AnsiTerminalPrinter.class);
176 TestSummary summary = createTestSummary(stubTarget, BlazeTestStatus.PASSED, NOT_CACHED);
177 TestSummaryPrinter.print(summary, terminalPrinter, true, false);
178 verify(terminalPrinter).print(not(contains("cached")));
179 }
180
181 @Test
182 public void testNoTiming() throws Exception {
183 String expectedString = ANY_STRING + "INFO" + ANY_STRING + BlazeTestStatus.PASSED;
184 AnsiTerminalPrinter terminalPrinter = Mockito.mock(AnsiTerminalPrinter.class);
185
186 TestSummary summary = createTestSummary(stubTarget, BlazeTestStatus.PASSED, NOT_CACHED);
187
188 TestSummaryPrinter.print(summary, terminalPrinter, true, false);
189 terminalPrinter.print(find(expectedString));
190 }
191
192 @Test
193 public void testBuilder() throws Exception {
194 // No need to copy if built twice in a row; no direct setters on the object.
195 TestSummary summary = basicBuilder.build();
196 TestSummary sameSummary = basicBuilder.build();
lberkiaea56b32017-05-30 12:35:33 +0200197 assertThat(sameSummary).isSameAs(summary);
Kristina Chodorowaa8b1522015-10-12 14:43:05 +0000198
199 basicBuilder.addTestTimes(ImmutableList.of(40L));
200
201 TestSummary summaryCopy = basicBuilder.build();
lberkiaea56b32017-05-30 12:35:33 +0200202 assertThat(summaryCopy.getTarget()).isEqualTo(summary.getTarget());
203 assertThat(summaryCopy.getStatus()).isEqualTo(summary.getStatus());
204 assertThat(summaryCopy.numCached()).isEqualTo(summary.numCached());
205 assertThat(summaryCopy).isNotSameAs(summary);
206 assertThat(summary.totalRuns()).isEqualTo(0);
207 assertThat(summaryCopy.totalRuns()).isEqualTo(1);
Kristina Chodorowaa8b1522015-10-12 14:43:05 +0000208
209 // Check that the builder can add a new warning to the copy,
210 // despite the immutability of the original.
211 basicBuilder.addTestTimes(ImmutableList.of(60L));
212
213 TestSummary fiftyCached = basicBuilder.setNumCached(50).build();
lberkiaea56b32017-05-30 12:35:33 +0200214 assertThat(fiftyCached.getStatus()).isEqualTo(summary.getStatus());
215 assertThat(fiftyCached.numCached()).isEqualTo(50);
216 assertThat(fiftyCached.totalRuns()).isEqualTo(2);
Kristina Chodorowaa8b1522015-10-12 14:43:05 +0000217
218 TestSummary sixtyCached = basicBuilder.setNumCached(60).build();
lberkiaea56b32017-05-30 12:35:33 +0200219 assertThat(sixtyCached.numCached()).isEqualTo(60);
220 assertThat(fiftyCached.numCached()).isEqualTo(50);
Kristina Chodorowaa8b1522015-10-12 14:43:05 +0000221
222 TestSummary failedCacheTemplate = TestSummary.newBuilderFromExisting(fiftyCached)
223 .setStatus(BlazeTestStatus.FAILED)
224 .build();
lberkiaea56b32017-05-30 12:35:33 +0200225 assertThat(failedCacheTemplate.numCached()).isEqualTo(50);
226 assertThat(failedCacheTemplate.getStatus()).isEqualTo(BlazeTestStatus.FAILED);
Eran Shalomf1db75c2018-08-27 07:09:19 -0700227 assertThat(failedCacheTemplate.getTotalTestCases()).isEqualTo(fiftyCached.getTotalTestCases());
Kristina Chodorowaa8b1522015-10-12 14:43:05 +0000228 }
229
230 @Test
231 public void testSingleTime() throws Exception {
232 String expectedString = ANY_STRING + "INFO" + ANY_STRING + BlazeTestStatus.PASSED + ANY_STRING +
233 "in 3.4s";
234 AnsiTerminalPrinter terminalPrinter = Mockito.mock(AnsiTerminalPrinter.class);
235
236 TestSummary summary = basicBuilder.addTestTimes(ImmutableList.of(3412L)).build();
237 TestSummaryPrinter.print(summary, terminalPrinter, true, false);
238 terminalPrinter.print(find(expectedString));
239 }
240
241 @Test
242 public void testNoTime() throws Exception {
243 // The last part matches anything not containing "in".
244 String expectedString = ANY_STRING + "INFO" + ANY_STRING + BlazeTestStatus.PASSED + "(?!in)*";
245 AnsiTerminalPrinter terminalPrinter = Mockito.mock(AnsiTerminalPrinter.class);
246
247 TestSummary summary = basicBuilder.addTestTimes(ImmutableList.of(3412L)).build();
248 TestSummaryPrinter.print(summary, terminalPrinter, false, false);
249 terminalPrinter.print(find(expectedString));
250 }
251
252 @Test
253 public void testMultipleTimes() throws Exception {
254 String expectedString = ANY_STRING + "INFO" + ANY_STRING + BlazeTestStatus.PASSED + ANY_STRING +
255 "\n Stats over 3 runs: max = 3.0s, min = 1.0s, " +
256 "avg = 2.0s, dev = 0.8s";
257 AnsiTerminalPrinter terminalPrinter = Mockito.mock(AnsiTerminalPrinter.class);
258 TestSummary summary = basicBuilder
259 .addTestTimes(ImmutableList.of(1000L, 2000L, 3000L))
260 .build();
261 TestSummaryPrinter.print(summary, terminalPrinter, true, false);
262 terminalPrinter.print(find(expectedString));
263 }
264
265 @Test
266 public void testCoverageDataReferences() throws Exception {
267 List<Path> paths = getPathList("/cov1.dat", "/cov2.dat", "/cov3.dat", "/cov4.dat");
268 FileSystemUtils.writeContentAsLatin1(paths.get(1), "something");
269 FileSystemUtils.writeContentAsLatin1(paths.get(3), "");
270 FileSystemUtils.writeContentAsLatin1(paths.get(3), "something else");
271 TestSummary summary = basicBuilder.addCoverageFiles(paths).build();
272
273 AnsiTerminalPrinter terminalPrinter = Mockito.mock(AnsiTerminalPrinter.class);
274 TestSummaryPrinter.print(summary, terminalPrinter, true, false);
275 verify(terminalPrinter).print(find(ANY_STRING + "INFO" + ANY_STRING + BlazeTestStatus.PASSED));
276 verify(terminalPrinter).print(find(" /cov2.dat"));
277 verify(terminalPrinter).print(find(" /cov4.dat"));
278 }
279
280 @Test
281 public void testFlakyAttempts() throws Exception {
282 String expectedString = ANY_STRING + "WARNING" + ANY_STRING + BlazeTestStatus.FLAKY +
283 ANY_STRING + ", failed in 2 out of 3";
284 AnsiTerminalPrinter terminalPrinter = Mockito.mock(AnsiTerminalPrinter.class);
285
286 TestSummary summary = basicBuilder
287 .setStatus(BlazeTestStatus.FLAKY)
288 .addPassedLogs(getPathList("/a"))
289 .addFailedLogs(getPathList("/b", "/c"))
290 .build();
291 TestSummaryPrinter.print(summary, terminalPrinter, true, false);
292 terminalPrinter.print(find(expectedString));
293 }
294
295 @Test
296 public void testNumberOfFailedRuns() throws Exception {
297 String expectedString = ANY_STRING + "ERROR" + ANY_STRING + BlazeTestStatus.FAILED +
298 ANY_STRING + "in 2 out of 3";
299 AnsiTerminalPrinter terminalPrinter = Mockito.mock(AnsiTerminalPrinter.class);
300
301 TestSummary summary = basicBuilder
302 .setStatus(BlazeTestStatus.FAILED)
303 .addPassedLogs(getPathList("/a"))
304 .addFailedLogs(getPathList("/b", "/c"))
305 .build();
306 TestSummaryPrinter.print(summary, terminalPrinter, true, false);
307 terminalPrinter.print(find(expectedString));
308 }
309
310 @Test
311 public void testFileNamesNotShown() throws Exception {
312 List<TestCase> emptyDetails = ImmutableList.of();
313 TestSummary summary = basicBuilder
314 .setStatus(BlazeTestStatus.FAILED)
315 .addPassedLogs(getPathList("/apple"))
316 .addFailedLogs(getPathList("/pear"))
317 .addCoverageFiles(getPathList("/maracuja"))
318 .addFailedTestCases(emptyDetails, FailedTestCasesStatus.FULL)
319 .build();
320
321 // Check that only //package:name is printed.
322 AnsiTerminalPrinter printer = Mockito.mock(AnsiTerminalPrinter.class);
323 TestSummaryPrinter.print(summary, printer, true, true);
324 verify(printer).print(contains("//package:name"));
325 }
326
327 @Test
328 public void testMessageShownWhenTestCasesMissing() throws Exception {
329 ImmutableList<TestCase> emptyList = ImmutableList.of();
330 TestSummary summary = createTestSummaryWithDetails(
331 BlazeTestStatus.FAILED, emptyList, FailedTestCasesStatus.NOT_AVAILABLE);
332
333 AnsiTerminalPrinter printer = Mockito.mock(AnsiTerminalPrinter.class);
334 TestSummaryPrinter.print(summary, printer, true, true);
335 verify(printer).print(contains("//package:name"));
336 verify(printer).print(contains("not available"));
337 }
338
339 @Test
340 public void testMessageShownForPartialResults() throws Exception {
341 ImmutableList<TestCase> testCases =
342 ImmutableList.of(newDetail("orange", TestCase.Status.FAILED, 1500L));
343 TestSummary summary = createTestSummaryWithDetails(BlazeTestStatus.FAILED, testCases,
344 FailedTestCasesStatus.PARTIAL);
345
346 AnsiTerminalPrinter printer = Mockito.mock(AnsiTerminalPrinter.class);
347 TestSummaryPrinter.print(summary, printer, true, true);
348 verify(printer).print(contains("//package:name"));
349 verify(printer).print(find("FAILED.*orange"));
350 verify(printer).print(contains("incomplete"));
351 }
352
353 private TestCase newDetail(String name, TestCase.Status status, long duration) {
354 return TestCase.newBuilder()
355 .setName(name)
356 .setStatus(status)
357 .setRunDurationMillis(duration).build();
358 }
359
360 @Test
361 public void testTestCaseNamesShownWhenNeeded() throws Exception {
362 TestCase detailPassed =
363 newDetail("strawberry", TestCase.Status.PASSED, 1000L);
364 TestCase detailFailed =
365 newDetail("orange", TestCase.Status.FAILED, 1500L);
366
367 TestSummary summaryPassed = createTestSummaryWithDetails(
368 BlazeTestStatus.PASSED, Arrays.asList(detailPassed));
369
370 TestSummary summaryFailed = createTestSummaryWithDetails(
371 BlazeTestStatus.FAILED, Arrays.asList(detailPassed, detailFailed));
lberkiaea56b32017-05-30 12:35:33 +0200372 assertThat(summaryFailed.getStatus()).isEqualTo(BlazeTestStatus.FAILED);
Kristina Chodorowaa8b1522015-10-12 14:43:05 +0000373
374 AnsiTerminalPrinter printerPassed = Mockito.mock(AnsiTerminalPrinter.class);
375 TestSummaryPrinter.print(summaryPassed, printerPassed, true, true);
376 verify(printerPassed).print(contains("//package:name"));
377
378 AnsiTerminalPrinter printerFailed = Mockito.mock(AnsiTerminalPrinter.class);
379 TestSummaryPrinter.print(summaryFailed, printerFailed, true, true);
380 verify(printerFailed).print(contains("//package:name"));
381 verify(printerFailed).print(find("FAILED.*orange *\\(1\\.5"));
382 }
383
384 @Test
385 public void testTestCaseNamesOrdered() throws Exception {
386 TestCase[] details = {
387 newDetail("apple", TestCase.Status.FAILED, 1000L),
388 newDetail("banana", TestCase.Status.FAILED, 1000L),
389 newDetail("cranberry", TestCase.Status.FAILED, 1000L)
390 };
391
392 // The exceedingly dumb approach: writing all the permutations down manually
393 // is simply easier than any way of generating them.
394 int[][] permutations = {
395 { 0, 1, 2 },
396 { 0, 2, 1 },
397 { 1, 0, 2 },
398 { 1, 2, 0 },
399 { 2, 0, 1 },
400 { 2, 1, 0 }
401 };
402
403 for (int[] permutation : permutations) {
404 List<TestCase> permutatedDetails = new ArrayList<>();
405
406 for (int element : permutation) {
407 permutatedDetails.add(details[element]);
408 }
409
410 TestSummary summary = createTestSummaryWithDetails(BlazeTestStatus.FAILED, permutatedDetails);
411
412 // A mock that checks the ordering of method calls
413 AnsiTerminalPrinter printer = Mockito.mock(AnsiTerminalPrinter.class);
414 TestSummaryPrinter.print(summary, printer, true, true);
415 InOrder order = Mockito.inOrder(printer);
416 order.verify(printer).print(contains("//package:name"));
417 order.verify(printer).print(find("FAILED.*apple"));
418 order.verify(printer).print(find("FAILED.*banana"));
419 order.verify(printer).print(find("FAILED.*cranberry"));
420 }
421 }
422
423 @Test
424 public void testCachedResultsFirstInSort() throws Exception {
425 TestSummary summaryFailedCached = createTestSummary(BlazeTestStatus.FAILED, CACHED);
426 TestSummary summaryFailedNotCached = createTestSummary(BlazeTestStatus.FAILED, NOT_CACHED);
427 TestSummary summaryPassedCached = createTestSummary(BlazeTestStatus.PASSED, CACHED);
428 TestSummary summaryPassedNotCached = createTestSummary(BlazeTestStatus.PASSED, NOT_CACHED);
429
430 // This way we can make the test independent from the sort order of FAILEd
431 // and PASSED.
432
lberkiaea56b32017-05-30 12:35:33 +0200433 assertThat(summaryFailedCached.compareTo(summaryPassedNotCached)).isLessThan(0);
434 assertThat(summaryPassedCached.compareTo(summaryFailedNotCached)).isLessThan(0);
Kristina Chodorowaa8b1522015-10-12 14:43:05 +0000435 }
436
437 @Test
438 public void testCollectingFailedDetails() throws Exception {
439 TestCase rootCase = TestCase.newBuilder()
440 .setName("tests")
Eran Shalomf1db75c2018-08-27 07:09:19 -0700441 .setClassName("testclass")
Kristina Chodorowaa8b1522015-10-12 14:43:05 +0000442 .setRunDurationMillis(5000L)
443 .addChild(newDetail("apple", TestCase.Status.FAILED, 1000L))
444 .addChild(newDetail("banana", TestCase.Status.PASSED, 1000L))
445 .addChild(newDetail("cherry", TestCase.Status.ERROR, 1000L))
446 .build();
447
448 TestSummary summary = getTemplateBuilder()
449 .collectFailedTests(rootCase)
450 .setStatus(BlazeTestStatus.FAILED)
451 .build();
452
453 AnsiTerminalPrinter printer = Mockito.mock(AnsiTerminalPrinter.class);
454 TestSummaryPrinter.print(summary, printer, true, true);
455 verify(printer).print(contains("//package:name"));
456 verify(printer).print(find("FAILED.*apple"));
457 verify(printer).print(find("ERROR.*cherry"));
458 }
459
Eran Shalomf1db75c2018-08-27 07:09:19 -0700460 @Test
461 public void countTotalTestCases() throws Exception {
462 TestCase rootCase =
463 TestCase.newBuilder()
464 .setName("tests")
465 .setRunDurationMillis(5000L)
466 .addChild(newDetail("apple", TestCase.Status.FAILED, 1000L))
467 .addChild(newDetail("banana", TestCase.Status.PASSED, 1000L))
468 .addChild(newDetail("cherry", TestCase.Status.ERROR, 1000L))
469 .build();
470
471 TestSummary summary =
472 getTemplateBuilder()
473 .countTotalTestCases(rootCase)
474 .setStatus(BlazeTestStatus.FAILED)
475 .build();
476
477 assertThat(summary.getTotalTestCases()).isEqualTo(3);
478 }
479
480 @Test
481 public void countTotalTestCasesInNestedTree() throws Exception {
482 TestCase aCase =
483 TestCase.newBuilder()
484 .setName("tests-1")
485 .setRunDurationMillis(5000L)
486 .addChild(newDetail("apple", TestCase.Status.FAILED, 1000L))
487 .addChild(newDetail("banana", TestCase.Status.PASSED, 1000L))
488 .addChild(newDetail("cherry", TestCase.Status.ERROR, 1000L))
489 .build();
490 TestCase anotherCase =
491 TestCase.newBuilder()
492 .setName("tests-2")
493 .setRunDurationMillis(5000L)
494 .addChild(newDetail("apple", TestCase.Status.FAILED, 1000L))
495 .addChild(newDetail("banana", TestCase.Status.PASSED, 1000L))
496 .addChild(newDetail("cherry", TestCase.Status.ERROR, 1000L))
497 .build();
498
499 TestCase rootCase =
500 TestCase.newBuilder().setName("tests").addChild(aCase).addChild(anotherCase).build();
501
502 TestSummary summary =
503 getTemplateBuilder()
504 .countTotalTestCases(rootCase)
505 .setStatus(BlazeTestStatus.FAILED)
506 .build();
507
508 assertThat(summary.getTotalTestCases()).isEqualTo(6);
509 }
510
Kristina Chodorowaa8b1522015-10-12 14:43:05 +0000511 private ConfiguredTarget target(String path, String targetName) throws Exception {
512 ConfiguredTarget target = Mockito.mock(ConfiguredTarget.class);
513 when(target.getLabel()).thenReturn(Label.create(path, targetName));
janakr73088a82018-03-27 18:01:46 -0700514 when(target.getConfigurationChecksum()).thenReturn("abcdef");
Kristina Chodorowaa8b1522015-10-12 14:43:05 +0000515 return target;
516 }
517
518 private ConfiguredTarget stubTarget() throws Exception {
519 return target(PATH, TARGET_NAME);
520 }
521
522 private TestSummary createTestSummaryWithDetails(BlazeTestStatus status,
523 List<TestCase> details) {
524 TestSummary summary = getTemplateBuilder()
525 .setStatus(status)
526 .addFailedTestCases(details, FailedTestCasesStatus.FULL)
527 .build();
528 return summary;
529 }
530
531 private TestSummary createTestSummaryWithDetails(
532 BlazeTestStatus status, List<TestCase> testCaseList,
533 FailedTestCasesStatus detailsStatus) {
534 TestSummary summary = getTemplateBuilder()
535 .setStatus(status)
536 .addFailedTestCases(testCaseList, detailsStatus)
537 .build();
538 return summary;
539 }
540
541 private static TestSummary createTestSummary(ConfiguredTarget target, BlazeTestStatus status,
542 int numCached) {
543 ImmutableList<TestCase> emptyList = ImmutableList.of();
544 TestSummary summary = TestSummary.newBuilder()
545 .setTarget(target)
546 .setStatus(status)
547 .setNumCached(numCached)
548 .setActionRan(true)
549 .setRanRemotely(false)
550 .setWasUnreportedWrongSize(false)
551 .addFailedTestCases(emptyList, FailedTestCasesStatus.FULL)
552 .addTestTimes(SMALL_TIMING)
553 .build();
554 return summary;
555 }
556
557 private TestSummary createTestSummary(BlazeTestStatus status, int numCached) {
558 TestSummary summary = getTemplateBuilder()
559 .setStatus(status)
560 .setNumCached(numCached)
561 .addTestTimes(SMALL_TIMING)
562 .build();
563 return summary;
564 }
565}