-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathmain.go
202 lines (175 loc) · 3.37 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
192
193
194
195
196
197
198
199
200
201
202
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// gotest is a tiny program that shells out to `go test`
// and prints the output in color.
package main
import (
"bufio"
"fmt"
"io"
"log"
"os"
"os/exec"
"os/signal"
"strings"
"sync"
"syscall"
"github.com/fatih/color"
)
var (
pass = color.FgGreen
skip = color.FgYellow
fail = color.FgHiRed
skipnotest bool
)
const (
paletteEnv = "GOTEST_PALETTE"
skipNoTestsEnv = "GOTEST_SKIPNOTESTS"
)
func main() {
enablePalette()
enableSkipNoTests()
enableOnCI()
os.Exit(gotest(os.Args[1:]))
}
func gotest(args []string) int {
var wg sync.WaitGroup
wg.Add(1)
defer wg.Wait()
r, w := io.Pipe()
defer w.Close()
args = append([]string{"test"}, args...)
cmd := exec.Command("go", args...)
cmd.Stderr = w
cmd.Stdout = w
cmd.Env = os.Environ()
if err := cmd.Start(); err != nil {
log.Print(err)
wg.Done()
return 1
}
go consume(&wg, r)
sigc := make(chan os.Signal)
done := make(chan struct{})
defer func() {
done <- struct{}{}
}()
signal.Notify(sigc)
go func() {
for {
select {
case sig := <-sigc:
cmd.Process.Signal(sig)
case <-done:
return
}
}
}()
if err := cmd.Wait(); err != nil {
if ws, ok := cmd.ProcessState.Sys().(syscall.WaitStatus); ok {
return ws.ExitStatus()
}
return 1
}
return 0
}
func consume(wg *sync.WaitGroup, r io.Reader) {
defer wg.Done()
reader := bufio.NewReader(r)
for {
l, _, err := reader.ReadLine()
if err == io.EOF {
return
}
if err != nil {
log.Print(err)
return
}
parse(string(l))
}
}
func parse(line string) {
trimmed := strings.TrimSpace(line)
defer color.Unset()
var c color.Attribute
switch {
case strings.Contains(trimmed, "[no test files]"):
if skipnotest {
return
}
case strings.HasPrefix(trimmed, "--- PASS"): // passed
fallthrough
case strings.HasPrefix(trimmed, "ok"):
fallthrough
case strings.HasPrefix(trimmed, "PASS"):
c = pass
// skipped
case strings.HasPrefix(trimmed, "--- SKIP"):
c = skip
// failed
case strings.HasPrefix(trimmed, "--- FAIL"):
fallthrough
case strings.HasPrefix(trimmed, "FAIL"):
c = fail
}
color.Set(c)
fmt.Printf("%s\n", line)
}
func enableOnCI() {
ci := strings.ToLower(os.Getenv("CI"))
switch ci {
case "true":
fallthrough
case "travis":
fallthrough
case "appveyor":
fallthrough
case "gitlab_ci":
fallthrough
case "circleci":
color.NoColor = false
}
}
func enablePalette() {
v := os.Getenv(paletteEnv)
if v == "" {
return
}
vals := strings.Split(v, ",")
if len(vals) != 2 {
return
}
if c, ok := colors[vals[0]]; ok {
fail = c
}
if c, ok := colors[vals[1]]; ok {
pass = c
}
}
func enableSkipNoTests() {
v := os.Getenv(skipNoTestsEnv)
if v == "" {
return
}
v = strings.ToLower(v)
skipnotest = v == "true"
}
var colors = map[string]color.Attribute{
"black": color.FgBlack,
"hiblack": color.FgHiBlack,
"red": color.FgRed,
"hired": color.FgHiRed,
"green": color.FgGreen,
"higreen": color.FgHiGreen,
"yellow": color.FgYellow,
"hiyellow": color.FgHiYellow,
"blue": color.FgBlue,
"hiblue": color.FgHiBlue,
"magenta": color.FgMagenta,
"himagenta": color.FgHiMagenta,
"cyan": color.FgCyan,
"hicyan": color.FgHiCyan,
"white": color.FgWhite,
"hiwhite": color.FgHiWhite,
}