forked from rakyll/gotest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
158 lines (137 loc) · 2.78 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
// 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"
"strings"
"sync"
"syscall"
"github.com/fatih/color"
)
var (
success = color.New(color.FgGreen)
fail = color.New(color.FgHiRed)
)
const paletteEnv = "GOTEST_PALETTE"
func main() {
setPalette()
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()
go consume(&wg, r)
if err := cmd.Run(); 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))
}
}
var c *color.Color
func parse(line string) {
trimmed := strings.TrimSpace(line)
switch {
case strings.HasPrefix(trimmed, "=== RUN"):
fallthrough
case strings.HasPrefix(trimmed, "?"):
c = nil
// success
case strings.HasPrefix(trimmed, "--- PASS"):
fallthrough
case strings.HasPrefix(trimmed, "ok"):
fallthrough
case strings.HasPrefix(trimmed, "PASS"):
c = success
// failure
case strings.HasPrefix(trimmed, "--- FAIL"):
fallthrough
case strings.HasPrefix(trimmed, "FAIL"):
c = fail
}
if c == nil {
fmt.Printf("%s\n", line)
return
}
c.Printf("%s\n", line)
}
func enableOnCI() {
ci := strings.ToLower(os.Getenv("CI"))
switch ci {
case "travis":
fallthrough
case "appveyor":
fallthrough
case "gitlab_ci":
fallthrough
case "circleci":
color.NoColor = false
}
}
func setPalette() {
v := os.Getenv(paletteEnv)
if v == "" {
return
}
vals := strings.Split(v, ",")
if len(vals) != 2 {
return
}
if c, ok := colors[vals[0]]; ok {
fail = color.New(c)
}
if c, ok := colors[vals[1]]; ok {
success = color.New(c)
}
}
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,
}