-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofiles.go
213 lines (168 loc) · 3.82 KB
/
profiles.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package main
import (
"bufio"
"fmt"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"sync"
"golang.org/x/sync/errgroup"
"golang.org/x/tools/cover"
"golang.org/x/tools/go/packages"
)
type profiles []*profile
func (s profiles) Len() int { return len(s) }
func (s profiles) Less(i, j int) bool { return s[i].filename < s[j].filename }
func (s profiles) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s profiles) getBase() string {
base := ""
for _, p := range s {
if base == "" {
base = p.filename
} else {
base = lcp(base, p.filename)
}
}
sep := fmt.Sprintf("%c", os.PathSeparator)
if !strings.HasSuffix(base, sep) {
base = filepath.Dir(base) + sep
}
return base
}
type profile struct {
filename string
exec, total int
missing []string
}
type profilesMaker struct {
covProfs []*cover.Profile
sourceFiles map[string]string
mtx sync.Mutex
s profiles
}
func makeProfiles(file string) (profiles, error) {
covProfs, err := cover.ParseProfiles(file)
if err != nil {
return nil, fmt.Errorf("invalid coverage profile: %v", err)
}
pm := profilesMaker{
covProfs: covProfs,
}
err = pm.loadPackageFiles()
if err != nil {
return nil, err
}
err = pm.addAllProfiles()
if err != nil {
return nil, err
}
sort.Sort(pm.s)
return pm.s, nil
}
func (pm *profilesMaker) loadPackageFiles() error {
var pkgs []string
seen := make(map[string]struct{})
for _, covProf := range pm.covProfs {
pkg := filepath.Dir(covProf.FileName)
if _, ok := seen[pkg]; !ok {
seen[pkg] = struct{}{}
pkgs = append(pkgs, pkg)
}
}
res, err := packages.Load(nil, pkgs...)
if err != nil {
return fmt.Errorf("failed to locate packages: %v", err)
}
pm.sourceFiles = make(map[string]string)
for _, pkg := range res {
for _, f := range pkg.GoFiles {
pkgFile := filepath.Join(pkg.PkgPath, filepath.Base(f))
pm.sourceFiles[pkgFile] = f
}
}
return nil
}
func (pm *profilesMaker) addAllProfiles() error {
var g errgroup.Group
for _, covProf := range pm.covProfs {
covProf := covProf
g.Go(func() error {
return pm.addProfile(covProf)
})
}
return g.Wait()
}
func (pm *profilesMaker) addProfile(covProf *cover.Profile) error {
absPath, ok := pm.sourceFiles[covProf.FileName]
if !ok {
return fmt.Errorf("could not locate source file for %s", covProf.FileName)
}
ignore, err := pm.ignoreFile(absPath)
if ignore || err != nil {
return err
}
p := profile{
filename: covProf.FileName,
}
for _, b := range pm.coalesce(covProf.Blocks) {
p.total += b.NumStmt
if b.Count > 0 {
p.exec += b.NumStmt
}
if b.Count == 0 && b.NumStmt > 0 {
if b.StartLine == b.EndLine {
p.missing = append(p.missing,
fmt.Sprintf("%d", b.StartLine))
} else {
p.missing = append(p.missing,
fmt.Sprintf("%d-%d", b.StartLine, b.EndLine))
}
}
}
pm.mtx.Lock()
pm.s = append(pm.s, &p)
pm.mtx.Unlock()
return nil
}
func (*profilesMaker) coalesce(bs []cover.ProfileBlock) (res []cover.ProfileBlock) {
for _, b := range bs {
if len(res) > 0 {
prev := &res[len(res)-1]
// Two "misses" next to each other can always be joined
if b.Count == 0 && prev.Count == 0 {
prev.EndLine = b.EndLine
prev.EndCol = b.EndCol
prev.NumStmt += b.NumStmt
prev.Count += b.Count
continue
}
}
res = append(res, b)
}
return
}
// https://pkg.go.dev/cmd/go#hdr-Generate_Go_files_by_processing_source/
var genRe = regexp.MustCompile(`^// Code generated .* DO NOT EDIT\.$`)
func (*profilesMaker) ignoreFile(path string) (bool, error) {
f, err := os.Open(path)
if err != nil {
return false, err
}
defer f.Close()
s := bufio.NewScanner(f)
for s.Scan() {
l := s.Text()
if strings.HasPrefix(l, "import ") {
break
}
if genRe.MatchString(l) {
return true, nil
}
if l == "//gocovr:skip-file" {
return true, nil
}
}
return false, s.Err()
}