File: reader_test.go

package info (click to toggle)
golang-github-jstemmer-go-junit-report 2.1.0-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 696 kB
  • sloc: xml: 975; makefile: 24
file content (101 lines) | stat: -rw-r--r-- 2,758 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package reader

import (
	"bufio"
	"io"
	"strings"
	"testing"

	"github.com/google/go-cmp/cmp"
)

const testingLimit = 4 * 1024 * 1024

func TestLimitedLineReader(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", testingLimit - 1},
		{"at limit", testingLimit},
		{"just over limit", testingLimit + 1},
		{"over limit", testingLimit + 128},
	}

	for _, test := range tests {
		t.Run(test.desc, func(t *testing.T) {
			line1 := string(make([]byte, test.inputSize))
			line2 := "other line"
			input := strings.NewReader(strings.Join([]string{line1, line2}, "\n"))
			r := NewLimitedLineReader(input, testingLimit)

			got, _, err := r.ReadLine()
			if err != nil {
				t.Fatalf("ReadLine() returned error %v", err)
			}

			want := line1
			if len(line1) > testingLimit {
				want = want[:testingLimit]
			}
			if got != want {
				t.Fatalf("ReadLine() returned incorrect line, got len %d want len %d", len(got), len(want))
			}

			got, _, err = r.ReadLine()
			if err != nil {
				t.Fatalf("ReadLine() returned error %v", err)
			}
			want = line2
			if got != want {
				t.Fatalf("ReadLine() returned incorrect line, got len %d want len %d", len(got), len(want))
			}

			got, _, err = r.ReadLine()
			if err != io.EOF {
				t.Fatalf("ReadLine() returned unexpected error, got %v want %v\n", err, io.EOF)
			}
			if got != "" {
				t.Fatalf("ReadLine() returned unexpected line, got %v want nothing\n", got)
			}
		})
	}
}

func TestJSONEventReader(t *testing.T) {
	input := `some other output
{"Time":"2019-10-09T00:00:00.708139047+00:00","Action":"output","Package":"package/name/ok","Test":"TestOK"}
{"Time":"2019-10-09T00:00:00.708139047+00:00","Action":"output","Package":"package/name/ok","Test":"TestOK","Output":"=== RUN   TestOK\n"}
`
	want := []struct {
		line     string
		metadata *Metadata
	}{
		{"some other output", nil},
		{"=== RUN   TestOK", &Metadata{Package: "package/name/ok"}},
	}

	r := NewJSONEventReader(strings.NewReader(input))
	for i := 0; i < len(want); i++ {
		line, metadata, err := r.ReadLine()
		if err == io.EOF {
			return
		} else if err != nil {
			t.Fatalf("ReadLine() returned error %v", err)
		}

		if diff := cmp.Diff(want[i].line, line); diff != "" {
			t.Errorf("ReadLine() returned incorrect line, diff (-want, +got):\n%s\n", diff)
		}
		if diff := cmp.Diff(want[i].metadata, metadata); diff != "" {
			t.Errorf("ReadLine() Returned incorrect metadata, diff (-want, +got):\n%s\n", diff)
		}
	}
}