Skip to content

Commit

Permalink
Merge pull request #274 from k1LoW/merge-nonblock-report
Browse files Browse the repository at this point in the history
Allow to merge no coverages reports
  • Loading branch information
k1LoW authored Sep 29, 2023
2 parents 0e54fd6 + 06782a9 commit 9b7705a
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 40 deletions.
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ var rootCmd = &cobra.Command{
log.Printf("%s: %v %s", s, err, string(b))
continue
}
// Select latest report
if rPrev == nil || rPrev.Timestamp.UnixNano() < rt.Timestamp.UnixNano() {
rPrev = rt
}
Expand Down
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 9b7705a

Please sign in to comment.