Skip to content

Commit bd844a3

Browse files
authored
optionally use absolute source path in lcov output (#10)
The lcov spec wants the SF attribute to contain an absolute path to the source file, thus far a relative one has been used which breaks some tools that use the lcov file. When using the -use-absolute-source-path argument the lcov output will contain the full absolute path.
1 parent 8092238 commit bd844a3

File tree

3 files changed

+36
-7
lines changed

3 files changed

+36
-7
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ Usage of ./gcov2lcov:
2929
go coverage file to read, default: <stdin>
3030
-outfile string
3131
lcov file to write, default: <stdout>
32+
-use-absolute-source-path
33+
use absolute paths for source file in lcov output, default: false
3234
```
3335

3436
### Example

main.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ func findRepositoryRoot(dir string) (string, bool) {
7373
return findRepositoryRoot(nextdir)
7474
}
7575

76+
func getSourceFileName(name string) string {
77+
return name
78+
}
79+
7680
func getCoverallsSourceFileName(name string) string {
7781
if dir, ok := findRepositoryRoot(name); ok {
7882
filename := strings.TrimPrefix(name, dir+string(os.PathSeparator))
@@ -199,7 +203,7 @@ func parseCoverageLine(line string) (string, *block, error) {
199203
return path[0], b, err
200204
}
201205

202-
func parseCoverage(coverage io.Reader) (map[string][]*block, error) {
206+
func parseCoverage(coverage io.Reader, pathResolverFunc func(string) string) (map[string][]*block, error) {
203207
scanner := bufio.NewScanner(coverage)
204208
blocks := map[string][]*block{}
205209
for scanner.Scan() {
@@ -213,7 +217,9 @@ func parseCoverage(coverage io.Reader) (map[string][]*block, error) {
213217
log.Printf("warn: %v", err)
214218
continue
215219
}
216-
f = getCoverallsSourceFileName(f)
220+
221+
f = pathResolverFunc(f)
222+
217223
// Make sure the filePath is a key in the map.
218224
if _, found := blocks[f]; !found {
219225
blocks[f] = []*block{}
@@ -230,8 +236,8 @@ func parseCoverage(coverage io.Reader) (map[string][]*block, error) {
230236
return blocks, nil
231237
}
232238

233-
func convertCoverage(in io.Reader, out io.Writer) error {
234-
blocks, err := parseCoverage(in)
239+
func convertCoverage(in io.Reader, out io.Writer, pathResolverFunc func(string) string) error {
240+
blocks, err := parseCoverage(in, pathResolverFunc)
235241
if err != nil {
236242
return err
237243
}
@@ -245,6 +251,8 @@ func main() {
245251
func gcovmain() int {
246252
infileName := flag.String("infile", "", "go coverage file to read, default: <stdin>")
247253
outfileName := flag.String("outfile", "", "lcov file to write, default: <stdout>")
254+
useAbsoluteSourcePath := flag.Bool("use-absolute-source-path", false,
255+
"use absolute paths for source file in lcov output, default: false")
248256
flag.Parse()
249257
if len(flag.Args()) > 0 {
250258
flag.Usage()
@@ -271,7 +279,14 @@ func gcovmain() int {
271279
defer outfile.Close()
272280
}
273281

274-
err = convertCoverage(infile, outfile)
282+
var pathResolverFunc func(string) string
283+
if *useAbsoluteSourcePath {
284+
pathResolverFunc = getSourceFileName
285+
} else {
286+
pathResolverFunc = getCoverallsSourceFileName
287+
}
288+
289+
err = convertCoverage(infile, outfile, pathResolverFunc)
275290
if err != nil {
276291
log.Printf("error: convert: %v", err)
277292
return 4

main_test.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package main
44

55
import (
66
"bytes"
7+
"os"
78
"strings"
89
"testing"
910

@@ -55,7 +56,7 @@ func TestParseCoverage(t *testing.T) {
5556
github.com/jandelgado/gcov2lcov/main.go:6.14,8.3 2 1`
5657

5758
reader := strings.NewReader(cov)
58-
res, err := parseCoverage(reader)
59+
res, err := parseCoverage(reader, getCoverallsSourceFileName)
5960

6061
assert.NoError(t, err)
6162
assert.Equal(t, 1, len(res))
@@ -83,7 +84,7 @@ github.com/jandelgado/gcov2lcov/main.go:10.1,11.10 2 2`
8384

8485
in := strings.NewReader(cov)
8586
out := bytes.NewBufferString("")
86-
err := convertCoverage(in, out)
87+
err := convertCoverage(in, out, getCoverallsSourceFileName)
8788

8889
expected := `TN:
8990
SF:main.go
@@ -100,3 +101,14 @@ end_of_record
100101
assert.NoError(t, err)
101102
assert.Equal(t, expected, out.String())
102103
}
104+
105+
func TestPathResolverFunc(t *testing.T) {
106+
pwd, err := os.Getwd()
107+
assert.NoError(t, err)
108+
109+
name := getCoverallsSourceFileName(pwd + "/main.go")
110+
assert.Equal(t, "main.go", name)
111+
112+
name = getSourceFileName(pwd + "/main.go")
113+
assert.Equal(t, pwd+"/main.go", name)
114+
}

0 commit comments

Comments
 (0)