-
Notifications
You must be signed in to change notification settings - Fork 171
Expand file tree
/
Copy pathformat.go
More file actions
610 lines (547 loc) · 15.5 KB
/
Copy pathformat.go
File metadata and controls
610 lines (547 loc) · 15.5 KB
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
package testjson
import (
"bufio"
"fmt"
"io"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"github.com/bitfield/gotestdox"
"github.com/fatih/color"
)
func debugFormat(out io.Writer) eventFormatterFunc {
return func(event TestEvent, _ *Execution) error {
_, err := fmt.Fprintf(out, "%s %s %s (%.3f) [%d] %s\n",
event.Package,
event.Test,
event.Action,
event.Elapsed,
event.Time.Unix(),
event.Output)
return err
}
}
// go test -v
func standardVerboseFormat(out io.Writer) EventFormatter {
buf := bufio.NewWriter(out)
return eventFormatterFunc(func(event TestEvent, _ *Execution) error {
if event.Action == ActionOutput {
_, _ = buf.WriteString(event.Output)
return buf.Flush()
}
return nil
})
}
// go test
func standardQuietFormat(out io.Writer) EventFormatter {
buf := bufio.NewWriter(out)
return eventFormatterFunc(func(event TestEvent, _ *Execution) error {
if !event.PackageEvent() {
return nil
}
if event.Output == "PASS\n" {
return nil
}
// Coverage line go1.20+
if strings.Contains(event.Output, event.Package+"\tcoverage:") {
return nil
}
if isCoverageOutputPreGo119(event.Output) {
return nil
}
if isWarningNoTestsToRunOutput(event.Output) {
return nil
}
_, _ = buf.WriteString(event.Output)
return buf.Flush()
})
}
// go test -json
func standardJSONFormat(out io.Writer) EventFormatter {
buf := bufio.NewWriter(out)
//nolint:errcheck // errors are returned by Flush
return eventFormatterFunc(func(event TestEvent, _ *Execution) error {
buf.Write(event.raw)
buf.WriteRune('\n')
return buf.Flush()
})
}
func testNameFormatTestEvent(out io.Writer, event TestEvent) {
pkgPath := RelativePackagePath(event.Package)
fmt.Fprintf(out, "%s %s%s (%.2fs)\n",
colorEvent(event)(strings.ToUpper(string(event.Action))),
joinPkgToTestName(pkgPath, event.Test),
formatRunID(event.RunID),
event.Elapsed)
}
func testDoxFormat(out io.Writer, opts FormatOptions) EventFormatter {
buf := bufio.NewWriter(out)
type Result struct {
Event TestEvent
Sentence string
}
getIcon := getIconFunc(opts)
results := map[string][]Result{}
return eventFormatterFunc(func(event TestEvent, _ *Execution) error {
switch {
case event.PackageEvent():
if !event.Action.IsTerminal() {
return nil
}
if opts.HideEmptyPackages && len(results[event.Package]) == 0 {
return nil
}
fmt.Fprintf(buf, "%s:\n", event.Package)
tests := results[event.Package]
sort.Slice(tests, func(i, j int) bool {
return tests[i].Sentence < tests[j].Sentence
})
for _, r := range tests {
fmt.Fprintf(buf, " %s %s (%.2fs)\n",
getIcon(r.Event.Action),
r.Sentence,
r.Event.Elapsed)
}
fmt.Fprintln(buf)
return buf.Flush()
case event.Action.IsTerminal():
// Fuzz test cases tend not to have interesting names,
// so only report these if they're failures
if isFuzzCase(event) {
return nil
}
results[event.Package] = append(results[event.Package], Result{
Event: event,
Sentence: gotestdox.Prettify(event.Test),
})
}
return nil
})
}
func isFuzzCase(event TestEvent) bool {
return strings.HasPrefix(event.Test, "Fuzz") &&
event.Action == ActionPass &&
TestName(event.Test).IsSubTest()
}
func testNameFormat(out io.Writer) EventFormatter {
buf := bufio.NewWriter(out)
//nolint:errcheck
return eventFormatterFunc(func(event TestEvent, exec *Execution) error {
formatTest := func() error {
testNameFormatTestEvent(buf, event)
return buf.Flush()
}
switch {
case isPkgFailureOutput(event):
buf.WriteString(event.Output)
return buf.Flush()
case event.PackageEvent():
if !event.Action.IsTerminal() {
return nil
}
result := colorEvent(event)(strings.ToUpper(string(event.Action)))
pkg := exec.Package(event.Package)
if event.Action == ActionSkip || (event.Action == ActionPass && pkg.Total == 0) {
event.Action = ActionSkip // always color these as skip actions
result = colorEvent(event)("EMPTY")
}
event.Elapsed = 0 // hide elapsed for now, for backwards compat
buf.WriteString(result)
buf.WriteRune(' ')
buf.WriteString(packageLine(event, exec.Package(event.Package)))
return buf.Flush()
case event.Action == ActionFail:
pkg := exec.Package(event.Package)
tc := pkg.LastFailedByName(event.Test)
pkg.WriteOutputTo(buf, tc.ID)
return formatTest()
case event.Action == ActionPass || event.Action == ActionSkip:
return formatTest()
}
return nil
})
}
// joinPkgToTestName for formatting.
// If the package path isn't the current directory, we add a period to separate
// the test name and the package path. If it is the current directory, we don't
// show it at all. This prevents output like ..MyTest when the test is in the
// current directory.
func joinPkgToTestName(pkg string, test string) string {
if pkg == "." {
return test
}
return pkg + "." + test
}
// formatRunID returns a formatted string of the runID.
func formatRunID(runID int) string {
if runID <= 0 {
return ""
}
return fmt.Sprintf(" (re-run %d)", runID)
}
// isPkgFailureOutput returns true if the event is package output, and the output
// doesn't match any of the expected framing messages. Events which match this
// pattern should be package-level failures (ex: exit(1) or panic in an init() or
// TestMain).
func isPkgFailureOutput(event TestEvent) bool {
out := event.Output
return all(
event.PackageEvent(),
event.Action == ActionOutput,
out != "PASS\n",
out != "FAIL\n",
!isWarningNoTestsToRunOutput(out),
!strings.HasPrefix(out, "FAIL\t"+event.Package),
!strings.HasPrefix(out, "ok \t"+event.Package),
!strings.HasPrefix(out, "? \t"+event.Package),
!isShuffleSeedOutput(out),
)
}
func all(cond ...bool) bool {
for _, c := range cond {
if !c {
return false
}
}
return true
}
func pkgNameFormat(out io.Writer, opts FormatOptions) eventFormatterFunc {
buf := bufio.NewWriter(out)
return func(event TestEvent, exec *Execution) error {
if !event.PackageEvent() {
return nil
}
_, _ = buf.WriteString(shortFormatPackageEvent(opts, event, exec))
return buf.Flush()
}
}
type icons struct {
pass string
fail string
skip string
color bool
}
func (i icons) forAction(action Action) string {
if i.color {
switch action {
case ActionPass:
return color.GreenString(i.pass)
case ActionSkip:
return color.YellowString(i.skip)
case ActionFail:
return color.RedString(i.fail)
default:
return " "
}
} else {
switch action {
case ActionPass:
return i.pass
case ActionSkip:
return i.skip
case ActionFail:
return i.fail
default:
return " "
}
}
}
func getIconFunc(opts FormatOptions) func(Action) string {
switch {
case opts.UseHiVisibilityIcons || opts.Icons == "hivis":
return icons{
pass: "✅", // WHITE HEAVY CHECK MARK
skip: "➖", // HEAVY MINUS SIGN
fail: "❌", // CROSS MARK
color: false,
}.forAction
case opts.Icons == "text":
return icons{
pass: "PASS",
skip: "SKIP",
fail: "FAIL",
color: true,
}.forAction
case opts.Icons == "codicons":
return icons{
pass: "\ueba4", // cod-pass
skip: "\ueabd", // cod-circle_slash
fail: "\uea87", // cod-error
color: true,
}.forAction
case opts.Icons == "octicons":
return icons{
pass: "\uf49e", // oct-check_circle
skip: "\uf517", // oct-skip
fail: "\uf52f", // oct-x_circle
color: true,
}.forAction
case opts.Icons == "emoticons":
return icons{
pass: "\U000f01f5", // md-emoticon_happy_outline
skip: "\U000f01f6", // md-emoticon_neutral_outline
fail: "\U000f01f8", // md-emoticon_sad_outline
color: true,
}.forAction
default:
return icons{
pass: "✓", // CHECK MARK
skip: "∅", // EMPTY SET
fail: "✖", // HEAVY MULTIPLICATION X
color: true,
}.forAction
}
}
func shortFormatPackageEvent(opts FormatOptions, event TestEvent, exec *Execution) string {
pkg := exec.Package(event.Package)
getIcon := getIconFunc(opts)
fmtEvent := func(action string) string {
return action + " " + packageLine(event, exec.Package(event.Package))
}
switch event.Action {
case ActionSkip:
if opts.HideEmptyPackages {
return ""
}
return fmtEvent(getIcon(event.Action))
case ActionPass:
if pkg.Total == 0 {
if opts.HideEmptyPackages {
return ""
}
return fmtEvent(getIcon(ActionSkip))
}
return fmtEvent(getIcon(event.Action))
case ActionFail:
return fmtEvent(getIcon(event.Action))
}
return ""
}
func packageLine(event TestEvent, pkg *Package) string {
var buf strings.Builder
buf.WriteString(RelativePackagePath(event.Package))
switch {
case pkg.cached:
buf.WriteString(" (cached)")
case event.Elapsed != 0:
d := elapsedDuration(event.Elapsed)
buf.WriteString(fmt.Sprintf(" (%s)", d))
}
if pkg.coverage != "" {
buf.WriteString(" (" + pkg.coverage + ")")
}
if event.Action == ActionFail && pkg.shuffleSeed != "" {
buf.WriteString(" (" + pkg.shuffleSeed + ")")
}
buf.WriteString("\n")
return buf.String()
}
func pkgNameWithFailuresFormat(out io.Writer, opts FormatOptions) eventFormatterFunc {
buf := bufio.NewWriter(out)
return func(event TestEvent, exec *Execution) error {
if !event.PackageEvent() {
if event.Action == ActionFail {
pkg := exec.Package(event.Package)
tc := pkg.LastFailedByName(event.Test)
pkg.WriteOutputTo(buf, tc.ID) //nolint:errcheck
return buf.Flush()
}
return nil
}
buf.WriteString(shortFormatPackageEvent(opts, event, exec))
return buf.Flush()
}
}
func colorEvent(event TestEvent) func(format string, a ...interface{}) string {
switch event.Action {
case ActionPass:
return color.GreenString
case ActionFail:
return color.RedString
case ActionSkip:
return color.YellowString
}
return color.WhiteString
}
// EventFormatter is a function which handles an event and returns a string to
// output for the event.
type EventFormatter interface {
Format(event TestEvent, output *Execution) error
}
type eventFormatterFunc func(event TestEvent, output *Execution) error
func (e eventFormatterFunc) Format(event TestEvent, output *Execution) error {
return e(event, output)
}
type FormatOptions struct {
HideEmptyPackages bool
UseHiVisibilityIcons bool // Deprecated
Icons string
}
// NewEventFormatter returns a formatter for printing events.
func NewEventFormatter(out io.Writer, format string, formatOpts FormatOptions) EventFormatter {
switch format {
case "none":
return eventFormatterFunc(func(TestEvent, *Execution) error { return nil })
case "debug":
return debugFormat(out)
case "standard-json":
return standardJSONFormat(out)
case "standard-verbose":
return standardVerboseFormat(out)
case "standard-quiet":
return standardQuietFormat(out)
case "dots", "dots-v1":
return dotsFormatV1(out)
case "dots-v2":
return newDotFormatter(out, formatOpts)
case "gotestdox", "testdox":
return testDoxFormat(out, formatOpts)
case "testname", "short-verbose":
if os.Getenv("GITHUB_ACTIONS") == "true" {
return githubActionsFormat(out)
}
return testNameFormat(out)
case "pkgname", "short":
return pkgNameFormat(out, formatOpts)
case "pkgname-and-test-fails", "short-with-failures":
return pkgNameWithFailuresFormat(out, formatOpts)
case "github-actions", "github-action":
return githubActionsFormat(out)
default:
return nil
}
}
func githubActionsFormat(out io.Writer) EventFormatter {
buf := bufio.NewWriter(out)
type name struct {
Package string
Test string
}
output := map[name][]string{}
// Compile regex patterns once for parsing test failure information
// fileLinePattern matches patterns like " filename.go:123: error message"
fileLinePattern := regexp.MustCompile(`^\s+([a-zA-Z0-9_\-./]+\.go):(\d+):`)
// panicStackPattern matches panic stack trace lines like "\t/path/to/file.go:123 +0x..."
panicStackPattern := regexp.MustCompile(`^\t(.+\.go):(\d+) \+0x`)
return eventFormatterFunc(func(event TestEvent, exec *Execution) error {
key := name{Package: event.Package, Test: event.Test}
// test case output
if event.Test != "" && event.Action == ActionOutput {
if !isFramingLine(event.Output, event.Test) {
output[key] = append(output[key], event.Output)
}
return nil
}
// test case end event
if event.Test != "" && event.Action.IsTerminal() {
// Emit error annotation for failed tests
if event.Action == ActionFail {
writeGitHubActionsError(buf, event, output[key], fileLinePattern, panicStackPattern)
}
if len(output[key]) > 0 {
buf.WriteString("::group::")
} else {
buf.WriteString(" ")
}
testNameFormatTestEvent(buf, event)
for _, item := range output[key] {
buf.WriteString(item)
}
if len(output[key]) > 0 {
buf.WriteString("\n::endgroup::\n")
}
delete(output, key)
return buf.Flush()
}
// package event
if !event.Action.IsTerminal() {
return nil
}
result := colorEvent(event)(strings.ToUpper(string(event.Action)))
pkg := exec.Package(event.Package)
if event.Action == ActionSkip || (event.Action == ActionPass && pkg.Total == 0) {
event.Action = ActionSkip // always color these as skip actions
result = colorEvent(event)("EMPTY")
}
buf.WriteString(" ")
buf.WriteString(result)
buf.WriteString(" Package ")
buf.WriteString(packageLine(event, exec.Package(event.Package)))
buf.WriteString("\n")
return buf.Flush()
})
}
// writeGitHubActionsError parses test output and emits GitHub Actions error annotations
func writeGitHubActionsError(
buf *bufio.Writer, event TestEvent, outputLines []string, fileLinePattern, panicStackPattern *regexp.Regexp,
) {
sanitize := func(s string) string {
// Percent must be escaped first
s = strings.ReplaceAll(s, "%", "%25")
// Escape newlines and carriage returns
s = strings.ReplaceAll(s, "\r", "%0D")
s = strings.ReplaceAll(s, "\n", "%0A")
return s
}
// Check if this is a panic by looking for panic: in the output
var isPanic bool
var panicMessage strings.Builder
for _, outputLine := range outputLines {
if strings.Contains(outputLine, "panic:") {
isPanic = true
panicMessage.WriteString(strings.TrimSpace(outputLine))
panicMessage.WriteString(" ")
}
}
if isPanic {
// For panics, emit a single annotation with the panic location
var file string
var line string
// Look for the test file in the stack trace
// Prefer _test.go files over other files (like testing.go or runtime files)
for _, outputLine := range outputLines {
if matches := panicStackPattern.FindStringSubmatch(outputLine); len(matches) == 3 {
stackFile := filepath.Base(matches[1])
stackLine := matches[2]
isTestFile := strings.HasSuffix(stackFile, "_test.go")
if file == "" || isTestFile {
file = stackFile
line = stackLine
if isTestFile {
break
}
}
}
}
message := strings.TrimSpace(panicMessage.String())
if message == "" {
message = "Test panicked"
}
if file != "" && line != "" {
fmt.Fprintf(buf, "::error file=%s,line=%s,title=%s::%s\n",
sanitize(file), line, sanitize(event.Test), sanitize(message))
} else {
fmt.Fprintf(buf, "::error title=%s::%s\n", sanitize(event.Test), sanitize(message))
}
} else {
// For regular test failures, emit one annotation per error line
for _, outputLine := range outputLines {
if matches := fileLinePattern.FindStringSubmatch(outputLine); len(matches) == 3 {
file := matches[1]
line := matches[2]
parts := strings.SplitN(outputLine, ":", 3)
var message string
if len(parts) >= 3 {
message = strings.TrimSpace(parts[2])
}
if message == "" {
message = "Test failed"
}
fmt.Fprintf(buf, "::error file=%s,line=%s,title=%s::%s\n",
sanitize(file), line, sanitize(event.Test), sanitize(message))
}
}
}
}