Debian Codegraph @

internal/testutils/test2json.go

raw symbols SCIP
1 package testutils
2
3 import (
4 "bytes"
5 "io"
6 "strings"
7 "sync"
8 "testing"
9 )
10
11 const marker = byte(0x16) // ^V
12
13 var (
14 t2jSkip = string(marker) + "--- SKIP:"
15 t2jFail = string(marker) + "--- FAIL:"
16 )
17
18 // t2jParser implements an io.Writer that parses test2json output. It looks for
19 // lines starting with the test2json markers for skip and fail, and applies
20 // those verdicts to the parent test when Apply is called. Lines without markers
21 // are passed through directly to the underlying writer (typically os.Stdout).
22 //
23 // Running a test with -test.v=test2json does not make it produce JSON output.
24 // Rather, it causes output lines of the test harness to be prefixed with a
25 // marker byte, making it easy to differentiate test output and harness output.
26 // See cmd/internal/test2json/test2json.go in the Go source tree for more
27 // details on the test2json format.
28 type t2jParser struct {
29 tb testing.TB
30 out io.Writer
31
32 mu sync.Mutex
33 line bytes.Buffer
34
35 skipped bool
36 failed bool
37 }
38
39 func newt2jParser(tb testing.TB, out io.Writer) *t2jParser {
40 return &t2jParser{
41 tb: tb,
42 out: out,
43 }
44 }
45
46 func (w *t2jParser) WriteString(s string) (int, error) {
47 return w.Write([]byte(s))
48 }
49
50 func (w *t2jParser) Write(p []byte) (int, error) {
51 w.mu.Lock()
52 defer w.mu.Unlock()
53
54 var written int
55 for len(p) > 0 {
56 var n int
57 var err error
58
59 if p[0] == marker || w.line.Len() > 0 {
60 // Line starts with marker, or this is a line continuation.
61 n, err = w.buffer(p)
62 } else {
63 // Test output without marker, pass through directly.
64 n, err = w.passthrough(p)
65 }
66
67 written += n
68 p = p[n:]
69
70 if err != nil {
71 return written, err
72 }
73 }
74
75 return written, nil
76 }
77
78 // buffer accumulates data until it sees a newline, then parses the line for
79 // test2json markers.
80 func (w *t2jParser) buffer(p []byte) (int, error) {
81 i := bytes.IndexByte(p, '\n')
82 if i == -1 {
83 // No newline in incoming data, keep it in buffer.
84 _, _ = w.line.Write(p)
85 return len(p), nil
86 }
87
88 // Write up to and including the newline, then parse the line.
89 _, _ = w.line.Write(p[:i+1])
90 return i + 1, w.parseLine()
91 }
92
93 func (w *t2jParser) parseLine() error {
94 line := w.line.String()
95
96 switch {
97 case strings.HasPrefix(line, t2jSkip):
98 w.skipped = true
99 case strings.HasPrefix(line, t2jFail):
100 w.failed = true
101 }
102
103 w.line.Reset()
104
105 return nil
106 }
107
108 // passthrough writes p to the output until it sees a newline.
109 func (w *t2jParser) passthrough(p []byte) (int, error) {
110 i := bytes.IndexByte(p, '\n')
111 if i == -1 {
112 return w.out.Write(p)
113 }
114
115 return w.out.Write(p[:i+1])
116 }
117
118 // Apply applies a skip/fail verdict to the parent test.
119 func (w *t2jParser) Apply() {
120 w.mu.Lock()
121 defer w.mu.Unlock()
122
123 if w.skipped {
124 w.tb.SkipNow()
125 }
126
127 if w.failed {
128 w.tb.FailNow()
129 }
130 }