Skip to content

Commit d6bf223

Browse files
committed
parser/gotest: Move event processing into reportBuilder
1 parent 0fc43a2 commit d6bf223

File tree

4 files changed

+262
-247
lines changed

4 files changed

+262
-247
lines changed

parser/gotest/gotest.go

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -169,34 +169,7 @@ func (p *Parser) report(events []Event) gtr.Report {
169169
rb.timestampFunc = p.timestampFunc
170170
}
171171
for _, ev := range events {
172-
switch ev.Type {
173-
case "run_test":
174-
rb.CreateTest(ev.Name)
175-
case "pause_test":
176-
rb.PauseTest(ev.Name)
177-
case "cont_test":
178-
rb.ContinueTest(ev.Name)
179-
case "end_test":
180-
rb.EndTest(ev.Name, ev.Result, ev.Duration, ev.Indent)
181-
case "run_benchmark":
182-
rb.CreateBenchmark(ev.Name)
183-
case "benchmark":
184-
rb.BenchmarkResult(ev.Name, ev.Iterations, ev.NsPerOp, ev.MBPerSec, ev.BytesPerOp, ev.AllocsPerOp)
185-
case "end_benchmark":
186-
rb.EndBenchmark(ev.Name, ev.Result)
187-
case "status":
188-
rb.End()
189-
case "summary":
190-
rb.CreatePackage(ev.Name, ev.Result, ev.Duration, ev.Data)
191-
case "coverage":
192-
rb.Coverage(ev.CovPct, ev.CovPackages)
193-
case "build_output":
194-
rb.CreateBuildError(ev.Name)
195-
case "output":
196-
rb.AppendOutput(ev.Data)
197-
default:
198-
fmt.Printf("unhandled event type: %v\n", ev.Type)
199-
}
172+
rb.ProcessEvent(ev)
200173
}
201174
return rb.Build()
202175
}

parser/gotest/gotest_test.go

Lines changed: 0 additions & 219 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import (
66
"testing"
77
"time"
88

9-
"github.com/jstemmer/go-junit-report/v2/gtr"
10-
119
"github.com/google/go-cmp/cmp"
1210
)
1311

