parser/gotest: Add Package to Event

This allows each event to belong to a package, if we know it in advance.
This commit is contained in:
Joël Stemmer 2022-08-11 23:54:11 +01:00
parent f6f9df42b9
commit bd21d54501
2 changed files with 15 additions and 2 deletions

View File

@ -1,12 +1,17 @@
package gotest
import "time"
import (
"time"
"github.com/jstemmer/go-junit-report/v2/parser/gotest/internal/reader"
)
// Event is a single event in a Go test or benchmark.
type Event struct {
Type string `json:"type"`
Name string `json:"name,omitempty"`
Package string `json:"pkg,omitempty"`
Result string `json:"result,omitempty"`
Duration time.Duration `json:"duration,omitempty"`
Data string `json:"data,omitempty"`
@ -23,3 +28,10 @@ type Event struct {
BytesPerOp int64 `json:"benchmark_bytes_per_op,omitempty"`
AllocsPerOp int64 `json:"benchmark_allocs_per_op,omitempty"`
}
func (e *Event) applyMetadata(m *reader.Metadata) {
if e == nil || m == nil {
return
}
e.Package = m.Package
}

View File

@ -136,7 +136,7 @@ func (p *Parser) Parse(r io.Reader) (gtr.Report, error) {
func (p *Parser) parse(r reader.LineReader) (gtr.Report, error) {
p.events = nil
for {
line, _, err := r.ReadLine()
line, metadata, err := r.ReadLine()
if err == io.EOF {
break
} else if err != nil {
@ -160,6 +160,7 @@ func (p *Parser) parse(r reader.LineReader) (gtr.Report, error) {
}
for _, ev := range evs {
ev.applyMetadata(metadata)
p.events = append(p.events, ev)
}
}