forked from bazelbuild/bazel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTerminalTestResultNotifier.java
More file actions
347 lines (318 loc) · 13.5 KB
/
TerminalTestResultNotifier.java
File metadata and controls
347 lines (318 loc) · 13.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// Copyright 2014 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 com.google.devtools.build.lib.exec.ExecutionOptions.TestSummaryFormat.DETAILED;
import static com.google.devtools.build.lib.exec.ExecutionOptions.TestSummaryFormat.DETAILED_UNCACHED;
import static com.google.devtools.build.lib.exec.ExecutionOptions.TestSummaryFormat.SHORT;
import static com.google.devtools.build.lib.exec.ExecutionOptions.TestSummaryFormat.SHORT_UNCACHED;
import static com.google.devtools.build.lib.exec.ExecutionOptions.TestSummaryFormat.TESTCASE;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import com.google.devtools.build.lib.analysis.test.TestResult;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.cmdline.RepositoryMapping;
import com.google.devtools.build.lib.exec.ExecutionOptions;
import com.google.devtools.build.lib.exec.ExecutionOptions.TestOutputFormat;
import com.google.devtools.build.lib.exec.ExecutionOptions.TestSummaryFormat;
import com.google.devtools.build.lib.exec.TestLogHelper;
import com.google.devtools.build.lib.runtime.TestSummaryPrinter.TestLogPathFormatter;
import com.google.devtools.build.lib.util.StringUtil;
import com.google.devtools.build.lib.util.io.AnsiTerminalPrinter;
import com.google.devtools.build.lib.view.test.TestStatus.BlazeTestStatus;
import com.google.devtools.common.options.OptionsParsingResult;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* Prints the test results to a terminal.
*/
public class TerminalTestResultNotifier implements TestResultNotifier {
// The number of failed-to-build tests to report.
// (We do not want to report hundreds of failed-to-build tests as it would probably be caused
// by some intermediate target not related to tests themselves.)
// The total number of failed-to-build tests will be reported in any case.
@VisibleForTesting public static final int NUM_FAILED_TO_BUILD = 5;
private static class TestResultStats {
int numberOfTargets;
int passCount;
int failedToBuildCount;
int failedCount;
int failedRemotelyCount;
int failedLocallyCount;
int noStatusCount;
int numberOfExecutedTargets;
boolean wasUnreportedWrongSize;
int totalTestCases;
int totalFailedTestCases;
int totalSkippedTestCases;
int totalUnknownTestCases;
}
private final AnsiTerminalPrinter printer;
private final TestLogPathFormatter testLogPathFormatter;
private final OptionsParsingResult options;
private final TestSummaryOptions summaryOptions;
private final RepositoryMapping mainRepoMapping;
/**
* @param printer The terminal to print to
*/
public TerminalTestResultNotifier(
AnsiTerminalPrinter printer,
TestLogPathFormatter testLogPathFormatter,
OptionsParsingResult options,
RepositoryMapping mainRepoMapping) {
this.printer = printer;
this.testLogPathFormatter = testLogPathFormatter;
this.options = options;
this.summaryOptions = options.getOptions(TestSummaryOptions.class);
this.mainRepoMapping = mainRepoMapping;
}
/**
* Decide if two tests with the same label are contained in the set of test summaries
*/
private boolean duplicateLabels(Set<TestSummary> summaries) {
Set<Label> labelsSeen = new HashSet<>();
for (TestSummary summary : summaries) {
if (labelsSeen.contains(summary.getLabel())) {
return true;
}
labelsSeen.add(summary.getLabel());
}
return false;
}
/**
* Prints test result summary.
*
* @param summaries summaries of tests {@link TestSummary}
* @param showAllTests if true, print information about each test regardless of its status
* @param showNoStatusTests if true, print information about not executed tests (no status tests)
* @param showAllTestCases if true, print all test cases status and detailed information
*/
private void printSummary(
Set<TestSummary> summaries,
boolean showAllTests,
boolean showNoStatusTests,
boolean showAllTestCases,
boolean showCachedTests) {
boolean withConfig = duplicateLabels(summaries);
int numFailedToBuildReported = 0;
for (TestSummary summary : summaries) {
if (!showAllTests
&& (BlazeTestStatus.PASSED == summary.getStatus()
|| (!showNoStatusTests && BlazeTestStatus.NO_STATUS == summary.getStatus()))) {
continue;
}
if (BlazeTestStatus.FAILED_TO_BUILD == summary.getStatus()) {
if (numFailedToBuildReported == NUM_FAILED_TO_BUILD) {
printer.printLn("(Skipping other failed to build tests)");
}
numFailedToBuildReported++;
if (numFailedToBuildReported > NUM_FAILED_TO_BUILD) {
continue;
}
}
if (!showCachedTests
&& summary.getStatus() == BlazeTestStatus.PASSED
&& !summary.actionRan()) {
continue;
}
TestSummaryPrinter.print(
summary,
printer,
testLogPathFormatter,
summaryOptions.verboseSummary,
showAllTestCases,
withConfig,
mainRepoMapping);
}
}
/**
* Returns true iff the --check_tests_up_to_date option is enabled.
*/
private boolean optionCheckTestsUpToDate() {
return options.getOptions(ExecutionOptions.class).testCheckUpToDate;
}
private static final ImmutableSet<ExecutionOptions.TestSummaryFormat> SHOW_ALL_TESTS_FORMATS =
Sets.immutableEnumSet(DETAILED, DETAILED_UNCACHED, SHORT, SHORT_UNCACHED);
private static final ImmutableSet<ExecutionOptions.TestSummaryFormat>
SHOW_NO_STATUS_TESTS_FORMATS = Sets.immutableEnumSet(DETAILED, DETAILED_UNCACHED);
private static final ImmutableSet<ExecutionOptions.TestSummaryFormat>
SHOW_ALL_TEST_CASES_FORMATS = Sets.immutableEnumSet(DETAILED, DETAILED_UNCACHED);
private static final ImmutableSet<ExecutionOptions.TestSummaryFormat> SHOW_CACHED_TESTS_FORMATS =
Sets.immutableEnumSet(DETAILED, SHORT);
/**
* Prints a test summary information for all tests to the terminal.
*
* @param summaries Summary of all targets that were ran
* @param numberOfExecutedTargets the number of targets that were actually ran
*/
@Override
public void notify(Set<TestSummary> summaries, int numberOfExecutedTargets) {
TestResultStats stats = new TestResultStats();
stats.numberOfTargets = summaries.size();
stats.numberOfExecutedTargets = numberOfExecutedTargets;
ExecutionOptions executionOptions =
Preconditions.checkNotNull(options.getOptions(ExecutionOptions.class));
TestOutputFormat testOutput = executionOptions.testOutput;
for (TestSummary summary : summaries) {
if (summary.isLocalActionCached()
&& TestLogHelper.shouldOutputTestLog(testOutput,
TestResult.isBlazeTestStatusPassed(summary.getStatus()))) {
TestSummaryPrinter.printCachedOutput(
summary,
testOutput,
printer,
testLogPathFormatter,
executionOptions.maxTestOutputBytes);
}
}
for (TestSummary summary : summaries) {
if (TestResult.isBlazeTestStatusPassed(summary.getStatus())) {
stats.passCount++;
} else if (summary.getStatus() == BlazeTestStatus.NO_STATUS
|| summary.getStatus() == BlazeTestStatus.BLAZE_HALTED_BEFORE_TESTING) {
stats.noStatusCount++;
} else if (summary.getStatus() == BlazeTestStatus.FAILED_TO_BUILD) {
stats.failedToBuildCount++;
} else if (summary.ranRemotely()) {
stats.failedRemotelyCount++;
} else {
stats.failedLocallyCount++;
}
if (summary.wasUnreportedWrongSize()) {
stats.wasUnreportedWrongSize = true;
}
stats.totalTestCases += summary.getTotalTestCases();
stats.totalUnknownTestCases += summary.getUnknownTestCases();
stats.totalFailedTestCases += summary.getFailedTestCases().size();
stats.totalSkippedTestCases += summary.getSkippedTestCases().size();
}
stats.failedCount = summaries.size() - stats.passCount;
TestSummaryFormat testSummaryFormat = executionOptions.testSummary;
switch (testSummaryFormat) {
case DETAILED:
case DETAILED_UNCACHED:
case SHORT:
case SHORT_UNCACHED:
case TERSE:
{
boolean showAllTests = SHOW_ALL_TESTS_FORMATS.contains(testSummaryFormat);
boolean showNoStatusTests = SHOW_NO_STATUS_TESTS_FORMATS.contains(testSummaryFormat);
boolean showAllTestCases = SHOW_ALL_TEST_CASES_FORMATS.contains(testSummaryFormat);
boolean showCachedTests = SHOW_CACHED_TESTS_FORMATS.contains(testSummaryFormat);
printSummary(
summaries, showAllTests, showNoStatusTests, showAllTestCases, showCachedTests);
break;
}
case TESTCASE:
case NONE:
break;
}
printStats(stats);
}
private void addFailureToErrorList(List<String> list, String failureDescription, int count) {
addToList(list, AnsiTerminalPrinter.Mode.ERROR, "fails", "fail", failureDescription, count);
}
private void addToWarningList(
List<String> list, String singularPrefix, String pluralPrefix, String message, int count) {
addToList(list, AnsiTerminalPrinter.Mode.WARNING, singularPrefix, pluralPrefix, message, count);
}
private void addToList(
List<String> list,
AnsiTerminalPrinter.Mode mode,
String singularPrefix,
String pluralPrefix,
String message,
int count) {
if (count > 0) {
list.add(
String.format(
"%s%d %s %s%s",
mode,
count,
count == 1 ? singularPrefix : pluralPrefix,
message,
AnsiTerminalPrinter.Mode.DEFAULT));
}
}
private void printStats(TestResultStats stats) {
TestSummaryFormat testSummaryFormat = options.getOptions(ExecutionOptions.class).testSummary;
if (testSummaryFormat == DETAILED
|| testSummaryFormat == DETAILED_UNCACHED
|| testSummaryFormat == TESTCASE) {
int passCount =
stats.totalTestCases
- stats.totalFailedTestCases
- stats.totalUnknownTestCases
- stats.totalSkippedTestCases;
String message =
String.format(
"Test cases: finished with %s%d passing%s, %s%d skipped%s and %s%d failing%s out of"
+ " %d test cases",
passCount > 0 ? AnsiTerminalPrinter.Mode.INFO : "",
passCount,
AnsiTerminalPrinter.Mode.DEFAULT,
stats.totalSkippedTestCases > 0 ? AnsiTerminalPrinter.Mode.WARNING : "",
stats.totalSkippedTestCases,
AnsiTerminalPrinter.Mode.DEFAULT,
stats.totalFailedTestCases > 0 ? AnsiTerminalPrinter.Mode.ERROR : "",
stats.totalFailedTestCases,
AnsiTerminalPrinter.Mode.DEFAULT,
stats.totalTestCases);
if (stats.totalUnknownTestCases != 0) {
// It is possible for a target to fail even if all of its test cases pass. To avoid
// confusion, we append the following disclaimer.
message += " (some targets did not have test case information)";
}
printer.printLn(message);
}
if (!optionCheckTestsUpToDate()) {
List<String> results = new ArrayList<>();
if (stats.passCount == 1) {
results.add(stats.passCount + " test passes");
} else if (stats.passCount > 0) {
results.add(stats.passCount + " tests pass");
}
addFailureToErrorList(results, "to build", stats.failedToBuildCount);
addFailureToErrorList(results, "locally", stats.failedLocallyCount);
addFailureToErrorList(results, "remotely", stats.failedRemotelyCount);
addToWarningList(results, "was", "were", "skipped", stats.noStatusCount);
printer.print(
String.format(
"\nExecuted %d out of %d %s: %s.\n",
stats.numberOfExecutedTargets,
stats.numberOfTargets,
stats.numberOfTargets == 1 ? "test" : "tests",
StringUtil.joinEnglishList(results, "and")));
} else {
int failingUpToDateCount = stats.failedCount - stats.noStatusCount;
printer.print(String.format(
"\nFinished with %d passing and %s%d failing%s tests up to date, %s%d out of date.%s\n",
stats.passCount,
failingUpToDateCount > 0 ? AnsiTerminalPrinter.Mode.ERROR : "",
failingUpToDateCount,
AnsiTerminalPrinter.Mode.DEFAULT,
stats.noStatusCount > 0 ? AnsiTerminalPrinter.Mode.ERROR : "",
stats.noStatusCount,
AnsiTerminalPrinter.Mode.DEFAULT));
}
if (stats.wasUnreportedWrongSize) {
printer.print("There were tests whose specified size is too big. Use the "
+ "--test_verbose_timeout_warnings command line option to see which "
+ "ones these are.\n");
}
}
}