/
tc.go
358 lines (303 loc) · 8.38 KB
/
tc.go
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"log"
)
var (
// Target patterns for text searching and editing
//
// We define these as byte slices since we use byte slices throughout this
// program for efficiency (e.g. writing to a buffer instead of concat
// strings). Alternatively, we could have found ways to use libraries such
// as src/strings/builder.go but choose to implement similar functions for
// our specific task
dashDashDashPass = []byte("--- PASS")
dashDashDashFail = []byte("--- FAIL")
dashDashDashSkip = []byte("--- SKIP")
equalEqualEqualRun = []byte("=== RUN")
underscoreTest = []byte("_test.go")
ok = []byte("ok")
pass = []byte("PASS")
fail = []byte("FAIL")
colon = []byte(":")
question = []byte("?")
fourSpaces = []byte(" ")
tenSpaces = []byte(" ")
// ANSI color escape codes
boldRed = []byte("\033[1;31m")
boldGreen = []byte("\033[1;32m")
boldYellow = []byte("\033[1;33m")
boldCyan = []byte("\033[1;36m")
red = []byte("\033[31m")
green = []byte("\033[32m")
yellow = []byte("\033[33m")
cyan = []byte("\033[36m")
grey = []byte("\033[90m")
reset = []byte("\033[0m")
)
type options struct {
nofmt bool
nocolor bool
}
type parser interface {
parseBuffer([]byte) []byte
}
// newParser returns an object that implements parser based on given options
func newParser(opt *options) parser {
if opt.nofmt || opt.nocolor {
return &customP{opt}
}
return &normalP{}
}
// normalP implements a normal parser
type normalP struct{}
// customP implements a custom parser
type customP struct {
opt *options
}
func runTestColor(p parser, rd io.Reader, wr io.Writer) {
// Allocate main buffer
buffer := bufio.NewReader(rd)
// We loop to read and parse input from stdin, line by line until EOF
for {
// ReadSlice returns of slice of buffer (does not allocate new memory)
// and WILL include '\n'
buf, err := buffer.ReadSlice('\n')
if err != nil {
if err == io.EOF {
return
}
log.Fatalf("testcolor: Unhandled error reading from stdin! %v", err)
}
// we ditch '\n' in order to annotate properly
editedBuf := p.parseBuffer(buf[:len(buf)-1])
fmt.Fprintf(wr, "%s\n", editedBuf)
}
}
func (p normalP) parseBuffer(buf []byte) []byte {
var annotatedSlice []byte
trimmedBuf := bytes.TrimLeft(buf, " ")
switch {
case bytes.HasPrefix(trimmedBuf, equalEqualEqualRun):
// === RUN
annotatedSlice = buf
case bytes.HasPrefix(trimmedBuf, dashDashDashPass):
// --- PASS
annotatedSlice = annotateWithColorAndIntent(buf, boldGreen)
case bytes.HasPrefix(trimmedBuf, pass):
// PASS
annotatedSlice = annotateWithColor(buf, boldGreen)
case bytes.HasPrefix(trimmedBuf, ok):
// ok
annotatedSlice = annotateWithColor(buf, green)
case bytes.HasPrefix(trimmedBuf, dashDashDashFail):
// --- FAIL
annotatedSlice = annotateWithColorAndIntent(buf, boldRed)
case bytes.HasPrefix(trimmedBuf, fail):
// FAIL
annotatedSlice = annotateWithColor(buf, boldRed)
case bytes.HasPrefix(trimmedBuf, dashDashDashSkip):
// --- SKIP
annotatedSlice = annotateWithColorAndIntent(buf, boldYellow)
case bytes.HasPrefix(trimmedBuf, question):
// ?
annotatedSlice = annotateWithColor(buf, cyan)
case bytes.Contains(trimmedBuf, underscoreTest):
// For lines containing '_test.go' like
//
// TestMockSucceed: mock_test.go:6: Checking if mocking succeed works
//
// we transform to
//
// mock_test.go:6:
// Checking if mocking succeed works
//
var edited bytes.Buffer
// TestMockSucceed: mock_test.go:6: Checking if mocking succeed works
// ^
// cut everything before this
//
colons := search(trimmedBuf, colon, 1)
if len(colons) < 1 {
panic("testcolor: unexpected nil from search function")
}
edited.Write(tenSpaces)
edited.Write(annotateWithColor(trimmedBuf[colons[0]+2:], grey))
annotatedSlice = edited.Bytes()
default:
annotatedSlice = buf
}
return annotatedSlice
}
func (p customP) parseBuffer(buf []byte) []byte {
var annotatedSlice []byte
trimmedBuf := bytes.TrimLeft(buf, " ")
// We have to handle 3 option possibilities
// tc -nofmt
// tc -nocolor
// tc -nofmt -nocolor
switch {
case bytes.HasPrefix(trimmedBuf, equalEqualEqualRun):
// === RUN
annotatedSlice = buf
case bytes.HasPrefix(trimmedBuf, dashDashDashPass):
// --- PASS
if p.opt.nofmt && !p.opt.nocolor {
annotatedSlice = annotateWithColor(buf, boldGreen)
break
}
if !p.opt.nofmt && p.opt.nocolor {
annotatedSlice = annotateWithIndent(buf)
break
}
annotatedSlice = buf
case bytes.HasPrefix(trimmedBuf, pass):
// PASS
if p.opt.nofmt && !p.opt.nocolor {
annotatedSlice = annotateWithColor(buf, boldGreen)
break
}
// No formatting is done here so if nocolor is given,
// just return
annotatedSlice = buf
case bytes.HasPrefix(trimmedBuf, ok):
// ok
if p.opt.nofmt && !p.opt.nocolor {
annotatedSlice = annotateWithColor(buf, boldGreen)
break
}
// No formatting is done here so if nocolor is given,
// just return
annotatedSlice = buf
case bytes.HasPrefix(trimmedBuf, dashDashDashFail):
// --- FAIL
if p.opt.nofmt && !p.opt.nocolor {
annotatedSlice = annotateWithColor(buf, boldRed)
break
}
if !p.opt.nofmt && p.opt.nocolor {
annotatedSlice = annotateWithIndent(buf)
break
}
annotatedSlice = buf
case bytes.HasPrefix(trimmedBuf, fail):
// FAIL
if p.opt.nofmt && !p.opt.nocolor {
annotatedSlice = annotateWithColor(buf, boldRed)
break
}
// No formatting is done here so if nocolor is given,
// just return
annotatedSlice = buf
case bytes.HasPrefix(trimmedBuf, dashDashDashSkip):
// --- SKIP
if p.opt.nofmt && !p.opt.nocolor {
annotatedSlice = annotateWithColor(buf, boldRed)
break
}
if !p.opt.nofmt && p.opt.nocolor {
annotatedSlice = annotateWithIndent(buf)
break
}
annotatedSlice = buf
case bytes.HasPrefix(trimmedBuf, question):
// ?
if p.opt.nofmt && !p.opt.nocolor {
annotatedSlice = annotateWithColor(buf, cyan)
break
}
// No formatting is done here so if nocolor is given,
// just return
annotatedSlice = buf
case bytes.Contains(trimmedBuf, underscoreTest):
var (
edited bytes.Buffer
colons []int
)
if p.opt.nofmt && !p.opt.nocolor {
edited.Write(fourSpaces)
edited.Write(annotateWithColor(trimmedBuf, grey))
annotatedSlice = edited.Bytes()
break
}
if !p.opt.nofmt && p.opt.nocolor {
colons = search(trimmedBuf, colon, 1)
if len(colons) < 1 {
panic("testcolor: unexpected nil from search function")
}
edited.Write(annotateWithIndent(trimmedBuf[colons[0]+2:]))
annotatedSlice = edited.Bytes()
break
}
annotatedSlice = buf
default:
annotatedSlice = buf
}
return annotatedSlice
}
// annotate builds a new slice of bytes with the ANSI escape code corresponding
// to color followed by text and ending with the reset escape code
func annotateWithColor(text []byte, color []byte) []byte {
var edited bytes.Buffer
// We don't check return val since err returned from Write is always nil
// will panic if fails
edited.Write(color)
edited.Write(text)
edited.Write(reset)
return edited.Bytes()
}
func annotateWithIndent(text []byte) []byte {
var edited bytes.Buffer
edited.Write(tenSpaces)
edited.Write(text)
return edited.Bytes()
}
func annotateWithColorAndIntent(text, color []byte) []byte {
var edited bytes.Buffer
edited.Write(tenSpaces)
edited.Write(color)
edited.Write(text)
edited.Write(reset)
return edited.Bytes()
}
// search performs a naive pattern search on s for pattern p. It returns a slice
// of indices where p begins in s, up to n occurrences (or all if n<0). It
// returns nil if no occurrences are found.
//
// TODO(Daniel): The naive implementation seems good enough for our use case.
// But we could be awesome and shoot for linear time later on with something
// like KMP
func search(s, p []byte, n int) []int {
var (
seen int
result []int
found bool
)
// Naive pattern search
for i := 0; i < len(s); i++ {
if seen == n {
break
}
// Check that we're still inbounds
if i+len(p) > len(s) {
break
}
// Lets assume s[i] is start of pattern. We check if s[i] to s[i+len(p)]
// matches p
found = true
for j := 0; j < len(p); j++ {
if s[i+j] != p[j] {
found = false
break
}
}
if found {
result = append(result, i)
seen++
}
}
return result
}