Skip to content

Commit 27ad87e

Browse files
committed
parser/gotest: parseLine now returns the events it creates
1 parent 2af321a commit 27ad87e

File tree

2 files changed

+105
-113
lines changed

2 files changed

+105
-113
lines changed

parser/gotest/gotest.go

Lines changed: 54 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ func (p *Parser) parse(r *reader.LimitedLineReader) (gtr.Report, error) {
143143
return gtr.Report{}, err
144144
}
145145

146+
var evs []Event
147+
146148
// Lines that exceed bufio.MaxScanTokenSize are not expected to contain
147149
// any relevant test infrastructure output, so instead of parsing them
148150
// we treat them as regular output to increase performance.
@@ -152,9 +154,13 @@ func (p *Parser) parse(r *reader.LimitedLineReader) (gtr.Report, error) {
152154
// turned out to be fine in almost all cases, it seemed an appropriate
153155
// value to use to decide whether or not to attempt parsing this line.
154156
if len(line) > bufio.MaxScanTokenSize {
155-
p.output(line)
157+
evs = p.output(line)
156158
} else {
157-
p.parseLine(line)
159+
evs = p.parseLine(line)
160+
}
161+
162+
for _, ev := range evs {
163+
p.events = append(p.events, ev)
158164
}
159165
}
160166
return p.report(p.events), nil
@@ -181,130 +187,124 @@ func (p *Parser) Events() []Event {
181187
return events
182188
}
183189

