generated from bool64/go-template
/
gocovdiff.go
378 lines (296 loc) · 7.9 KB
/
gocovdiff.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
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
package app
import (
"flag"
"fmt"
"io"
"log"
"os"
"os/exec"
"path/filepath"
"sort"
"strings"
"github.com/bool64/dev/version"
"github.com/waigani/diffparser"
)
type flags struct {
diffFile string
parentCommit string
covFile string
module string
ghaAnnotations string
exclude string
funcCov string
funcMaxCov float64
funcBaseCov string
targetDeltaCov float64
deltaCovFile string
version bool
}
func parseFlags() flags {
var f flags
flag.StringVar(&f.diffFile, "diff", "", "Git diff file for changes (optional)")
flag.StringVar(&f.parentCommit, "parent", "", "Parent commit hash (optional)")
flag.StringVar(&f.covFile, "cov", "coverage.txt", "Coverage file")
flag.StringVar(&f.module, "mod", "", "Module name to strip from file names (optional)")
flag.StringVar(&f.ghaAnnotations, "gha-annotations", "", "File to store GitHub Actions annotations")
flag.StringVar(&f.exclude, "exclude", "", "Exclude directories by prefix and files by name pattern, comma separated (optional)")
flag.StringVar(&f.funcCov, "func-cov", "", "Current func coverage from 'go tool cover -func', requires -func-base-cov or -func-max-cov (optional)")
flag.StringVar(&f.funcBaseCov, "func-base-cov", "", "Base func coverage from 'go tool cover -func', requires -func-cov (optional)")
flag.Float64Var(&f.funcMaxCov, "func-max-cov", 0, "Max func coverage from 'go tool cover -func' to keep in report of undercovered functions, requires -func-cov (optional)")
flag.Float64Var(&f.targetDeltaCov, "target-delta-cov", 80, "Target coverage of changed lines, to be used together with -delta-cov-file")
flag.StringVar(&f.deltaCovFile, "delta-cov-file", "", "File to store delta coverage message")
flag.BoolVar(&f.version, "version", false, "Show version and exit")
flag.Parse()
if f.version {
fmt.Println(version.Module("github.com/vearutop/gocovdiff").Version)
os.Exit(0)
}
if f.covFile == "" {
flag.Usage()
os.Exit(0)
}
if f.funcMaxCov == 0 && (f.funcCov != "" || f.funcBaseCov != "") {
if f.funcCov == "" || f.funcBaseCov == "" {
flag.Usage()
os.Exit(1)
}
}
if f.funcMaxCov > 0 && f.funcCov == "" {
flag.Usage()
os.Exit(1)
}
return f
}
// Main runs application.
func Main() {
if err := run(parseFlags(), os.Stdout); err != nil {
log.Fatal(err)
}
}
//nolint:maintidx
func run(f flags, report io.Writer) (err error) {
if f.funcMaxCov > 0 && f.funcCov != "" {
cur, err := os.ReadFile(f.funcCov)
if err != nil {
return fmt.Errorf("failed to read current coverage file: %w", err)
}
return reportUndercoveredFuncs(report, f.funcMaxCov, cur)
}
if f.funcCov != "" && f.funcBaseCov != "" {
base, err := os.ReadFile(f.funcBaseCov)
if err != nil {
return fmt.Errorf("failed to read base coverage file: %w", err)
}
cur, err := os.ReadFile(f.funcCov)
if err != nil {
return fmt.Errorf("failed to read current coverage file: %w", err)
}
return reportCoverFuncDiff(report, f.module, base, cur)
}
if f.module == "" {
o, err := exec.Command("go", "list", "-m").CombinedOutput()
if err != nil {
return fmt.Errorf("failed to get module name: %w", err)
}
f.module = strings.TrimSpace(string(o))
}
diff, err := getDiff(f.diffFile, f.parentCommit)
if err != nil {
return err
}
var ga githubAnnotator
if f.ghaAnnotations != "" {
f, err := os.Create(f.ghaAnnotations)
if err != nil {
return fmt.Errorf("failed to create GitHub annotations file: %w", err)
}
ga.w = f
defer func() {
if err := f.Close(); err != nil {
log.Fatal("failed to close GitHub annotations file: ", err)
}
}()
}
modified := map[string]map[int]*profileBlock{}
exclude := []string(nil)
if f.exclude != "" {
exclude = strings.Split(f.exclude, ",")
}
fileLoop:
for _, f := range diff.Files {
if !strings.HasSuffix(f.NewName, ".go") || strings.HasSuffix(f.NewName, "_test.go") {
continue
}
for _, e := range exclude {
if strings.HasPrefix(f.NewName, e) {
continue fileLoop
}
if ok, err := filepath.Match(e, filepath.Base(f.NewName)); ok && err == nil {
continue fileLoop
}
}
lines := map[int]*profileBlock{}
for _, h := range f.Hunks {
for _, l := range h.NewRange.Lines {
if l.Mode == diffparser.UNCHANGED {
continue
}
lines[l.Number] = &profileBlock{Count: -1}
}
}
modified[f.NewName] = lines
}
testedFiles := map[string]bool{}
totStmt := 0
covStmt := 0
fileCoverage := map[string]stat{}
err = parseProfiles(f.covFile, func(fn string, block profileBlock) {
fn = strings.TrimPrefix(fn, f.module+"/")
testedFiles[fn] = true
fStat := fileCoverage[fn]
lines, ok := modified[fn]
if !ok {
return
}
totCounted := false
for i := block.StartLine; i <= block.EndLine; i++ {
l, ok := lines[i]
if !ok {
continue
}
if !totCounted {
totStmt += block.NumStmt
fStat.totStmt += block.NumStmt
if block.Count > 0 {
covStmt += block.NumStmt
fStat.covStmt += block.NumStmt
}
totCounted = true
}
if l.Count == -1 {
lines[i] = &block
continue
}
// Do not merge blocks that has coverage.
if block.Count > 0 {
continue
}
l.NumStmt += block.NumStmt
if l.StartLine == block.StartLine && l.StartCol > block.StartCol {
l.StartCol = block.StartCol
}
if l.StartLine > block.StartLine {
l.StartLine = block.StartLine
l.StartCol = block.StartCol
}
if l.EndLine == block.EndLine && l.EndCol < block.EndCol {
l.EndCol = block.EndCol
}
if l.EndLine < block.EndLine {
l.EndLine = block.EndLine
l.EndCol = block.EndCol
}
}
fileCoverage[fn] = fStat
})
if err != nil {
return fmt.Errorf("failed to parse profiles: %w", err)
}
files := make([]string, 0, len(modified))
for fn := range modified {
files = append(files, fn)
}
sort.Strings(files)
var (
functions []stat
untestedFiles []string
)
for _, fn := range files {
if !testedFiles[fn] {
ga.printNotTested(fn)
untestedFiles = append(untestedFiles, fn)
}
lines := modified[fn]
ll := make([]int, 0, len(lines))
for i, l := range lines {
if l.Count == 0 {
ll = append(ll, i)
}
}
if len(ll) > 0 {
sort.Ints(ll)
p := ll[0]
start := 0
for _, i := range ll {
if start == 0 {
start = p
}
if i-p > 1 {
ga.printNotice(fn, start, p, lines)
start = 0
}
p = i
}
if start == 0 {
start = p
}
ga.printNotice(fn, start, p, lines)
}
funcs, err := findFuncs(fn)
if err != nil {
return fmt.Errorf("failed to find functions: %w", err)
}
for _, fu := range funcs {
totStmt := 0
covStmt := 0
for i := fu.startLine; i <= fu.endLine; i++ {
if l, ok := lines[i]; ok {
totStmt += l.NumStmt
if l.Count > 0 {
covStmt += l.NumStmt
}
}
}
if totStmt > 0 {
functions = append(functions, stat{
name: fu.name,
file: fn,
line: fu.startLine,
covPercent: float64(covStmt) / float64(totStmt) * 100,
})
}
}
}
printReport(report, covStmt, totStmt, functions, fileCoverage, untestedFiles)
if f.deltaCovFile == "" {
return nil
}
df, err := os.Create(f.deltaCovFile)
if err != nil {
return fmt.Errorf("failed to create delta coverage file: %w", err)
}
defer func() {
if err := df.Close(); err != nil {
log.Fatal("failed to close delta coverage file: ", err)
}
}()
res := ""
if totStmt > 0 {
deltaCov := float64(covStmt) / float64(totStmt) * 100
res = fmt.Sprintf("changed lines: (statements) %.1f%%", deltaCov)
if deltaCov < f.targetDeltaCov {
res += fmt.Sprintf(", coverage is less than %.1f%%, consider testing the changes more thoroughly", f.targetDeltaCov)
}
}
if _, err = df.WriteString(res); err != nil {
return fmt.Errorf("failed to write to delta coverage file: %w", err)
}
return nil
}
type stat struct {
name string
file string
line int
covPercent float64
covStmt, totStmt int
}