Skip to content

Commit 3da41fd

Browse files
Fix gocov formatting issue #479 (#480)
Fixes issue #479 The formatter used to assume that the number of statements always equals the number of lines in a block. This is not true when there are blank lines for example. This change colours by line number rather than by statement. Co-authored-by: Georgi Sabev <georgethebeatle@gmail.com> Co-authored-by: Danail Branekov <danailster@gmail.com> Co-authored-by: Danail Branekov <danailster@gmail.com>
1 parent 445d4f4 commit 3da41fd

File tree

2 files changed

+10
-12
lines changed

2 files changed

+10
-12
lines changed

formatters/gocov/gocov.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ func (r Formatter) Format() (formatters.Report, error) {
6060
if err != nil {
6161
return rep, errors.WithStack(err)
6262
}
63-
num := 0
6463
blocks := []cover.ProfileBlock{}
6564
for _, b := range p.Blocks {
6665
lstIdx := len(blocks) - 1
@@ -70,14 +69,15 @@ func (r Formatter) Format() (formatters.Report, error) {
7069
}
7170
blocks[lstIdx].Count += b.Count
7271
}
72+
lineNum := 1
7373
for _, b := range blocks {
74-
for num < b.StartLine {
74+
for lineNum < b.StartLine {
7575
sf.Coverage = append(sf.Coverage, formatters.NullInt{})
76-
num++
76+
lineNum++
7777
}
78-
for i := 0; i < b.NumStmt; i++ {
78+
for lineNum <= b.EndLine {
7979
sf.Coverage = append(sf.Coverage, formatters.NewNullInt(b.Count))
80-
num++
80+
lineNum++
8181
}
8282
}
8383
err = rep.AddSourceFile(sf)

formatters/gocov/gocov_test.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
)
1111

1212
func Test_Parse(t *testing.T) {
13-
1413
t.Run("should parse report from single package", func(t *testing.T) {
1514
gb := env.GitBlob
1615
defer func() { env.GitBlob = gb }()
@@ -27,17 +26,17 @@ func Test_Parse(t *testing.T) {
2726
r.Len(rep.SourceFiles, 4)
2827

2928
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)
29+
r.InDelta(75.8, sf.CoveredPercent, 1)
30+
r.Len(sf.Coverage, 115)
3231
r.False(sf.Coverage[5].Valid)
3332
r.True(sf.Coverage[54].Valid)
34-
r.Equal(0, sf.Coverage[53].Int)
33+
r.Equal(0, sf.Coverage[52].Int)
3534
r.Equal(1, sf.Coverage[55].Int)
3635
})
3736

3837
//
3938
// Test parsing report that was generated using `-coverpkg` flag.
40-
//
39+
//
4140
// go test \
4241
// -coverpkg="github.com/codeclimate/test-reporter/formatters/gocov/example/foo,github.com/codeclimate/test-reporter/formatters/gocov/example/bar" \
4342
// -coverprofile=example_foobar.out \
@@ -61,9 +60,8 @@ func Test_Parse(t *testing.T) {
6160
sfFoo := rep.SourceFiles["example/foo/foo.go"]
6261
sfBar := rep.SourceFiles["example/bar/bar.go"]
6362

64-
r.EqualValues(87.5, rep.CoveredPercent)
63+
r.EqualValues(85, rep.CoveredPercent)
6564
r.EqualValues(100, sfFoo.CoveredPercent)
6665
r.InDelta(66.66, sfBar.CoveredPercent, 0.01)
6766
})
68-
6967
}

0 commit comments

Comments
 (0)