@@ -215,220 +213,3 @@ func TestParseLine(t *testing.T) {
215213
})
216214
}
217215
}
218-
219-
func TestReport(t *testing.T) {
220-
events := []Event{
221-
{Type: "run_test", Name: "TestOne"},
222-
{Type: "output", Data: "\tHello"},
223-
{Type: "end_test", Name: "TestOne", Result: "PASS", Duration: 1 * time.Millisecond},
224-
{Type: "status", Result: "PASS"},
225-
{Type: "run_test", Name: "TestSkip"},
226-
{Type: "end_test", Name: "TestSkip", Result: "SKIP", Duration: 1 * time.Millisecond},
227-
{Type: "summary", Result: "ok", Name: "package/name", Duration: 1 * time.Millisecond},
228-
{Type: "run_test", Name: "TestOne"},
229-
{Type: "output", Data: "\tfile_test.go:10: error"},
230-
{Type: "end_test", Name: "TestOne", Result: "FAIL", Duration: 1 * time.Millisecond},
231-
{Type: "status", Result: "FAIL"},
232-
{Type: "summary", Result: "FAIL", Name: "package/name2", Duration: 1 * time.Millisecond},
233-
{Type: "output", Data: "goarch: amd64"},
234-
{Type: "run_benchmark", Name: "BenchmarkOne"},
235-
{Type: "benchmark", Name: "BenchmarkOne", NsPerOp: 100},
236-
{Type: "end_benchmark", Name: "BenchmarkOne", Result: "BENCH"},
237-
{Type: "run_benchmark", Name: "BenchmarkTwo"},
238-
{Type: "benchmark", Name: "BenchmarkTwo"},
239-
{Type: "end_benchmark", Name: "BenchmarkTwo", Result: "FAIL"},
240-
{Type: "status", Result: "PASS"},
241-
{Type: "summary", Result: "ok", Name: "package/name3", Duration: 1234 * time.Millisecond},
242-
{Type: "build_output", Name: "package/failing1"},
243-
{Type: "output", Data: "error message"},
244-
{Type: "summary", Result: "FAIL", Name: "package/failing1", Data: "[build failed]"},
245-
}
246-
want := gtr.Report{
247-
Packages: []gtr.Package{
248-
{
249-
Name: "package/name",
250-
Duration: 1 * time.Millisecond,
251-
Timestamp: testTimestamp,
252-
Tests: []gtr.Test{
253-
{
254-
ID: 1,
255-
Name: "TestOne",
256-
Duration: 1 * time.Millisecond,
257-
Result: gtr.Pass,
258-
Output: []string{
259-
"\tHello", // TODO: strip tabs?
260-
},
261-
Data: map[string]interface{}{},
262-
},
263-
{
264-
ID: 2,
265-
Name: "TestSkip",
266-
Duration: 1 * time.Millisecond,
267-
Result: gtr.Skip,
268-
Data: map[string]interface{}{},
269-
},
270-
},
271-
},
272-
{
273-
Name: "package/name2",
274-
Duration: 1 * time.Millisecond,
275-
Timestamp: testTimestamp,
276-
Tests: []gtr.Test{
277-
{
278-
ID: 3,
279-
Name: "TestOne",
280-
Duration: 1 * time.Millisecond,
281-
Result: gtr.Fail,
282-
Output: []string{
283-
"\tfile_test.go:10: error",
284-
},
285-
Data: map[string]interface{}{},
286-
},
287-
},
288-
},
289-
{
290-
Name: "package/name3",
291-
Duration: 1234 * time.Millisecond,
292-
Timestamp: testTimestamp,
293-
Tests: []gtr.Test{
294-
{
295-
ID: 4,
296-
Name: "BenchmarkOne",
297-
Result: gtr.Pass,
298-
Data: map[string]interface{}{key: Benchmark{NsPerOp: 100}},
299-
},
300-
{
301-
ID: 5,
302-
Name: "BenchmarkTwo",
303-
Result: gtr.Fail,
304-
Data: map[string]interface{}{},
305-
},
306-
},
307-
Output: []string{"goarch: amd64"},
308-
},
309-
{
310-
Name: "package/failing1",
311-
Timestamp: testTimestamp,
312-
BuildError: gtr.Error{
313-
ID: 6,
314-
Name: "package/failing1",
315-
Cause: "[build failed]",
316-
Output: []string{"error message"},
317-
},
318-
},
319-
},
320-
}
321-
322-
parser := NewParser(TimestampFunc(testTimestampFunc))
323-
got := parser.report(events)
324-
if diff := cmp.Diff(want, got); diff != "" {
325-
t.Errorf("FromEvents report incorrect, diff (-want, +got):\n%v", diff)
326-
}
327-
}
328-
329-
func TestSubtestModes(t *testing.T) {
330-
events := []Event{
331-
{Type: "run_test", Name: "TestParent"},
332-
{Type: "output", Data: "TestParent before"},
333-
{Type: "run_test", Name: "TestParent/Subtest#1"},
334-
{Type: "output", Data: "Subtest#1 output"},
335-
{Type: "run_test", Name: "TestParent/Subtest#2"},
336-
{Type: "output", Data: "Subtest#2 output"},
337-
{Type: "cont_test", Name: "TestParent"},
338-
{Type: "output", Data: "TestParent after"},
339-
{Type: "end_test", Name: "TestParent", Result: "PASS", Duration: 1 * time.Millisecond},
340-
{Type: "end_test", Name: "TestParent/Subtest#1", Result: "FAIL", Duration: 2 * time.Millisecond},
341-
{Type: "end_test", Name: "TestParent/Subtest#2", Result: "PASS", Duration: 3 * time.Millisecond},
342-
{Type: "output", Data: "output"},
343-
{Type: "summary", Result: "FAIL", Name: "package/name", Duration: 1 * time.Millisecond},
344-
}
345-
346-
tests := []struct {
347-
name string
348-
mode SubtestMode
349-
want gtr.Report
350-
}{
351-
{
352-
name: "ignore subtest parent results",
353-
mode: IgnoreParentResults,
354-
want: gtr.Report{
355-
Packages: []gtr.Package{
356-
{
357-
Name: "package/name",
358-
Duration: 1 * time.Millisecond,
359-
Timestamp: testTimestamp,
360-
Tests: []gtr.Test{
361-
{
362-
ID: 1,
363-
Name: "TestParent",
364-
Duration: 1 * time.Millisecond,
365-
Result: gtr.Pass,
366-
Output: []string{"TestParent before", "TestParent after"},
367-
Data: map[string]interface{}{},
368-
},
369-
{
370-
ID: 2,
371-
Name: "TestParent/Subtest#1",
372-
Duration: 2 * time.Millisecond,
373-
Result: gtr.Fail,
374-
Output: []string{"Subtest#1 output"},
375-
Data: map[string]interface{}{},
376-
},
377-
{
378-
ID: 3,
379-
Name: "TestParent/Subtest#2",
380-
Duration: 3 * time.Millisecond,
381-
Result: gtr.Pass,
382-
Output: []string{"Subtest#2 output"},
383-
Data: map[string]interface{}{},
384-
},
385-
},
386-
Output: []string{"output"},
387-
},
388-
},
389-
},
390-
},
391-
{
392-
name: "exclude subtest parents",
393-
mode: ExcludeParents,
394-
want: gtr.Report{
395-
Packages: []gtr.Package{
396-
{
397-
Name: "package/name",
398-
Duration: 1 * time.Millisecond,
399-
Timestamp: testTimestamp,
400-
Tests: []gtr.Test{
401-
{
402-
ID: 2,
403-
Name: "TestParent/Subtest#1",
404-
Duration: 2 * time.Millisecond,
405-
Result: gtr.Fail,
406-
Output: []string{"Subtest#1 output"},
407-
Data: map[string]interface{}{},
408-
},
409-
{
410-
ID: 3,
411-
Name: "TestParent/Subtest#2",
412-
Duration: 3 * time.Millisecond,
413-
Result: gtr.Pass,
414-
Output: []string{"Subtest#2 output"},
415-
Data: map[string]interface{}{},
416-
},
417-
},
418-
Output: []string{"TestParent before", "TestParent after", "output"},
419-
},
420-
},
421-
},
422-
},
423-
}
424-
425-
for _, test := range tests {
426-
t.Run(test.name, func(t *testing.T) {
427-
parser := NewParser(TimestampFunc(testTimestampFunc), SetSubtestMode(test.mode))
428-
got := parser.report(events)
429-
if diff := cmp.Diff(test.want, got); diff != "" {
430-
t.Errorf("Invalid report created from events, diff (-want, +got):\n%v", diff)
431-
}
432-
})
433-
}
434-
}

