/
JUnitResultArchiver.java
317 lines (279 loc) · 11.3 KB
/
JUnitResultArchiver.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
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
/*
* The MIT License
*
* Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Martin Eigenbrodt,
* Tom Huybrechts, Yahoo!, Inc., Richard Hierlmeier
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package hudson.tasks.junit;
import hudson.AbortException;
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.BuildListener;
import hudson.model.Descriptor;
import hudson.model.Result;
import hudson.model.Run;
import hudson.model.Saveable;
import hudson.model.TaskListener;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.BuildStepMonitor;
import hudson.tasks.Publisher;
import hudson.tasks.Recorder;
import hudson.tasks.junit.TestResultAction.Data;
import hudson.util.DescribableList;
import hudson.util.FormValidation;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.types.FileSet;
import org.jenkinsci.Symbol;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import javax.annotation.Nonnull;
import jenkins.tasks.SimpleBuildStep;
import org.kohsuke.stapler.DataBoundSetter;
/**
* Generates HTML report from JUnit test result XML files.
*
* @author Kohsuke Kawaguchi
*/
public class JUnitResultArchiver extends Recorder implements SimpleBuildStep {
/**
* {@link FileSet} "includes" string, like "foo/bar/*.xml"
*/
private final String testResults;
/**
* If true, retain a suite's complete stdout/stderr even if this is huge and the suite passed.
* @since 1.358
*/
private boolean keepLongStdio;
/**
* {@link TestDataPublisher}s configured for this archiver, to process the recorded data.
* For compatibility reasons, can be null.
* @since 1.320
*/
private DescribableList<TestDataPublisher, Descriptor<TestDataPublisher>> testDataPublishers;
private Double healthScaleFactor;
/**
* If true, don't throw exception on missing test results or no files found.
*/
private boolean allowEmptyResults;
@DataBoundConstructor
public JUnitResultArchiver(String testResults) {
this.testResults = testResults;
}
@Deprecated
public JUnitResultArchiver(String testResults,
DescribableList<TestDataPublisher, Descriptor<TestDataPublisher>> testDataPublishers) {
this(testResults, false, testDataPublishers);
}
@Deprecated
public JUnitResultArchiver(
String testResults,
boolean keepLongStdio,
DescribableList<TestDataPublisher, Descriptor<TestDataPublisher>> testDataPublishers) {
this(testResults, keepLongStdio, testDataPublishers, 1.0);
}
@Deprecated
public JUnitResultArchiver(
String testResults,
boolean keepLongStdio,
DescribableList<TestDataPublisher, Descriptor<TestDataPublisher>> testDataPublishers,
double healthScaleFactor) {
this.testResults = testResults;
setKeepLongStdio(keepLongStdio);
setTestDataPublishers(testDataPublishers == null ? Collections.<TestDataPublisher>emptyList() : testDataPublishers);
setHealthScaleFactor(healthScaleFactor);
setAllowEmptyResults(false);
}
private TestResult parse(String expandedTestResults, Run<?,?> run, @Nonnull FilePath workspace, Launcher launcher, TaskListener listener)
throws IOException, InterruptedException
{
return new JUnitParser(this.isKeepLongStdio(),
this.isAllowEmptyResults()).parseResult(expandedTestResults, run, workspace, launcher, listener);
}
@Deprecated
protected TestResult parse(String expandedTestResults, AbstractBuild build, Launcher launcher, BuildListener listener)
throws IOException, InterruptedException
{
final FilePath workspace = build.getWorkspace();
if (workspace == null) {
throw new IllegalArgumentException("The provided build has no workspace");
}
return parse(expandedTestResults, build, workspace, launcher, listener);
}
@Override
public void perform(Run build, FilePath workspace, Launcher launcher,
TaskListener listener) throws InterruptedException, IOException {
listener.getLogger().println(Messages.JUnitResultArchiver_Recording());
final String testResults = build.getEnvironment(listener).expand(this.testResults);
TestResult result = parse(testResults, build, workspace, launcher, listener);
synchronized (build) {
// TODO can the build argument be omitted now, or is it used prior to the call to addAction?
TestResultAction action = build.getAction(TestResultAction.class);
boolean appending;
if (action == null) {
appending = false;
action = new TestResultAction(build, result, listener);
} else {
appending = true;
result.freeze(action);
action.mergeResult(result, listener);
}
action.setHealthScaleFactor(getHealthScaleFactor()); // overwrites previous value if appending
if (result.isEmpty()) {
if (build.getResult() == Result.FAILURE) {
// most likely a build failed before it gets to the test phase.
// don't report confusing error message.
return;
}
if (this.allowEmptyResults) {
// User allow empty results
listener.getLogger().println(Messages.JUnitResultArchiver_ResultIsEmpty());
return;
}
// most likely a configuration error in the job - e.g. false pattern to match the JUnit result files
throw new AbortException(Messages.JUnitResultArchiver_ResultIsEmpty());
}
// TODO: Move into JUnitParser [BUG 3123310]
if (testDataPublishers != null) {
for (TestDataPublisher tdp : testDataPublishers) {
Data d = tdp.contributeTestData(build, workspace, launcher, listener, result);
if (d != null) {
action.addData(d);
}
}
}
if (appending) {
build.save();
} else {
build.addAction(action);
}
if (action.getResult().getFailCount() > 0)
build.setResult(Result.UNSTABLE);
}
}
/**
* Not actually used, but left for backward compatibility
* @param ds Directory scanner.
* @param buildTime Build Time.
*
* @return a {@link TestResult}.
* @throws IOException if an error occurs.
* @deprecated since 2009-08-10.
*/
protected TestResult parseResult(DirectoryScanner ds, long buildTime)
throws IOException {
return new TestResult(buildTime, ds);
}
public BuildStepMonitor getRequiredMonitorService() {
return BuildStepMonitor.NONE;
}
public String getTestResults() {
return testResults;
}
public double getHealthScaleFactor() {
return healthScaleFactor == null ? 1.0 : healthScaleFactor;
}
/**
* @param healthScaleFactor Health scale factor.
*
* @since 1.2-beta-1
*/
@DataBoundSetter public final void setHealthScaleFactor(double healthScaleFactor) {
this.healthScaleFactor = Math.max(0.0, healthScaleFactor);
}
public @Nonnull List<TestDataPublisher> getTestDataPublishers() {
return testDataPublishers == null ? Collections.<TestDataPublisher>emptyList() : testDataPublishers;
}
/**
* @param testDataPublishers Test data publishers.
*
* @since 1.2
*/
@DataBoundSetter public final void setTestDataPublishers(@Nonnull List<TestDataPublisher> testDataPublishers) {
this.testDataPublishers = new DescribableList<TestDataPublisher,Descriptor<TestDataPublisher>>(Saveable.NOOP);
this.testDataPublishers.addAll(testDataPublishers);
}
/**
* @return the keepLongStdio.
*/
public boolean isKeepLongStdio() {
return keepLongStdio;
}
/**
* @param keepLongStdio Whether to keep long stdio.
*
* @since 1.2-beta-1
*/
@DataBoundSetter public final void setKeepLongStdio(boolean keepLongStdio) {
this.keepLongStdio = keepLongStdio;
}
/**
*
* @return the allowEmptyResults
*/
public boolean isAllowEmptyResults() {
return allowEmptyResults;
}
@DataBoundSetter public final void setAllowEmptyResults(boolean allowEmptyResults) {
this.allowEmptyResults = allowEmptyResults;
}
private static final long serialVersionUID = 1L;
@Extension @Symbol("junit")
public static class DescriptorImpl extends BuildStepDescriptor<Publisher> {
public String getDisplayName() {
return Messages.JUnitResultArchiver_DisplayName();
}
/**
* Performs on-the-fly validation on the file mask wildcard.
* @param project Project.
* @param value File mask to validate.
*
* @return the validation result.
* @throws IOException if an error occurs.
*/
public FormValidation doCheckTestResults(
@AncestorInPath AbstractProject project,
@QueryParameter String value) throws IOException {
if (project == null) {
return FormValidation.ok();
}
return FilePath.validateFileMask(project.getSomeWorkspace(), value);
}
public boolean isApplicable(Class<? extends AbstractProject> jobType) {
return true;
}
public FormValidation doCheckHealthScaleFactor(@QueryParameter double value) {
if (value < 1e-7) return FormValidation.warning("Test health reporting disabled");
return FormValidation.ok(Messages.JUnitResultArchiver_HealthScaleFactorAnalysis(
1,
(int) (100.0 - Math.max(0.0, Math.min(100.0, 1 * value))),
5,
(int) (100.0 - Math.max(0.0, Math.min(100.0, 5 * value)))
));
}
}
}