Skip to content

Commit

Permalink
Allow to merge no coverages reports
Browse files Browse the repository at this point in the history
  • Loading branch information
k1LoW committed Sep 29, 2023
1 parent 38bd104 commit 06782a9
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 40 deletions.
58 changes: 18 additions & 40 deletions pkg/coverage/merge.go
Original file line number Diff line number Diff line change
@@ -1,49 +1,27 @@
package coverage

import (
"errors"
)

func (c *Coverage) Merge(c2 *Coverage) error {
{
deleted := true
for _, f := range c.Files {
if len(f.Blocks) > 0 {
deleted = false
}
}
if len(c.Files) > 0 && deleted {
return errors.New("can not merge: BlockCoverages are already deleted.")
}
if c2 == nil {
c2 = &Coverage{}
}
{
deleted := true
for _, f := range c2.Files {
if len(f.Blocks) > 0 {
deleted = false
}
fc, err := c.Files.FindByFile(f.File)
if err == nil {
switch {
case fc.Covered > 0 && f.Covered == 0:
// nothing to do
case f.Covered > 0 && fc.Covered == 0:
fc.Blocks = f.Blocks
default:
fc.Blocks = append(fc.Blocks, f.Blocks...)
}
} else {
c.Files = append(c.Files, f)
}
}
if len(c2.Files) > 0 && deleted {
return errors.New("can not merge: BlockCoverages are already deleted.")
}
}
if c.Type != TypeLOC || c2.Type != TypeLOC {
// Type
switch {
case c.Type == "":
c.Type = c2.Type
case c2.Type == "":
case c.Type != TypeLOC || c2.Type != TypeLOC:
c.Type = TypeMerged
}

// Files
for _, fc2 := range c2.Files {
fc, err := c.Files.FindByFile(fc2.File)
if err == nil {
fc.Blocks = append(fc.Blocks, fc2.Blocks...)
} else {
c.Files = append(c.Files, fc2)
}
}
// Recalculate
total := 0
covered := 0
for _, f := range c.Files {
Expand Down
116 changes: 116 additions & 0 deletions pkg/coverage/merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,122 @@ func TestMerge(t *testing.T) {
},
},
},
{
&Coverage{
Type: TypeLOC,
Files: FileCoverages{
&FileCoverage{
File: "file_a.go",
Blocks: BlockCoverages{
newBlockCoverage(TypeLOC, 1, -1, 1, -1, -1, 1),
newBlockCoverage(TypeLOC, 2, -1, 2, -1, -1, 0),
newBlockCoverage(TypeLOC, 3, -1, 3, -1, -1, 1),
},
},
&FileCoverage{
File: "file_b.go",
Blocks: BlockCoverages{
newBlockCoverage(TypeLOC, 1, -1, 1, -1, -1, 0),
newBlockCoverage(TypeLOC, 2, -1, 2, -1, -1, 1),
newBlockCoverage(TypeLOC, 3, -1, 3, -1, -1, 1),
},
},
},
},
nil,
&Coverage{
Type: TypeLOC,
Total: 6,
Covered: 4,
Files: FileCoverages{
&FileCoverage{
File: "file_a.go",
Total: 3,
Covered: 2,
Blocks: BlockCoverages{
newBlockCoverage(TypeLOC, 1, -1, 1, -1, -1, 1),
newBlockCoverage(TypeLOC, 2, -1, 2, -1, -1, 0),
newBlockCoverage(TypeLOC, 3, -1, 3, -1, -1, 1),
},
},
&FileCoverage{
File: "file_b.go",
Total: 3,
Covered: 2,
Blocks: BlockCoverages{
newBlockCoverage(TypeLOC, 1, -1, 1, -1, -1, 0),
newBlockCoverage(TypeLOC, 2, -1, 2, -1, -1, 1),
newBlockCoverage(TypeLOC, 3, -1, 3, -1, -1, 1),
},
},
},
},
},
{
&Coverage{
Type: TypeLOC,
Files: FileCoverages{
&FileCoverage{
File: "file_a.go",
Blocks: BlockCoverages{
newBlockCoverage(TypeLOC, 1, -1, 1, -1, -1, 1),
newBlockCoverage(TypeLOC, 2, -1, 2, -1, -1, 0),
newBlockCoverage(TypeLOC, 3, -1, 3, -1, -1, 1),
},
},
&FileCoverage{
File: "file_b.go",
Blocks: BlockCoverages{
newBlockCoverage(TypeLOC, 1, -1, 1, -1, -1, 0),
newBlockCoverage(TypeLOC, 2, -1, 2, -1, -1, 1),
newBlockCoverage(TypeLOC, 3, -1, 3, -1, -1, 1),
},
},
},
},
&Coverage{
Type: TypeLOC,
Files: FileCoverages{
&FileCoverage{
File: "file_c.go",
Total: 0,
Covered: 0,
},
},
},
&Coverage{
Type: TypeLOC,
Total: 6,
Covered: 4,
Files: FileCoverages{
&FileCoverage{
File: "file_a.go",
Total: 3,
Covered: 2,
Blocks: BlockCoverages{
newBlockCoverage(TypeLOC, 1, -1, 1, -1, -1, 1),
newBlockCoverage(TypeLOC, 2, -1, 2, -1, -1, 0),
newBlockCoverage(TypeLOC, 3, -1, 3, -1, -1, 1),
},
},
&FileCoverage{
File: "file_b.go",
Total: 3,
Covered: 2,
Blocks: BlockCoverages{
newBlockCoverage(TypeLOC, 1, -1, 1, -1, -1, 0),
newBlockCoverage(TypeLOC, 2, -1, 2, -1, -1, 1),
newBlockCoverage(TypeLOC, 3, -1, 3, -1, -1, 1),
},
},
&FileCoverage{
File: "file_c.go",
Total: 0,
Covered: 0,
},
},
},
},
}
for _, tt := range tests {
if err := tt.c1.Merge(tt.c2); err != nil {
Expand Down

0 comments on commit 06782a9

Please sign in to comment.