184-
func (p *Parser) parseLine(line string) {
190+
func (p *Parser) parseLine(line string) (events []Event) {
185191
if strings.HasPrefix(line, "=== RUN ") {
186-
p.runTest(strings.TrimSpace(line[8:]))
192+
return p.runTest(strings.TrimSpace(line[8:]))
187193
} else if strings.HasPrefix(line, "=== PAUSE ") {
188-
p.pauseTest(strings.TrimSpace(line[10:]))
194+
return p.pauseTest(strings.TrimSpace(line[10:]))
189195
} else if strings.HasPrefix(line, "=== CONT ") {
190-
p.contTest(strings.TrimSpace(line[9:]))
196+
return p.contTest(strings.TrimSpace(line[9:]))
191197
} else if matches := regexEndTest.FindStringSubmatch(line); len(matches) == 5 {
192-
p.endTest(line, matches[1], matches[2], matches[3], matches[4])
198+
return p.endTest(line, matches[1], matches[2], matches[3], matches[4])
193199
} else if matches := regexStatus.FindStringSubmatch(line); len(matches) == 2 {
194-
p.status(matches[1])
200+
return p.status(matches[1])
195201
} else if matches := regexSummary.FindStringSubmatch(line); len(matches) == 8 {
196-
p.summary(matches[1], matches[2], matches[3], matches[4], matches[5], matches[6], matches[7])
202+
return p.summary(matches[1], matches[2], matches[3], matches[4], matches[5], matches[6], matches[7])
197203
} else if matches := regexCoverage.FindStringSubmatch(line); len(matches) == 3 {
198-
p.coverage(matches[1], matches[2])
204+
return p.coverage(matches[1], matches[2])
199205
} else if matches := regexBenchmark.FindStringSubmatch(line); len(matches) == 2 {
200-
p.runBench(matches[1])
206+
return p.runBench(matches[1])
201207
} else if matches := regexBenchSummary.FindStringSubmatch(line); len(matches) == 7 {
202-
p.benchSummary(matches[1], matches[2], matches[3], matches[4], matches[5], matches[6])
208+
return p.benchSummary(matches[1], matches[2], matches[3], matches[4], matches[5], matches[6])
203209
} else if matches := regexEndBenchmark.FindStringSubmatch(line); len(matches) == 3 {
204-
p.endBench(matches[1], matches[2])
210+
return p.endBench(matches[1], matches[2])
205211
} else if strings.HasPrefix(line, "# ") {
206-
// TODO(jstemmer): this should just be output; we should detect build output when building report
207212
fields := strings.Fields(strings.TrimPrefix(line, "# "))
208213
if len(fields) == 1 || len(fields) == 2 {
209-
p.buildOutput(fields[0])
210-
} else {
211-
p.output(line)
214+
return p.buildOutput(fields[0])
212215
}
213-
} else {
214-
p.output(line)
215216
}
217+
return p.output(line)
216218
}
217219

218-
func (p *Parser) add(event Event) {
219-
p.events = append(p.events, event)
220+
func (p *Parser) runTest(name string) []Event {
221+
return []Event{{Type: "run_test", Name: name}}
220222
}
221223

222-
func (p *Parser) runTest(name string) {
223-
p.add(Event{Type: "run_test", Name: name})
224+
func (p *Parser) pauseTest(name string) []Event {
225+
return []Event{{Type: "pause_test", Name: name}}
224226
}
225227

226-
func (p *Parser) pauseTest(name string) {
227-
p.add(Event{Type: "pause_test", Name: name})
228+
func (p *Parser) contTest(name string) []Event {
229+
return []Event{{Type: "cont_test", Name: name}}
228230
}
229231

230-
func (p *Parser) contTest(name string) {
231-
p.add(Event{Type: "cont_test", Name: name})
232-
}
233-
234-
func (p *Parser) endTest(line, indent, result, name, duration string) {
232+
func (p *Parser) endTest(line, indent, result, name, duration string) []Event {
233+
var events []Event
235234
if idx := strings.Index(line, fmt.Sprintf("%s--- %s:", indent, result)); idx > 0 {
236-
p.output(line[:idx])
235+
events = append(events, p.output(line[:idx])...)
237236
}
238237
_, n := stripIndent(indent)
239-
p.add(Event{
238+
events = append(events, Event{
240239
Type: "end_test",
241240
Name: name,
242241
Result: result,
243242
Indent: n,
244243
Duration: parseSeconds(duration),
245244
})
245+
return events
246246
}
247247

248-
func (p *Parser) status(result string) {
249-
p.add(Event{Type: "status", Result: result})
248+
func (p *Parser) status(result string) []Event {
249+
return []Event{{Type: "status", Result: result}}
250250
}
251251

252-
func (p *Parser) summary(result, name, duration, cached, status, covpct, packages string) {
253-
p.add(Event{
252+
func (p *Parser) summary(result, name, duration, cached, status, covpct, packages string) []Event {
253+
return []Event{{
254254
Type: "summary",
255255
Result: result,
256256
Name: name,
257257
Duration: parseSeconds(duration),
258258
Data: strings.TrimSpace(cached + " " + status),
259259
CovPct: parseFloat(covpct),
260260
CovPackages: parsePackages(packages),
261-
})
261+
}}
262262
}
263263

264-
func (p *Parser) coverage(percent, packages string) {
265-
p.add(Event{
264+
func (p *Parser) coverage(percent, packages string) []Event {
265+
return []Event{{
266266
Type: "coverage",
267267
CovPct: parseFloat(percent),
268268
CovPackages: parsePackages(packages),
269-
})
269+
}}
270270
}
271271

272-
func (p *Parser) runBench(name string) {
273-
p.add(Event{
272+
func (p *Parser) runBench(name string) []Event {
273+
return []Event{{
274274
Type: "run_benchmark",
275275
Name: name,
276-
})
276+
}}
277277
}
278278

279-
func (p *Parser) benchSummary(name, iterations, nsPerOp, mbPerSec, bytesPerOp, allocsPerOp string) {
280-
p.add(Event{
279+
func (p *Parser) benchSummary(name, iterations, nsPerOp, mbPerSec, bytesPerOp, allocsPerOp string) []Event {
280+
return []Event{{
281281
Type: "benchmark",
282282
Name: name,
283283
Iterations: parseInt(iterations),
284284
NsPerOp: parseFloat(nsPerOp),
285285
MBPerSec: parseFloat(mbPerSec),
286286
BytesPerOp: parseInt(bytesPerOp),
287287
AllocsPerOp: parseInt(allocsPerOp),
288-
})
288+
}}
289289
}
290290

291-
func (p *Parser) endBench(result, name string) {
292-
p.add(Event{
291+
func (p *Parser) endBench(result, name string) []Event {
292+
return []Event{{
293293
Type: "end_benchmark",
294294
Name: name,
295295
Result: result,
296-
})
296+
}}
297297
}
298298

299-
func (p *Parser) buildOutput(packageName string) {
300-
p.add(Event{
299+
func (p *Parser) buildOutput(packageName string) []Event {
300+
return []Event{{
301301
Type: "build_output",
302302
Name: packageName,
303-
})
303+
}}
304304
}
305305

306-
func (p *Parser) output(line string) {
307-
p.add(Event{Type: "output", Data: line})
306+
func (p *Parser) output(line string) []Event {
307+
return []Event{{Type: "output", Data: line}}
308308
}
309309

310310
func parseSeconds(s string) time.Duration {

0 commit comments

Comments
 (0)