/
main.go
417 lines (352 loc) · 10.1 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
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
// MIT License
// Copyright (c) 2018 Blend Labs, inc.
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// From https://raw.githubusercontent.com/blend/go-sdk/master/_bin/coverage/main.go
package main
import (
"bufio"
"bytes"
"flag"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"regexp"
"strconv"
"strings"
"golang.org/x/tools/cover"
)
var (
version = "dev"
commit = "none"
date = "unknown"
)
const (
star = "*"
defaultFileFlags = 0644
)
var reportOutputPath = flag.String("output", "coverage.html", "the path to write the full html coverage report")
var temporaryOutputPath = flag.String("tmp", "coverage.cov", "the path to write the intermediate results")
var update = flag.Bool("update", false, "if we should write the current coverage to `COVERAGE` files")
var enforce = flag.Bool("enforce", false, "if we should enforce coverage minimums defined in `COVERAGE` files")
var include = flag.String("include", "", "the include file filter in glob form, can be a csv.")
var exclude = flag.String("exclude", "", "the exclude file filter in glob form, can be a csv.")
func main() {
flag.Parse()
pwd, err := os.Getwd()
maybeFatal(err)
fmt.Fprintln(os.Stdout, "coverage starting")
fullCoverageData, err := removeAndOpen(*temporaryOutputPath)
if err != nil {
maybeFatal(err)
}
fmt.Fprintln(fullCoverageData, "mode: set")
maybeFatal(filepath.Walk("./", func(currentPath string, info os.FileInfo, err error) error {
if os.IsNotExist(err) {
return nil
}
if err != nil {
return err
}
if !info.IsDir() {
return nil
}
if info.Name() == ".git" {
return filepath.SkipDir
}
if strings.HasPrefix(info.Name(), "_") {
return filepath.SkipDir
}
if info.Name() == "vendor" {
return filepath.SkipDir
}
if !dirHasGlob(currentPath, "*.go") {
return nil
}
if len(*include) > 0 {
if matches := globAnyMatch(*include, currentPath); !matches {
return nil
}
}
if len(*exclude) > 0 {
if matches := globAnyMatch(*exclude, currentPath); matches {
return nil
}
}
packageCoverReport := filepath.Join(currentPath, "profile.cov")
err = removeIfExists(packageCoverReport)
if err != nil {
return err
}
var output []byte
output, err = execCoverage(currentPath)
if err != nil {
fmt.Fprintln(os.Stderr, string(output))
return err
}
coverage := extractCoverage(string(output))
fmt.Fprintf(os.Stdout, "%s: %v%%\n", currentPath, coverage)
if enforce != nil && *enforce {
err = enforceCoverage(currentPath, coverage)
if err != nil {
return err
}
}
if update != nil && *update {
fmt.Fprintf(os.Stdout, "%s updating coverage\n", currentPath)
err = writeCoverage(currentPath, coverage)
if err != nil {
return err
}
}
err = mergeCoverageOutput(packageCoverReport, fullCoverageData)
if err != nil {
return err
}
err = removeIfExists(packageCoverReport)
if err != nil {
return err
}
return nil
}))
maybeFatal(fullCoverageData.Close())
covered, total, err := parseFullCoverProfile(pwd, *temporaryOutputPath)
maybeFatal(err)
finalCoverage := (float64(covered) / float64(total)) * 100
maybeFatal(writeCoverage(pwd, fmt.Sprintf("%.2f", finalCoverage)))
fmt.Fprintf(os.Stdout, "final coverage: %.2f\n", finalCoverage)
fmt.Fprintf(os.Stdout, "merging coverage output: %s\n", *reportOutputPath)
maybeFatal(execCoverageReportCompile())
maybeFatal(removeIfExists(*temporaryOutputPath))
fmt.Fprintln(os.Stdout, "coverage complete")
}
// --------------------------------------------------------------------------------
// utilities
// --------------------------------------------------------------------------------
// globIncludeMatch tests if a file matches a (potentially) csv of glob filters.
func globAnyMatch(filter, file string) bool {
parts := strings.Split(filter, ",")
for _, part := range parts {
if matches := glob(strings.TrimSpace(part), file); matches {
return true
}
}
return false
}
func glob(pattern, subj string) bool {
// Empty pattern can only match empty subject
if pattern == "" {
return subj == pattern
}
// If the pattern _is_ a glob, it matches everything
if pattern == star {
return true
}
parts := strings.Split(pattern, star)
if len(parts) == 1 {
// No globs in pattern, so test for equality
return subj == pattern
}
leadingGlob := strings.HasPrefix(pattern, star)
trailingGlob := strings.HasSuffix(pattern, star)
end := len(parts) - 1
// Go over the leading parts and ensure they match.
for i := 0; i < end; i++ {
idx := strings.Index(subj, parts[i])
switch i {
case 0:
// Check the first section. Requires special handling.
if !leadingGlob && idx != 0 {
return false
}
default:
// Check that the middle parts match.
if idx < 0 {
return false
}
}
// Trim evaluated text from subj as we loop over the pattern.
subj = subj[idx+len(parts[i]):]
}
// Reached the last section. Requires special handling.
return trailingGlob || strings.HasSuffix(subj, parts[end])
}
func enforceCoverage(path, actualCoverage string) error {
actual, err := strconv.ParseFloat(actualCoverage, 64)
if err != nil {
return err
}
contents, err := ioutil.ReadFile(filepath.Join(path, "COVERAGE"))
if err != nil {
return err
}
expected, err := strconv.ParseFloat(strings.TrimSpace(string(contents)), 64)
if err != nil {
return err
}
if expected == 0 {
return nil
}
if actual < expected {
return fmt.Errorf(
"%s fails coverage: %0.2f%% vs. %0.2f%%",
path, expected, actual,
)
}
return nil
}
func extractCoverage(corpus string) string {
regex := `coverage: ([0-9,.]+)% of statements`
expr := regexp.MustCompile(regex)
results := expr.FindStringSubmatch(corpus)
if len(results) > 1 {
return results[1]
}
return "0"
}
func writeCoverage(path, coverage string) error {
return ioutil.WriteFile(filepath.Join(path, "COVERAGE"), []byte(strings.TrimSpace(coverage)), defaultFileFlags)
}
func dirHasGlob(path, glob string) bool {
files, _ := filepath.Glob(filepath.Join(path, glob))
return len(files) > 0
}
func gobin() string {
gobin, err := exec.LookPath("go")
maybeFatal(err)
return gobin
}
func execCoverage(path string) ([]byte, error) {
cmd := exec.Command(gobin(), "test", "-timeout", "10s", "-short", "-covermode=set", "-coverprofile=profile.cov")
cmd.Dir = path
return cmd.CombinedOutput()
}
func execCoverageReportCompile() error {
cmd := exec.Command(gobin(), "tool", "cover", fmt.Sprintf("-html=%s", *temporaryOutputPath), fmt.Sprintf("-o=%s", *reportOutputPath))
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, "GOCACHE=off")
return cmd.Run()
}
func mergeCoverageOutput(temp string, outFile *os.File) error {
contents, err := ioutil.ReadFile(temp)
if err != nil {
return err
}
scanner := bufio.NewScanner(bytes.NewBuffer(contents))
var skip int
for scanner.Scan() {
skip++
if skip < 2 {
continue
}
_, err = fmt.Fprintln(outFile, scanner.Text())
if err != nil {
return err
}
}
return nil
}
func removeIfExists(path string) error {
if _, err := os.Stat(path); err == nil {
return os.Remove(path)
}
return nil
}
func maybeFatal(err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "%+v\n", err)
os.Exit(1)
}
}
func removeAndOpen(path string) (*os.File, error) {
if _, err := os.Stat(path); err == nil {
if err = os.Remove(path); err != nil {
return nil, err
}
}
return os.Create(path)
}
func countFileLines(path string) (lines int, err error) {
if filepath.Ext(path) != ".go" {
err = fmt.Errorf("count lines path must be a .go file")
return
}
var contents []byte
contents, err = ioutil.ReadFile(path)
if err != nil {
return
}
lines = bytes.Count(contents, []byte{'\n'})
return
}
// joinCoverPath takes a pwd, and a filename, and joins them
// overlaying parts of the suffix of the pwd, and the prefix
// of the filename that match.
// ex:
// - pwd: /foo/bar/baz, filename: bar/baz/buzz.go => /foo/bar/baz/buzz.go
func joinCoverPath(pwd, fileName string) string {
pwdPath := lessEmpty(strings.Split(pwd, "/"))
fileDirPath := lessEmpty(strings.Split(filepath.Dir(fileName), "/"))
for index, dir := range pwdPath {
if dir == first(fileDirPath) {
pwdPath = pwdPath[:index]
break
}
}
return filepath.Join(maybePrefix(strings.Join(pwdPath, "/"), "/"), fileName)
}
// parseFullCoverProfile parses the final / merged cover output.
func parseFullCoverProfile(pwd string, path string) (covered, total int, err error) {
files, err := cover.ParseProfiles(path)
if err != nil {
return
}
var fileTotal int
for _, file := range files {
fileTotal, err = countFileLines(joinCoverPath(pwd, file.FileName))
if err != nil {
return
}
for _, block := range file.Blocks {
covered += (block.EndLine - block.StartLine) + 1
}
total += fileTotal
}
return
}
func lessEmpty(values []string) (output []string) {
for _, value := range values {
if len(value) > 0 {
output = append(output, value)
}
}
return
}
func first(values []string) (output string) {
if len(values) == 0 {
return
}
output = values[0]
return
}
func maybePrefix(root, prefix string) string {
if strings.HasPrefix(root, prefix) {
return root
}
return prefix + root
}