Skip to content

Commit

Permalink
Merge pull request #395 from k1LoW/fix-merge
Browse files Browse the repository at this point in the history
Always count by LOC when merging multiple coverage.
  • Loading branch information
k1LoW authored Jul 10, 2024
2 parents 552b07f + 64717db commit 7c4b330
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 23 deletions.
8 changes: 5 additions & 3 deletions coverage/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package coverage

func (c *Coverage) Merge(c2 *Coverage) error {
if c2 == nil {
c2 = &Coverage{}
return c.reCalc()
}
// Type
switch {
case c2.Type == "":
case c.Type != TypeLOC || c2.Type != TypeLOC:
// If either is not LOC, merge as Merged
c.Type = TypeMerged
}
// Files
Expand All @@ -17,6 +18,7 @@ func (c *Coverage) Merge(c2 *Coverage) error {
if fc2.Type != fc.Type {
fc.Type = TypeMerged
}
// Merged coverage should be counted as LOC as duplicate blocks may be stacked.
fc.Blocks = append(fc.Blocks, fc2.Blocks...)
} else {
c.Files = append(c.Files, fc2)
Expand All @@ -31,13 +33,13 @@ func (c *Coverage) reCalc() error {
for _, f := range c.Files {
var fileTotal, fileCovered int

switch f.Type {
switch c.Type {
case TypeLOC, TypeMerged:
lcs := f.Blocks.ToLineCoverages()
fileTotal = lcs.Total()
fileCovered = lcs.Covered()

case TypeStmt:
case TypeStmt: // Coverage of a single unmerged TypeStmt.
for _, b := range f.Blocks {
fileTotal += *b.NumStmt
if *b.Count > 0 {
Expand Down
49 changes: 29 additions & 20 deletions coverage/merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import (

func TestMerge(t *testing.T) {
tests := []struct {
name string
c1 *Coverage
c2 *Coverage
want *Coverage
}{
{
"Simple",
&Coverage{
Type: TypeLOC,
Files: FileCoverages{
Expand Down Expand Up @@ -93,6 +95,7 @@ func TestMerge(t *testing.T) {
},
},
{
"Merge file_b.go (LOC)",
&Coverage{
Type: TypeLOC,
Files: FileCoverages{
Expand Down Expand Up @@ -164,6 +167,7 @@ func TestMerge(t *testing.T) {
},
},
{
"Merge file_a.go (LOC)",
&Coverage{
Type: TypeLOC,
Files: FileCoverages{
Expand Down Expand Up @@ -215,6 +219,7 @@ func TestMerge(t *testing.T) {
},
},
{
"Merge file_a.go (LOC and Stmt)",
&Coverage{
Type: TypeLOC,
Files: FileCoverages{
Expand Down Expand Up @@ -266,6 +271,7 @@ func TestMerge(t *testing.T) {
},
},
{
"Merge file_a.go and file_b.go (LOC)",
&Coverage{
Type: TypeLOC,
Files: FileCoverages{
Expand Down Expand Up @@ -321,6 +327,7 @@ func TestMerge(t *testing.T) {
},
},
{
"Merge no covered file (file_c.go)",
&Coverage{
Type: TypeLOC,
Files: FileCoverages{
Expand Down Expand Up @@ -392,6 +399,7 @@ func TestMerge(t *testing.T) {
},
},
{
"Simple (stmt)",
&Coverage{
Type: TypeStmt,
Files: FileCoverages{
Expand Down Expand Up @@ -431,14 +439,14 @@ func TestMerge(t *testing.T) {
},
&Coverage{
Type: TypeMerged,
Total: 16,
Covered: 13,
Total: 23,
Covered: 20,
Files: FileCoverages{
&FileCoverage{
File: "file_a.go",
Type: TypeStmt,
Total: 3,
Covered: 2,
Total: 10,
Covered: 9,
Blocks: BlockCoverages{
newBlockCoverage(TypeStmt, 1, 1, 1, 1, 1, 1),
newBlockCoverage(TypeStmt, 2, 1, 2, 1, 1, 0),
Expand Down Expand Up @@ -471,6 +479,7 @@ func TestMerge(t *testing.T) {
},
},
{
"Merge LOC and Stmt",
&Coverage{
Type: TypeLOC,
Files: FileCoverages{
Expand Down Expand Up @@ -501,8 +510,8 @@ func TestMerge(t *testing.T) {
},
&Coverage{
Type: TypeMerged,
Total: 7,
Covered: 5,
Total: 6,
Covered: 4,
Files: FileCoverages{
&FileCoverage{
File: "file_a.go",
Expand All @@ -518,8 +527,8 @@ func TestMerge(t *testing.T) {
&FileCoverage{
File: "file_b.go",
Type: TypeStmt,
Total: 4,
Covered: 3,
Total: 3,
Covered: 2,
Blocks: BlockCoverages{
newBlockCoverage(TypeStmt, 1, 0, 1, 10, 1, 1),
newBlockCoverage(TypeStmt, 2, 0, 2, 10, 1, 0),
Expand All @@ -530,18 +539,18 @@ func TestMerge(t *testing.T) {
},
},
}
opts := []cmp.Option{
cmpopts.IgnoreUnexported(FileCoverage{}),
}
for _, tt := range tests {
if err := tt.c1.Merge(tt.c2); err != nil {
t.Fatal(err)
}
got := tt.c1

opts := []cmp.Option{
cmpopts.IgnoreUnexported(FileCoverage{}),
}

if diff := cmp.Diff(got, tt.want, opts...); diff != "" {
t.Error(diff)
}
t.Run(tt.name, func(t *testing.T) {
if err := tt.c1.Merge(tt.c2); err != nil {
t.Fatal(err)
}
got := tt.c1
if diff := cmp.Diff(got, tt.want, opts...); diff != "" {
t.Error(diff)
}
})
}
}

0 comments on commit 7c4b330

Please sign in to comment.