Skip to content

Commit

Permalink
neotest: exclude RET statements from coverage
Browse files Browse the repository at this point in the history
The sequence point bounds of all RET statements include the whole method
body, for example:
https://github.com/nspcc-dev/neo-go/blob/86ed214e8a53859fd85482370be20eb613c61419/pkg/compiler/codegen.go#L542-L543

Such behaviour breaks the resulting coverage report. A part of #3559.

Signed-off-by: Anna Shaleva <[email protected]>
  • Loading branch information
AnnaShaleva committed Oct 16, 2024
1 parent 86ed214 commit de4b133
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions pkg/neotest/coverage.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,14 @@ var (
)

type scriptRawCoverage struct {
debugInfo *compiler.DebugInfo
offsetsVisited []int
debugInfo *compiler.DebugInfo
// methodStartOffsets holds the set of script offsets that correspond to method
// declaration start.
methodStartOffsets map[uint16]struct{}
// methodStartOffsets holds the set of script offsets that correspond to method
// declaration end.
methodEndOffsets map[uint16]struct{}
offsetsVisited []int
}

type coverBlock struct {
Expand Down Expand Up @@ -174,6 +180,12 @@ func processCover() map[documentName][]*coverBlock {
documentSeqPoints := documentSeqPoints(di, documentName)

for _, point := range documentSeqPoints {
// Don't track RET statements since their bounds overlap the whole function
// which break the resulting coverage report.
_, isRET := scriptRawCoverage.methodEndOffsets[uint16(point.Opcode)]
if isRET {
continue

Check warning on line 187 in pkg/neotest/coverage.go

View check run for this annotation

Codecov / codecov/patch

pkg/neotest/coverage.go#L185-L187

Added lines #L185 - L187 were not covered by tests
}
b := coverBlock{
startLine: uint(point.StartLine),
startCol: uint(point.StartCol),
Expand All @@ -193,6 +205,10 @@ func processCover() map[documentName][]*coverBlock {
for _, offset := range scriptRawCoverage.offsetsVisited {
for _, point := range documentSeqPoints {
if point.Opcode == offset {
_, ok := mappedBlocks[point.Opcode]
if !ok {
continue

Check warning on line 210 in pkg/neotest/coverage.go

View check run for this annotation

Codecov / codecov/patch

pkg/neotest/coverage.go#L208-L210

Added lines #L208 - L210 were not covered by tests
}
mappedBlocks[point.Opcode].counts++
}
}
Expand Down Expand Up @@ -239,6 +255,16 @@ func addScriptToCoverage(c *Contract) {
coverageLock.Lock()
defer coverageLock.Unlock()
if _, ok := rawCoverage[c.Hash]; !ok {
rawCoverage[c.Hash] = &scriptRawCoverage{debugInfo: c.DebugInfo}
startOffsets := make(map[uint16]struct{})
endOffsets := make(map[uint16]struct{})
for _, m := range c.DebugInfo.Methods {
startOffsets[m.Range.Start] = struct{}{}
endOffsets[m.Range.End] = struct{}{}

Check warning on line 262 in pkg/neotest/coverage.go

View check run for this annotation

Codecov / codecov/patch

pkg/neotest/coverage.go#L258-L262

Added lines #L258 - L262 were not covered by tests
}
rawCoverage[c.Hash] = &scriptRawCoverage{
debugInfo: c.DebugInfo,
methodStartOffsets: startOffsets,
methodEndOffsets: endOffsets,

Check warning on line 267 in pkg/neotest/coverage.go

View check run for this annotation

Codecov / codecov/patch

pkg/neotest/coverage.go#L264-L267

Added lines #L264 - L267 were not covered by tests
}
}
}

0 comments on commit de4b133

Please sign in to comment.