forked from haya14busa/goverage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
289 lines (271 loc) · 7.38 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
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
package main
import (
"bytes"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"sort"
"strings"
"golang.org/x/tools/cover"
)
const usageMessage = "" +
`Usage: goverage [flags] -coverprofile=coverage.out package...
`
var (
coverprofile string
covermode string
cpu string
parallel string
timeout string
short bool
v bool
x bool
race bool
gobinary string
)
func init() {
flag.StringVar(&coverprofile, "coverprofile", "coverage.out", "Write a coverage profile to the file after all tests have passed")
flag.StringVar(&covermode, "covermode", "", "sent as covermode argument to go test")
flag.StringVar(&cpu, "cpu", "", "sent as cpu argument to go test")
flag.StringVar(¶llel, "parallel", "", "sent as parallel argument to go test")
flag.StringVar(&timeout, "timeout", "", "sent as timeout argument to go test")
flag.BoolVar(&short, "short", false, "sent as short argument to go test")
flag.BoolVar(&v, "v", false, "sent as v argument to go test")
flag.BoolVar(&x, "x", false, "sent as x argument to go test")
flag.BoolVar(&race, "race", false, "enable data race detection")
flag.StringVar(&gobinary, "go-binary", "go", "Use an alternative test runner such as 'richgo'")
}
func usage() {
fmt.Fprintln(os.Stderr, usageMessage)
fmt.Fprintln(os.Stderr, "Flags:")
flag.PrintDefaults()
os.Exit(2)
}
type ExitError struct {
Msg string
Code int
}
func (e *ExitError) Error() string {
return e.Msg
}
func main() {
flag.Usage = usage
flag.Parse()
if err := run(coverprofile, flag.Args(), covermode, cpu, parallel, timeout, short, v); err != nil {
code := 1
if err, ok := err.(*ExitError); ok {
code = err.Code
}
if err.Error() != "" {
fmt.Fprintln(os.Stderr, err)
}
os.Exit(code)
}
}
func run(coverprofile string, args []string, covermode, cpu, parallel, timeout string, short, v bool) error {
if coverprofile == "" {
usage()
return nil
}
if race && covermode != "" && covermode != "atomic" {
return fmt.Errorf("cannot use race flag and covermode=%s. See more detail on golang/go#12118.", covermode)
}
file, err := os.Create(coverprofile)
if err != nil {
return err
}
defer file.Close()
// pkgs is packages to run tests and get coverage.
var pkgs []string
for _, pkg := range args {
ps, err := getPkgs(pkg)
if err != nil {
return err
}
pkgs = append(pkgs, ps...)
}
if len(pkgs) == 0 {
pkgs = []string{"."}
}
coverpkg := strings.Join(pkgs, ",")
optionalArgs := buildOptionalTestArgs(coverpkg, covermode, cpu, parallel, timeout, short, v)
cpss := make([][]*cover.Profile, len(pkgs))
hasFailedTest := false
for i, pkg := range pkgs {
cps, success, err := coverage(pkg, optionalArgs, v)
if !success {
hasFailedTest = true
}
if err != nil {
// Do not return err here. It could be just tests are not found for the package.
log.Printf("got error for package %q: %v", pkg, err)
continue
}
if cps != nil {
cpss[i] = cps
}
}
dumpcp(file, mergeProfiles(cpss))
if hasFailedTest {
return &ExitError{Code: 1}
}
return nil
}
// buildOptionalTestArgs returns common optional args for go test regardless
// target packages. coverpkg must not be empty.
func buildOptionalTestArgs(coverpkg, covermode, cpu, parallel, timeout string, short, v bool) []string {
args := []string{"-coverpkg", coverpkg}
if covermode != "" {
args = append(args, "-covermode", covermode)
}
if cpu != "" {
args = append(args, "-cpu", cpu)
}
if parallel != "" {
args = append(args, "-parallel", parallel)
}
if timeout != "" {
args = append(args, "-timeout", timeout)
}
if short {
args = append(args, "-short")
}
if v {
args = append(args, "-v")
}
if x {
args = append(args, "-x")
}
if race {
args = append(args, "-race")
}
return args
}
// getPkgs returns packages for mesuring coverage. Returned packages doesn't
// contain vendor packages.
func getPkgs(pkg string) ([]string, error) {
if pkg == "" {
pkg = "./..."
}
out, err := exec.Command("go", "list", pkg).CombinedOutput()
if err != nil {
return nil, err
}
allPkgs := strings.Split(strings.Trim(string(out), "\n"), "\n")
pkgs := make([]string, 0, len(allPkgs))
for _, p := range allPkgs {
if !(strings.Contains(p, "/vendor/") || strings.HasPrefix(p, "vendor/")) {
pkgs = append(pkgs, p)
}
}
return pkgs, nil
}
// coverage runs test for the given pkg and returns cover profile.
// success indicates "go test" succeeded or not. coverage may return profiles
// even when success=false. When "go test" fails, coverage outputs "go test"
// result to stdout even when verbose=false.
func coverage(pkg string, optArgs []string, verbose bool) (profiles []*cover.Profile, success bool, err error) {
coverprofile, err := tmpProfileName()
if err != nil {
return nil, false, err
}
// Remove coverprofile created by "go test".
defer os.Remove(coverprofile)
args := append([]string{"test", pkg, "-coverprofile", coverprofile}, optArgs...)
cmd := exec.Command(gobinary, args...)
stdout := new(bytes.Buffer)
stderr := new(bytes.Buffer)
if verbose {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
} else {
cmd.Stdout = stdout
cmd.Stderr = stderr
}
if err := cmd.Run(); err != nil {
fmt.Fprint(os.Stdout, stdout.String())
fmt.Fprint(os.Stderr, stderr.String())
// "go test" can creates coverprofile even when "go test" failes, so do not
// return error here if coverprofile is created.
if !isExist(coverprofile) {
return nil, false, fmt.Errorf("failed to run 'go test %v': %v", pkg, err)
}
} else {
if !isExist(coverprofile) {
// There are no test and coverprofile is not created.
return nil, true, nil
}
success = true
}
profiles, err = cover.ParseProfiles(coverprofile)
return profiles, success, err
}
func tmpProfileName() (string, error) {
f, err := ioutil.TempFile("", "goverage")
if err != nil {
return "", err
}
if err := f.Close(); err != nil {
return "", err
}
if err := os.Remove(f.Name()); err != nil {
return "", err
}
return f.Name(), nil
}
func isExist(filename string) bool {
_, err := os.Stat(filename)
return err == nil
}
// mergeProfiles merges cover profiles. It assumes target packages of each
// cover profile are same and sorted.
func mergeProfiles(cpss [][]*cover.Profile) []*cover.Profile {
// File name to profile.
profiles := map[string]*cover.Profile{}
for _, ps := range cpss {
for _, p := range ps {
if _, ok := profiles[p.FileName]; !ok {
// Insert profile.
profiles[p.FileName] = p
continue
}
// Merge blocks.
for i, block := range p.Blocks {
switch p.Mode {
case "set":
profiles[p.FileName].Blocks[i].Count |= block.Count
case "count", "atomic":
profiles[p.FileName].Blocks[i].Count += block.Count
}
}
}
}
result := make([]*cover.Profile, 0, len(profiles))
for _, p := range profiles {
result = append(result, p)
}
sort.Slice(result, func(i, j int) bool {
return result[i].FileName < result[j].FileName
})
return result
}
// dumpcp dumps cover profile result to io.Writer.
func dumpcp(w io.Writer, cps []*cover.Profile) {
if len(cps) == 0 {
return
}
fmt.Fprintf(w, "mode: %v\n", cps[0].Mode)
for _, cp := range cps {
for _, b := range cp.Blocks {
_ = b
// ref: golang.org/x/tools/cover
// name.go:line.column,line.column numberOfStatements count
const blockFmt = "%s:%d.%d,%d.%d %d %d\n"
fmt.Fprintf(w, blockFmt, cp.FileName, b.StartLine, b.StartCol, b.EndLine, b.EndCol, b.NumStmt, b.Count)
}
}
}