/
types.go
80 lines (61 loc) · 1.35 KB
/
types.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
package coverage
import (
"regexp"
"strings"
"github.com/vladopajic/go-test-coverage/v2/pkg/testcoverage/path"
)
type Stats struct {
Name string
Total int64
Covered int64
Threshold int
}
func (s Stats) CoveredPercentage() int {
return CoveredPercentage(s.Total, s.Covered)
}
//nolint:gomnd // relax
func CoveredPercentage(total, covered int64) int {
if total == 0 {
return 0
}
if covered == total {
return 100
}
return int(float64(covered*100) / float64(total))
}
func stripPrefix(name, prefix string) string {
if prefix == "" {
return name
}
if string(prefix[len(prefix)-1]) != "/" {
prefix += "/"
}
return strings.Replace(name, prefix, "", 1)
}
func matches(regexps []*regexp.Regexp, str string) bool {
for _, r := range regexps {
if r.MatchString(str) {
return true
}
}
return false
}
func compileExcludePathRules(excludePaths []string) []*regexp.Regexp {
if len(excludePaths) == 0 {
return nil
}
compiled := make([]*regexp.Regexp, len(excludePaths))
for i, pattern := range excludePaths {
pattern = path.NormalizePathInRegex(pattern)
compiled[i] = regexp.MustCompile(pattern)
}
return compiled
}
func CalcTotalStats(coverageStats []Stats) Stats {
totalStats := Stats{}
for _, stats := range coverageStats {
totalStats.Total += stats.Total
totalStats.Covered += stats.Covered
}
return totalStats
}