-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstats.go
More file actions
131 lines (109 loc) · 2.74 KB
/
Copy pathstats.go
File metadata and controls
131 lines (109 loc) · 2.74 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
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
package sweet
import (
"fmt"
"os"
"sort"
"sync"
"sync/atomic"
"github.com/mgutz/ansi"
"golang.org/x/crypto/ssh/terminal"
)
type statsPlugin struct {
suitesLock sync.Mutex
suites map[string]*suiteStats
}
type suiteStats struct {
Name string
Passed int64
Failed int64
Skipped int64
}
func newStatsPlugin() *statsPlugin {
return &statsPlugin{
suites: make(map[string]*suiteStats),
}
}
func (p *statsPlugin) Name() string {
return "Test Stats"
}
func (p *statsPlugin) Options() *PluginOptions {
return nil
}
func (p *statsPlugin) SetOption(name, value string) {
}
func (p *statsPlugin) Starting() {
}
func (p *statsPlugin) SuiteStarting(suite string) {
// Get the suite so stats are aware of it and it shows up
// in the final results
p.getSuite(suite)
}
func (p *statsPlugin) TestStarting(testName *TestName) {
}
func (p *statsPlugin) TestPassed(testName *TestName, stats *TestPassedStats) {
s := p.getSuite(testName.SuiteName)
atomic.AddInt64(&s.Passed, 1)
}
func (p *statsPlugin) TestSkipped(testName *TestName, stats *TestSkippedStats) {
s := p.getSuite(testName.SuiteName)
atomic.AddInt64(&s.Skipped, 1)
}
func (p *statsPlugin) TestFailed(testName *TestName, stats *TestFailedStats) {
s := p.getSuite(testName.SuiteName)
atomic.AddInt64(&s.Failed, 1)
}
func (p *statsPlugin) SuiteFinished(suite string, stats *SuiteFinishedStats) {
}
func (p *statsPlugin) Finished() {
sortedNames := make([]string, 0)
for key := range p.suites {
sortedNames = append(sortedNames, key)
}
sort.Strings(sortedNames)
out := os.Stdout
isTerm := terminal.IsTerminal(int(out.Fd()))
passColor := ansi.ColorFunc("green")
failColor := ansi.ColorFunc("red")
skipColor := ansi.ColorFunc("yellow")
if len(sortedNames) > 0 {
fmt.Fprintln(out, "")
fmt.Fprintf(out, "Suite Results:\n")
fmt.Fprintf(out, "--------------\n")
for _, name := range sortedNames {
suite := p.suites[name]
totalStr := fmt.Sprintf("%d", suite.Passed+suite.Failed+suite.Skipped)
passedStr := fmt.Sprintf("%d", suite.Passed)
if isTerm && suite.Passed > 0 {
passedStr = passColor(passedStr)
}
failedStr := fmt.Sprintf("%d", suite.Failed)
if isTerm && suite.Failed > 0 {
failedStr = failColor(failedStr)
}
skippedStr := fmt.Sprintf("%d", suite.Skipped)
if isTerm && suite.Skipped > 0 {
skippedStr = skipColor(skippedStr)
}
fmt.Fprintf(out, "%s - Total: %s, Passed: %s, Failed: %s, Skipped: %s\n",
name,
totalStr,
passedStr,
failedStr,
skippedStr,
)
}
fmt.Fprintln(out, "")
}
}
func (p *statsPlugin) getSuite(name string) *suiteStats {
p.suitesLock.Lock()
defer p.suitesLock.Unlock()
suite, ok := p.suites[name]
if !ok {
suite = &suiteStats{
Name: name,
}
p.suites[name] = suite
}
return suite
}