forked from pierrre/gotestcover
/
gotestcover.go
296 lines (264 loc) · 6.12 KB
/
gotestcover.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// Package gotestcover provides multiple packages support for Go test cover.
package main
import (
"bufio"
"bytes"
"flag"
"fmt"
"io/ioutil"
"os"
"os/exec"
"runtime"
"sync"
"golang.org/x/tools/cover"
)
var (
// go build
flagA bool
flagX bool
flagRace bool
flagTags string
// go test
flagV bool
flagCount int
flagCPU string
flagParallel string
flagRun string
flagShort bool
flagTimeout string
flagCoverMode string
flagCoverProfile string
// custom
flagParallelPackages = runtime.GOMAXPROCS(0)
// reports
flagReportJSON bool
)
func main() {
err := run()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func run() error {
err := parseFlags()
if err != nil {
return err
}
pkgs, err := getPackages()
if err != nil {
return err
}
cov, failed := runAllPackageTests(pkgs, func(out string) {
fmt.Print(out)
})
// write the merged profile
file, err := os.Create(flagCoverProfile)
if err != nil {
return err
}
writeCoverProfile(cov, file)
file.Close()
cov, ignored := ignore(cov)
if len(ignored) > 0 {
writeIgnoredReport(ignored, os.Stdout)
}
writeHotspotsReport(cov, os.Stdout)
writeSummaryReport(cov, os.Stdout)
if failed {
return fmt.Errorf("test failed")
}
return nil
}
func parseFlags() error {
flag.BoolVar(&flagA, "a", flagA, "see 'go build' help")
flag.BoolVar(&flagX, "x", flagX, "see 'go build' help")
flag.BoolVar(&flagRace, "race", flagRace, "see 'go build' help")
flag.StringVar(&flagTags, "tags", flagTags, "see 'go build' help")
flag.BoolVar(&flagV, "v", flagV, "see 'go test' help")
flag.IntVar(&flagCount, "count", flagCount, "see 'go test' help")
flag.StringVar(&flagCPU, "cpu", flagCPU, "see 'go test' help")
flag.StringVar(&flagParallel, "parallel", flagParallel, "see 'go test' help")
flag.StringVar(&flagRun, "run", flagRun, "see 'go test' help")
flag.BoolVar(&flagShort, "short", flagShort, "see 'go test' help")
flag.StringVar(&flagTimeout, "timeout", flagTimeout, "see 'go test' help")
flag.StringVar(&flagCoverMode, "covermode", flagCoverMode, "see 'go test' help")
flag.StringVar(&flagCoverProfile, "coverprofile", flagCoverProfile, "see 'go test' help")
flag.IntVar(&flagParallelPackages, "parallelpackages", flagParallelPackages, "Number of package test run in parallel")
flag.Parse()
if flagCoverProfile == "" {
flagCoverProfile = "out.coverprofile"
}
if flagParallelPackages < 1 {
return fmt.Errorf("flag parallelpackages must be greater than or equal to 1")
}
return nil
}
func argsBefore(this string) []string {
args := flag.Args()
for i, arg := range args {
if arg == this {
return args[:i]
}
}
return args
}
func argsAfter(this string) []string {
args := flag.Args()
for i, arg := range args {
if arg == this {
return args[i+1:]
}
}
return []string{}
}
func getPackages() ([]string, error) {
cmdArgs := []string{"list"}
cmdArgs = append(cmdArgs, argsBefore("--")...)
cmdOut, err := runGoCommand(cmdArgs...)
if err != nil {
return nil, err
}
var pkgs []string
sc := bufio.NewScanner(bytes.NewReader(cmdOut))
for sc.Scan() {
pkgs = append(pkgs, sc.Text())
}
return pkgs, nil
}
func runAllPackageTests(pkgs []string, pf func(string)) ([]*cover.Profile, bool) {
pkgch := make(chan string)
type res struct {
out string
cov []*cover.Profile
err error
}
resch := make(chan res)
wg := new(sync.WaitGroup)
wg.Add(flagParallelPackages)
go func() {
for _, pkg := range pkgs {
pkgch <- pkg
}
close(pkgch)
wg.Wait()
close(resch)
}()
for i := 0; i < flagParallelPackages; i++ {
go func() {
for p := range pkgch {
out, cov, err := runPackageTests(p)
if len(cov) == 0 {
// we assume the package contains no test.
// for the total coverage to be meaningful we still have to take
// the number of statements in this package into account.
var profileErr error
cov, profileErr = emptyProfile(p)
if err == nil && profileErr != nil {
err = profileErr
}
}
resch <- res{
out: out,
cov: cov,
err: err,
}
}
wg.Done()
}()
}
failed := false
cov := []*cover.Profile{}
for r := range resch {
for _, p := range r.cov {
cov = addProfile(cov, p)
}
if r.err == nil {
pf(r.out)
} else {
pf(r.err.Error())
failed = true
}
}
return cov, failed
}
func runPackageTests(pkg string) (out string, cov []*cover.Profile, err error) {
coverFile, err := ioutil.TempFile("", "gotestcover-")
if err != nil {
return "", nil, err
}
defer coverFile.Close()
defer os.Remove(coverFile.Name())
var args []string
args = append(args, "test")
if flagA {
args = append(args, "-a")
}
if flagX {
args = append(args, "-x")
}
if flagRace {
args = append(args, "-race")
}
if flagTags != "" {
args = append(args, "-tags", flagTags)
}
if flagV {
args = append(args, "-v")
}
if flagCount != 0 {
args = append(args, "-count", fmt.Sprint(flagCount))
}
if flagCPU != "" {
args = append(args, "-cpu", flagCPU)
}
if flagParallel != "" {
args = append(args, "-parallel", flagParallel)
}
if flagRun != "" {
args = append(args, "-run", flagRun)
}
if flagShort {
args = append(args, "-short")
}
if flagTimeout != "" {
args = append(args, "-timeout", flagTimeout)
}
args = append(args, "-cover")
if flagCoverMode != "" {
args = append(args, "-covermode", flagCoverMode)
}
args = append(args, "-coverprofile", coverFile.Name())
args = append(args, pkg)
args = append(args, argsAfter("--")...)
cmdOut, err := runGoCommand(args...)
if err != nil {
return "", nil, err
}
cov, err = cover.ParseProfiles(coverFile.Name())
if err != nil {
return "", nil, err
}
return string(cmdOut), cov, nil
}
func runGoCommand(args ...string) ([]byte, error) {
cmd := exec.Command("go", args...)
out, err := cmd.CombinedOutput()
if err != nil {
return nil, fmt.Errorf("command %s: %s\n%s", cmd.Args, err, out)
}
return out, nil
}
func removeFirstLine(b []byte) []byte {
out := new(bytes.Buffer)
sc := bufio.NewScanner(bytes.NewReader(b))
firstLine := true
for sc.Scan() {
if firstLine {
firstLine = false
continue
}
fmt.Fprintf(out, "%s\n", sc.Bytes())
}
return out.Bytes()
}