-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
types.go
75 lines (59 loc) · 1.52 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
package testcoverage
import (
"strings"
"golang.org/x/exp/maps"
"github.com/vladopajic/go-test-coverage/v2/pkg/testcoverage/coverage"
)
type AnalyzeResult struct {
Threshold Threshold
FilesBelowThreshold []coverage.Stats
PackagesBelowThreshold []coverage.Stats
MeetsTotalCoverage bool
TotalCoverage int
}
func (r *AnalyzeResult) Pass() bool {
return r.MeetsTotalCoverage &&
len(r.FilesBelowThreshold) == 0 &&
len(r.PackagesBelowThreshold) == 0
}
func packageForFile(filename string) string {
i := strings.LastIndex(filename, "/")
if i == -1 {
return filename
}
return filename[:i]
}
func checkCoverageStatsBelowThreshold(
coverageStats []coverage.Stats,
threshold int,
overrideRules []regRule,
) []coverage.Stats {
var belowThreshold []coverage.Stats
for _, s := range coverageStats {
thr := threshold
if override, ok := matches(overrideRules, s.Name); ok {
thr = override
}
if s.CoveredPercentage() < thr {
s.Threshold = thr
belowThreshold = append(belowThreshold, s)
}
}
return belowThreshold
}
func makePackageStats(coverageStats []coverage.Stats) []coverage.Stats {
packageStats := make(map[string]coverage.Stats)
for _, stats := range coverageStats {
pkg := packageForFile(stats.Name)
var pkgStats coverage.Stats
if s, ok := packageStats[pkg]; ok {
pkgStats = s
} else {
pkgStats = coverage.Stats{Name: pkg}
}
pkgStats.Total += stats.Total
pkgStats.Covered += stats.Covered
packageStats[pkg] = pkgStats
}
return maps.Values(packageStats)
}