-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
48 lines (38 loc) · 880 Bytes
/
parser.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
package prettycov
import (
"fmt"
"golang.org/x/tools/cover"
)
func ParseProfile(path string) ([]FileCoverage, error) {
profiles, err := cover.ParseProfiles(path)
if err != nil {
return nil, fmt.Errorf("cannot parse coverage profile: %w", err)
}
return parse(profiles)
}
func parse(profiles []*cover.Profile) ([]FileCoverage, error) {
items := make([]FileCoverage, 0, len(profiles))
for _, p := range profiles {
var (
covered int
uncovered int
)
for _, block := range p.Blocks {
if block.Count > 0 {
covered += block.NumStmt
} else {
uncovered += block.NumStmt
}
}
coverage := float64(covered) / float64(covered+uncovered) * 100
items = append(items, FileCoverage{
File: p.FileName,
Coverage: CoverageStats{
Covered: covered,
Uncovered: uncovered,
Ratio: coverage,
},
})
}
return items, nil
}