forked from gocd/gocd-plugin-gradle-task-helpers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-task-helper.gradle
120 lines (102 loc) · 3.94 KB
/
test-task-helper.gradle
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
/*
* Copyright 2019 ThoughtWorks, 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.
*/
class TestHelperANSI {
static Map<String, String> COLOR = [
ANSI_BOLD_WHITE: "\u001B[0;1m",
ANSI_RESET : "\u001B[0m",
ANSI_RED : "\u001B[31m",
ANSI_GREEN : "\u001B[32m",
ANSI_YELLOW : "\u001B[33m",
ANSI_WHITE : "\u001B[37m"
]
static Map<String, String> SYMBOLS = [
CHECK_MARK : "\u2714",
NEUTRAL_FACE: "\u0CA0_\u0CA0",
X_MARK : "\u2718"
]
}
allprojects {
tasks.withType(Test) {
maxParallelForks = 1
testLogging {
def previousFailed = false
exceptionFormat 'full'
beforeSuite { suite ->
if (suite.name.startsWith("Test Run") || suite.name.startsWith("Gradle Worker")) return
if (suite.parent != null && suite.className != null) {
println(TestHelperANSI.COLOR.ANSI_BOLD_WHITE + suite.name + TestHelperANSI.COLOR.ANSI_RESET)
}
}
beforeTest {
if (previousFailed) {
System.err.println("")
}
}
afterTest { descriptor, result ->
previousFailed = false
def executionTime = (result.endTime - result.startTime) / 1000
println(" ${resultIndicator(result)}$TestHelperANSI.COLOR.ANSI_RESET $descriptor.name $TestHelperANSI.COLOR.ANSI_YELLOW($executionTime seconds)$TestHelperANSI.COLOR.ANSI_RESET")
if (result.failedTestCount > 0) {
previousFailed = true
println('')
println(result.exception)
}
}
afterSuite { desc, result ->
if (desc.parent != null && desc.className != null) {
println("")
}
if (!desc.parent) { // will match the outermost suite
def failStyle = TestHelperANSI.COLOR.ANSI_RED
def skipStyle = TestHelperANSI.COLOR.ANSI_YELLOW
def summaryStyle = summaryStyle(result)
if (result.failedTestCount > 0) {
failStyle = TestHelperANSI.COLOR.ANSI_RED
}
if (result.skippedTestCount > 0) {
skipStyle = TestHelperANSI.COLOR.ANSI_YELLOW
}
println("--------------------------------------------------------------------------")
println("Results: $summaryStyle$result.resultType$TestHelperANSI.COLOR.ANSI_RESET ($result.testCount tests, $TestHelperANSI.COLOR.ANSI_GREEN$result.successfulTestCount passed$TestHelperANSI.COLOR.ANSI_RESET, $failStyle$result.failedTestCount failed$TestHelperANSI.COLOR.ANSI_RESET, $skipStyle$result.skippedTestCount skipped$TestHelperANSI.COLOR.ANSI_RESET)")
println("--------------------------------------------------------------------------")
}
}
}
}
}
private String summaryStyle(result) {
def summaryStyle = TestHelperANSI.COLOR.ANSI_WHITE
switch (result.resultType) {
case TestResult.ResultType.SUCCESS:
summaryStyle = TestHelperANSI.COLOR.ANSI_GREEN
break
case TestResult.ResultType.FAILURE:
summaryStyle = TestHelperANSI.COLOR.ANSI_RED
break
}
summaryStyle
}
private String resultIndicator(result) {
def indicator = TestHelperANSI.COLOR.ANSI_WHITE
if (result.failedTestCount > 0) {
indicator = TestHelperANSI.COLOR.ANSI_RED + TestHelperANSI.SYMBOLS.X_MARK
} else if (result.skippedTestCount > 0) {
indicator = TestHelperANSI.COLOR.ANSI_YELLOW + TestHelperANSI.SYMBOLS.NEUTRAL_FACE
} else {
indicator = TestHelperANSI.COLOR.ANSI_GREEN + TestHelperANSI.SYMBOLS.CHECK_MARK
}
indicator
}