This repository has been archived by the owner on Jun 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
125 lines (101 loc) · 2.58 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
package main
import (
"encoding/json"
"fmt"
"io"
"os"
"regexp"
"github.com/logrusorgru/aurora"
)
func printIndent(level int) {
for i := 0; i <= level; i++ {
fmt.Print(" ")
}
}
func printTests(name string, tests *testNode, level int, states ...TestState) {
for _, s := range states {
if tests.State == s {
if level == 0 {
fmt.Println()
}
printIndent(level)
switch tests.State {
case Unknown:
fmt.Println(aurora.Bold(aurora.Cyan("?")), name)
case Passed:
fmt.Println(aurora.Bold(aurora.Green("✓")), name)
case Failed:
fmt.Println(aurora.Bold(aurora.Red("✗")), name)
if tests.Output != nil {
fmt.Println()
for _, line := range tests.Output {
printIndent(level + 1)
fmt.Println(aurora.Red(line))
}
fmt.Println()
}
}
break
}
}
for childName, child := range tests.ChildrenByName {
printTests(childName, child, level+1, states...)
}
}
func main() {
jsonDecoder := json.NewDecoder(os.Stdin)
var actionPattern = regexp.MustCompile(`^\S+`)
var testSuite = newTestSuite()
var allPassed = true
var eventCount = 0
for {
var event TestEvent
err := jsonDecoder.Decode(&event)
if err != nil {
if err != io.EOF {
if eventCount > 0 {
fmt.Fprintf(os.Stderr, "go-passe: %v\n", err)
} else {
fmt.Fprintf(os.Stderr, "go-passe: %v\n\nForgot to pass -json to 'go test'?\n", err)
}
os.Exit(2)
}
if eventCount == 0 {
fmt.Fprintln(os.Stderr, "go-passe: no Go test events detected")
os.Exit(3)
}
break
}
eventCount++
action := actionPattern.FindString(event.Action)
switch action {
case "pass":
if event.Test != "" {
fmt.Printf("%s %s %s %gs\n", aurora.Bold(aurora.Green("✓")), event.Package, event.Test, event.Elapsed)
} else {
fmt.Printf("%s %s %gs\n", aurora.Bold(aurora.Green("✓")), event.Package, event.Elapsed)
}
testSuite.MarkPassed(event.TestID)
case "fail":
if event.Test != "" {
fmt.Printf("%s %s %s %gs\n", aurora.Bold(aurora.Red("✗")), event.Package, event.Test, event.Elapsed)
} else {
fmt.Printf("%s %s %gs\n", aurora.Bold(aurora.Red("✗")), event.Package, event.Elapsed)
}
testSuite.MarkFailed(event.TestID)
allPassed = false
case "output":
testSuite.Get(event.TestID).AppendOutput(event.Output)
default:
}
}
if allPassed {
fmt.Println(aurora.Bold(aurora.Green("\nAll tests passed")))
} else {
fmt.Println(aurora.Bold(aurora.Red("\nTest failures:")))
for packageName, tests := range testSuite.TestsByPackage {
printTests(packageName, tests, 0, Failed)
}
os.Exit(1)
}
}