-
Notifications
You must be signed in to change notification settings - Fork 231
/
CucumberReportPublisher.java
800 lines (662 loc) · 29.3 KB
/
CucumberReportPublisher.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
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
package net.masterthought.jenkins;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.UUID;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.AbortException;
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.AbstractDescribableImpl;
import hudson.model.Descriptor;
import hudson.model.Result;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.tasks.BuildStepMonitor;
import hudson.tasks.Recorder;
import jenkins.tasks.SimpleBuildStep;
import net.masterthought.cucumber.Configuration;
import net.masterthought.cucumber.ReportBuilder;
import net.masterthought.cucumber.Reportable;
import net.masterthought.cucumber.json.support.Status;
import net.masterthought.cucumber.presentation.PresentationMode;
import net.masterthought.cucumber.reducers.ReducingMethod;
import net.masterthought.cucumber.sorting.SortingMethod;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.tools.ant.DirectoryScanner;
import org.jenkinsci.Symbol;
import org.jenkinsci.plugins.tokenmacro.MacroEvaluationException;
import org.jenkinsci.plugins.tokenmacro.TokenMacro;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
public class CucumberReportPublisher extends Recorder implements SimpleBuildStep {
private final static String DEFAULT_FILE_INCLUDE_PATTERN_JSONS = "**/*.json";
private final static String DEFAULT_FILE_INCLUDE_PATTERN_CLASSIFICATIONS = "**/*.properties";
private final static String TRENDS_DIR = "cucumber-reports";
private final static String TRENDS_FILE = "cucumber-trends.json";
private final String fileIncludePattern;
private String fileExcludePattern = "";
private String jsonReportDirectory = "";
private String reportTitle = "";
private int failedStepsNumber;
private int skippedStepsNumber;
private int pendingStepsNumber;
private int undefinedStepsNumber;
private int failedScenariosNumber;
private int failedFeaturesNumber;
private double failedStepsPercentage;
private double skippedStepsPercentage;
private double pendingStepsPercentage;
private double undefinedStepsPercentage;
private double failedScenariosPercentage;
private double failedFeaturesPercentage;
private String buildStatus;
private boolean stopBuildOnFailedReport;
private boolean failedAsNotFailingStatus;
private boolean skippedAsNotFailingStatus;
private boolean pendingAsNotFailingStatus;
private boolean undefinedAsNotFailingStatus;
private int trendsLimit;
private String sortingMethod;
private List<Classification> classifications;
private String customJsFiles;
private String customCssFiles;
private boolean mergeFeaturesById;
private boolean mergeFeaturesWithRetest;
private boolean hideEmptyHooks;
private boolean skipEmptyJSONFiles;
private boolean expandAllSteps;
private String classificationsFilePattern = "";
@DataBoundConstructor
public CucumberReportPublisher(String fileIncludePattern) {
this.fileIncludePattern = fileIncludePattern;
}
/**
* This method, invoked after object is resurrected from persistence,
* to keep backward compatibility.
*/
protected void keepBackwardCompatibility() {
if (classifications == null) {
classifications = new ArrayList<>();
}
if (sortingMethod == null) {
sortingMethod = SortingMethod.NATURAL.name();
}
reportTitle = StringUtils.defaultString(reportTitle);
}
private static void log(TaskListener listener, String message) {
listener.getLogger().println("[CucumberReport] " + message);
}
public String getFileIncludePattern() {
return fileIncludePattern;
}
public List<Classification> getClassifications() {
return classifications;
}
@DataBoundSetter
public void setClassifications(List<Classification> classifications) {
// don't store the classifications if there was no element provided
if (CollectionUtils.isNotEmpty(classifications)) {
this.classifications = classifications;
}
}
@DataBoundSetter
public void setCustomJsFiles(String customJsFiles) {
this.customJsFiles = customJsFiles;
}
public String getCustomJsFiles() {
return customJsFiles;
}
@DataBoundSetter
public void setCustomCssFiles(String customCssFiles) {
this.customCssFiles = customCssFiles;
}
public String getCustomCssFiles() {
return customCssFiles;
}
public int getTrendsLimit() {
return trendsLimit;
}
@DataBoundSetter
public void setTrendsLimit(int trendsLimit) {
this.trendsLimit = trendsLimit;
}
public String getFileExcludePattern() {
return fileExcludePattern;
}
@DataBoundSetter
public void setFileExcludePattern(String fileExcludePattern) {
this.fileExcludePattern = fileExcludePattern;
}
public String getJsonReportDirectory() {
return jsonReportDirectory;
}
@DataBoundSetter
public void setJsonReportDirectory(String jsonReportDirectory) {
this.jsonReportDirectory = jsonReportDirectory;
}
public String getReportTitle() {
return reportTitle;
}
@DataBoundSetter
public void setReportTitle(String reportTitle) {
this.reportTitle = StringUtils.isEmpty(reportTitle) ? "" : reportTitle.trim();
}
public String getDirectorySuffix() {
return StringUtils.isEmpty(this.reportTitle)
? ""
: UUID.nameUUIDFromBytes(reportTitle.getBytes(StandardCharsets.UTF_8)).toString();
}
public String getDirectorySuffixWithSeparator() {
return StringUtils.isEmpty(this.reportTitle)
? ""
: ReportBuilder.SUFFIX_SEPARATOR + getDirectorySuffix();
}
public int getFailedStepsNumber() {
return failedStepsNumber;
}
@DataBoundSetter
public void setFailedStepsNumber(int failedStepsNumber) {
this.failedStepsNumber = failedStepsNumber;
}
public int getSkippedStepsNumber() {
return skippedStepsNumber;
}
@DataBoundSetter
public void setSkippedStepsNumber(int skippedStepsNumber) {
this.skippedStepsNumber = skippedStepsNumber;
}
public int getPendingStepsNumber() {
return pendingStepsNumber;
}
@DataBoundSetter
public void setPendingStepsNumber(int pendingStepsNumber) {
this.pendingStepsNumber = pendingStepsNumber;
}
public int getUndefinedStepsNumber() {
return undefinedStepsNumber;
}
@DataBoundSetter
public void setUndefinedStepsNumber(int undefinedStepsNumber) {
this.undefinedStepsNumber = undefinedStepsNumber;
}
public int getFailedScenariosNumber() {
return failedScenariosNumber;
}
@DataBoundSetter
public void setFailedScenariosNumber(int failedScenariosNumber) {
this.failedScenariosNumber = failedScenariosNumber;
}
public int getFailedFeaturesNumber() {
return failedFeaturesNumber;
}
@DataBoundSetter
public void setFailedFeaturesNumber(int failedFeaturesNumber) {
this.failedFeaturesNumber = failedFeaturesNumber;
}
public double getFailedStepsPercentage() {
return failedStepsPercentage;
}
@DataBoundSetter
public void setFailedStepsPercentage(double failedStepsPercentage) {
this.failedStepsPercentage = failedStepsPercentage;
}
public double getSkippedStepsPercentage() {
return skippedStepsPercentage;
}
@DataBoundSetter
public void setSkippedStepsPercentage(double skippedStepsPercentage) {
this.skippedStepsPercentage = skippedStepsPercentage;
}
public double getPendingStepsPercentage() {
return pendingStepsPercentage;
}
@DataBoundSetter
public void setPendingStepsPercentage(double pendingStepsPercentage) {
this.pendingStepsPercentage = pendingStepsPercentage;
}
public double getUndefinedStepsPercentage() {
return undefinedStepsPercentage;
}
@DataBoundSetter
public void setUndefinedStepsPercentage(double undefinedStepsPercentage) {
this.undefinedStepsPercentage = undefinedStepsPercentage;
}
public double getFailedScenariosPercentage() {
return failedScenariosPercentage;
}
@DataBoundSetter
public void setFailedScenariosPercentage(double failedScenariosPercentage) {
this.failedScenariosPercentage = failedScenariosPercentage;
}
public double getFailedFeaturesPercentage() {
return failedFeaturesPercentage;
}
@DataBoundSetter
public void setFailedFeaturesPercentage(double failedFeaturesPercentage) {
this.failedFeaturesPercentage = failedFeaturesPercentage;
}
public String getBuildStatus() {
return buildStatus;
}
@DataBoundSetter
public void setBuildStatus(String buildStatus) {
this.buildStatus = buildStatus;
}
@DataBoundSetter
public void setStopBuildOnFailedReport(boolean stopBuildOnFailedReport) {
this.stopBuildOnFailedReport = stopBuildOnFailedReport;
}
public boolean getStopBuildOnFailedReport() {
return stopBuildOnFailedReport;
}
@DataBoundSetter
public void setFailedAsNotFailingStatus(boolean failedAsNotFailingStatus) {
this.failedAsNotFailingStatus = failedAsNotFailingStatus;
}
public boolean getFailedAsNotFailingStatus() {
return failedAsNotFailingStatus;
}
@DataBoundSetter
public void setSkippedAsNotFailingStatus(boolean skippedAsNotFailingStatus) {
this.skippedAsNotFailingStatus = skippedAsNotFailingStatus;
}
public boolean getSkippedAsNotFailingStatus() {
return skippedAsNotFailingStatus;
}
@DataBoundSetter
public void setPendingAsNotFailingStatus(boolean pendingAsNotFailingStatus) {
this.pendingAsNotFailingStatus = pendingAsNotFailingStatus;
}
public boolean getPendingAsNotFailingStatus() {
return pendingAsNotFailingStatus;
}
@DataBoundSetter
public void setUndefinedAsNotFailingStatus(boolean undefinedAsNotFailingStatus) {
this.undefinedAsNotFailingStatus = undefinedAsNotFailingStatus;
}
public boolean getUndefinedAsNotFailingStatus() {
return undefinedAsNotFailingStatus;
}
@DataBoundSetter
public void setSortingMethod(String sortingMethod) {
this.sortingMethod = sortingMethod;
}
public String getSortingMethod() {
return sortingMethod;
}
@DataBoundSetter
public void setClassificationsFilePattern(String classificationsFilePattern) {
this.classificationsFilePattern = classificationsFilePattern;
}
public String getClassificationsFilePattern() {
return classificationsFilePattern;
}
@DataBoundSetter
public void setMergeFeaturesById(boolean mergeFeaturesById) {
this.mergeFeaturesById = mergeFeaturesById;
}
public boolean getMergeFeaturesById() {
return mergeFeaturesById;
}
@DataBoundSetter
public void setMergeFeaturesWithRetest(boolean mergeFeaturesWithRetest) {
this.mergeFeaturesWithRetest = mergeFeaturesWithRetest;
}
public boolean getMergeFeaturesWithRetest() {
return mergeFeaturesWithRetest;
}
@DataBoundSetter
public void setHideEmptyHooks(boolean hideEmptyHooks) {
this.hideEmptyHooks = hideEmptyHooks;
}
public boolean getHideEmptyHooks() {
return hideEmptyHooks;
}
@DataBoundSetter
public void setSkipEmptyJSONFiles(boolean skipEmptyJSONFiles) {
this.skipEmptyJSONFiles = skipEmptyJSONFiles;
}
public boolean getSkipEmptyJSONFiles() {
return skipEmptyJSONFiles;
}
@DataBoundSetter
public void setExpandAllSteps(boolean expandAllSteps) {
this.expandAllSteps = expandAllSteps;
}
public boolean getExpandAllSteps() {
return expandAllSteps;
}
@Override
public void perform(@NonNull Run<?, ?> run, @NonNull FilePath workspace, @NonNull Launcher launcher, @NonNull TaskListener listener)
throws InterruptedException, IOException {
keepBackwardCompatibility();
generateReport(run, workspace, listener);
SafeArchiveServingRunAction caa = new SafeArchiveServingRunAction(
run,
new File(run.getRootDir(), ReportBuilder.BASE_DIRECTORY + getDirectorySuffixWithSeparator()),
ReportBuilder.BASE_DIRECTORY + getDirectorySuffixWithSeparator(),
ReportBuilder.HOME_PAGE,
CucumberReportBaseAction.ICON_NAME,
getActionName(),
getDirectorySuffixWithSeparator()
);
run.addAction(caa);
}
private String getActionName() {
return StringUtils.isEmpty(reportTitle) ? Messages.SidePanel_DisplayNameNoTitle() : String.format(Messages.SidePanel_DisplayName(), reportTitle);
}
private void generateReport(Run<?, ?> build, FilePath workspace, TaskListener listener) throws InterruptedException, IOException {
log(listener, "Using Cucumber Reports version " + getPomVersion(listener));
// create directory where trends will be stored
final File trendsDir = new File(build.getParent().getRootDir(), TRENDS_DIR + getDirectorySuffixWithSeparator());
if (!trendsDir.exists() && !trendsDir.mkdirs()) {
throw new IllegalStateException("Could not create directory for trends: " + trendsDir);
}
// source directory (possibly on slave)
String parsedJsonReportDirectory = evaluateMacro(build, workspace, listener, jsonReportDirectory);
log(listener, String.format("JSON report directory is \"%s\"", parsedJsonReportDirectory));
FilePath inputReportDirectory = new FilePath(workspace, parsedJsonReportDirectory);
File directoryForReport = build.getRootDir();
File directoryCache = new File(
directoryForReport,
ReportBuilder.BASE_DIRECTORY + getDirectorySuffixWithSeparator() + File.separatorChar + ".cache"
);
if (directoryCache.exists()) {
throw new IllegalStateException("Cache directory " + directoryCache + " already exists. Another report with the same title already generated?");
} else if (!directoryCache.mkdirs()) {
throw new IllegalStateException("Could not create directory for cache: " + directoryCache);
}
// copies Classifications files to cache...
int copiedFilesProperties = inputReportDirectory.copyRecursiveTo(DEFAULT_FILE_INCLUDE_PATTERN_CLASSIFICATIONS, new FilePath(directoryCache));
log(listener, String.format("Copied %d properties files from workspace \"%s\" to reports directory \"%s\"",
copiedFilesProperties, inputReportDirectory.getRemote(), directoryCache));
// copies custom JS and CSS files to cache...
List<String> cachedCustomJsFiles = new ArrayList<>();
if (StringUtils.isNotEmpty(customJsFiles)) {
cachedCustomJsFiles.addAll(copyFilesAndGetList(listener,
workspace,
directoryCache,
customJsFiles,
null)
);
}
List<String> cachedCustomCssFiles = new ArrayList<>();
if (StringUtils.isNotEmpty(customCssFiles)) {
cachedCustomCssFiles.addAll(copyFilesAndGetList(listener,
workspace,
directoryCache,
customCssFiles,
null)
);
}
// copies JSON files to cache and
// exclude JSONs that should be skipped (as configured by the user)
List<String> jsonFilesToProcess = copyFilesAndGetList(listener, inputReportDirectory, directoryCache, fileIncludePattern, fileExcludePattern);
log(listener, String.format("Processing %d json files:", jsonFilesToProcess.size()));
for (String jsonFile : jsonFilesToProcess) {
log(listener, jsonFile);
}
String buildNumber = Integer.toString(build.getNumber());
// this works for normal and multi-config/matrix jobs
// for matrix jobs, this will include the matrix job name and the specific
// configuration/permutation name as well. this also includes the '/' so
// we don't have to modify how the cucumber plugin report generator's links
String projectName = build.getParent().getDisplayName();
Configuration configuration = new Configuration(directoryForReport, projectName);
configuration.setBuildNumber(buildNumber);
configuration.setDirectorySuffix(getDirectorySuffix());
configuration.setTrends(new File(trendsDir, TRENDS_FILE), trendsLimit);
configuration.setSortingMethod(SortingMethod.valueOf(sortingMethod));
if (mergeFeaturesById) {
configuration.addReducingMethod(ReducingMethod.MERGE_FEATURES_BY_ID);
}
if (mergeFeaturesWithRetest) {
configuration.addReducingMethod(ReducingMethod.MERGE_FEATURES_WITH_RETEST);
}
if (skipEmptyJSONFiles) {
configuration.addReducingMethod(ReducingMethod.SKIP_EMPTY_JSON_FILES);
}
if (hideEmptyHooks) {
configuration.addReducingMethod(ReducingMethod.HIDE_EMPTY_HOOKS);
}
if (expandAllSteps) {
configuration.addPresentationModes(PresentationMode.EXPAND_ALL_STEPS);
}
configuration.addPresentationModes(PresentationMode.RUN_WITH_JENKINS);
if (CollectionUtils.isNotEmpty(classifications)) {
log(listener, String.format("Adding %d classification(s)", classifications.size()));
addClassificationsToBuildReport(build, workspace, listener, configuration, classifications);
}
if (CollectionUtils.isNotEmpty(cachedCustomJsFiles)) {
configuration.addCustomJsFiles(cachedCustomJsFiles);
}
if (CollectionUtils.isNotEmpty(cachedCustomCssFiles)) {
configuration.addCustomCssFiles(cachedCustomCssFiles);
}
List<String> classificationFiles = fetchPropertyFiles(directoryCache, listener);
if (CollectionUtils.isNotEmpty(classificationFiles)) {
configuration.addClassificationFiles(classificationFiles);
}
setFailingStatuses(configuration);
ReportBuilder reportBuilder = new ReportBuilder(jsonFilesToProcess, configuration);
Reportable result = reportBuilder.generateReports();
if (hasReportFailed(result, listener)) {
// redefine build result if it was provided by plugin configuration
if (Result.FAILURE.toString().equals(buildStatus) || Result.UNSTABLE.toString().equals(buildStatus)) {
log(listener, "Build status is changed to " + buildStatus);
build.setResult(Result.fromString(buildStatus));
} else {
log(listener, "Build status is left unchanged");
}
if (stopBuildOnFailedReport) {
throw new AbortException(Messages.StopBuildOnFailedReport_FailNote());
}
}
// removes cache which may run out of the free space on storage
FileUtils.deleteQuietly(directoryCache);
}
private List<String> copyFilesAndGetList(TaskListener listener, FilePath inputDirectory, File directoryJsonCache, String includePattern, String excludePattern) throws IOException, InterruptedException {
int count = inputDirectory.copyRecursiveTo(includePattern, new FilePath(directoryJsonCache));
log(listener, String.format("Copied %d files from workspace \"%s\" to reports directory \"%s\"",
count, inputDirectory.getRemote(), directoryJsonCache));
String[] copiedFiles = findFilesByPattern(directoryJsonCache, includePattern, excludePattern);
return getFullPath(copiedFiles, directoryJsonCache);
}
private String getPomVersion(TaskListener listener) {
Properties properties = new Properties();
try (InputStream inputStream = this.getClass().getClassLoader()
.getResourceAsStream("plugin.properties")) {
properties.load(inputStream);
return properties.getProperty("plugin.version");
} catch (IOException e) {
log(listener, e.getMessage());
return "";
}
}
private String[] findFilesByPattern(File targetDirectory, String fileIncludePattern, String fileExcludePattern) {
DirectoryScanner scanner = new DirectoryScanner();
scanner.setBasedir(targetDirectory);
if (StringUtils.isEmpty(fileIncludePattern)) {
scanner.setIncludes(new String[]{DEFAULT_FILE_INCLUDE_PATTERN_JSONS});
} else {
scanner.setIncludes(fileIncludePattern.split(",\\s*"));
}
if (StringUtils.isNotEmpty(fileExcludePattern)) {
scanner.setExcludes(fileExcludePattern.split(",\\s*"));
}
scanner.setBasedir(targetDirectory);
scanner.scan();
return scanner.getIncludedFiles();
}
private List<String> getFullPath(String[] jsonFiles, File targetBuildDirectory) {
List<String> fullPathList = new ArrayList<>();
for (String file : jsonFiles) {
fullPathList.add(new File(targetBuildDirectory, file).getAbsolutePath());
}
return fullPathList;
}
private boolean hasReportFailed(Reportable result, TaskListener listener) {
// happens when the report could not be generated
if (result == null) {
log(listener, "Missing report result - report was not successfully completed");
return true;
}
if (failedStepsNumber != -1 && result.getFailedSteps() > failedStepsNumber) {
log(listener, String.format("Found %d failed steps, while expected at most %d",
result.getFailedSteps(), failedStepsNumber));
return true;
}
if (skippedStepsNumber != -1 && result.getSkippedSteps() > skippedStepsNumber) {
log(listener, String.format("Found %d skipped steps, while expected at most %d",
result.getSkippedSteps(), skippedStepsNumber));
return true;
}
if (pendingStepsNumber != -1 && result.getPendingSteps() > pendingStepsNumber) {
log(listener, String.format("Found %d pending steps, while expected at most %d",
result.getPendingSteps(), pendingStepsNumber));
return true;
}
if (undefinedStepsNumber != -1 && result.getUndefinedSteps() > undefinedStepsNumber) {
log(listener, String.format("Found %d undefined steps, while expected at most %d",
result.getUndefinedSteps(), undefinedStepsNumber));
return true;
}
if (failedScenariosNumber != -1 && result.getFailedScenarios() > failedScenariosNumber) {
log(listener, String.format("Found %d failed scenarios, while expected at most %d",
result.getFailedScenarios(), failedScenariosNumber));
return true;
}
if (failedFeaturesNumber != -1 && result.getFailedFeatures() > failedFeaturesNumber) {
log(listener, String.format("Found %d failed features, while expected at most %d",
result.getFailedFeatures(), failedFeaturesNumber));
return true;
}
double failedStepsThreshold = 100.0 * result.getFailedSteps() / result.getSteps();
if (failedStepsThreshold > failedStepsPercentage) {
log(listener, String.format("Found %f failed steps, while expected not more than %f percent",
failedStepsThreshold, failedStepsPercentage));
return true;
}
double skippedStepsThreshold = 100.0 * result.getSkippedSteps() / result.getSteps();
if (skippedStepsThreshold > skippedStepsPercentage) {
log(listener, String.format("Found %f skipped steps, while expected not more than %f percent",
skippedStepsThreshold, skippedStepsPercentage));
return true;
}
double pendingStepsThreshold = 100.0 * result.getPendingSteps() / result.getSteps();
if (pendingStepsThreshold > pendingStepsPercentage) {
log(listener, String.format("Found %f pending steps, while expected not more than %f percent",
pendingStepsThreshold, pendingStepsPercentage));
return true;
}
double undefinedStepsThreshold = 100.0 * result.getUndefinedSteps() / result.getSteps();
if (undefinedStepsThreshold > undefinedStepsPercentage) {
log(listener, String.format("Found %f undefined steps, while expected not more than %f percent",
undefinedStepsThreshold, undefinedStepsPercentage));
return true;
}
double failedScenariosThreshold = 100.0 * result.getFailedScenarios() / result.getScenarios();
if (failedScenariosThreshold > failedScenariosPercentage) {
log(listener, String.format("Found %f failed scenarios, while expected not more than %f percent",
failedScenariosThreshold, failedScenariosPercentage));
return true;
}
double failedFeaturesThreshold = 100.0 * result.getFailedFeatures() / result.getFeatures();
if (failedFeaturesThreshold > failedFeaturesPercentage) {
log(listener, String.format("Found %f failed features, while expected not more than %f percent",
failedFeaturesThreshold, failedFeaturesPercentage));
return true;
}
return false;
}
private String evaluateMacro(Run<?, ?> build, FilePath workspace, TaskListener listener, String value) throws InterruptedException, IOException {
try {
return TokenMacro.expandAll(build, workspace, listener, value);
} catch (MacroEvaluationException e) {
log(listener, String.format("Could not evaluate macro '%s': %s", value, e.getMessage()));
}
return value;
}
private void addClassificationsToBuildReport(Run<?, ?> build, FilePath workspace, TaskListener listener, Configuration configuration, List<Classification> listToAdd) throws InterruptedException, IOException {
for (Classification classification : listToAdd) {
log(listener, String.format("Adding classification - %s -> %s", classification.key, classification.value));
configuration.addClassifications(classification.key, evaluateMacro(build, workspace, listener, classification.value));
}
if (StringUtils.isNotEmpty(reportTitle)) {
configuration.addClassifications(Messages.Classification_ReportTitle(), reportTitle);
}
}
private void setFailingStatuses(Configuration configuration) {
Set<Status> notFailingStatuses = new HashSet<>();
if (failedAsNotFailingStatus) {
notFailingStatuses.add(Status.FAILED);
}
if (skippedAsNotFailingStatus) {
notFailingStatuses.add(Status.SKIPPED);
}
if (pendingAsNotFailingStatus) {
notFailingStatuses.add(Status.PENDING);
}
if (undefinedAsNotFailingStatus) {
notFailingStatuses.add(Status.UNDEFINED);
}
configuration.setNotFailingStatuses(notFailingStatuses);
}
private List<String> fetchPropertyFiles(File targetDirectory, TaskListener listener) {
List<String> propertyFiles = new ArrayList<>();
if (StringUtils.isNotEmpty(classificationsFilePattern)) {
DirectoryScanner scanner = new DirectoryScanner();
scanner.setIncludes(new String[]{classificationsFilePattern});
scanner.setBasedir(targetDirectory);
scanner.setCaseSensitive(false);
scanner.scan();
propertyFiles = getFullMetaDataPath(scanner.getIncludedFiles(), targetDirectory.toString());
for (String propertyFile : propertyFiles) {
log(listener, String.format("Found Properties File - %s ", propertyFile));
}
}
return propertyFiles;
}
private List<String> getFullMetaDataPath(String[] files, String propertiesDirectory) {
List<String> fullPathList = new ArrayList<>();
for (String file : files) {
fullPathList.add(propertiesDirectory + File.separator + file);
}
return fullPathList;
}
@Override
public BuildStepMonitor getRequiredMonitorService() {
return BuildStepMonitor.NONE;
}
public static class Classification extends AbstractDescribableImpl<Classification> implements Serializable {
public String key;
public String value;
@DataBoundConstructor
public Classification(String key, String value) {
this.key = key;
this.value = value;
}
@Extension
public static class DescriptorImpl extends Descriptor<Classification> {
@Override
public String getDisplayName() {
return "";
}
}
}
@Extension
@Symbol("cucumber")
public static class BuildStatusesDescriptorImpl extends CucumberReportDescriptor {
}
}