-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
191 lines (143 loc) · 4.29 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"sort"
"strings"
"github.com/fatih/color"
)
// Copy of https://pkg.go.dev/cmd/test2json#hdr-Output_Format format.
type testOutputLine struct {
Test string `json:"Test"`
Package string `json:"Package"`
Action string `json:"Action"`
}
type testTree struct {
name string
action string
subTests []*testTree
parentName string
}
const (
failAction = "fail"
)
func main() {
if err := run(os.Stdin, os.Stdout); err != nil {
fmt.Fprintf(os.Stderr, "Error occurred: %v\n", err)
os.Exit(1)
}
}
func outputToLines(input io.Reader) ([]*testOutputLine, error) {
data, err := ioutil.ReadAll(input)
if err != nil {
return nil, fmt.Errorf("reading input: %w", err)
}
dataPerLine := strings.Split(string(data), "\n")
lines := []*testOutputLine{}
for _, lineRaw := range dataPerLine {
line := &testOutputLine{}
if lineRaw == "" {
continue
}
if err := json.Unmarshal([]byte(lineRaw), line); err != nil {
return nil, fmt.Errorf("decoding line %q: %w", lineRaw, err)
}
if line.Test == "" {
continue
}
lines = append(lines, line)
}
return lines, nil
}
func getFinalLines(lines []*testOutputLine) []*testOutputLine {
finalLines := []*testOutputLine{}
for _, line := range lines {
if line.Action == "pass" || line.Action == failAction {
finalLines = append(finalLines, line)
}
}
return finalLines
}
func formatName(name string) string {
nameWithoutPrefixAndUnderscores := strings.ReplaceAll(strings.TrimPrefix(name, "Test_"), "_", " ")
if strings.Contains(nameWithoutPrefixAndUnderscores, "/") {
return strings.TrimSpace(strings.ReplaceAll(nameWithoutPrefixAndUnderscores, "/", " "))
}
return strings.TrimSpace(nameWithoutPrefixAndUnderscores)
}
func formatTestTree(trees []*testTree, parentName string) []string {
result := []string{}
for _, test := range trees {
output := ""
normalizedName := formatName(test.name)
dimmedWhite := color.New(color.FgHiBlack)
childName := strings.TrimPrefix(normalizedName, parentName)
switch {
case parentName == "" && test.action == failAction:
output += color.New(color.FgRed).Sprintf(normalizedName)
case parentName == "" && test.action != failAction:
output += normalizedName
case parentName != "" && test.action == failAction:
output += dimmedWhite.Sprintf(parentName)
output += color.New(color.FgHiRed).Sprintf(childName)
case parentName != "" && test.action != failAction:
output += dimmedWhite.Sprintf(parentName)
output += childName
}
result = append(result, output)
result = append(result, formatTestTree(test.subTests, normalizedName)...)
}
return result
}
func linesToTestTrees(lines []*testOutputLine, parentKeys []string) []*testTree {
result := []*testTree{}
for _, line := range lines {
splitted := strings.Split(line.Test, "/")
// Consider only child items, not grand-children etc.
if len(splitted) != len(parentKeys)+1 || !strings.HasPrefix(line.Test, strings.Join(parentKeys, "/")) {
continue
}
result = append(result, &testTree{
name: line.Test,
action: line.Action,
parentName: strings.Join(parentKeys, "/"),
subTests: linesToTestTrees(lines, splitted),
})
}
// Put failed test cases on top.
sort.Slice(result, func(i, j int) bool {
return result[i].action == failAction
})
return result
}
func groupLinesPerPackage(lines []*testOutputLine) (map[string][]*testOutputLine, []string) {
packages := []string{}
linesByPackage := map[string][]*testOutputLine{}
for _, lineRaw := range getFinalLines(lines) {
if _, ok := linesByPackage[lineRaw.Package]; !ok {
packages = append(packages, lineRaw.Package)
}
linesByPackage[lineRaw.Package] = append(linesByPackage[lineRaw.Package], lineRaw)
}
sort.Strings(packages)
return linesByPackage, packages
}
func run(input io.Reader, output io.Writer) error {
lines, err := outputToLines(input)
if err != nil {
return fmt.Errorf("converting input to lines format: %w", err)
}
linesByPackage, packages := groupLinesPerPackage(getFinalLines(lines))
for _, p := range packages {
fmt.Fprintf(output, "%s:\n", p)
lines := formatTestTree(linesToTestTrees(linesByPackage[p], []string{}), "")
for _, line := range lines {
fmt.Fprintf(output, " %s\n", line)
}
fmt.Fprintln(output)
}
return nil
}