forked from jandelgado/gcov2lcov
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
114 lines (94 loc) · 2.79 KB
/
main_test.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
// gcov2lcov - convert golang coverage files to the lcov format.
// (c) 2019 Jan Delgado
package main
import (
"bytes"
"os"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestKeysOfMapReturnsAllKeysOfMap(t *testing.T) {
m := map[int]int{1: 10, 10: 100}
keys := keysOfMap(m)
assert.Contains(t, keys, 1)
assert.Contains(t, keys, 10)
assert.Equal(t, 2, len(keys))
}
func TestParseCoverageLineFailsOnInvalidLines(t *testing.T) {
_, _, err := parseCoverageLine("main.go")
assert.NotNil(t, err)
_, _, err = parseCoverageLine("main.go:A B")
assert.NotNil(t, err)
_, _, err = parseCoverageLine("main.go:A B C")
assert.NotNil(t, err)
_, _, err = parseCoverageLine("main.go:6.14,8.3 X 1")
assert.NotNil(t, err)
}
func TestParseCoverageLineOfParsesValidLineCorrectly(t *testing.T) {
line := "github.com/jandelgado/gcov2lcov/main.go:6.14,8.3 2 1"
file, b, err := parseCoverageLine(line)
assert.Nil(t, err)
assert.Equal(t, "github.com/jandelgado/gcov2lcov/main.go", file)
assert.Equal(t, 6, b.startLine)
assert.Equal(t, 14, b.startChar)
assert.Equal(t, 8, b.endLine)
assert.Equal(t, 3, b.endChar)
assert.Equal(t, 2, b.statements)
assert.Equal(t, 1, b.covered)
}
func TestParseCoverage(t *testing.T) {
// note: in this integrative test, the package path must match the actual
// repository name of this project.
cov := `mode: set
github.com/jandelgado/gcov2lcov/main.go:6.14,8.3 2 1`
reader := strings.NewReader(cov)
res, err := parseCoverage(reader, getCoverallsSourceFileName)
assert.NoError(t, err)
assert.Equal(t, 1, len(res))
for k, blks := range res {
assert.Equal(t, 1, len(blks))
b := blks[0]
assert.Equal(t, "main.go", k)
assert.Equal(t, 6, b.startLine)
assert.Equal(t, 14, b.startChar)
assert.Equal(t, 8, b.endLine)
assert.Equal(t, 3, b.endChar)
assert.Equal(t, 2, b.statements)
assert.Equal(t, 1, b.covered)
}
}
func TestConvertCoverage(t *testing.T) {
// note: in this integrative test, the package path must match the actual
// repository name of this project. Format:
// name.go:line.column,line.column numberOfStatements count
cov := `mode: set
github.com/jandelgado/gcov2lcov/main.go:6.14,8.3 2 1
github.com/jandelgado/gcov2lcov/main.go:7.14,9.3 2 0
github.com/jandelgado/gcov2lcov/main.go:10.1,11.10 2 2`
in := strings.NewReader(cov)
out := bytes.NewBufferString("")
err := convertCoverage(in, out, getCoverallsSourceFileName)
expected := `TN:
SF:main.go
DA:6,1
DA:7,1
DA:8,1
DA:9,0
DA:10,2
DA:11,2
LF:6
LH:5
end_of_record
`
assert.NoError(t, err)
assert.Equal(t, expected, out.String())
}
func TestPathResolverFunc(t *testing.T) {
pwd, err := os.Getwd()
assert.NoError(t, err)
name := getCoverallsSourceFileName(pwd + "/main.go")
assert.Equal(t, "main.go", name)
name = getSourceFileName(pwd + "/main.go")
assert.Equal(t, pwd+"/main.go", name)
}