-
Notifications
You must be signed in to change notification settings - Fork 1
/
coverfail.go
115 lines (101 loc) · 2.8 KB
/
coverfail.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
package main
import (
"bytes"
"flag"
"fmt"
"strconv"
"strings"
"os"
"os/exec"
"regexp"
)
const usageMessage = "" +
`Usage: coverfail -threshold X.X`
var (
threshold float64
coverprofile string
modMode string
)
func init() {
flag.Float64Var(&threshold, "threshold", 0, "Sets the threshold the actual coverage number will be compared against")
flag.StringVar(&coverprofile, "coverprofile", "coverage.out", "Write a coverage profile to the file after all tests have passed")
flag.StringVar(&modMode, "mod", "", "Mode for handling modules; select from \"readonly\" or \"vendor\"")
}
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, threshold, modMode); 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, threshold float64, modMode string) error {
optionalArgs := buildOptionalTestArgs(coverprofile, modMode)
err := coverage(optionalArgs, threshold)
if err != nil {
return err
}
return nil
}
func buildOptionalTestArgs(coverprofile, modMode string) []string {
args := []string{}
if coverprofile != "" {
args = append(args, "-coverprofile", coverprofile)
}
if modMode != "" {
args = append(args, "-mod", modMode)
}
return args
}
func coverage(optArgs []string, threshold float64) error {
args := append([]string{"test", "-cover", "-coverpkg=./...", "./..."}, optArgs...)
cmd := exec.Command("go", args...)
stdout := new(bytes.Buffer)
stderr := new(bytes.Buffer)
cmd.Stdout = stdout
cmd.Stderr = stderr
if err := cmd.Run(); err != nil {
fmt.Fprint(os.Stdout, stdout.String())
fmt.Fprint(os.Stderr, stderr.String())
return &ExitError{Code: 1, Msg: "'go test' exited with an error, no coverage results available"}
}
totalpct := parsePackagePercentages(stdout)
fmt.Println("Threshold is: ", threshold)
fmt.Printf("Overall coverage: %.1f%% of statements\n\n", totalpct)
if totalpct < threshold {
return &ExitError{Code: 1, Msg: "Overall coverage is lower than provided threshold number, bailing with nonzero exit code..."}
}
return nil
}
func parsePackagePercentages(output *bytes.Buffer) float64 {
var total float64
pctMatch := regexp.MustCompile(`([\d*\.?\d*]+)(%)`)
outputStr := output.String()
percents := pctMatch.FindAllString(outputStr, -1)
for _, pct:= range percents {
coveragePct, err := strconv.ParseFloat(strings.Trim(pct, "%"), 64); if err != nil {
panic(fmt.Sprintf("Could not parse code coverage output, error was: %s", err))
}
total += coveragePct
}
return total
}