parser/gotest/report_builder.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package gotest
22

33
import (
4+
"fmt"
45
"strings"
56
"time"
67

@@ -49,6 +50,39 @@ func newReportBuilder() *reportBuilder {
4950
}
5051
}
5152

53+
// ProcessEvent gives an event to this reportBuilder to be processed for this
54+
// report.
55+
func (b *reportBuilder) ProcessEvent(ev Event) {
56+
switch ev.Type {
57+
case "run_test":
58+
b.CreateTest(ev.Name)
59+
case "pause_test":
60+
b.PauseTest(ev.Name)
61+
case "cont_test":
62+
b.ContinueTest(ev.Name)
63+
case "end_test":
64+
b.EndTest(ev.Name, ev.Result, ev.Duration, ev.Indent)
65+
case "run_benchmark":
66+
b.CreateBenchmark(ev.Name)
67+
case "benchmark":
68+
b.BenchmarkResult(ev.Name, ev.Iterations, ev.NsPerOp, ev.MBPerSec, ev.BytesPerOp, ev.AllocsPerOp)
69+
case "end_benchmark":
70+
b.EndBenchmark(ev.Name, ev.Result)
71+
case "status":
72+
b.End()
73+
case "summary":
74+
b.CreatePackage(ev.Name, ev.Result, ev.Duration, ev.Data)
75+
case "coverage":
76+
b.Coverage(ev.CovPct, ev.CovPackages)
77+
case "build_output":
78+
b.CreateBuildError(ev.Name)
79+
case "output":
80+
b.AppendOutput(ev.Data)
81+
default:
82+
fmt.Printf("reportBuilder: unhandled event type: %v\n", ev.Type)
83+
}
84+
}
85+
5286
// newID returns a new unique id and sets the active context this id.
5387
func (b *reportBuilder) newID() int {
5488
id := b.nextID

0 commit comments

Comments
 (0)