-
Notifications
You must be signed in to change notification settings - Fork 1
/
color.go
104 lines (93 loc) · 2.43 KB
/
color.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
package main
import (
"fmt"
"regexp"
"strings"
"github.com/fatih/color"
)
type ColoredSprintf func(format string, a ...interface{}) string
func CreateColor(attributes ...color.Attribute) ColoredSprintf {
var c = color.New()
for _, attribute := range attributes {
c.Add(attribute)
}
return c.SprintfFunc()
}
// A color palette for highlighting harness output
type Palette struct {
// Passed test
Pass ColoredSprintf
// Failed test
Fail ColoredSprintf
// New tes
New ColoredSprintf
// Skipped or disabled test
Skip ColoredSprintf
// Path
Path ColoredSprintf
// diff +
DiffIn ColoredSprintf
// diff -
DiffOut ColoredSprintf
// A warning or important information
Warn ColoredSprintf
// Critical error
Crit ColoredSprintf
// Normal output - this solely for documenting purposes,
// don't use, use fmt.*print* instead
Info ColoredSprintf
}
var palette = Palette{
Pass: CreateColor(color.FgGreen),
Fail: CreateColor(color.FgRed),
New: CreateColor(color.FgBlue),
Skip: CreateColor(color.Faint),
Path: CreateColor(color.Bold),
DiffIn: CreateColor(color.FgGreen),
DiffOut: CreateColor(color.FgRed),
Warn: CreateColor(color.FgYellow),
Crit: CreateColor(color.FgRed),
}
func PrintSuiteBeginBlurb() {
fmt.Printf("%s\n", strings.Repeat("=", 80))
fmt.Printf("LANE ")
fmt.Printf("%-52s", "TEST")
fmt.Printf(palette.Warn("%-11s", "MODE"))
fmt.Printf(palette.Pass("RESULT"))
fmt.Printf("\n")
fmt.Printf("%s\n", strings.Repeat("-", 75))
}
func PrintSuiteEndBlurb() {
fmt.Printf("%s\n", strings.Repeat("-", 75))
}
func PrintTestBlurb(lane string, name string, mode string, result string) {
switch result {
case "pass":
result = palette.Pass("[ %s ]", result)
case "fail":
result = palette.Fail("[ %s ]", result)
case "new":
result = palette.New("[ %s ]", result)
default:
result = palette.Skip(result)
}
mode = palette.Warn("%.12s", mode)
fmt.Printf("[%3s] %-50s %-18s %-8s\n", lane, name, mode, result)
}
var inRE = regexp.MustCompile(`^\+.*$`)
var outRE = regexp.MustCompile(`^\-.*$`)
func TrimAndColorizeDiff(diff string) string {
var lines = strings.Split(diff, "\n")
if len(lines) > 60 {
lines = lines[:60]
}
// Skip the first two lines of the diff
for i := 2; i < len(lines); i++ {
if inRE.MatchString(lines[i]) {
lines[i] = palette.DiffIn("%s", lines[i])
} else if outRE.MatchString(lines[i]) {
lines[i] = palette.DiffOut("%s", lines[i])
}
}
return strings.Join(lines, "\n")
}