1
+ /*
2
+ * This file was generated by the Gradle 'init' task.
3
+ *
4
+ * This is a general purpose Gradle build.
5
+ * Learn more about Gradle by exploring our samples at https://docs.gradle.org/7.5.1/samples
6
+ */
7
+ apply plugin : ' groovy'
8
+
9
+ repositories {
10
+ mavenCentral()
11
+ mavenLocal()
12
+ maven { url ' https://repo.jenkins-ci.org/releases/' }
13
+ }
14
+
15
+ sourceSets {
16
+ main {
17
+ groovy {
18
+ srcDirs = [' src' ]
19
+ }
20
+ }
21
+
22
+ test {
23
+ groovy {
24
+ srcDirs = [' test' ]
25
+ }
26
+ }
27
+ }
28
+
29
+ dependencies {
30
+ implementation ' org.codehaus.groovy:groovy-all:2.4.21'
31
+
32
+ testImplementation ' com.lesfurets:jenkins-pipeline-unit:1.12'
33
+ testImplementation " junit:junit:4.12"
34
+ }
35
+
36
+ // Prettify the test result output.
37
+ tasks. withType(Test ) {
38
+ String ANSI_BOLD_WHITE = " \u 001B[0;1m"
39
+ String ANSI_RESET = " \u 001B[0m"
40
+ String ANSI_BLACK = " \u 001B[30m"
41
+ String ANSI_RED = " \u 001B[31m"
42
+ String ANSI_GREEN = " \u 001B[32m"
43
+ String ANSI_YELLOW = " \u 001B[33m"
44
+ String ANSI_BLUE = " \u 001B[34m"
45
+ String ANSI_PURPLE = " \u 001B[35m"
46
+ String ANSI_CYAN = " \u 001B[36m"
47
+ String ANSI_WHITE = " \u 001B[37m"
48
+ String CHECK_MARK = " \u 2713"
49
+ String NEUTRAL_FACE = " \u 0CA0_\u 0CA0"
50
+ String X_MARK = " \u 274C"
51
+
52
+ // Used to collect data to create a markdown file with the test results after
53
+ // the suite has finished.
54
+ // The GitHub action uses this markdown file to create the job summary.
55
+ // https://github.blog/2022-05-09-supercharging-github-actions-with-job-summaries/
56
+ String resultsFilePath = " build/test-results.md"
57
+ String resultsFileContent = " "
58
+
59
+ dependsOn cleanTest
60
+
61
+ testLogging {
62
+ outputs. upToDateWhen {false }
63
+ lifecycle. events = []
64
+ }
65
+
66
+ beforeSuite { suite ->
67
+ if (suite. parent != null && suite. className != null ){
68
+ out. println (ANSI_BOLD_WHITE + suite. name + ANSI_RESET )
69
+ resultsFileContent + = " #### " + suite. name + " \n "
70
+ }
71
+ }
72
+
73
+ afterTest { descriptor , result ->
74
+ def indicator = ANSI_WHITE
75
+ def mdIndicator
76
+
77
+ if (result. failedTestCount > 0 ) {
78
+ indicator = ANSI_RED + X_MARK
79
+ mdIndicator = " :x:"
80
+ } else if (result. skippedTestCount > 0 ) {
81
+ indicator = ANSI_YELLOW + NEUTRAL_FACE
82
+ mdIndicator = " :leftwards_arrow_with_hook:"
83
+ }
84
+ else {
85
+ indicator = ANSI_GREEN + CHECK_MARK
86
+ mdIndicator = " :white_check_mark:"
87
+ }
88
+
89
+ def message = ' ' + indicator + ANSI_RESET + " " + descriptor. name
90
+ def mdMessage = ' - ' + mdIndicator + " " + descriptor. name
91
+ if (result. failedTestCount > 0 ) {
92
+ message + = ' -> ' + result. exception
93
+ mdMessage + = ' -> `' + result. exception + ' `'
94
+ } else {
95
+ message + = ' '
96
+ }
97
+ out. println (message)
98
+ resultsFileContent + = mdMessage + " \n "
99
+ }
100
+
101
+ afterSuite { desc , result ->
102
+ if (desc. parent != null && desc. className != null ){
103
+ out. println (" " )
104
+ }
105
+
106
+ if (! desc. parent) { // will match the outermost suite
107
+ def failStyle = ANSI_RED
108
+ def skipStyle = ANSI_YELLOW
109
+ def summaryStyle = ANSI_WHITE
110
+
111
+ switch (result. resultType){
112
+ case TestResult.ResultType . SUCCESS :
113
+ summaryStyle = ANSI_GREEN
114
+ break
115
+ case TestResult.ResultType . FAILURE :
116
+ summaryStyle = ANSI_RED
117
+ break
118
+ }
119
+
120
+ out. println ( " --------------------------------------------------------------------------" )
121
+ out. println ( " Results: " + summaryStyle + " ${ result.resultType} " + ANSI_RESET
122
+ + " (${ result.testCount} tests, "
123
+ + ANSI_GREEN + " ${ result.successfulTestCount} passed" + ANSI_RESET
124
+ + " , " + failStyle + " ${ result.failedTestCount} failed" + ANSI_RESET
125
+ + " , " + skipStyle + " ${ result.skippedTestCount} skipped" + ANSI_RESET
126
+ + " )" )
127
+ out. println ( " --------------------------------------------------------------------------" )
128
+
129
+ // Write the results file for GitHub action job summary.
130
+ new File (projectDir, resultsFilePath). text = resultsFileContent
131
+ }
132
+ }
133
+ }
0 commit comments