-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.go
More file actions
197 lines (176 loc) · 4.83 KB
/
Copy pathstorage.go
File metadata and controls
197 lines (176 loc) · 4.83 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
package main
import (
"maps"
"slices"
"github.com/maruel/natural"
)
// TestStorage maps keys (package + test name) to all associated events.
type TestStorage map[Key]Events
// OrderedKeys returns sorted keys for stable presentation order.
func (ts TestStorage) OrderedKeys() []Key {
var tks []Key
for k := range ts {
tks = append(tks, k)
}
slices.SortStableFunc(tks, func(a, b Key) int {
if a.Package == b.Package && (a.Test == "" || b.Test == "") {
if len(a.Test) != len(b.Test) {
if len(a.Test) > len(b.Test) {
return -1
}
return 1
}
}
if natural.Less(a.String(), b.String()) {
return -1
}
if natural.Less(b.String(), a.String()) {
return 1
}
return 0
})
return tks
}
// Append adds an event into the storage.
func (ts TestStorage) Append(e Event) {
if e.Package == "" && e.ImportPath != "" {
e.Package = e.ImportPath
}
if e.Package == "" && e.FailedBuild != "" {
e.Package = e.FailedBuild
}
if e.Action == ActionBuildOutput {
e.Action = ActionOutput
}
key := e.Key()
events := ts[key]
events = append(events, e)
ts[key] = events
}
// Union merges multiple TestStorage instances.
func (ts TestStorage) Union(values ...TestStorage) TestStorage {
tests := make(TestStorage, len(ts))
maps.Copy(tests, ts)
for _, v := range values {
maps.Copy(tests, v)
}
return tests
}
// Filter returns a new TestStorage containing only entries that satisfy the predicate.
func (ts TestStorage) Filter(predicate func(k Key, events Events) bool) TestStorage {
tests := make(TestStorage)
for key, events := range ts {
if predicate(key, events) {
tests[key] = events
}
}
return tests
}
// FilterPackageResults returns only test-level events (non-empty Test).
func (ts TestStorage) FilterPackageResults() TestStorage {
return ts.Filter(func(k Key, _ Events) bool {
return k.Test != ""
})
}
// FindPackageResults returns only package-level events (empty Test).
func (ts TestStorage) FindPackageResults() TestStorage {
return ts.Filter(func(k Key, _ Events) bool {
return k.Test == ""
})
}
// FilterKeys returns tests excluding those in the exclude map.
func (ts TestStorage) FilterKeys(exclude map[Key]bool) TestStorage {
return ts.Filter(func(k Key, _ Events) bool {
return !exclude[k]
})
}
// FindPackageTests returns all events matching a package name.
func (ts TestStorage) FindPackageTests(name string) TestStorage {
return ts.Filter(func(k Key, _ Events) bool {
return k.Package == name
})
}
// FindByAction returns tests containing at least one event matching the action.
func (ts TestStorage) FindByAction(action Action) TestStorage {
return ts.Filter(func(_ Key, events Events) bool {
for _, e := range events {
if e.Action == action {
return true
}
if action == ActionBuildFail && e.Action == ActionFail && e.FailedBuild != "" {
return true
}
}
return false
})
}
// FilterAction returns tests that do NOT contain any of the specified actions.
func (ts TestStorage) FilterAction(actions ...Action) TestStorage {
actionMatch := make(map[Action]bool, len(actions))
for _, action := range actions {
actionMatch[action] = true
}
return ts.Filter(func(_ Key, events Events) bool {
for _, e := range events {
if actionMatch[e.Action] {
return false
}
}
return true
})
}
// FindByStatus returns tests matching a terminal status.
func (ts TestStorage) FindByStatus(status Status) TestStorage {
return ts.Filter(func(_ Key, events Events) bool {
return events.Status() == status
})
}
// WithCoverage returns package-level tests that have coverage data.
func (ts TestStorage) WithCoverage() TestStorage {
return ts.Filter(func(k Key, events Events) bool {
return k.Test == "" && k.Package != "" && events.FindCoverage() != ""
})
}
// FilterNotests returns tests excluding packages without any test files.
func (ts TestStorage) FilterNotests() TestStorage {
return ts.Filter(func(_ Key, events Events) bool {
return !events.IsPackageWithoutTest()
})
}
// CountTests counts distinct tests and package build failures.
func (ts TestStorage) CountTests() int {
count := 0
for key, events := range ts {
if key.Test != "" {
count++
continue
}
if events.Status() == StatusBuildFail {
count++
}
}
return count
}
// TestStats holds aggregated counts of test results.
type TestStats struct {
Pass int
Fail int
BuildFail int
None int
Skip int
}
// Stats aggregates summary test metrics in a single pass.
func (ts TestStorage) Stats() TestStats {
allPass := ts.FindByStatus(StatusPass)
allFail := ts.FindByStatus(StatusFail)
allBuildFail := ts.FindByStatus(StatusBuildFail)
allSkip := ts.FindByStatus(StatusSkip)
allNone := ts.FindByStatus(StatusNone)
return TestStats{
Pass: allPass.CountTests(),
Fail: allFail.CountTests(),
BuildFail: allBuildFail.CountTests(),
None: len(allNone),
Skip: allSkip.CountTests(),
}
}