-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparser.go
281 lines (228 loc) · 5.58 KB
/
parser.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
277
278
279
280
281
package main
import (
"bufio"
"encoding/json"
"fmt"
"log/slog"
"os"
"sort"
"time"
)
type testOutput struct {
Time time.Time `json:"Time"`
Action action `json:"Action"`
Package string `json:"Package"`
Test string `json:"Test"`
}
type action string
const (
actionRun action = "run"
actionPause action = "pause"
actionPass action = "pass"
actionCont action = "cont"
actionFail action = "fail"
actionSkip action = "skip"
)
func (t testOutput) IsZero() bool {
return t == testOutput{}
}
type TestName struct {
Package string
TestName string
}
func (t TestName) String() string {
return fmt.Sprintf("%s/%s", t.Package, t.TestName)
}
type TestExecution struct {
Test TestName
Start time.Time
End time.Time
Passed bool
}
func (t TestExecution) Duration() time.Duration {
if t.Start.IsZero() || t.End.IsZero() {
return 0
}
return t.End.Sub(t.Start)
}
type TestExecutions map[TestName]TestExecution
func (t TestExecutions) MarshalJSON() ([]byte, error) {
m := map[string]TestExecution{}
for k, v := range t {
m[k.String()] = v
}
return json.Marshal(m)
}
func (t TestExecutions) Update(testName TestName, updateFn func(TestExecution) TestExecution) {
if _, ok := t[testName]; !ok {
t[testName] = TestExecution{
Test: testName,
}
}
t[testName] = updateFn(t[testName])
}
func (t TestExecutions) ByTestName(testName TestName) (TestExecution, bool) {
te, ok := t[testName]
return te, ok
}
func (t TestExecutions) AsSlice() []TestExecution {
slice := make([]TestExecution, 0, len(t))
for _, execution := range t {
slice = append(slice, execution)
}
return slice
}
type ParseResult struct {
TestPauses TestExecutions
TestRuns TestExecutions
Start time.Time
End time.Time
MaxDuration time.Duration
Failed bool
}
func (p ParseResult) TestNamesOrderedByStart() []TestName {
allExecutions := append(p.TestPauses.AsSlice(), p.TestRuns.AsSlice()...)
sort.Slice(allExecutions, func(i, j int) bool {
return allExecutions[i].Start.Before(allExecutions[j].Start)
})
testNames := make([]TestName, 0, len(p.TestRuns))
uniqTestNames := make(map[TestName]struct{}, len(p.TestRuns))
for _, execution := range allExecutions {
if _, ok := uniqTestNames[execution.Test]; !ok {
testNames = append(testNames, execution.Test)
uniqTestNames[execution.Test] = struct{}{}
}
}
return testNames
}
func Parse(scanner *bufio.Scanner) ParseResult {
testRuns := make(TestExecutions)
testPauses := make(TestExecutions)
start := time.Time{}
end := time.Time{}
maxDuration := time.Duration(0)
failed := false
i := 0
for scanner.Scan() {
s := scanner.Text()
i++
if !dontPassOutput {
_, _ = fmt.Fprintln(os.Stderr, s)
}
if s == "" {
continue
}
var out testOutput
if err := json.Unmarshal([]byte(s), &out); err != nil {
slog.Debug("failed to unmarshal", "line", i, "error", err)
continue
}
if out.IsZero() {
slog.Debug("zero value", "line", i)
continue
}
if out.Action == actionFail {
failed = true
}
if !out.Time.IsZero() {
if start.IsZero() || out.Time.Before(start) {
start = out.Time
}
if end.IsZero() || out.Time.After(end) {
end = out.Time
}
}
tn := TestName{
Package: out.Package,
TestName: out.Test,
}
switch out.Action {
case actionPause:
testPauses.Update(tn, func(te TestExecution) TestExecution {
te.Start = out.Time
return te
})
case actionCont:
testPauses.Update(tn, func(te TestExecution) TestExecution {
te.End = out.Time
return te
})
testRuns.Update(tn, func(te TestExecution) TestExecution {
te.Start = out.Time
return te
})
case actionRun:
testRuns.Update(tn, func(te TestExecution) TestExecution {
te.Start = out.Time
return te
})
case actionPass, actionFail, actionSkip:
testRuns.Update(tn, func(te TestExecution) TestExecution {
te.End = out.Time
te.Passed = out.Action == actionPass
return te
})
}
}
for test, execution := range testPauses {
if execution.Duration() == 0 {
delete(testPauses, test)
slog.Debug("removed invalid test pause", "test", test)
continue
}
if execution.Duration() <= testDurationCutoffDuration {
delete(testPauses, test)
slog.Debug("removed test pause below threshold", "test", test, "duration", execution.Duration())
continue
}
if execution.Test == (TestName{}) {
delete(testPauses, test)
slog.Debug("removed test pause with empty test name", "test", test)
continue
}
slog.Debug(
"parsed test pause",
"test", execution.Test,
"paused_from", execution.Start,
"to", execution.End,
"for", execution.Duration(),
)
}
for test, execution := range testRuns {
if execution.Duration() == 0 {
delete(testRuns, test)
slog.Debug("removed invalid test run", "test", test)
continue
}
if execution.Duration() <= testDurationCutoffDuration {
delete(testRuns, test)
slog.Debug("removed test run below threshold", "test", test, "duration", execution.Duration())
continue
}
if execution.Test == (TestName{}) {
delete(testRuns, test)
slog.Debug("removed test run with empty test name", "test", test)
continue
}
slog.Debug(
"parsed test",
"test", execution.Test,
"ran_from", execution.Start,
"to", execution.End,
"for", execution.Duration(),
"passed", execution.Passed,
)
if execution.Duration() > maxDuration {
maxDuration = execution.Duration()
}
}
slog.Debug("parsed", "start", start, "end", end)
return ParseResult{
TestPauses: testPauses,
TestRuns: testRuns,
Start: start,
End: end,
MaxDuration: maxDuration,
Failed: failed,
}
}