...
1
16
17 package framework
18
19 import (
20 "bytes"
21 "fmt"
22 "sync"
23 )
24
25
26 type FlakeReport struct {
27 lock sync.RWMutex
28 Flakes []string `json:"flakes"`
29 FlakeCount int `json:"flakeCount"`
30 }
31
32
33 func NewFlakeReport() *FlakeReport {
34 return &FlakeReport{
35 Flakes: []string{},
36 }
37 }
38
39 func buildDescription(optionalDescription ...interface{}) string {
40 switch len(optionalDescription) {
41 case 0:
42 return ""
43 default:
44 return fmt.Sprintf(optionalDescription[0].(string), optionalDescription[1:]...)
45 }
46 }
47
48
49
50
51 func (f *FlakeReport) RecordFlakeIfError(err error, optionalDescription ...interface{}) {
52 if err == nil {
53 return
54 }
55 msg := fmt.Sprintf("Unexpected error occurred: %v", err)
56 desc := buildDescription(optionalDescription...)
57 if desc != "" {
58 msg = fmt.Sprintf("%v (Description: %v)", msg, desc)
59 }
60 Logf(msg)
61 f.lock.Lock()
62 defer f.lock.Unlock()
63 f.Flakes = append(f.Flakes, msg)
64 f.FlakeCount++
65 }
66
67
68 func (f *FlakeReport) GetFlakeCount() int {
69 f.lock.RLock()
70 defer f.lock.RUnlock()
71 return f.FlakeCount
72 }
73
74
75 func (f *FlakeReport) PrintHumanReadable() string {
76 f.lock.RLock()
77 defer f.lock.RUnlock()
78 buf := bytes.Buffer{}
79 buf.WriteString(fmt.Sprintf("FlakeCount: %v\n", f.FlakeCount))
80 buf.WriteString("Flakes:\n")
81 for _, flake := range f.Flakes {
82 buf.WriteString(fmt.Sprintf("%v\n", flake))
83 }
84 return buf.String()
85 }
86
87
88 func (f *FlakeReport) PrintJSON() string {
89 f.lock.RLock()
90 defer f.lock.RUnlock()
91 return PrettyPrintJSON(f)
92 }
93
94
95 func (f *FlakeReport) SummaryKind() string {
96 return "FlakeReport"
97 }
98
View as plain text