-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsole_filter.go
More file actions
278 lines (243 loc) · 7.71 KB
/
console_filter.go
File metadata and controls
278 lines (243 loc) · 7.71 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
package devflow
import (
"fmt"
"strings"
)
type ConsoleFilter struct {
buffer []string
output func(string) // callback to write output
hasDataRace bool
shownRaceMsg bool
releasedFuncCalls int
incompleteLine string
inPanicMode bool // true when we detect a panic/timeout
}
func NewConsoleFilter(output func(string)) *ConsoleFilter {
if output == nil {
output = func(s string) { fmt.Println(s) }
}
return &ConsoleFilter{
output: output,
}
}
func (cf *ConsoleFilter) Add(input string) {
// Handle fragmentation: ensure we only process complete lines (ending in \n)
fullInput := cf.incompleteLine + input
lines := strings.Split(fullInput, "\n")
// The last element of Split is either an empty string (if input ended in \n)
// or the incomplete part of the line.
cf.incompleteLine = lines[len(lines)-1]
// Process all complete lines
for i := 0; i < len(lines)-1; i++ {
cf.addLine(lines[i])
}
}
func (cf *ConsoleFilter) addLine(line string) {
// ALWAYS show DEBUG messages
if strings.Contains(line, "DEBUG") {
cf.output(line)
return
}
// Detect panic or timeout - Enter panic mode
if strings.HasPrefix(line, "panic:") || strings.Contains(line, "test timed out") {
cf.inPanicMode = true
cf.Flush() // Flush any pending buffer content to preserve context
}
// Global markers - reset panic mode and flush buffer (package boundary).
// MUST come before the inPanicMode check so package boundaries always reset state,
// preventing panic mode from leaking into subsequent packages.
if strings.HasPrefix(line, "FAIL\t") ||
strings.HasPrefix(line, "ok\t") ||
strings.HasPrefix(line, "coverage:") ||
strings.HasPrefix(line, "pkg:") {
cf.inPanicMode = false
cf.Flush()
return
}
// If in panic mode, show everything to aid debugging
if cf.inPanicMode {
cf.output(line)
return
}
// Detect WASM released function callbacks
if line == "call to released function" {
cf.releasedFuncCalls++
return
}
// Detect data races
if strings.Contains(line, "WARNING: DATA RACE") {
cf.hasDataRace = true
return // Skip individual warnings
}
// Skip packages with no test files (noise)
if strings.Contains(line, "[no test files]") {
return
}
// Skip noise
if strings.HasPrefix(line, "go: warning:") ||
strings.HasPrefix(line, "package ") ||
strings.HasPrefix(line, "ok\t") ||
strings.HasPrefix(line, "ok \t") ||
strings.Contains(line, "build constraints exclude all Go files") ||
strings.Contains(line, "[setup failed]") ||
strings.Contains(line, "no packages to test") ||
strings.Contains(line, "(cached)") ||
strings.Contains(line, "✅ All tests passed!") ||
strings.Contains(line, "All tests passed!") ||
strings.Contains(line, "❌ WASM tests failed") ||
strings.Contains(line, "WASM tests failed") ||
strings.Contains(line, "Badges saved to") ||
strings.Contains(line, "tests passed") ||
(strings.Contains(line, "coverage:") && strings.Contains(line, "% of statements")) ||
line == "FAIL" ||
line == "PASS" ||
strings.HasPrefix(line, "exit with status") ||
strings.HasPrefix(line, "exit status") ||
// Data race details
strings.HasPrefix(line, "Read at ") ||
strings.HasPrefix(line, "Write at ") ||
strings.HasPrefix(line, "Previous write at ") ||
strings.HasPrefix(line, "Previous read at ") ||
strings.Contains(line, "by goroutine") ||
// Panic/crash details
strings.HasPrefix(line, "[signal ") ||
strings.HasPrefix(line, "goroutine ") ||
strings.HasPrefix(line, "created by ") {
return
}
trimmed := strings.TrimSpace(line)
// Skip stack traces from stdlib (/usr/local/go, /usr/lib/go)
if strings.HasPrefix(trimmed, "/usr/") {
return
}
// Keep first project file reference, skip subsequent ones
// Format: /path/to/project/file.go:line +0xhex
if strings.Contains(trimmed, ".go:") && (strings.HasPrefix(trimmed, "/") || strings.HasPrefix(trimmed, "./")) {
// Extract just filename:line from full path
// Find where the path ends (at .go:)
goIdx := strings.Index(trimmed, ".go:")
fullPath := trimmed[:goIdx+3] // includes ".go"
// Find last slash in the PATH part only
lastSlash := strings.LastIndex(fullPath, "/")
lastPart := ""
if lastSlash != -1 {
lastPart = trimmed[lastSlash+1:]
} else {
lastPart = trimmed
}
// Remove hex offset like +0x38 if present (stack traces)
if idx := strings.Index(lastPart, " +0x"); idx != -1 {
lastPart = lastPart[:idx]
}
// Add shortened reference and continue filtering
cf.buffer = append(cf.buffer, " "+lastPart)
return
}
// NOTE: We no longer filter indented .go: lines here because they may contain
// test error messages. The removePassingTestLogs mechanism handles cleanup
// for passing tests, so these lines will only remain for failing tests.
// Skip function calls with memory addresses like TestNilPointer(0xc0000a6b60)
if strings.Contains(line, "(0x") && !strings.Contains(line, ".go:") {
return
}
// Keep error lines
cf.buffer = append(cf.buffer, line)
// Remove passing test logs
if strings.Contains(line, "--- PASS:") {
cf.removePassingTestLogs(line)
}
}
func (cf *ConsoleFilter) removePassingTestLogs(passLine string) {
// Extract TestName from passLine
// Fields: "---", "PASS:", "TestName", "(0.00s)"
fields := strings.Fields(passLine)
var testName string
for i, f := range fields {
if f == "PASS:" && i+1 < len(fields) {
testName = fields[i+1]
break
}
}
if testName == "" {
return
}
// Search backwards for "=== RUN TestName"
foundRun := -1
runLinesInBetween := false
// Iterate backwards from the line before the PASS line
// (PASS line is already in buffer at last index)
searchStart := len(cf.buffer) - 2
/*
if searchStart < 0 {
// Even if we can't search backwards, we should fall through to the "RUN not found" logic
// which removes the PASS line itself. This handles "orphaned" PASS lines where the RUN
// line was flushed earlier or missing.
}
*/
for i := searchStart; i >= 0; i-- {
lineFields := strings.Fields(cf.buffer[i])
if len(lineFields) >= 3 && lineFields[0] == "===" && lineFields[1] == "RUN" {
runName := lineFields[2]
if runName == testName {
foundRun = i
break
}
// Found a RUN line for another test (nested or interleaved)
runLinesInBetween = true
}
}
if foundRun != -1 {
if !runLinesInBetween {
// Clean block: No other RUN lines in between. Safe to truncate.
// Remove from foundRun to end.
cf.buffer = cf.buffer[:foundRun]
} else {
// Interleaved or nested.
// Remove the PASS line (last element)
if len(cf.buffer) > 0 {
cf.buffer = cf.buffer[:len(cf.buffer)-1]
}
// Remove the RUN line (at foundRun)
cf.buffer = append(cf.buffer[:foundRun], cf.buffer[foundRun+1:]...)
}
} else {
// If we couldn't find the RUN line, but found a PASS line, remove the PASS line.
if len(cf.buffer) > 0 {
cf.buffer = cf.buffer[:len(cf.buffer)-1]
}
}
}
func (cf *ConsoleFilter) Flush() {
// Process any remaining partial line
if cf.incompleteLine != "" {
cf.addLine(cf.incompleteLine)
cf.incompleteLine = ""
}
// Show data race warning once
if cf.hasDataRace && !cf.shownRaceMsg {
cf.output("⚠️ WARNING: DATA RACE detected")
cf.shownRaceMsg = true
}
// Show WASM released function summary
if cf.releasedFuncCalls > 0 {
cf.output(fmt.Sprintf("⚠️ WASM: %d call(s) to released function", cf.releasedFuncCalls))
cf.releasedFuncCalls = 0
}
// Deduplicate: count repeated lines, show with ×N suffix
seen := make(map[string]int)
var unique []string
for _, line := range cf.buffer {
seen[line]++
if seen[line] == 1 {
unique = append(unique, line)
}
}
for _, line := range unique {
if count := seen[line]; count > 1 {
cf.output(fmt.Sprintf("%s (×%d)", line, count))
} else {
cf.output(line)
}
}
cf.buffer = nil
}