This repository has been archived by the owner on Oct 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_main.go
276 lines (257 loc) · 6.21 KB
/
test_main.go
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
package log
import (
"fmt"
"io"
"os"
"path"
"sort"
"strconv"
"strings"
"sync"
"testing"
"time"
)
var testLoggerActive = false
// RunTests is a helper function that transforms the test output depending on the CI system running.
// This is most prominently used with GitHub Actions to produce a nice-looking output.
func RunTests(m *testing.M) {
var result int
switch {
case os.Getenv("GITHUB_ACTIONS") != "":
result = runWithGitHubActions(m)
default:
result = m.Run()
}
os.Exit(result)
}
func runWithGitHubActions(m *testing.M) int {
w := &gitHubActionsWriter{
os.Stdout,
&sync.Mutex{},
map[string]*testCase{},
"",
"",
}
oldStdout := os.Stdout
tmpFile, err := os.CreateTemp(os.TempDir(), "test-")
if err != nil {
panic(err)
}
reader, err := os.Open(tmpFile.Name())
if err != nil {
panic(err)
}
var result int
done := make(chan struct{})
go func() {
os.Stdout = tmpFile
testLoggerActive = true
result = m.Run()
testLoggerActive = false
os.Stdout = oldStdout
done <- struct{}{}
}()
<-done
_, _ = io.Copy(w, reader)
_ = reader.Close()
_ = tmpFile.Close()
_ = os.Remove(tmpFile.Name())
var testCases []string
for testCaseName := range w.testCases {
testCases = append(testCases, testCaseName)
}
sort.Strings(testCases)
for _, testCaseName := range testCases {
writeTestcase(w.testCases[testCaseName])
}
return result
}
type logOutputFormat struct {
symbol string
color string
symbolColor string
}
var logLevelConfig = map[LevelString]logOutputFormat{
LevelDebugString: {
symbol: "⚙️",
color: "",
symbolColor: "",
},
LevelInfoString: {
symbol: "ⓘ️",
color: "\033[34m",
symbolColor: "\033[34m",
},
LevelNoticeString: {
symbol: "🏷️",
color: "\033[33m",
symbolColor: "\033[33m",
},
LevelWarningString: {
symbol: "⚠️",
color: "\033[33m",
symbolColor: "\033[33m",
},
LevelErrorString: {
symbol: "❌",
color: "\033[31m",
symbolColor: "\033[31m",
},
LevelCriticalString: {
symbol: "🛑",
color: "\033[31m",
symbolColor: "\033[31m",
},
LevelAlertString: {
symbol: "🔔",
color: "\033[31m",
symbolColor: "\033[31m",
},
LevelEmergencyString: {
symbol: "💣",
color: "\033[31m",
symbolColor: "\033[31m",
},
}
func writeTestcase(c *testCase) {
fmt.Printf("::group::")
if c.pass {
fmt.Printf("\033[0;32m✅ %s\033[0m (%s)\n", c.name, c.time)
} else {
fmt.Printf("\033[0;31m❌ %s\033[0m (%s)\n", c.name, c.time)
}
for _, line := range c.lines {
format := logLevelConfig[line.level]
fmt.Printf(
"%s%s\033[0m %s%s \033[0;37m(%s:%d)\033[0m\n",
format.symbolColor,
format.symbol,
format.color,
line.message,
path.Base(line.file),
line.line,
)
}
fmt.Printf("::endgroup::\n")
}
type gitHubActionsWriter struct {
backend io.Writer
lock *sync.Mutex
testCases map[string]*testCase
lastTestCase string
lastLine string
}
func (g *gitHubActionsWriter) Write(p []byte) (n int, err error) {
g.lock.Lock()
defer g.lock.Unlock()
lines := strings.Split(fmt.Sprintf("%s%s", g.lastLine, string(p)), "\n")
for _, line := range lines[:len(lines)-1] {
if strings.TrimSpace(line) == "" {
continue
}
switch {
case strings.HasPrefix(strings.TrimSpace(line), "=== RUN "):
g.lastTestCase = g.processRun(line)
case strings.HasPrefix(strings.TrimSpace(line), "=== CONT "):
g.lastTestCase = g.processCont(line)
case strings.HasPrefix(strings.TrimSpace(line), "=== PAUSE "):
g.lastTestCase = g.processPause(line)
case strings.HasPrefix(strings.TrimSpace(line), "--- PASS:"):
g.lastTestCase = g.processPass(line)
case strings.HasPrefix(strings.TrimSpace(line), "--- FAIL:"):
g.lastTestCase = g.processFail(line)
case line == "PASS":
case line == "FAIL":
case line == "":
default:
g.processDefault(line)
}
}
g.lastLine = lines[len(lines)-1]
return len(p), nil
}
func (g *gitHubActionsWriter) processCont(line string) string {
return strings.TrimSpace(strings.Replace(line, "=== CONT ", "", 1))
}
func (g *gitHubActionsWriter) processPause(line string) string {
return strings.TrimSpace(strings.Replace(line, "=== PAUSE ", "", 1))
}
func (g *gitHubActionsWriter) processPass(line string) string {
parts := strings.SplitN(strings.TrimSpace(line), " ", 4)
lastTestCase := parts[2]
g.testCases[lastTestCase].pass = true
lastTestCase = ""
return lastTestCase
}
func (g *gitHubActionsWriter) processFail(line string) string {
parts := strings.SplitN(strings.TrimSpace(line), " ", 4)
lastTestCase := parts[2]
t, err := time.ParseDuration(strings.Trim(parts[3], "()"))
if err != nil {
panic(err)
}
g.testCases[lastTestCase].time = t
lastTestCase = ""
return lastTestCase
}
func (g *gitHubActionsWriter) processDefault(line string) {
parts := strings.SplitN(strings.TrimSpace(line), "\t", 6)
if len(parts) == 6 {
lineNumber, err := strconv.ParseUint(parts[2], 10, 64)
if err != nil {
panic(err)
}
if g.lastTestCase != "" {
g.testCases[g.lastTestCase].lines = append(
g.testCases[g.lastTestCase].lines,
testCaseLine{
file: parts[1],
line: uint(lineNumber),
level: LevelString(parts[3]),
code: parts[4],
message: strings.TrimSpace(parts[5]),
},
)
} else {
panic(fmt.Errorf("no test case for %s, line: %s", g.lastTestCase, line))
}
} else {
if g.lastTestCase != "" {
if _, ok := g.testCases[g.lastTestCase]; !ok {
panic(fmt.Errorf("no test case for %s, line: %s", g.lastTestCase, line))
}
g.testCases[g.lastTestCase].lines = append(
g.testCases[g.lastTestCase].lines,
testCaseLine{
file: "",
line: 0,
level: LevelDebugString,
code: "",
message: strings.TrimSpace(line),
},
)
}
}
}
func (g *gitHubActionsWriter) processRun(line string) string {
lastTestCase := strings.TrimSpace(strings.Replace(line, "=== RUN ", "", 1))
if _, ok := g.testCases[lastTestCase]; !ok {
g.testCases[lastTestCase] = &testCase{
name: lastTestCase,
}
}
return lastTestCase
}
type testCase struct {
name string
pass bool
time time.Duration
lines []testCaseLine
}
type testCaseLine struct {
file string
line uint
level LevelString
code string
message string
}