-
Notifications
You must be signed in to change notification settings - Fork 7
/
report.go
169 lines (144 loc) · 3.63 KB
/
report.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package main
import (
"bufio"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
)
type CoverProfile struct {
Mode string
Packages map[string]*Package
Total int
Covered int
}
func (c *CoverProfile) Coverage() int {
if c == nil {
return -1
}
if c.Total < 1 {
return -1
}
return int(float64(c.Covered) / float64(c.Total) * 10000)
}
type Package struct {
Name string
Blocks []Block
Total int
Covered int
}
func (p *Package) Coverage() int {
if p == nil {
return -1
}
if p.Total < 1 {
return -1
}
return int(float64(p.Covered) / float64(p.Total) * 10000)
}
type Block struct {
Filename string
Start Position
End Position
StatementCount int
HitCount int
}
type Position struct {
Line int
Column int
}
func LoadCoverProfile(filename string) (*CoverProfile, error) {
// open cover profile file
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
// parse contents and return results
return parseCoverProfile(file)
}
func parseCoverProfile(r io.Reader) (*CoverProfile, error) {
scanner := bufio.NewScanner(r)
if !scanner.Scan() {
return nil, errors.New("missing header")
}
header := scanner.Text()
if !strings.HasPrefix(header, "mode: ") {
return nil, errors.New("profile must start with [mode: ] header")
}
profile := &CoverProfile{
Mode: strings.TrimPrefix(header, "mode: "),
Packages: map[string]*Package{},
}
line := 0
for scanner.Scan() {
line++
match := lineRegexp.FindStringSubmatch(scanner.Text())
if match == nil {
return nil, fmt.Errorf("malformed coverage line: %s", scanner.Text())
}
// note: format of each coverage line https://github.com/golang/tools/blob/e8f417a962ed6ed4ce93226507cc6e6d007c386b/cover/profile.go#L55-L58
path := match[1]
pkgName := filepath.Dir(path)
fileName := filepath.Base(path)
startLine, err := strconv.Atoi(match[2])
if err != nil {
return nil, fmt.Errorf("invalid startLine on line %d: %w", line, err)
}
startCol, err := strconv.Atoi(match[3])
if err != nil {
return nil, fmt.Errorf("invalid startCol on line %d: %w", line, err)
}
endLine, err := strconv.Atoi(match[4])
if err != nil {
return nil, fmt.Errorf("invalid endLine on line %d: %w", line, err)
}
endCol, err := strconv.Atoi(match[5])
if err != nil {
return nil, fmt.Errorf("invalid endCol on line %d: %w", line, err)
}
statementCount, err := strconv.Atoi(match[6])
if err != nil {
return nil, fmt.Errorf("invalid statementCount on line %d: %w", line, err)
}
hitCount, err := strconv.Atoi(match[7])
if err != nil {
return nil, fmt.Errorf("invalid hitCount on line %d: %w", line, err)
}
pkgData := profile.Packages[pkgName]
if pkgData == nil {
// package not yet seen - create new struct
pkgData = &Package{
Name: pkgName,
}
profile.Packages[pkgName] = pkgData
}
// increment statement and coverage (hit) counts at both a package and overall profile level
pkgData.Total += statementCount
profile.Total += statementCount
if hitCount > 0 {
pkgData.Covered += statementCount
profile.Covered += statementCount
}
pkgData.Blocks = append(pkgData.Blocks, Block{
Filename: fileName,
Start: Position{
Line: startLine,
Column: startCol,
},
End: Position{
Line: endLine,
Column: endCol,
},
StatementCount: statementCount,
HitCount: hitCount,
})
}
return profile, scanner.Err()
}
// spec: https://github.com/golang/tools/blob/0cf4e2708ac840da8674eb3947b660a931bd2c1f/cover/profile.go#L119-L123
var lineRegexp = regexp.MustCompile(`^([^:]+):([0-9]+)\.([0-9]+),([0-9]+)\.([0-9]+) ([0-9]+) ([0-9]+)$`)