Skip to content

Commit dac63c8

Browse files
authored
Adding support for Golang coverage generated using -coverpkg flag (#461)
* Added support for parsing Golang coverage report generated using `-coverpkg` flag * Moved `example_foobar.out` under `gocov/example/` folder, to sit next to the relevant tests
1 parent 9dfc52c commit dac63c8

File tree

8 files changed

+166
-21
lines changed

8 files changed

+166
-21
lines changed

Diff for: formatters/gocov/example/bar/bar.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package bar
2+
3+
type Bar struct{}
4+
5+
func New() *Bar {
6+
return &Bar{}
7+
}
8+
9+
func (b *Bar) String() string {
10+
return "bar"
11+
}
12+
13+
func (b *Bar) Bytes() []byte {
14+
return []byte("bar")
15+
}

Diff for: formatters/gocov/example/bar/bar_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package bar_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
8+
"github.com/codeclimate/test-reporter/formatters/gocov/example/bar"
9+
)
10+
11+
func Test_String(t *testing.T) {
12+
bar := bar.New()
13+
require.Equal(t, "bar", bar.String())
14+
}

Diff for: formatters/gocov/example/foo/foo.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package foo
2+
3+
import "fmt"
4+
5+
type Foo struct {
6+
anotherFoo interface{}
7+
}
8+
9+
func New(foo interface{}) *Foo {
10+
return &Foo{
11+
anotherFoo: foo,
12+
}
13+
}
14+
15+
func (f *Foo) String() string {
16+
i, ok := f.anotherFoo.(interface{ String() string })
17+
if ok {
18+
return fmt.Sprintf("foo %s", i)
19+
}
20+
return "foo"
21+
}

Diff for: formatters/gocov/example/foo/foo_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package foo_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
8+
"github.com/codeclimate/test-reporter/formatters/gocov/example/foo"
9+
)
10+
11+
func Test_String(t *testing.T) {
12+
foo := &foo.Foo{}
13+
require.Equal(t, "foo", foo.String())
14+
}

Diff for: formatters/gocov/example/foobar_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package example_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
8+
"github.com/codeclimate/test-reporter/formatters/gocov/example/bar"
9+
"github.com/codeclimate/test-reporter/formatters/gocov/example/foo"
10+
)
11+
12+
func Test_String(t *testing.T) {
13+
foo := foo.New(bar.New())
14+
require.Equal(t, "foo bar", foo.String())
15+
}

Diff for: formatters/gocov/example/foobar_test.out

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
mode: set
2+
github.com/codeclimate/test-reporter/formatters/gocov/example/foo/foo.go:9.32,13.2 1 0
3+
github.com/codeclimate/test-reporter/formatters/gocov/example/foo/foo.go:15.31,17.8 2 0
4+
github.com/codeclimate/test-reporter/formatters/gocov/example/foo/foo.go:20.2,20.14 1 0
5+
github.com/codeclimate/test-reporter/formatters/gocov/example/foo/foo.go:17.8,19.3 1 0
6+
github.com/codeclimate/test-reporter/formatters/gocov/example/bar/bar.go:5.17,7.2 1 1
7+
github.com/codeclimate/test-reporter/formatters/gocov/example/bar/bar.go:9.31,11.2 1 1
8+
github.com/codeclimate/test-reporter/formatters/gocov/example/bar/bar.go:13.30,15.2 1 0
9+
github.com/codeclimate/test-reporter/formatters/gocov/example/foo/foo.go:9.32,13.2 1 1
10+
github.com/codeclimate/test-reporter/formatters/gocov/example/foo/foo.go:15.31,17.8 2 1
11+
github.com/codeclimate/test-reporter/formatters/gocov/example/foo/foo.go:20.2,20.14 1 0
12+
github.com/codeclimate/test-reporter/formatters/gocov/example/foo/foo.go:17.8,19.3 1 1
13+
github.com/codeclimate/test-reporter/formatters/gocov/example/bar/bar.go:5.17,7.2 1 1
14+
github.com/codeclimate/test-reporter/formatters/gocov/example/bar/bar.go:9.31,11.2 1 1
15+
github.com/codeclimate/test-reporter/formatters/gocov/example/bar/bar.go:13.30,15.2 1 0
16+
github.com/codeclimate/test-reporter/formatters/gocov/example/bar/bar.go:5.17,7.2 1 0
17+
github.com/codeclimate/test-reporter/formatters/gocov/example/bar/bar.go:9.31,11.2 1 0
18+
github.com/codeclimate/test-reporter/formatters/gocov/example/bar/bar.go:13.30,15.2 1 0
19+
github.com/codeclimate/test-reporter/formatters/gocov/example/foo/foo.go:9.32,13.2 1 0
20+
github.com/codeclimate/test-reporter/formatters/gocov/example/foo/foo.go:15.31,17.8 2 1
21+
github.com/codeclimate/test-reporter/formatters/gocov/example/foo/foo.go:20.2,20.14 1 1
22+
github.com/codeclimate/test-reporter/formatters/gocov/example/foo/foo.go:17.8,19.3 1 0

