From 06782a9ca3dd7e2c056db2fac92b6e7ffcfe4561 Mon Sep 17 00:00:00 2001 From: k1LoW Date: Fri, 29 Sep 2023 21:01:03 +0900 Subject: [PATCH] Allow to merge no coverages reports --- pkg/coverage/merge.go | 58 ++++++------------- pkg/coverage/merge_test.go | 116 +++++++++++++++++++++++++++++++++++++ 2 files changed, 134 insertions(+), 40 deletions(-) diff --git a/pkg/coverage/merge.go b/pkg/coverage/merge.go index 4f0df0f3..f2dd42ed 100644 --- a/pkg/coverage/merge.go +++ b/pkg/coverage/merge.go @@ -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 { diff --git a/pkg/coverage/merge_test.go b/pkg/coverage/merge_test.go index c94941b4..d65a8fcd 100644 --- a/pkg/coverage/merge_test.go +++ b/pkg/coverage/merge_test.go @@ -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 {