-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtypes.go
More file actions
433 lines (367 loc) · 10.1 KB
/
types.go
File metadata and controls
433 lines (367 loc) · 10.1 KB
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
package parser
import (
"fmt"
"sort"
"time"
"github.com/google/uuid"
)
// Properties maps additional parameters for test suites
type Properties map[string]string
// State indicates state of specific test
type State string
const (
StatePassed State = "passed" // test was successful
StateError State = "error" // test errored due to unexpected behaviour when running test i.e. exception
StateFailed State = "failed" // test failed due to invalid test result
StateSkipped State = "skipped" // test was skipped
StateDisabled State = "disabled" // test was disabled
)
// Status stores information about parsing results
type Status string
const (
StatusSuccess Status = "success" // parsing was successful
StatusError Status = "error" // parsing failed due to error
)
// Result is a collection of test results
type Result struct {
TestResults []TestResults `json:"testResults"`
}
// NewResult ...
func NewResult() Result {
return Result{
TestResults: []TestResults{},
}
}
// Combine test results that are part of result
func (me *Result) Combine(other Result) {
for i := range other.TestResults {
testResult := other.TestResults[i]
testResult.Flatten()
foundTestResultsIdx, found := me.hasTestResults(testResult)
if found {
me.TestResults[foundTestResultsIdx].Combine(testResult)
me.TestResults[foundTestResultsIdx].Aggregate()
} else {
me.TestResults = append(me.TestResults, testResult)
}
}
sort.SliceStable(me.TestResults, func(i, j int) bool { return me.TestResults[i].ID < me.TestResults[j].ID })
for i := range me.TestResults {
me.TestResults[i].Aggregate()
}
}
// Flatten ...
func (me *TestResults) Flatten() {
testResults := NewTestResults()
for i := range me.Suites {
foundSuiteIdx, found := testResults.hasSuite(me.Suites[i])
if found {
testResults.Suites[foundSuiteIdx].Combine(me.Suites[i])
testResults.Suites[foundSuiteIdx].Aggregate()
} else {
testResults.Suites = append(testResults.Suites, me.Suites[i])
}
}
me.Suites = testResults.Suites
sort.SliceStable(me.Suites, func(i, j int) bool {
return me.Suites[i].ID < me.Suites[j].ID
})
}
func (me *Result) hasTestResults(testResults TestResults) (int, bool) {
for i := range me.TestResults {
if me.TestResults[i].ID == testResults.ID {
return i, true
}
}
return -1, false
}
// TestResults represents well defined group of test suites and.
type TestResults struct {
ID string `json:"id"` // deterministic identifiers are required for test analytics, please follow https://github.com/semaphoreci/test-results/blob/master/docs/id-generation.md
Name string `json:"name"` //
Framework string `json:"framework"` // parsers use this field to determine if they're applicable
IsDisabled bool `json:"isDisabled"` //
Suites []Suite `json:"suites"` //
Summary Summary `json:"summary"` //
Status Status `json:"status"` // combined with StatusMessage can be used to provide insights into parser failures
StatusMessage string `json:"statusMessage"` //
}
// NewTestResults ...
func NewTestResults() TestResults {
return TestResults{
Suites: []Suite{},
Status: StatusSuccess,
StatusMessage: "",
}
}
// Combine ...
func (me *TestResults) Combine(other TestResults) {
if me.ID == other.ID {
for i := range other.Suites {
foundSuiteIdx, found := me.hasSuite(other.Suites[i])
if found {
me.Suites[foundSuiteIdx].Combine(other.Suites[i])
me.Suites[foundSuiteIdx].Aggregate()
} else {
me.Suites = append(me.Suites, other.Suites[i])
}
}
sort.SliceStable(me.Suites, func(i, j int) bool {
return me.Suites[i].ID < me.Suites[j].ID
})
}
}
func (me *TestResults) hasSuite(suite Suite) (int, bool) {
for i := range me.Suites {
if me.Suites[i].ID == suite.ID {
return i, true
}
}
return -1, false
}
// ArrangeSuitesByTestFile ...
func (me *TestResults) ArrangeSuitesByTestFile() {
newSuites := []Suite{}
for _, suite := range me.Suites {
for _, test := range suite.Tests {
var (
idx int
foundSuite *Suite
)
if test.File != "" {
idx, foundSuite = EnsureSuiteByName(newSuites, test.File)
} else {
idx, foundSuite = EnsureSuiteByName(newSuites, suite.Name)
}
foundSuite.Tests = append(foundSuite.Tests, test)
foundSuite.Aggregate()
if idx == -1 {
foundSuite.EnsureID(*me)
newSuites = append(newSuites, *foundSuite)
}
}
}
me.Suites = newSuites
me.Aggregate()
}
// EnsureSuiteByName ...
func EnsureSuiteByName(suites []Suite, name string) (int, *Suite) {
for i := range suites {
if suites[i].Name == name {
return i, &suites[i]
}
}
suite := NewSuite()
suite.Name = name
return -1, &suite
}
// EnsureID ...
func (me *TestResults) EnsureID() {
if me.ID == "" {
me.ID = me.Name
}
if me.Framework != "" {
me.ID = fmt.Sprintf("%s%s", me.ID, me.Framework)
}
me.ID = UUID(uuid.Nil, me.ID).String()
}
// RegenerateID ...
func (me *TestResults) RegenerateID() {
me.ID = ""
me.EnsureID()
for suiteIdx := range me.Suites {
me.Suites[suiteIdx].ID = ""
me.Suites[suiteIdx].EnsureID(*me)
for testIdx := range me.Suites[suiteIdx].Tests {
me.Suites[suiteIdx].Tests[testIdx].ID = ""
me.Suites[suiteIdx].Tests[testIdx].EnsureID(me.Suites[suiteIdx])
}
}
}
// Aggregate all test suite summaries
func (me *TestResults) Aggregate() {
summary := Summary{}
for i := range me.Suites {
summary.Duration += me.Suites[i].Summary.Duration
summary.Skipped += me.Suites[i].Summary.Skipped
summary.Error += me.Suites[i].Summary.Error
summary.Total += me.Suites[i].Summary.Total
summary.Failed += me.Suites[i].Summary.Failed
summary.Passed += me.Suites[i].Summary.Passed
summary.Disabled += me.Suites[i].Summary.Disabled
}
me.Summary = summary
}
// Suite ...
type Suite struct {
ID string `json:"id"`
Name string `json:"name"`
IsSkipped bool `json:"isSkipped"`
IsDisabled bool `json:"isDisabled"`
Timestamp string `json:"timestamp"`
Hostname string `json:"hostname"`
Package string `json:"package"`
Tests []Test `json:"tests"`
Properties Properties `json:"properties"`
Summary Summary `json:"summary"`
SystemOut string `json:"systemOut"`
SystemErr string `json:"systemErr"`
}
// NewSuite ...
func NewSuite() Suite {
return Suite{Tests: []Test{}}
}
// Combine ...
func (me *Suite) Combine(other Suite) {
if me.ID == other.ID {
for i := range other.Tests {
if !me.hasTest(other.Tests[i]) {
me.Tests = append(me.Tests, other.Tests[i])
}
shouldReplace, indexToReplace := me.shouldReplaceTest(other.Tests[i])
if shouldReplace && indexToReplace != -1 {
me.Tests[indexToReplace] = other.Tests[i]
}
}
sort.SliceStable(me.Tests, func(i, j int) bool {
return me.Tests[i].ID < me.Tests[j].ID
})
}
}
func (me *Suite) shouldReplaceTest(test Test) (shouldReplace bool, foundIndex int) {
foundIndex = -1
shouldReplace = false
for i := range me.Tests {
if me.Tests[i].ID == test.ID {
foundIndex = i
break
}
}
if foundIndex == -1 {
return
} else {
foundTest := me.Tests[foundIndex]
if foundTest.State == StateSkipped {
shouldReplace = true
return
}
if foundTest.State == StatePassed && test.State == StateFailed || test.State == StateError {
shouldReplace = true
return
}
return
}
}
func (me *Suite) hasTest(test Test) bool {
for i := range me.Tests {
if me.Tests[i].ID == test.ID {
return true
}
}
return false
}
// Aggregate all tests in suite
// TODO: add flag to skip aggregating already present data
func (me *Suite) Aggregate() {
summary := Summary{}
for _, test := range me.Tests {
summary.Duration += test.Duration
summary.Total++
switch test.State {
case StateSkipped:
summary.Skipped++
case StateFailed:
summary.Failed++
case StateError:
summary.Error++
case StatePassed:
summary.Passed++
case StateDisabled:
summary.Disabled++
}
}
me.Summary = summary
}
// EnsureID ...
func (me *Suite) EnsureID(tr TestResults) {
if me.ID == "" {
me.ID = me.Name
}
oldID, err := uuid.Parse(tr.ID)
if err != nil {
oldID = uuid.Nil
}
me.ID = UUID(oldID, me.ID).String()
}
// AppendTest ...
func (me *Suite) AppendTest(test Test) {
me.Tests = append(me.Tests, test)
me.Aggregate()
}
// Test ...
type Test struct {
ID string `json:"id"`
File string `json:"file"`
Classname string `json:"classname"`
Package string `json:"package"`
Name string `json:"name"`
Duration time.Duration `json:"duration"`
State State `json:"state"`
Failure *Failure `json:"failure"`
Error *Error `json:"error"`
SystemOut string `json:"systemOut"`
SystemErr string `json:"systemErr"`
}
// NewTest ...
func NewTest() Test {
return Test{
State: StatePassed,
}
}
// EnsureID ...
func (me *Test) EnsureID(s Suite) {
if me.ID == "" {
me.ID = me.Name
}
if me.Classname != "" {
me.ID = fmt.Sprintf("%s.%s", me.Classname, me.ID)
}
if me.Failure != nil {
me.ID = fmt.Sprintf("%s.%s", "Failure", me.ID)
}
if me.Error != nil {
me.ID = fmt.Sprintf("%s.%s", "Error", me.ID)
}
me.ID = UUID(uuid.MustParse(s.ID), me.ID).String()
}
type err struct {
Message string `json:"message"`
Type string `json:"type"`
Body string `json:"body"`
}
// Failure ...
type Failure err
// NewFailure ...
func NewFailure() Failure {
return Failure{}
}
// Error ...
type Error err
// NewError ...
func NewError() Error {
return Error{}
}
// Summary contains group metrics
type Summary struct {
Total int `json:"total"` // Total tests in group
Passed int `json:"passed"` // Passed tests in group
Skipped int `json:"skipped"` // Skipped tests in group
Error int `json:"error"` // Errored tests in group
Failed int `json:"failed"` // Failed tests in group
Disabled int `json:"disabled"` // Disabled tests in group
Duration time.Duration `json:"duration"` // Total duration of the group
}
// UUID ...
func UUID(id uuid.UUID, str string) uuid.UUID {
return uuid.NewMD5(id, []byte(str))
}