-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
cli_utils.go
63 lines (54 loc) · 1.31 KB
/
cli_utils.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
package got
import (
"fmt"
"os"
"os/exec"
"regexp"
"strconv"
"strings"
)
// EnsureCoverage via report file generated from, for example:
//
// go test -coverprofile=coverage.out
//
// Return error if any file's coverage is less than min, min is a percentage value.
func EnsureCoverage(path string, min float64) error {
tmp, _ := os.CreateTemp("", "")
report := tmp.Name()
defer func() { _ = os.Remove(report) }()
_ = tmp.Close()
_, err := exec.Command("go", "tool", "cover", "-html", path, "-o", report).CombinedOutput()
if err != nil {
return err
}
list := parseReport(report)
rejected := []string{}
for _, c := range list {
if c.coverage < min {
rejected = append(rejected, fmt.Sprintf(" %s (%0.1f%%)", c.path, c.coverage))
}
}
if len(rejected) > 0 {
return fmt.Errorf(
"Test coverage for these files should be greater than %.2f%%:\n%s",
min,
strings.Join(rejected, "\n"),
)
}
return nil
}
type cov struct {
path string
coverage float64
}
var regCov = regexp.MustCompile(`<option value="file\d+">(.+) \((\d+\.\d+)%\)</option>`)
func parseReport(path string) []cov {
out, _ := os.ReadFile(path)
ms := regCov.FindAllStringSubmatch(string(out), -1)
list := []cov{}
for _, m := range ms {
c, _ := strconv.ParseFloat(m[2], 32)
list = append(list, cov{m[1], c})
}
return list
}