-
Notifications
You must be signed in to change notification settings - Fork 8
/
coverage.go
284 lines (259 loc) · 7.86 KB
/
coverage.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// Package coverage is for writing coverage reports.
package coverage
import (
"bufio"
"fmt"
"io"
"os"
"path"
"path/filepath"
"regexp"
"strings"
"golang.org/x/tools/cover"
"oss.indeed.com/go/go-opine/internal/run"
)
// Generate testdata/cover.out by running the ./testdata tests.
//go:generate go test -v -race -coverprofile=testdata/cover.out -covermode=atomic ./testdata
var generatedFileRegexp = regexp.MustCompile(`(?m:^// Code generated .* DO NOT EDIT\.$)`)
type Coverage struct {
profiles []*cover.Profile
modPaths map[string]string
}
// Load a Go coverprofile file. Generated files are excluded from the result.
func Load(inPath string) (*Coverage, error) {
profiles, err := cover.ParseProfiles(inPath)
if err != nil {
return nil, err
}
paths, err := findModPaths(profiles)
if err != nil {
return nil, err
}
profiles, err = profilesWithoutGenerated(profiles, paths)
if err != nil {
return nil, err
}
return &Coverage{profiles: profiles, modPaths: paths}, nil
}
// CoverProfile writes the coverage to a file in the Go "coverprofile" format.
func (cov *Coverage) CoverProfile(outPath string) error {
f, err := os.OpenFile(outPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
if err != nil {
return err
}
err = writeProfiles(cov.profiles, f)
if closeErr := f.Close(); err == nil {
err = closeErr
}
return err
}
// XML writes the coverage to a file in the Cobertura-style XML format.
func (cov *Coverage) XML(outPath string) error {
// Work around an issue with gocover-cobertura and Jenkins where the files
// cannot be found.
profilesWithRealPaths := make([]*cover.Profile, len(cov.profiles))
for i, profile := range cov.profiles {
fileLoc, err := findFileRel(profile.FileName, cov.modPaths)
if err != nil {
return err
}
profileWithRealPath := *profile
profileWithRealPath.FileName = fileLoc
profilesWithRealPaths[i] = &profileWithRealPath
}
var modifiedGoCov strings.Builder
if err := writeProfiles(profilesWithRealPaths, &modifiedGoCov); err != nil {
return err
}
xmlCovOut, _, err := run.Cmd(
"go",
run.Args("run", "github.com/t-yuki/gocover-cobertura@v0.0.0-20180217150009-aaee18c8195c"),
run.Stdin(modifiedGoCov.String()),
run.SuppressStdout(),
)
if err != nil {
return err
}
return os.WriteFile(outPath, []byte(xmlCovOut), 0666) //nolint:gosec
}
// Ratio returns the ratio of covered statements over all statements. The
// value returned will always be between 0 and 1. If there are no statements
// then 1 is returned.
func (cov *Coverage) Ratio() float64 {
statementCnt := 0
statementHit := 0
for _, profile := range cov.profiles {
for _, block := range profile.Blocks {
statementCnt += block.NumStmt
if block.Count > 0 {
statementHit += block.NumStmt
}
}
}
if statementCnt == 0 {
return 1
}
return float64(statementHit) / float64(statementCnt)
}
// isGenerated checks if the provided file was generated or not. The file
// path should be a real filesystem path, not a Go module path (e.g.
// "/home/you/github.com/proj/main.go", not "github.com/proj/main.go").
func isGenerated(filePath string) (bool, error) {
file, err := os.Open(filePath)
if err != nil {
return false, err
}
defer file.Close() // ignore close error (we are not writing)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
if generatedFileRegexp.MatchString(scanner.Text()) {
return true, nil
}
}
return isGeneratedReader(bufio.NewReader(file))
}
// isGeneratedReader checks if the file being read by the provided
// io.RuneReader was generated or not.
func isGeneratedReader(rr io.RuneReader) (bool, error) {
var err error
res := generatedFileRegexp.MatchReader(
// Snooping required due to https://github.com/golang/go/issues/40509.
runeReaderFunc(
func() (rune, int, error) {
var (
r rune
n int
)
r, n, err = rr.ReadRune()
return r, n, err
},
),
)
if err == io.EOF {
err = nil
}
return res, err
}
// profilesWithoutGenerated returns a new slice of profiles with all
// generated files removed. The provided modPaths must be a map from each
// known Go module to the absolute filesystem path of the module directory,
// as returned by findModPaths.
func profilesWithoutGenerated(profiles []*cover.Profile, modPaths map[string]string) ([]*cover.Profile, error) {
res := make([]*cover.Profile, 0, len(profiles))
for _, profile := range profiles {
filePath, err := findFile(profile.FileName, modPaths)
if err != nil {
return nil, err
}
if gen, err := isGenerated(filePath); err != nil {
return nil, err
} else if !gen {
res = append(res, profile)
}
}
return res, nil
}
// findFile finds the absolute filesystem path of a file name specified
// relative to a Go module. The provided modPaths must be a map from each
// known Go module to the absolute filesystem path of the module directory,
// as returned by findModPaths.
//
// If the module of the provided file name is not in the paths an error is
// returned.
func findFile(fileName string, modPaths map[string]string) (string, error) {
pkg := path.Dir(fileName)
dir, ok := modPaths[pkg]
if !ok {
return "", fmt.Errorf("could not determine the filesystem path of %q", fileName)
}
return filepath.Join(dir, path.Base(fileName)), nil
}
// findFileRel finds the relative (to the current working directory)
// filesystem path of a file name specified relative to a Go module.
//
// See findFile for more information.
func findFileRel(fileName string, modPaths map[string]string) (string, error) {
filePath, err := findFile(fileName, modPaths)
if err != nil {
return "", err
}
cwd, err := os.Getwd()
if err != nil {
return "", err
}
return filepath.Rel(cwd, filePath)
}
// writeProfiles writes cover profiles to the provided io.Writer in the Go
// "coverprofile" format.
func writeProfiles(profiles []*cover.Profile, w io.Writer) error {
if len(profiles) == 0 {
return nil
}
if _, err := io.WriteString(w, "mode: "+profiles[0].Mode+"\n"); err != nil {
return err
}
for _, profile := range profiles {
for _, block := range profile.Blocks {
_, err := fmt.Fprintf(
w, "%s:%d.%d,%d.%d %d %d\n", profile.FileName,
block.StartLine, block.StartCol, block.EndLine, block.EndCol,
block.NumStmt, block.Count,
)
if err != nil {
return err
}
}
}
return nil
}
// findModPaths returns a map from module name to the filesystem path of
// the module directory. The filesystem paths are absolute and do not include
// a trailing "/".
func findModPaths(profiles []*cover.Profile) (map[string]string, error) {
// Use %q to ensure the ImportPath and Dir boundaries can be easily
// distinguished. This also protects us against file names containing
// new line characters... you know you want to make them.
mods := modsInProfiles(profiles)
args := append(
[]string{"list", "-f", `{{ .ImportPath | printf "%q" }} {{ .Dir | printf "%q" }}`},
mods...,
)
stdout, stderr, err := run.Cmd("go", args, run.Log(io.Discard))
if err != nil {
return nil, fmt.Errorf("error running go list [stdout=%q stderr=%q]: %w", stdout, stderr, err)
}
modPaths := make(map[string]string, len(mods))
for _, line := range strings.Split(stdout, "\n") {
if line == "" {
continue
}
var (
mod string
dir string
)
_, err := fmt.Sscanf(line, "%q %q", &mod, &dir)
if err != nil {
return nil, fmt.Errorf("got unexpected line %q from go list: %w", line, err)
}
modPaths[mod] = dir
}
return modPaths, nil
}
// modsInProfiles returns a list of modules in the provided cover
// profiles. Duplicate modules will show up only once in the result.
func modsInProfiles(profiles []*cover.Profile) []string {
mods := make([]string, 0)
seen := make(map[string]bool)
for _, profile := range profiles {
mod, _ := filepath.Split(profile.FileName)
if !seen[mod] {
mods = append(mods, mod)
seen[mod] = true
}
}
return mods
}
type runeReaderFunc func() (rune, int, error)
func (f runeReaderFunc) ReadRune() (rune, int, error) {
return f()
}