Diff for: formatters/gocov/gocov.go

+9
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,16 @@ func (r Formatter) Format() (formatters.Report, error) {
6161
return rep, errors.WithStack(err)
6262
}
6363
num := 0
64+
blocks := []cover.ProfileBlock{}
6465
for _, b := range p.Blocks {
66+
lstIdx := len(blocks) - 1
67+
if lstIdx < 0 || blocks[lstIdx].StartLine != b.StartLine || blocks[lstIdx].EndLine != b.EndLine {
68+
blocks = append(blocks, b)
69+
continue
70+
}
71+
blocks[lstIdx].Count += b.Count
72+
}
73+
for _, b := range blocks {
6574
for num < b.StartLine {
6675
sf.Coverage = append(sf.Coverage, formatters.NullInt{})
6776
num++

Diff for: formatters/gocov/gocov_test.go

+56-21
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,60 @@ import (
1010
)
1111

1212
func Test_Parse(t *testing.T) {
13-
gb := env.GitBlob
14-
defer func() { env.GitBlob = gb }()
15-
env.GitBlob = func(s string, c *object.Commit) (string, error) {
16-
return s, nil
17-
}
18-
19-
r := require.New(t)
20-
21-
f := &Formatter{Path: "./example.out"}
22-
rep, err := f.Format()
23-
r.NoError(err)
24-
25-
r.Len(rep.SourceFiles, 4)
26-
27-
sf := rep.SourceFiles["github.com/codeclimate/test-reporter/formatters/source_file.go"]
28-
r.InDelta(77.4, sf.CoveredPercent, 1)
29-
r.Len(sf.Coverage, 116)
30-
r.False(sf.Coverage[5].Valid)
31-
r.True(sf.Coverage[54].Valid)
32-
r.Equal(0, sf.Coverage[53].Int)
33-
r.Equal(1, sf.Coverage[55].Int)
13+
14+
t.Run("should parse report from single package", func(t *testing.T) {
15+
gb := env.GitBlob
16+
defer func() { env.GitBlob = gb }()
17+
env.GitBlob = func(s string, c *object.Commit) (string, error) {
18+
return s, nil
19+
}
20+
21+
r := require.New(t)
22+
23+
f := &Formatter{Path: "./example.out"}
24+
rep, err := f.Format()
25+
r.NoError(err)
26+
27+
r.Len(rep.SourceFiles, 4)
28+
29+
sf := rep.SourceFiles["github.com/codeclimate/test-reporter/formatters/source_file.go"]
30+
r.InDelta(77.4, sf.CoveredPercent, 1)
31+
r.Len(sf.Coverage, 116)
32+
r.False(sf.Coverage[5].Valid)
33+
r.True(sf.Coverage[54].Valid)
34+
r.Equal(0, sf.Coverage[53].Int)
35+
r.Equal(1, sf.Coverage[55].Int)
36+
})
37+
38+
//
39+
// Test parsing report that was generated using `-coverpkg` flag.
40+
//
41+
// go test \
42+
// -coverpkg="github.com/codeclimate/test-reporter/formatters/gocov/example/foo,github.com/codeclimate/test-reporter/formatters/gocov/example/bar" \
43+
// -coverprofile=example_foobar.out \
44+
// ./...
45+
//
46+
t.Run("should parse coverage report from multiple packages", func(t *testing.T) {
47+
gb := env.GitBlob
48+
defer func() { env.GitBlob = gb }()
49+
env.GitBlob = func(s string, c *object.Commit) (string, error) {
50+
return s, nil
51+
}
52+
53+
r := require.New(t)
54+
55+
f := &Formatter{Path: "./example/foobar_test.out"}
56+
rep, err := f.Format()
57+
r.NoError(err)
58+
59+
r.Len(rep.SourceFiles, 2)
60+
61+
sfFoo := rep.SourceFiles["example/foo/foo.go"]
62+
sfBar := rep.SourceFiles["example/bar/bar.go"]
63+
64+
r.EqualValues(87.5, rep.CoveredPercent)
65+
r.EqualValues(100, sfFoo.CoveredPercent)
66+
r.InDelta(66.66, sfBar.CoveredPercent, 0.01)
67+
})
68+
3469
}

0 commit comments

Comments
 (0)