mirror of
https://github.com/jstemmer/go-junit-report.git
synced 2025-04-04 20:50:14 -05:00
parser/gotest: Create interface for reading lines with metadata
This commit is contained in:
parent
27ad87e370
commit
f6f9df42b9
@ -133,10 +133,10 @@ 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) {
|
||||
func (p *Parser) parse(r reader.LineReader) (gtr.Report, error) {
|
||||
p.events = nil
|
||||
for {
|
||||
line, err := r.ReadLine()
|
||||
line, _, err := r.ReadLine()
|
||||
if err == io.EOF {
|
||||
break
|
||||
} else if err != nil {
|
||||
|
@ -6,6 +6,16 @@ import (
|
||||
"io"
|
||||
)
|
||||
|
||||
// LineReader is an interface to read lines with optional Metadata.
|
||||
type LineReader interface {
|
||||
ReadLine() (string, *Metadata, error)
|
||||
}
|
||||
|
||||
// Metadata contains metadata that belongs to a line.
|
||||
type Metadata struct {
|
||||
Package string
|
||||
}
|
||||
|
||||
// LimitedLineReader reads lines from an io.Reader object with a configurable
|
||||
// line size limit. Lines exceeding the limit will be truncated, but read
|
||||
// completely from the underlying io.Reader.
|
||||
@ -14,6 +24,8 @@ type LimitedLineReader struct {
|
||||
limit int
|
||||
}
|
||||
|
||||
var _ LineReader = &LimitedLineReader{}
|
||||
|
||||
// NewLimitedLineReader returns a LimitedLineReader to read lines from r with a
|
||||
// maximum line size of limit.
|
||||
func NewLimitedLineReader(r io.Reader, limit int) *LimitedLineReader {
|
||||
@ -23,14 +35,14 @@ func NewLimitedLineReader(r io.Reader, limit int) *LimitedLineReader {
|
||||
// ReadLine returns the next line from the underlying reader. The length of the
|
||||
// line will not exceed the configured limit. ReadLine either returns a line or
|
||||
// it returns an error, never both.
|
||||
func (r *LimitedLineReader) ReadLine() (string, error) {
|
||||
func (r *LimitedLineReader) ReadLine() (string, *Metadata, error) {
|
||||
line, isPrefix, err := r.r.ReadLine()
|
||||
if err != nil {
|
||||
return "", err
|
||||
return "", nil, err
|
||||
}
|
||||
|
||||
if !isPrefix {
|
||||
return string(line), nil
|
||||
return string(line), nil, nil
|
||||
}
|
||||
|
||||
// Line is incomplete, keep reading until we reach the end of the line.
|
||||
@ -39,7 +51,7 @@ func (r *LimitedLineReader) ReadLine() (string, error) {
|
||||
for isPrefix {
|
||||
line, isPrefix, err = r.r.ReadLine()
|
||||
if err != nil {
|
||||
return "", err
|
||||
return "", nil, err
|
||||
}
|
||||
|
||||
if buf.Len() >= r.limit {
|
||||
@ -54,5 +66,5 @@ func (r *LimitedLineReader) ReadLine() (string, error) {
|
||||
if buf.Len() > r.limit {
|
||||
buf.Truncate(r.limit)
|
||||
}
|
||||
return buf.String(), nil
|
||||
return buf.String(), nil, nil
|
||||
}
|
||||
|
@ -34,7 +34,7 @@ func TestLimitedLineReader(t *testing.T) {
|
||||
input := strings.NewReader(strings.Join([]string{line1, line2}, "\n"))
|
||||
r := NewLimitedLineReader(input, testingLimit)
|
||||
|
||||
got, err := r.ReadLine()
|
||||
got, _, err := r.ReadLine()
|
||||
if err != nil {
|
||||
t.Fatalf("ReadLine() returned error %v", err)
|
||||
}
|
||||
@ -47,7 +47,7 @@ func TestLimitedLineReader(t *testing.T) {
|
||||
t.Fatalf("ReadLine() returned incorrect line, got len %d want len %d", len(got), len(want))
|
||||
}
|
||||
|
||||
got, err = r.ReadLine()
|
||||
got, _, err = r.ReadLine()
|
||||
if err != nil {
|
||||
t.Fatalf("ReadLine() returned error %v", err)
|
||||
}
|
||||
@ -56,7 +56,7 @@ func TestLimitedLineReader(t *testing.T) {
|
||||
t.Fatalf("ReadLine() returned incorrect line, got len %d want len %d", len(got), len(want))
|
||||
}
|
||||
|
||||
got, err = r.ReadLine()
|
||||
got, _, err = r.ReadLine()
|
||||
if err != io.EOF {
|
||||
t.Fatalf("ReadLine() returned unexpected error, got %v want %v\n", err, io.EOF)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user