-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.go
297 lines (260 loc) · 6.66 KB
/
main.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
285
286
287
288
289
290
291
292
293
294
295
296
297
// gcov2lcov - convert golang coverage files to the lcov format.
//
// Copyright (c) 2019 Jan Delgado
// Copyright (c) 2019 Richard S Allinson
//
// Credits:
// This tool is based on covfmt (https://github.com/ricallinson/covfmt) and
// uses some parts of goveralls (https://github.com/mattn/goveralls).
package main
import (
"bufio"
"errors"
"flag"
"go/build"
"io"
"log"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
)
type block struct {
startLine int
startChar int
endLine int
endChar int
statements int
covered int
}
var vscDirs = []string{".git", ".hg", ".bzr", ".svn"}
type cacheEntry struct {
dir string
err error
}
var pkgCache = map[string]cacheEntry{}
// given a module+file spec (e.g. github.com/jandelgado/gcov2lcov/main.go),
// strip of the module name and return the file name (e.g. main.go).
func findFile(filePath string) (string, error) {
dir, file := filepath.Split(filePath)
var result cacheEntry
var ok bool
if result, ok = pkgCache[dir]; !ok {
pkg, err := build.Import(dir, ".", build.FindOnly)
if err == nil {
result = cacheEntry{pkg.Dir, nil}
} else {
result = cacheEntry{"", err}
}
pkgCache[dir] = result
}
return filepath.Join(result.dir, file), result.err
}
// findRepositoryRoot finds the VCS root dir of a given dir
func findRepositoryRoot(dir string) (string, bool) {
for _, vcsdir := range vscDirs {
if d, err := os.Stat(filepath.Join(dir, vcsdir)); err == nil && d.IsDir() {
return dir, true
}
}
nextdir := filepath.Dir(dir)
if nextdir == dir {
return "", false
}
return findRepositoryRoot(nextdir)
}
func getSourceFileName(name string) string {
return name
}
func getCoverallsSourceFileName(name string) string {
if dir, ok := findRepositoryRoot(name); ok {
filename := strings.TrimPrefix(name, dir+string(os.PathSeparator))
return filename
}
return name
}
func keysOfMap(m map[int]int) []int {
keys := make([]int, len(m))
i := 0
for k := range m {
keys[i] = k
i++
}
return keys
}
func writeLcovRecord(filePath string, blocks []*block, w io.StringWriter) error {
writer := func(err error, s string) error {
if err != nil {
return err
}
_, err = w.WriteString(s)
return err
}
var err error
err = writer(err, "TN:\nSF:"+filePath+"\n")
// Loop over functions
// FN: line,name
// FNF: total functions
// FNH: covered functions
// Loop over functions
// FNDA: stats,name ?
total := 0
covered := 0
// maps line number to sum of covered
coverMap := map[int]int{}
// Loop over each block and extract the lcov data needed.
for _, b := range blocks {
// For each line in a block we add an lcov entry and count the lines.
for i := b.startLine; i <= b.endLine; i++ {
coverMap[i] += b.covered
}
}
lines := keysOfMap(coverMap)
sort.Ints(lines)
for _, line := range lines {
err = writer(err, "DA:"+strconv.Itoa(line)+","+strconv.Itoa(coverMap[line])+"\n")
total++
if coverMap[line] > 0 {
covered++
}
}
// LH:<number of lines with a non-zero execution count>
// LF:<number of instrumented lines>
err = writer(err, "LF:"+strconv.Itoa(total)+"\n")
err = writer(err, "LH:"+strconv.Itoa(covered)+"\n")
// Loop over branches
// BRDA: ?
// BRF: total branches
// BRH: covered branches
return writer(err, "end_of_record\n")
}
func writeLcov(blocks map[string][]*block, f io.Writer) error {
w := bufio.NewWriter(f)
for file, fileBlocks := range blocks {
if err := writeLcovRecord(file, fileBlocks, w); err != nil {
return err
}
}
w.Flush()
return nil
}
// Format being parsed is:
//
// name.go:line.column,line.column numberOfStatements count
//
// e.g.
//
// github.com/jandelgado/golang-ci-template/main.go:6.14,8.2 1 1
func parseCoverageLine(line string) (string, *block, error) {
path := strings.Split(line, ":")
if len(path) != 2 {
return "", nil, errors.New("unexpected format (path sep): " + line)
}
parts := strings.Split(path[1], " ")
if len(parts) != 3 {
return "", nil, errors.New("unexpected format (parts): " + line)
}
sections := strings.Split(parts[0], ",")
if len(sections) != 2 {
return "", nil, errors.New("unexpected format (pos): " + line)
}
start := strings.Split(sections[0], ".")
end := strings.Split(sections[1], ".")
safeAtoi := func(err error, s string) (int, error) {
if err != nil {
return 0, err
}
return strconv.Atoi(s)
}
b := &block{}
var err error
b.startLine, err = safeAtoi(nil, start[0])
b.startChar, err = safeAtoi(err, start[1])
b.endLine, err = safeAtoi(err, end[0])
b.endChar, err = safeAtoi(err, end[1])
b.statements, err = safeAtoi(err, parts[1])
b.covered, err = safeAtoi(err, parts[2])
return path[0], b, err
}
func parseCoverage(coverage io.Reader, pathResolverFunc func(string) string) (map[string][]*block, error) {
scanner := bufio.NewScanner(coverage)
blocks := map[string][]*block{}
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "mode:") {
continue
}
if f, b, err := parseCoverageLine(line); err == nil {
f, err := findFile(f)
if err != nil {
log.Printf("warn: %v", err)
continue
}
f = pathResolverFunc(f)
// Make sure the filePath is a key in the map.
if _, found := blocks[f]; !found {
blocks[f] = []*block{}
}
blocks[f] = append(blocks[f], b)
} else {
log.Printf("warn: %v", err)
}
}
if err := scanner.Err(); err != nil {
return nil, err
}
return blocks, nil
}
func convertCoverage(in io.Reader, out io.Writer, pathResolverFunc func(string) string) error {
blocks, err := parseCoverage(in, pathResolverFunc)
if err != nil {
return err
}
return writeLcov(blocks, out)
}
func main() {
os.Exit(gcovmain())
}
func gcovmain() int {
infileName := flag.String("infile", "", "go coverage file to read, default: <stdin>")
outfileName := flag.String("outfile", "", "lcov file to write, default: <stdout>")
useAbsoluteSourcePath := flag.Bool("use-absolute-source-path", false,
"use absolute paths for source file in lcov output, default: false")
flag.Parse()
if len(flag.Args()) > 0 {
flag.Usage()
return 1
}
infile := os.Stdin
outfile := os.Stdout
var err error
if *infileName != "" {
infile, err = os.Open(*infileName)
if err != nil {
log.Printf("error opening input file: %v\n", err)
return 2
}
defer infile.Close()
}
if *outfileName != "" {
outfile, err = os.Create(*outfileName)
if err != nil {
log.Printf("error opening output file: %v\n", err)
return 3
}
defer outfile.Close()
}
var pathResolverFunc func(string) string
if *useAbsoluteSourcePath {
pathResolverFunc = getSourceFileName
} else {
pathResolverFunc = getCoverallsSourceFileName
}
err = convertCoverage(infile, outfile, pathResolverFunc)
if err != nil {
log.Printf("error: convert: %v", err)
return 4
}
return 0
}