mirror of
https://github.com/jstemmer/go-junit-report.git
synced 2025-05-20 19:04:29 -05:00
parser/gotest: Use the new LimitedLineReader in the gotest parser
This commit is contained in:
parent
3e3223a05b
commit
01f2cdde22
@ -3,7 +3,6 @@ package gotest
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"bytes"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"regexp"
|
"regexp"
|
||||||
@ -12,6 +11,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/jstemmer/go-junit-report/v2/gtr"
|
"github.com/jstemmer/go-junit-report/v2/gtr"
|
||||||
|
"github.com/jstemmer/go-junit-report/v2/parser/gotest/internal/reader"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -130,45 +130,19 @@ func NewParser(options ...Option) *Parser {
|
|||||||
// Parse parses Go test output from the given io.Reader r and returns
|
// Parse parses Go test output from the given io.Reader r and returns
|
||||||
// gtr.Report.
|
// gtr.Report.
|
||||||
func (p *Parser) Parse(r io.Reader) (gtr.Report, error) {
|
func (p *Parser) Parse(r io.Reader) (gtr.Report, error) {
|
||||||
|
return p.parse(reader.NewLimitedLineReader(r, maxLineSize))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Parser) parse(r *reader.LimitedLineReader) (gtr.Report, error) {
|
||||||
p.events = nil
|
p.events = nil
|
||||||
s := bufio.NewReader(r)
|
|
||||||
for {
|
for {
|
||||||
line, isPrefix, err := s.ReadLine()
|
line, err := r.ReadLine()
|
||||||
if err == io.EOF {
|
if err == io.EOF {
|
||||||
break
|
break
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
return gtr.Report{}, err
|
return gtr.Report{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if !isPrefix {
|
|
||||||
p.parseLine(string(line))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// Line is incomplete, keep reading until we reach the end of the line.
|
|
||||||
var buf bytes.Buffer
|
|
||||||
buf.Write(line) // ignore err, always nil
|
|
||||||
for isPrefix {
|
|
||||||
line, isPrefix, err = s.ReadLine()
|
|
||||||
if err == io.EOF {
|
|
||||||
break
|
|
||||||
} else if err != nil {
|
|
||||||
return gtr.Report{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if buf.Len() >= maxLineSize {
|
|
||||||
// Stop writing to buf if we exceed maxLineSize. We continue
|
|
||||||
// reading however to make sure we consume the entire line.
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
buf.Write(line) // ignore err, always nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if buf.Len() > maxLineSize {
|
|
||||||
buf.Truncate(maxLineSize)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Lines that exceed bufio.MaxScanTokenSize are not expected to contain
|
// Lines that exceed bufio.MaxScanTokenSize are not expected to contain
|
||||||
// any relevant test infrastructure output, so instead of parsing them
|
// any relevant test infrastructure output, so instead of parsing them
|
||||||
// we treat them as regular output to increase performance.
|
// we treat them as regular output to increase performance.
|
||||||
@ -177,10 +151,10 @@ func (p *Parser) Parse(r io.Reader) (gtr.Report, error) {
|
|||||||
// reading lines up to bufio.MaxScanTokenSize in length. Since this
|
// reading lines up to bufio.MaxScanTokenSize in length. Since this
|
||||||
// turned out to be fine in almost all cases, it seemed an appropriate
|
// turned out to be fine in almost all cases, it seemed an appropriate
|
||||||
// value to use to decide whether or not to attempt parsing this line.
|
// value to use to decide whether or not to attempt parsing this line.
|
||||||
if buf.Len() > bufio.MaxScanTokenSize {
|
if len(line) > bufio.MaxScanTokenSize {
|
||||||
p.output(buf.String())
|
p.output(line)
|
||||||
} else {
|
} else {
|
||||||
p.parseLine(buf.String())
|
p.parseLine(line)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return p.report(p.events), nil
|
return p.report(p.events), nil
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
package gotest
|
package gotest
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"bytes"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
@ -218,58 +216,6 @@ func TestParseLine(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseLargeLine(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
desc string
|
|
||||||
inputSize int
|
|
||||||
}{
|
|
||||||
{"small size", 128},
|
|
||||||
{"under buf size", 4095},
|
|
||||||
{"buf size", 4096},
|
|
||||||
{"multiple of buf size ", 4096 * 2},
|
|
||||||
{"not multiple of buf size", 10 * 1024},
|
|
||||||
{"bufio.MaxScanTokenSize", bufio.MaxScanTokenSize},
|
|
||||||
{"over bufio.MaxScanTokenSize", bufio.MaxScanTokenSize + 1},
|
|
||||||
{"under limit", maxLineSize - 1},
|
|
||||||
{"at limit", maxLineSize},
|
|
||||||
{"just over limit", maxLineSize + 1},
|
|
||||||
{"over limit", maxLineSize + 128},
|
|
||||||
}
|
|
||||||
|
|
||||||
createInput := func(lines ...string) *bytes.Buffer {
|
|
||||||
buf := &bytes.Buffer{}
|
|
||||||
buf.WriteString("=== RUN TestOne\n--- PASS: TestOne (0.00s)\n")
|
|
||||||
buf.WriteString(strings.Join(lines, "\n"))
|
|
||||||
return buf
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, test := range tests {
|
|
||||||
t.Run(test.desc, func(t *testing.T) {
|
|
||||||
line1 := string(make([]byte, test.inputSize))
|
|
||||||
line2 := "other line"
|
|
||||||
report, err := NewParser().Parse(createInput(line1, line2))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Parse() returned error %v", err)
|
|
||||||
} else if len(report.Packages) != 1 {
|
|
||||||
t.Fatalf("Parse() returned unexpected number of packages, got %d want 1.", len(report.Packages))
|
|
||||||
} else if len(report.Packages[0].Output) != 2 {
|
|
||||||
t.Fatalf("Parse() returned unexpected number of output lines, got %d want 1.", len(report.Packages[0].Output))
|
|
||||||
}
|
|
||||||
|
|
||||||
want := line1
|
|
||||||
if len(want) > maxLineSize {
|
|
||||||
want = want[:maxLineSize]
|
|
||||||
}
|
|
||||||
if got := report.Packages[0].Output[0]; got != want {
|
|
||||||
t.Fatalf("Parse() output line1 mismatch, got len %d want len %d", len(got), len(want))
|
|
||||||
}
|
|
||||||
if report.Packages[0].Output[1] != line2 {
|
|
||||||
t.Fatalf("Parse() output line2 mismatch, got %v want %v", report.Packages[0].Output[1], line2)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestReport(t *testing.T) {
|
func TestReport(t *testing.T) {
|
||||||
events := []Event{
|
events := []Event{
|
||||||
{Type: "run_test", Name: "TestOne"},
|
{Type: "run_test", Name: "TestOne"},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user