-
-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathCoveragePublisher.java
More file actions
356 lines (308 loc) · 11.6 KB
/
CoveragePublisher.java
File metadata and controls
356 lines (308 loc) · 11.6 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
348
349
350
351
package io.jenkins.plugins.coverage;
import org.jenkinsci.test.acceptance.po.AbstractStep;
import org.jenkinsci.test.acceptance.po.Control;
import org.jenkinsci.test.acceptance.po.Describable;
import org.jenkinsci.test.acceptance.po.Job;
import org.jenkinsci.test.acceptance.po.PageArea;
import org.jenkinsci.test.acceptance.po.PageAreaImpl;
import org.jenkinsci.test.acceptance.po.PostBuildStep;
import io.jenkins.plugins.coverage.GlobalThreshold.GlobalThresholdTarget;
/**
* Coverage Publisher which can be added in the configuration of a FreeStyle Project.
*/
@Describable("Record code coverage results")
@SuppressWarnings("PMD.TooManyFields")
public class CoveragePublisher extends AbstractStep implements PostBuildStep {
private final Control adapter = control("hetero-list-add[adapters]");
private final Control advancedOptions = control("advanced-button");
private final Control applyThresholdRecursively = control("applyThresholdRecursively");
private final Control failUnhealthy = control("failUnhealthy");
private final Control failUnstable = control("failUnstable");
private final Control failNoReports = control("failNoReports");
private final Control failBuildIfCoverageDecreasedInChangeRequest = control(
"failBuildIfCoverageDecreasedInChangeRequest");
private final Control sourceFileResolver = control("sourceFileResolver/level");
private final Control globalThreshold = control("repeatable-add");
private final Control sourceDirectories = findRepeatableAddButtonFor("sourceDirectories");
private final Control id = control("id");
private final Control name = control("name");
private final Control skipPublishingChecks = control("skipPublishingChecks");
private final Control checksName = control("checksName");
private final Control checksAnnotationScope = control("checksAnnotationScope");
private final Control ignoreParsingErrors = control("ignoreParsingErrors");
private final Control failOnError = control("failOnError");
private final Control enabledForFailure = control("enabledForFailure");
private final Control skipSymbolicLinks = control("skipSymbolicLinks");
private final Control scm = control("scm");
private final Control sourceCodeEncoding = control("sourceCodeEncoding");
private final Control sourceCodeRetention = control("sourceCodeRetention");
/**
* Creates a new page object for the {@link CoveragePublisher}.
*
* @param parent
* the parent job
* @param path
* the path on the configuration page
*/
public CoveragePublisher(final Job parent, final String path) {
super(parent, path);
}
CoveragePublisher setIgnoreParsingErrors(final boolean ignoreParsingErrors) {
ensureAdvancedOptionsIsActivated();
this.ignoreParsingErrors.check(ignoreParsingErrors);
return this;
}
CoveragePublisher setFailOnError(final boolean failOnError) {
ensureAdvancedOptionsIsActivated();
this.failOnError.check(failOnError);
return this;
}
private Control findRepeatableAddButtonFor(final String propertyName) {
return control(by.xpath("//div[@id='" + propertyName + "']//button[contains(@path,'-add')]"));
}
CoveragePublisher setTool(final String tool, final String pattern) {
var tools = new CoverageTool(this, "tools");
tools.setTool("COBERTURA").setPattern(pattern);
return this;
}
/**
* Sets the encoding when reading source code files.
*
* @param sourceCodeEncoding
* the source code encoding (e.g., UTF-8)
*
* @return this publisher
*/
public CoveragePublisher setSourceCodeEncoding(final String sourceCodeEncoding) {
ensureAdvancedOptionsIsActivated();
this.sourceCodeEncoding.set(sourceCodeEncoding);
return this;
}
/**
* Adds the path to the folder that contains the source code. If not relative and thus not part of the workspace
* then this folder needs to be added in Jenkins global configuration.
*
* @param sourceDirectory
* a folder containing the source code
*
* @return this publisher
*/
public CoveragePublisher addSourceDirectory(final String sourceDirectory) {
ensureAdvancedOptionsIsActivated();
String path = createPageArea("sourceDirectories", sourceDirectories::click);
SourceCodeDirectoryPanel panel = new SourceCodeDirectoryPanel(this, path);
panel.setPath(sourceDirectory);
return this;
}
/**
* Setter for applying threshold recursively.
*
* @param applyTresholds
* boolean for using applying threshold recursively
*/
public void setApplyThresholdRecursively(final boolean applyTresholds) {
ensureAdvancedOptionsIsActivated();
applyThresholdRecursively.check(applyTresholds);
}
/**
* Setter for fail on unhealthy.
*
* @param failOnUnhealthy
* boolean for failing on unhealthy
*/
public void setFailUnhealthy(final boolean failOnUnhealthy) {
ensureAdvancedOptionsIsActivated();
failUnhealthy.check(failOnUnhealthy);
}
/**
* Setter for fail on unstable.
*
* @param failOnUnstable
* boolean for failing on unstable
*/
public void setFailUnstable(final boolean failOnUnstable) {
ensureAdvancedOptionsIsActivated();
failUnstable.check(failOnUnstable);
}
/**
* Setter for fail on no reports.
*
* @param failOnNoReports
* boolean for fail on no reports
*/
public void setFailNoReports(final boolean failOnNoReports) {
ensureAdvancedOptionsIsActivated();
failNoReports.check(failOnNoReports);
}
/**
* Setter for fail build if coverage decreased in Change Request.
*
* @param failOnCoverageDecreases
* boolean for failing if coverage decreased in Change Request
*/
public void setFailBuildIfCoverageDecreasedInChangeRequest(final boolean failOnCoverageDecreases) {
ensureAdvancedOptionsIsActivated();
failBuildIfCoverageDecreasedInChangeRequest.check(failOnCoverageDecreases);
}
/**
* Setter for skipping publishing checks.
*
* @param skipPublishing
* boolean for skipping publishing checks
*/
public void setSkipPublishingChecks(final boolean skipPublishing) {
ensureAdvancedOptionsIsActivated();
skipPublishingChecks.check(skipPublishing);
}
/**
* Setter for setting the SCM checks name.
*
* @param checksName
* the SCM check name
*/
public void setChecksName(final String checksName) {
ensureAdvancedOptionsIsActivated();
this.checksName.set(checksName);
}
/**
* Ensures advanced options of CoveragePublisher is activated, so that values like
* {@link CoveragePublisher#setFailUnhealthy(boolean)} or {@link CoveragePublisher#setFailNoReports(boolean)} are
* visible and can be set.
*/
public void ensureAdvancedOptionsIsActivated() {
if (advancedOptions.exists()) {
advancedOptions.click();
}
}
/**
* Creates an {@link Adapter} for {@link CoveragePublisher}.
*
* @param adapterName
* type which should be created, f. e. jacoco or cobertura
*
* @return added {@link Adapter}
*/
public Adapter createAdapterPageArea(final String adapterName) {
String path = createPageArea("adapters", () -> this.adapter.selectDropdownMenu(adapterName));
Adapter newAdapter = new Adapter(this, path);
return newAdapter;
}
/**
* Creates {@link GlobalThreshold} for {@link Adapter}.
*
* @return added {@link Adapter}
*/
public GlobalThreshold createGlobalThresholdsPageArea() {
ensureAdvancedOptionsIsActivated();
String path = createPageArea("globalthresholds", () -> this.globalThreshold.click());
return new GlobalThreshold(this, path);
}
/**
* Creates {@link GlobalThreshold} for {@link Adapter}.
*
* @param thresholdTarget
* which should be set
* @param unhealthyThreshold
* which should be set
* @param unstableThreshold
* which should be set
* @param failOnUnhealthy
* boolean for failing build on unhealthy
*
* @return added {@link Adapter} with setted configuration
*/
public GlobalThreshold createGlobalThresholdsPageArea(final GlobalThresholdTarget thresholdTarget,
final double unhealthyThreshold,
final double unstableThreshold, final boolean failOnUnhealthy) {
ensureAdvancedOptionsIsActivated();
String path = createPageArea("globalThresholds", () -> this.globalThreshold.click());
GlobalThreshold threshold = new GlobalThreshold(this, path);
threshold.setThresholdTarget(thresholdTarget);
threshold.setUnhealthyThreshold(unhealthyThreshold);
threshold.setUnstableThreshold(unstableThreshold);
threshold.setFailUnhealthy(failOnUnhealthy);
return threshold;
}
/**
* Setter for Source File Resolver.
*
* @param storingLevel
* which should be applied
*/
public void setSourceFileResolver(final SourceFileResolver storingLevel) {
ensureAdvancedOptionsIsActivated();
sourceFileResolver.select(storingLevel.getValue());
}
/**
* Enum for Options of Source File Storing Level of {@link CoveragePublisher}.
*/
public enum SourceFileResolver {
NEVER_STORE("NEVER_STORE"),
STORE_LAST_BUIlD("STORE_LAST_BUILD"),
STORE_ALL_BUILD("STORE_ALL_BUILD");
private final String value;
/**
* Constructor of enum.
*
* @param value
* is value-attribute of option-tag.
*/
SourceFileResolver(final String value) {
this.value = value;
}
/**
* Get value of option-tag which should be selected.
*
* @return value of option-tag to select.
*/
public String getValue() {
return value;
}
}
/**
* Page area of a source code path configuration.
*/
private static class SourceCodeDirectoryPanel extends PageAreaImpl {
private final Control path = control("path");
SourceCodeDirectoryPanel(final PageArea area, final String path) {
super(area, path);
}
public void setPath(final String path) {
this.path.set(path);
}
}
/**
* Page area of a coverage tool configuration.
*/
public static class CoverageTool extends PageAreaImpl {
private final Control tool = control("");
private final Control pattern = control("pattern");
CoverageTool(final PageArea coverageRecorder, final String path) {
super(coverageRecorder, path);
}
/**
* Sets the name of the tool.
*
* @param toolName
* the name of the tool, e.g., PIT, JACOCO, COBERTURA
*
* @return this
*/
public CoverageTool setTool(final String toolName) {
tool.select(toolName);
return this;
}
/**
* Sets the pattern of the files to parse.
*
* @param pattern
* the pattern
*
* @return this
*/
public CoverageTool setPattern(final String pattern) {
this.pattern.set(pattern);
return this;
}
}
}