-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd_cover.go
More file actions
501 lines (452 loc) · 12.8 KB
/
cmd_cover.go
File metadata and controls
501 lines (452 loc) · 12.8 KB
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
package main
import (
"flag"
"fmt"
"os"
"sort"
"strings"
"text/tabwriter"
"github.com/twmb/gr/cover"
)
func coverCmd(args []string) {
flags := flag.NewFlagSet("cover", flag.ExitOnError)
var (
dir = flags.String("dir", "", "project directory for resolving source files")
uncovered = flags.Bool("uncovered", false, "show uncovered line ranges instead of function summary")
pkg = flags.Bool("pkg", false, "aggregate coverage by package instead of per-function")
top = flags.Int("top", 0, "show only top N results")
funcPat = flags.String("func", "", "filter to functions/files matching pattern")
minStmts = flags.Int("min-statements", 0, "hide functions/blocks with fewer than N statements")
sortBy = flags.String("sort", "", `sort order: "stmts" sorts by uncovered statement count descending`)
no100 = flags.Bool("no-100", false, "hide functions with 100% coverage")
diff = flags.Bool("diff", false, "compare two coverage profiles: gr c -diff old.out new.out")
)
flags.Usage = func() {
fmt.Fprint(os.Stderr, `gr cover - coverage profile analyzer
Usage: gr cover [flags] [coverprofile]
gr cover -diff [flags] old.out new.out
Analyzes Go coverage profiles produced by "go test -coverprofile=FILE".
Default output shows per-function coverage sorted by percentage (ascending).
Flags:
-dir <path> project directory for resolving source files
-func <pattern> filter to functions/files matching pattern
-top <N> show only top N results
-uncovered show uncovered line ranges instead of function summary
-pkg aggregate coverage by package
-min-statements <N> hide functions/blocks with fewer than N statements
-sort stmts sort by uncovered statement count (descending)
-no-100 hide fully covered functions/packages
-diff compare two profiles (old.out new.out)
Examples:
gr c coverage.out functions sorted by coverage (ascending)
gr c -pkg coverage.out package-level summary
gr c -uncovered coverage.out show uncovered line ranges
gr c -top 10 coverage.out 10 least covered functions
gr c -func mypackage coverage.out filter to matching functions/files
gr c -sort stmts -min-statements 5 biggest coverage gaps first
gr c -diff old.out new.out compare coverage before/after
gr c -diff -func pkg old.out new.out compare, filtered to pkg
`)
}
flags.Parse(args)
fo := filterOpts{
pattern: *funcPat,
topN: *top,
minStmts: *minStmts,
sortByStmts: *sortBy == "stmts",
no100: *no100,
}
if *diff {
if flags.NArg() != 2 {
die("-diff requires exactly two positional args: old.out new.out")
}
oldResult := loadProfile(flags.Arg(0), *dir)
newResult := loadProfile(flags.Arg(1), *dir)
writeDiff(oldResult, newResult, fo)
return
}
var input *os.File
switch {
case flags.NArg() == 1:
f, err := os.Open(flags.Arg(0))
if err != nil {
die("unable to open %s: %v", flags.Arg(0), err)
}
defer f.Close()
input = f
case flags.NArg() == 0:
input = os.Stdin
default:
flags.Usage()
os.Exit(1)
}
profile, err := cover.ParseProfile(input)
if err != nil {
die("parse error: %v", err)
}
result, err := cover.Analyze(profile, *dir)
if err != nil {
die("analysis error: %v", err)
}
if *uncovered {
writeUncovered(result, fo)
} else if *pkg {
writePkgCoverage(result, fo)
} else {
writeFuncCoverage(result, fo)
}
}
func loadProfile(path, dir string) *cover.Result {
f, err := os.Open(path)
if err != nil {
die("unable to open %s: %v", path, err)
}
defer f.Close()
profile, err := cover.ParseProfile(f)
if err != nil {
die("parse error in %s: %v", path, err)
}
result, err := cover.Analyze(profile, dir)
if err != nil {
die("analysis error in %s: %v", path, err)
}
return result
}
type filterOpts struct {
pattern string
topN int
minStmts int
sortByStmts bool
no100 bool
}
func writeFuncCoverage(r *cover.Result, o filterOpts) {
funcs := make([]cover.FuncCoverage, 0, len(r.Funcs))
for _, f := range r.Funcs {
if o.pattern != "" && !strings.Contains(f.Func, o.pattern) && !strings.Contains(f.File, o.pattern) {
continue
}
if o.minStmts > 0 && f.Statements < o.minStmts {
continue
}
if o.no100 && f.Percent() >= 100 {
continue
}
funcs = append(funcs, f)
}
if o.sortByStmts {
sort.Slice(funcs, func(i, j int) bool {
ui := funcs[i].Statements - funcs[i].Covered
uj := funcs[j].Statements - funcs[j].Covered
if ui != uj {
return ui > uj
}
return funcs[i].File < funcs[j].File
})
} else {
sort.Slice(funcs, func(i, j int) bool {
pi, pj := funcs[i].Percent(), funcs[j].Percent()
if pi != pj {
return pi < pj
}
if funcs[i].File != funcs[j].File {
return funcs[i].File < funcs[j].File
}
return funcs[i].StartLine < funcs[j].StartLine
})
}
if o.topN > 0 && o.topN < len(funcs) {
funcs = funcs[:o.topN]
}
if len(funcs) == 0 {
fmt.Println("no functions match filters")
return
}
w := tabwriter.NewWriter(os.Stdout, 0, 4, 2, ' ', 0)
for _, f := range funcs {
fmt.Fprintf(w, "%s\t%s\t%.1f%%\t(%d/%d)\n",
cover.ShortFile(f.File, r.ModPath), f.Func, f.Percent(), f.Covered, f.Statements)
}
w.Flush()
if r.TotalStmt > 0 {
pct := float64(r.CoveredStmt) / float64(r.TotalStmt) * 100
fmt.Printf("\ntotal: %.1f%% (%d/%d statements)\n", pct, r.CoveredStmt, r.TotalStmt)
}
}
type pkgCoverage struct {
pkg string
statements int
covered int
}
func (p *pkgCoverage) percent() float64 {
if p.statements == 0 {
return 100.0
}
return float64(p.covered) / float64(p.statements) * 100.0
}
func writePkgCoverage(r *cover.Result, o filterOpts) {
pkgMap := make(map[string]*pkgCoverage)
for _, f := range r.Funcs {
if o.pattern != "" && !strings.Contains(f.Func, o.pattern) && !strings.Contains(f.File, o.pattern) {
continue
}
pkg := pkgOf(f.File, r.ModPath)
pc := pkgMap[pkg]
if pc == nil {
pc = &pkgCoverage{pkg: pkg}
pkgMap[pkg] = pc
}
pc.statements += f.Statements
pc.covered += f.Covered
}
pkgs := make([]*pkgCoverage, 0, len(pkgMap))
for _, pc := range pkgMap {
if o.minStmts > 0 && pc.statements < o.minStmts {
continue
}
if o.no100 && pc.percent() >= 100 {
continue
}
pkgs = append(pkgs, pc)
}
if o.sortByStmts {
sort.Slice(pkgs, func(i, j int) bool {
ui := pkgs[i].statements - pkgs[i].covered
uj := pkgs[j].statements - pkgs[j].covered
if ui != uj {
return ui > uj
}
return pkgs[i].pkg < pkgs[j].pkg
})
} else {
sort.Slice(pkgs, func(i, j int) bool {
pi, pj := pkgs[i].percent(), pkgs[j].percent()
if pi != pj {
return pi < pj
}
return pkgs[i].pkg < pkgs[j].pkg
})
}
if o.topN > 0 && o.topN < len(pkgs) {
pkgs = pkgs[:o.topN]
}
if len(pkgs) == 0 {
fmt.Println("no packages match filters")
return
}
w := tabwriter.NewWriter(os.Stdout, 0, 4, 2, ' ', 0)
for _, pc := range pkgs {
fmt.Fprintf(w, "%s\t%.1f%%\t(%d/%d)\n", pc.pkg, pc.percent(), pc.covered, pc.statements)
}
w.Flush()
if r.TotalStmt > 0 {
pct := float64(r.CoveredStmt) / float64(r.TotalStmt) * 100
fmt.Printf("\ntotal: %.1f%% (%d/%d statements)\n", pct, r.CoveredStmt, r.TotalStmt)
}
}
func pkgOf(file, modPath string) string {
short := cover.ShortFile(file, modPath)
if i := strings.LastIndexByte(short, '/'); i >= 0 {
return short[:i]
}
return "."
}
type funcDiff struct {
file string
funcName string
oldPct, newPct float64
oldCovered, oldStmts int
newCovered, newStmts int
oldUncov, newUncov int
deltaPct float64
}
func writeDiff(oldR, newR *cover.Result, o filterOpts) {
type funcKey struct{ file, fn string }
oldMap := make(map[funcKey]cover.FuncCoverage)
for _, f := range oldR.Funcs {
oldMap[funcKey{f.File, f.Func}] = f
}
seen := make(map[funcKey]bool)
var diffs []funcDiff
for _, nf := range newR.Funcs {
k := funcKey{nf.File, nf.Func}
seen[k] = true
of, existed := oldMap[k]
oldPct := float64(0)
if existed {
oldPct = of.Percent()
}
d := funcDiff{
file: nf.File,
funcName: nf.Func,
oldPct: oldPct,
newPct: nf.Percent(),
oldCovered: of.Covered, oldStmts: of.Statements,
newCovered: nf.Covered, newStmts: nf.Statements,
}
d.oldUncov = d.oldStmts - d.oldCovered
d.newUncov = d.newStmts - d.newCovered
d.deltaPct = d.newPct - d.oldPct
diffs = append(diffs, d)
}
// Functions removed in new profile.
for _, of := range oldR.Funcs {
k := funcKey{of.File, of.Func}
if seen[k] {
continue
}
d := funcDiff{
file: of.File,
funcName: of.Func,
oldPct: of.Percent(),
newPct: -1, // sentinel for "removed"
oldCovered: of.Covered, oldStmts: of.Statements,
}
d.oldUncov = d.oldStmts - d.oldCovered
d.deltaPct = -d.oldPct
diffs = append(diffs, d)
}
// Filter.
modPath := newR.ModPath
if modPath == "" {
modPath = oldR.ModPath
}
filtered := diffs[:0]
for _, d := range diffs {
if o.pattern != "" && !strings.Contains(d.funcName, o.pattern) && !strings.Contains(d.file, o.pattern) {
continue
}
stmts := d.newStmts
if stmts == 0 {
stmts = d.oldStmts
}
if o.minStmts > 0 && stmts < o.minStmts {
continue
}
if o.no100 && d.newPct >= 100 {
continue
}
if d.oldStmts == 0 && d.newStmts == 0 {
continue
}
// Skip unchanged.
if d.oldCovered == d.newCovered && d.oldStmts == d.newStmts {
continue
}
filtered = append(filtered, d)
}
diffs = filtered
if o.sortByStmts {
sort.Slice(diffs, func(i, j int) bool {
di := diffs[i].oldUncov - diffs[i].newUncov
dj := diffs[j].oldUncov - diffs[j].newUncov
if di != dj {
return di > dj // biggest uncov reduction first
}
return diffs[i].file < diffs[j].file
})
} else {
sort.Slice(diffs, func(i, j int) bool {
if diffs[i].deltaPct != diffs[j].deltaPct {
return diffs[i].deltaPct > diffs[j].deltaPct
}
if diffs[i].file != diffs[j].file {
return diffs[i].file < diffs[j].file
}
return diffs[i].funcName < diffs[j].funcName
})
}
if o.topN > 0 && o.topN < len(diffs) {
diffs = diffs[:o.topN]
}
if len(diffs) == 0 {
fmt.Println("no coverage changes match filters")
return
}
w := tabwriter.NewWriter(os.Stdout, 0, 4, 2, ' ', 0)
for _, d := range diffs {
file := cover.ShortFile(d.file, modPath)
sign := "+"
if d.deltaPct < 0 {
sign = ""
}
if d.newPct < 0 {
// removed function
fmt.Fprintf(w, "%s\t%s\t%.1f%% → removed\t(%d/%d → 0/0)\n",
file, d.funcName, d.oldPct, d.oldCovered, d.oldStmts)
} else if d.oldStmts == 0 {
// new function
fmt.Fprintf(w, "%s\t%s\tnew → %.1f%%\t(0/0 → %d/%d)\n",
file, d.funcName, d.newPct, d.newCovered, d.newStmts)
} else {
fmt.Fprintf(w, "%s\t%s\t%.1f%% → %.1f%%\t%s%.1f%%\t(%d/%d → %d/%d)\n",
file, d.funcName, d.oldPct, d.newPct, sign, d.deltaPct,
d.oldCovered, d.oldStmts, d.newCovered, d.newStmts)
}
}
w.Flush()
// Total summary.
if newR.TotalStmt > 0 || oldR.TotalStmt > 0 {
oldPct := float64(0)
if oldR.TotalStmt > 0 {
oldPct = float64(oldR.CoveredStmt) / float64(oldR.TotalStmt) * 100
}
newPct := float64(0)
if newR.TotalStmt > 0 {
newPct = float64(newR.CoveredStmt) / float64(newR.TotalStmt) * 100
}
delta := newPct - oldPct
sign := "+"
if delta < 0 {
sign = ""
}
fmt.Printf("\ntotal: %.1f%% → %.1f%% (%s%.1f%%, %d/%d → %d/%d statements)\n",
oldPct, newPct, sign, delta, oldR.CoveredStmt, oldR.TotalStmt, newR.CoveredStmt, newR.TotalStmt)
}
}
func writeUncovered(r *cover.Result, o filterOpts) {
blocks := make([]cover.UncoveredBlock, 0, len(r.Uncovered))
for _, b := range r.Uncovered {
if o.pattern != "" && !strings.Contains(b.File, o.pattern) {
continue
}
if o.minStmts > 0 && b.NumStmt < o.minStmts {
continue
}
blocks = append(blocks, b)
}
if o.sortByStmts {
sort.Slice(blocks, func(i, j int) bool {
if blocks[i].NumStmt != blocks[j].NumStmt {
return blocks[i].NumStmt > blocks[j].NumStmt
}
if blocks[i].File != blocks[j].File {
return blocks[i].File < blocks[j].File
}
return blocks[i].StartLine < blocks[j].StartLine
})
} else {
sort.Slice(blocks, func(i, j int) bool {
if blocks[i].File != blocks[j].File {
return blocks[i].File < blocks[j].File
}
return blocks[i].StartLine < blocks[j].StartLine
})
}
if o.topN > 0 && o.topN < len(blocks) {
blocks = blocks[:o.topN]
}
if len(blocks) == 0 {
fmt.Println("no uncovered blocks match filters")
return
}
w := tabwriter.NewWriter(os.Stdout, 0, 4, 2, ' ', 0)
for _, b := range blocks {
file := cover.ShortFile(b.File, r.ModPath)
if b.StartLine == b.EndLine {
fmt.Fprintf(w, "%s:%d\t%d statements\n", file, b.StartLine, b.NumStmt)
} else {
fmt.Fprintf(w, "%s:%d-%d\t%d statements\n", file, b.StartLine, b.EndLine, b.NumStmt)
}
}
w.Flush()
}