forked from facebook/buck
-
Notifications
You must be signed in to change notification settings - Fork 4
/
TestCaseSummary.java
213 lines (186 loc) · 6.1 KB
/
TestCaseSummary.java
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
/*
* Copyright 2012-present Facebook, Inc.
*
* 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.facebook.buck.test;
import com.facebook.buck.util.Ansi;
import com.facebook.buck.util.TimeFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableList;
import java.util.List;
import java.util.Locale;
import javax.annotation.concurrent.Immutable;
@Immutable
public class TestCaseSummary {
/**
* Transformation to annotate TestCaseSummary marking them as being read from cached results
*/
public static final Function<TestCaseSummary, TestCaseSummary> TO_CACHED_TRANSFORMATION =
new Function<TestCaseSummary, TestCaseSummary>() {
@Override
public TestCaseSummary apply(TestCaseSummary summary) {
return new TestCaseSummary(summary, /* isCached */ true);
}
};
public static final int MAX_STATUS_WIDTH = 7;
private final String testCaseName;
private final ImmutableList<TestResultSummary> testResults;
private final boolean isSuccess;
private final boolean isDryRun;
private final boolean hasAssumptionViolations;
private final int skippedCount;
private final int failureCount;
private final long totalTime;
private final boolean isCached;
/**
* Creates a TestCaseSummary which is assumed to be not read from cached results
*/
public TestCaseSummary(String testCaseName, List<TestResultSummary> testResults) {
this.testCaseName = testCaseName;
this.testResults = ImmutableList.copyOf(testResults);
boolean isSuccess = true;
boolean isDryRun = false;
boolean hasAssumptionViolations = false;
int skippedCount = 0;
int failureCount = 0;
long totalTime = 0L;
for (TestResultSummary result : testResults) {
totalTime += result.getTime();
switch (result.getType()) {
case SUCCESS:
break;
case DRY_RUN:
isDryRun = true;
break;
case ASSUMPTION_VIOLATION:
hasAssumptionViolations = true;
++skippedCount;
break;
case FAILURE:
isSuccess = false;
++failureCount;
break;
}
}
this.isSuccess = isSuccess;
this.isDryRun = isDryRun;
this.hasAssumptionViolations = hasAssumptionViolations;
this.skippedCount = skippedCount;
this.failureCount = failureCount;
this.totalTime = totalTime;
this.isCached = false;
}
/** Creates a copy of {@code summary} with the specified value of {@code isCached}. */
private TestCaseSummary(TestCaseSummary summary, boolean isCached) {
this.testCaseName = summary.testCaseName;
this.testResults = summary.testResults;
this.isSuccess = summary.isSuccess;
this.isDryRun = summary.isDryRun;
this.hasAssumptionViolations = summary.hasAssumptionViolations;
this.skippedCount = summary.skippedCount;
this.failureCount = summary.failureCount;
this.totalTime = summary.totalTime;
this.isCached = isCached;
}
public boolean isSuccess() {
return isSuccess;
}
public boolean hasAssumptionViolations() {
return hasAssumptionViolations;
}
public String getTestCaseName() {
return testCaseName;
}
/** @return the total time to run all of the tests in this test case, in milliseconds */
public long getTotalTime() {
return totalTime;
}
/** @return a one-line, printable summary */
public String getOneLineSummary(boolean hasPassingDependencies, Ansi ansi) {
String statusText;
Ansi.SeverityLevel severityLevel;
if (isDryRun) {
severityLevel = Ansi.SeverityLevel.WARNING;
statusText = "DRYRUN";
} else if (!isSuccess()) {
if (hasPassingDependencies) {
severityLevel = Ansi.SeverityLevel.ERROR;
statusText = "FAIL";
} else {
severityLevel = Ansi.SeverityLevel.WARNING;
statusText = "DROP";
}
} else {
if (hasAssumptionViolations) {
severityLevel = Ansi.SeverityLevel.WARNING;
statusText = "ASSUME";
} else {
if (testResults.isEmpty()) {
severityLevel = Ansi.SeverityLevel.WARNING;
statusText = "NOTESTS";
} else {
severityLevel = Ansi.SeverityLevel.OK;
statusText = "PASS";
}
}
}
String status = ansi.asHighlightedStatusText(severityLevel, statusText);
int paddingWidth = MAX_STATUS_WIDTH - statusText.length();
String padding = "";
for (int position = 0; position < paddingWidth; position++) {
padding += ' ';
}
return String.format("%s%s %s %2d Passed %2d Skipped %2d Failed %s",
status,
padding,
// TODO(bhamiltoncx): Refactor this class to pass in the Locale.
!isCached ? TimeFormat.formatForConsole(Locale.US, totalTime, ansi)
: ansi.asHighlightedStatusText(severityLevel, "CACHED"),
getPassedCount(),
skippedCount,
failureCount,
testCaseName);
}
public ImmutableList<TestResultSummary> getTestResults() {
return testResults;
}
@JsonIgnore
public int getPassedCount() {
return testResults.size() - failureCount - skippedCount;
}
public int getSkippedCount() {
return skippedCount;
}
public int getFailureCount() {
return failureCount;
}
@Override
public String toString() {
return String.format("%s %s",
getShortStatusSummaryString(),
testCaseName);
}
private String getShortStatusSummaryString() {
if (isSuccess) {
if (hasAssumptionViolations) {
return "ASSUME";
} else {
return "PASS";
}
} else {
return "FAIL";
}
}
}