-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_collector.go
More file actions
132 lines (122 loc) · 4.16 KB
/
test_collector.go
File metadata and controls
132 lines (122 loc) · 4.16 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
package main
import (
"strings"
"time"
"github.com/rsanheim/plur/types"
)
// TestCollector collects test notifications and builds the final test result
type TestCollector struct {
tests []types.TestCaseNotification
failures []types.TestCaseNotification
pending []types.TestCaseNotification
suiteInfo *types.SuiteNotification
rawOutput strings.Builder
formattedFailures string
formattedPending string
formattedSummary string
}
const rawOutputBufferSize = 1024 * 8
// NewTestCollector creates a new test collector
func NewTestCollector() *TestCollector {
tc := &TestCollector{
tests: make([]types.TestCaseNotification, 0, 100), // Pre-allocate for ~100 tests
failures: make([]types.TestCaseNotification, 0, 10), // Pre-allocate for ~10 failures
pending: make([]types.TestCaseNotification, 0, 10), // Pre-allocate for ~10 pending
}
// Pre-allocate string builder for typical output size (8KB)
tc.rawOutput.Grow(rawOutputBufferSize)
return tc
}
// AddNotification adds a notification to the collector
func (collector *TestCollector) AddNotification(n types.TestNotification) {
switch n.GetEvent() {
case types.TestPassed, types.TestFailed, types.TestPending:
if tc, ok := n.(types.TestCaseNotification); ok {
collector.tests = append(collector.tests, tc)
switch n.GetEvent() {
case types.TestFailed:
collector.failures = append(collector.failures, tc)
case types.TestPending:
collector.pending = append(collector.pending, tc)
}
}
case types.SuiteStarted:
if suite, ok := n.(types.SuiteNotification); ok {
// Store the suite info from SuiteStarted which contains the load time
if collector.suiteInfo == nil {
collector.suiteInfo = &suite
} else {
// Preserve the load time from SuiteStarted
collector.suiteInfo.LoadTime = suite.LoadTime
if suite.TestCount > 0 {
collector.suiteInfo.TestCount = suite.TestCount
}
}
}
case types.SuiteFinished:
if suite, ok := n.(types.SuiteNotification); ok {
if collector.suiteInfo == nil {
collector.suiteInfo = &suite
} else {
// Update suite info with finish data, but preserve LoadTime from SuiteStarted
loadTime := collector.suiteInfo.LoadTime
collector.suiteInfo = &suite
collector.suiteInfo.LoadTime = loadTime
}
}
case types.RawOutput:
// Handle special formatted notifications
switch v := n.(type) {
case types.FormattedFailuresNotification:
collector.formattedFailures = v.Content
case types.FormattedPendingNotification:
collector.formattedPending = v.Content
case types.FormattedSummaryNotification:
collector.formattedSummary = v.Content
case types.OutputNotification:
collector.rawOutput.WriteString(v.Content + "\n")
}
}
}
func (collector *TestCollector) BuildResult(duration time.Duration) WorkerResult {
result := WorkerResult{
Output: collector.rawOutput.String(),
Duration: duration,
ExampleCount: len(collector.tests),
AssertionCount: 0,
FailureCount: len(collector.failures),
ErrorCount: 0,
PendingCount: len(collector.pending),
Tests: collector.tests,
State: types.StateSuccess,
FormattedFailures: collector.formattedFailures,
FormattedPending: collector.formattedPending,
FormattedSummary: collector.formattedSummary,
}
// Set state based on failures
if len(collector.failures) > 0 {
result.State = types.StateFailed
}
// If we have suite info, use its values
if collector.suiteInfo != nil {
result.FileLoadTime = collector.suiteInfo.LoadTime
if collector.suiteInfo.TestCount >= 0 {
result.ExampleCount = collector.suiteInfo.TestCount
}
if collector.suiteInfo.AssertionCount >= 0 {
result.AssertionCount = collector.suiteInfo.AssertionCount
}
// Use suite's failure count if available
if collector.suiteInfo.FailureCount >= 0 {
result.FailureCount = collector.suiteInfo.FailureCount
}
if collector.suiteInfo.ErrorCount >= 0 {
result.ErrorCount = collector.suiteInfo.ErrorCount
}
// Use suite's pending count if available
if collector.suiteInfo.PendingCount >= 0 {
result.PendingCount = collector.suiteInfo.PendingCount
}
}
return result
}