-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathcli.go
More file actions
105 lines (87 loc) · 2.21 KB
/
cli.go
File metadata and controls
105 lines (87 loc) · 2.21 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
package output
import (
"fmt"
"io"
"log"
"os"
run "runtime"
"time"
"github.com/SimonBaeumer/commander/pkg/runtime"
"github.com/logrusorgru/aurora"
)
var au aurora.Aurora
// OutputWriter represents the output
type OutputWriter struct {
out io.Writer
color bool
}
// NewCliOutput creates a new OutputWriter with a stdout writer
func NewCliOutput(color bool) OutputWriter {
return OutputWriter{
out: os.Stdout,
color: color,
}
}
// Start starts the writing sequence
func (w *OutputWriter) Start(results <-chan runtime.TestResult) bool {
au = aurora.NewAurora(w.color)
if run.GOOS == "windows" {
au = aurora.NewAurora(false)
}
failed := 0
testResults := []runtime.TestResult{}
start := time.Now()
for r := range results {
testResults = append(testResults, r)
if r.ValidationResult.Success {
s := w.addTries("✓ "+r.TestCase.Title, r)
w.fprintf(s)
}
if !r.ValidationResult.Success {
failed++
s := w.addTries("✗ "+r.TestCase.Title, r)
w.fprintf(au.Red(s))
w.fprintf(r.TestCase.Result.Combined)
}
}
duration := time.Since(start)
if failed > 0 {
w.printFailures(testResults)
}
w.fprintf("")
w.fprintf(fmt.Sprintf("Duration: %.3fs", duration.Seconds()))
summary := fmt.Sprintf("Count: %d, Failed: %d", len(testResults), failed)
if failed > 0 {
w.fprintf(au.Red(summary))
} else {
w.fprintf(au.Green(summary))
}
return failed == 0
}
func (w *OutputWriter) addTries(s string, r runtime.TestResult) string {
if r.Tries > 1 {
s = fmt.Sprintf("%s, retries %d", s, r.Tries)
}
return s
}
func (w *OutputWriter) printFailures(results []runtime.TestResult) {
w.fprintf("")
w.fprintf(au.Bold("Results"))
w.fprintf(au.Bold(""))
for _, r := range results {
if r.TestCase.Result.Error != nil {
w.fprintf(au.Bold(au.Red("✗ '" + r.TestCase.Title + "' could not be executed with error message:")))
w.fprintf(r.TestCase.Result.Error.Error())
continue
}
if !r.ValidationResult.Success {
w.fprintf(au.Bold(au.Red("✗ '" + r.TestCase.Title + "', on property '" + r.FailedProperty + "'")))
w.fprintf(r.ValidationResult.Diff)
}
}
}
func (w *OutputWriter) fprintf(a ...interface{}) {
if _, err := fmt.Fprintln(w.out, a...); err != nil {
log.Fatal(err)
}
}