-
-
Notifications
You must be signed in to change notification settings - Fork 140
/
gocover.go
188 lines (170 loc) · 4.8 KB
/
gocover.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
package main
// Much of the core of this is copied from go's cover tool itself.
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// The rest is written by Dustin Sallings
import (
"fmt"
"go/build"
"io/ioutil"
"os"
"path/filepath"
"strings"
"golang.org/x/mod/modfile"
"golang.org/x/tools/cover"
)
func findFile(rootPackage string, rootDir string, file string) (string, error) {
// If we find a file that is inside the root package, we already know
// where it should be!
if rootPackage != "" {
if relPath, _ := filepath.Rel(rootPackage, file); !strings.HasPrefix(relPath, "..") {
// The file is inside the root package...
return filepath.Join(rootDir, relPath), nil
}
}
dir, file := filepath.Split(file)
pkg, err := build.Import(dir, ".", build.FindOnly)
if err != nil {
return "", fmt.Errorf("can't find %q: %v", file, err)
}
return filepath.Join(pkg.Dir, file), nil
}
// mergeProfs merges profiles for same target packages.
// It assumes each profiles have same sorted FileName and Blocks.
func mergeProfs(pfss [][]*cover.Profile) []*cover.Profile {
if len(pfss) == 0 {
return nil
}
for len(pfss) > 1 {
i := 0
for ; 2*i+1 < len(pfss); i++ {
pfss[i] = mergeTwoProfs(pfss[2*i], pfss[2*i+1])
}
if 2*i < len(pfss) {
pfss[i] = pfss[2*i]
i++
}
pfss = pfss[:i]
}
return pfss[0]
}
func mergeTwoProfs(left, right []*cover.Profile) []*cover.Profile {
ret := make([]*cover.Profile, 0, len(left)+len(right))
for len(left) > 0 && len(right) > 0 {
if left[0].FileName == right[0].FileName {
profile := &cover.Profile{
FileName: left[0].FileName,
Mode: left[0].Mode,
Blocks: mergeTwoProfBlock(left[0].Blocks, right[0].Blocks),
}
ret = append(ret, profile)
left = left[1:]
right = right[1:]
} else if left[0].FileName < right[0].FileName {
ret = append(ret, left[0])
left = left[1:]
} else {
ret = append(ret, right[0])
right = right[1:]
}
}
ret = append(ret, left...)
ret = append(ret, right...)
return ret
}
func mergeTwoProfBlock(left, right []cover.ProfileBlock) []cover.ProfileBlock {
ret := make([]cover.ProfileBlock, 0, len(left)+len(right))
for len(left) > 0 && len(right) > 0 {
a, b := left[0], right[0]
if a.StartLine == b.StartLine && a.StartCol == b.StartCol && a.EndLine == b.EndLine && a.EndCol == b.EndCol {
ret = append(ret, cover.ProfileBlock{
StartLine: a.StartLine,
StartCol: a.StartCol,
EndLine: a.EndLine,
EndCol: a.EndCol,
NumStmt: a.NumStmt,
Count: a.Count + b.Count,
})
left = left[1:]
right = right[1:]
} else if a.StartLine < b.StartLine || (a.StartLine == b.StartLine && a.StartCol < b.StartCol) {
ret = append(ret, a)
left = left[1:]
} else {
ret = append(ret, b)
right = right[1:]
}
}
ret = append(ret, left...)
ret = append(ret, right...)
return ret
}
// toSF converts profiles to sourcefiles for coveralls.
func toSF(profs []*cover.Profile) ([]*SourceFile, error) {
// find root package to reduce build.Import calls when importing files from relative root
// https://github.com/mattn/goveralls/pull/195
rootDirectory, err := os.Getwd()
if err != nil {
return nil, fmt.Errorf("get working dir: %v", err)
}
rootPackage := findRootPackage(rootDirectory)
var rv []*SourceFile
for _, prof := range profs {
path, err := findFile(rootPackage, rootDirectory, prof.FileName)
if err != nil {
return nil, fmt.Errorf("cannot find file %q: %v", prof.FileName, err)
}
sf := &SourceFile{
Name: getCoverallsSourceFileName(path),
}
lineLookup := map[int]int{}
maxLineNo := 0
for _, block := range prof.Blocks {
for i := block.StartLine; i <= block.EndLine; i++ {
lineLookup[i] += block.Count
}
if block.EndLine > maxLineNo {
maxLineNo = block.EndLine
}
}
sf.Coverage = make([]interface{}, maxLineNo)
for i := 1; i <= maxLineNo; i++ {
if c, ok := lineLookup[i]; ok {
sf.Coverage[i-1] = c
}
}
if *uploadSource {
fb, err := ioutil.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("cannot read source of file %q: %v", path, err)
}
sf.Source = string(fb)
}
rv = append(rv, sf)
}
return rv, nil
}
func parseCover(fn string) ([]*SourceFile, error) {
var pfss [][]*cover.Profile
for _, p := range strings.Split(fn, ",") {
profs, err := cover.ParseProfiles(p)
if err != nil {
return nil, fmt.Errorf("error parsing coverage: %v", err)
}
pfss = append(pfss, profs)
}
sourceFiles, err := toSF(mergeProfs(pfss))
if err != nil {
return nil, err
}
return sourceFiles, nil
}
func findRootPackage(rootDirectory string) string {
modPath := filepath.Join(rootDirectory, "go.mod")
content, err := ioutil.ReadFile(modPath)
if err != nil {
return ""
}
return modfile.ModulePath(content)
}