-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
108 lines (86 loc) · 1.98 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
package main
import (
"bufio"
"fmt"
"io"
"log"
"os"
"os/exec"
"strings"
"github.com/fatih/color"
)
const cmdName = "go-test-color"
func main() {
code := runGoTest()
os.Exit(code)
}
// Run go test with args
func runGoTest() int {
// Pass all args
args := []string{"test"}
args = append(args, os.Args[1:]...)
cmd := exec.Command("go", args...)
// Read stdout and stderr
outReader, err := cmd.StdoutPipe()
if err != nil {
log.Printf("%s failed to get stdout pipe: %s", cmdName, err)
return 1
}
errReader, err := cmd.StderrPipe()
if err != nil {
log.Printf("%s failed to get stderr pipe: %s", cmdName, err)
return 1
}
// See https://stackoverflow.com/questions/8875038/redirect-stdout-pipe-of-child-process-in-go
if err := cmd.Start(); err != nil {
log.Printf("%s failed to start: %s", cmdName, err)
return 1
}
// Add color to both stdout and stderr
colorOutputReader(outReader)
colorErrorReader(errReader)
if err := cmd.Wait(); err != nil {
log.Printf("%s failed to wait: %s", cmdName, err)
return 1
}
return 0
}
func colorOutputReader(reader io.Reader) {
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
line := scanner.Text()
line = strings.TrimSpace(line)
if strings.HasSuffix(line, "[no test files]") {
continue
}
if strings.HasPrefix(line, "--- PASS") ||
strings.HasPrefix(line, "PASS") ||
strings.HasPrefix(line, "ok") {
color.Green("%s\n", line)
continue
}
if strings.HasPrefix(line, "--- SKIP") {
color.Yellow("%s\n", line)
continue
}
if strings.HasPrefix(line, "--- FAIL") ||
strings.HasPrefix(line, "FAIL") {
color.Red("%s\n", line)
continue
}
fmt.Println(line)
}
if err := scanner.Err(); err != nil {
log.Printf("scanner error: %s", err)
}
}
func colorErrorReader(reader io.Reader) {
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
line := scanner.Text()
color.Red("%s\n", line)
}
if err := scanner.Err(); err != nil {
log.Printf("scanner error: %s", err)
}
}