/
output.go
379 lines (317 loc) · 9.34 KB
/
output.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package commands
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"os"
"github.com/logrusorgru/aurora"
table "github.com/olekukonko/tablewriter"
)
const (
outputSTD = "stdout"
outputJSON = "json"
outputTAP = "tap"
outputTable = "table"
)
// ValidOutputs returns the available output formats for reporting tests
func ValidOutputs() []string {
return []string{
outputSTD,
outputJSON,
outputTAP,
outputTable,
}
}
// GetOutputManager returns the OutputManager based on the user input
func GetOutputManager(outputFormat string, color bool) OutputManager {
switch outputFormat {
case outputSTD:
return NewDefaultStandardOutputManager(color)
case outputJSON:
return NewDefaultJSONOutputManager()
case outputTAP:
return NewDefaultTAPOutputManager()
case outputTable:
return NewDefaultTableOutputManager()
default:
return NewDefaultStandardOutputManager(color)
}
}
// OutputManager controls how results of an evaluation will be recorded and reported to the end user
type OutputManager interface {
Put(cr CheckResult) error
Flush() error
}
// StandardOutputManager writes to stdout
type StandardOutputManager struct {
logger *log.Logger
color aurora.Aurora
results []CheckResult
}
// NewDefaultStandardOutputManager creates a new StandardOutputManager using the default logger
func NewDefaultStandardOutputManager(color bool) *StandardOutputManager {
return NewStandardOutputManager(log.New(os.Stdout, "", 0), color)
}
// NewStandardOutputManager creates a new StandardOutputManager given a logger instance
func NewStandardOutputManager(l *log.Logger, color bool) *StandardOutputManager {
return &StandardOutputManager{
logger: l,
color: aurora.NewAurora(color),
}
}
// Put puts the result of the check to the manager in the managers buffer
func (s *StandardOutputManager) Put(cr CheckResult) error {
s.results = append(s.results, cr)
return nil
}
// Flush writes the contents of the managers buffer to the console
func (s *StandardOutputManager) Flush() error {
var totalFailures int
var totalWarnings int
var totalSuccesses int
for _, cr := range s.results {
var indicator string
if cr.FileName == "-" {
indicator = " - "
} else {
indicator = fmt.Sprintf(" - %s - ", cr.FileName)
}
currentPolicies := len(cr.Successes) + len(cr.Warnings) + len(cr.Failures)
if currentPolicies == 0 {
s.logger.Print(s.color.Colorize("?", aurora.WhiteFg), indicator, "no policies found")
continue
}
printResults := func(r Result, prefix string, color aurora.Color) {
s.logger.Print(s.color.Colorize(prefix, color), indicator, r.Message)
for _, t := range r.Traces {
s.logger.Print(s.color.Colorize("TRAC", aurora.BlueFg), indicator, t)
}
}
for _, r := range cr.Successes {
if len(r.Traces) == 0 {
continue
}
printResults(r, "PASS", aurora.GreenFg)
}
for _, r := range cr.Warnings {
printResults(r, "WARN", aurora.YellowFg)
}
for _, r := range cr.Failures {
printResults(r, "FAIL", aurora.RedFg)
}
totalFailures += len(cr.Failures)
totalWarnings += len(cr.Warnings)
totalSuccesses += len(cr.Successes)
}
totalPolicies := totalFailures + totalWarnings + totalSuccesses
var outputColor aurora.Color
if totalFailures > 0 {
outputColor = aurora.RedFg
} else if totalWarnings > 0 {
outputColor = aurora.YellowFg
} else {
outputColor = aurora.GreenFg
}
var pluralSuffixTests string
if totalPolicies != 1 {
pluralSuffixTests = "s"
}
var pluralSuffixWarnings string
if totalWarnings != 1 {
pluralSuffixWarnings = "s"
}
var pluralSuffixFailures string
if totalFailures != 1 {
pluralSuffixFailures = "s"
}
s.logger.Println()
outputText := fmt.Sprintf("%v test%s, %v passed, %v warning%s, %v failure%s", totalPolicies, pluralSuffixTests, totalSuccesses, totalWarnings, pluralSuffixWarnings, totalFailures, pluralSuffixFailures)
s.logger.Println(s.color.Colorize(outputText, outputColor))
return nil
}
type jsonResult struct {
Message string `json:"msg"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
Traces []string `json:"traces,omitempty"`
}
type jsonCheckResult struct {
Filename string `json:"filename"`
Warnings []jsonResult `json:"warnings"`
Failures []jsonResult `json:"failures"`
Successes []jsonResult `json:"successes"`
}
// JSONOutputManager formats its output to JSON
type JSONOutputManager struct {
logger *log.Logger
data []jsonCheckResult
}
// NewDefaultJSONOutputManager creates a new JSONOutputManager using the default logger
func NewDefaultJSONOutputManager() *JSONOutputManager {
return NewJSONOutputManager(log.New(os.Stdout, "", 0))
}
// NewJSONOutputManager creates a new JSONOutputManager with a given logger instance
func NewJSONOutputManager(l *log.Logger) *JSONOutputManager {
return &JSONOutputManager{
logger: l,
}
}
func errsToStrings(errs []error) []string {
res := []string{}
for _, err := range errs {
res = append(res, err.Error())
}
return res
}
// Put puts the result of the check to the manager in the managers buffer
func (j *JSONOutputManager) Put(cr CheckResult) error {
if cr.FileName == "-" {
cr.FileName = ""
}
result := jsonCheckResult{
Filename: cr.FileName,
Warnings: []jsonResult{},
Failures: []jsonResult{},
Successes: []jsonResult{},
}
for _, warning := range cr.Warnings {
result.Warnings = append(result.Warnings, jsonResult{
Message: warning.Message,
Metadata: warning.Metadata,
Traces: errsToStrings(warning.Traces),
})
}
for _, failure := range cr.Failures {
result.Failures = append(result.Failures, jsonResult{
Message: failure.Message,
Metadata: failure.Metadata,
Traces: errsToStrings(failure.Traces),
})
}
for _, successes := range cr.Successes {
result.Successes = append(result.Successes, jsonResult{
Message: successes.Message,
Metadata: successes.Metadata,
Traces: errsToStrings(successes.Traces),
})
}
j.data = append(j.data, result)
return nil
}
// Flush writes the contents of the managers buffer to the console
func (j *JSONOutputManager) Flush() error {
b, err := json.Marshal(j.data)
if err != nil {
return err
}
var out bytes.Buffer
err = json.Indent(&out, b, "", "\t")
if err != nil {
return err
}
j.logger.Print(out.String())
return nil
}
// TAPOutputManager formats its output in TAP format
type TAPOutputManager struct {
logger *log.Logger
}
// NewDefaultTAPOutputManager creates a new TAPOutputManager using the default logger
func NewDefaultTAPOutputManager() *TAPOutputManager {
return NewTAPOutputManager(log.New(os.Stdout, "", 0))
}
// NewTAPOutputManager creates a new TAPOutputManager with a given logger instance
func NewTAPOutputManager(l *log.Logger) *TAPOutputManager {
return &TAPOutputManager{
logger: l,
}
}
// Put puts the result of the check to the manager in the managers buffer
func (t *TAPOutputManager) Put(cr CheckResult) error {
var indicator string
if cr.FileName == "-" {
indicator = " - "
} else {
indicator = fmt.Sprintf(" - %s - ", cr.FileName)
}
printResults := func(r Result, prefix string, counter int) {
t.logger.Print(prefix, counter, indicator, r.Message)
if len(r.Traces) > 0 {
t.logger.Print("# Traces")
for j, trace := range r.Traces {
t.logger.Print("trace ", counter, j+1, indicator, trace.Error())
}
}
}
issues := len(cr.Failures) + len(cr.Warnings) + len(cr.Successes)
if issues > 0 {
t.logger.Print(fmt.Sprintf("1..%d", issues))
for i, r := range cr.Failures {
printResults(r, "not ok ", i+1)
}
if len(cr.Warnings) > 0 {
t.logger.Print("# Warnings")
for i, r := range cr.Warnings {
counter := i + 1 + len(cr.Failures)
printResults(r, "not ok ", counter)
}
}
if len(cr.Successes) > 0 {
t.logger.Print("# Successes")
for i, r := range cr.Successes {
counter := i + 1 + len(cr.Failures) + len(cr.Warnings)
printResults(r, "ok ", counter)
}
}
}
return nil
}
// Flush is currently a NOOP
func (t *TAPOutputManager) Flush() error {
return nil
}
// TableOutputManager formats its output in a table
type TableOutputManager struct {
table *table.Table
}
// NewDefaultTableOutputManager creates a new TableOutputManager using standard out
func NewDefaultTableOutputManager() *TableOutputManager {
return NewTableOutputManager(os.Stdout)
}
// NewTableOutputManager creates a new TableOutputManager with a given Writer
func NewTableOutputManager(w io.Writer) *TableOutputManager {
table := table.NewWriter(w)
table.SetHeader([]string{"result", "file", "message"})
return &TableOutputManager{
table: table,
}
}
// Put puts the result of the check to the manager in the managers buffer
func (t *TableOutputManager) Put(cr CheckResult) error {
printResults := func(r Result, prefix string, filename string) {
d := []string{prefix, filename, r.Error()}
t.table.Append(d)
for _, trace := range r.Traces {
dt := []string{"trace", filename, trace.Error()}
t.table.Append(dt)
}
}
for _, r := range cr.Successes {
printResults(r, "success", cr.FileName)
}
for _, r := range cr.Warnings {
printResults(r, "warning", cr.FileName)
}
for _, r := range cr.Failures {
printResults(r, "failure", cr.FileName)
}
return nil
}
// Flush writes the contents of the managers buffer to the console
func (t *TableOutputManager) Flush() error {
if t.table.NumLines() > 0 {
t.table.Render()
}
return nil
}