[#5] Add covertest.Cover structure
It has embedded testing.Cover to collect coverage in standard form and sync.Mutex to be thread-safe Signed-off-by: Ekaterina Lebedeva <ekaterina.lebedeva@yadro.com>
This commit is contained in:
parent
8e45fee116
commit
c51343019c
1 changed files with 117 additions and 59 deletions
|
@ -6,32 +6,36 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/nspcc-dev/neo-go/pkg/compiler"
|
"github.com/nspcc-dev/neo-go/pkg/compiler"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
var mu sync.Mutex
|
type Cover struct {
|
||||||
|
sync.Mutex
|
||||||
type coverline struct {
|
testing.Cover
|
||||||
Doc string
|
Opcodes map[string][]int
|
||||||
Opcode int
|
|
||||||
StartLine int
|
|
||||||
StartCol int
|
|
||||||
EndLine int
|
|
||||||
EndCol int
|
|
||||||
Statements int
|
|
||||||
IsCovered bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var cover = &Cover{}
|
||||||
|
|
||||||
// MakeCoverage generates an output file with coverage info in correct format.
|
// MakeCoverage generates an output file with coverage info in correct format.
|
||||||
func (c *ContractInvoker) MakeCoverage(t testing.TB, ctrdi *ContractWithDebugInfo, ctrPath string, fileName string) {
|
func (c *ContractInvoker) MakeCoverage(t testing.TB, ctrdi *ContractWithDebugInfo, substr string, fileName string) {
|
||||||
docs := getDocuments(t, ctrdi.DebugInfo.Documents, ctrPath)
|
cover.Lock()
|
||||||
cov := getSeqPoints(t, ctrdi.DebugInfo, docs)
|
defer cover.Unlock()
|
||||||
for _, iMethod := range c.Methods {
|
if cover.Opcodes == nil {
|
||||||
countInstructions(cov, iMethod.Instructions)
|
cover.Opcodes = make(map[string][]int)
|
||||||
|
cover.Blocks = make(map[string][]testing.CoverBlock)
|
||||||
|
cover.Counters = make(map[string][]uint32)
|
||||||
}
|
}
|
||||||
printToFile(t, cov, fileName)
|
|
||||||
|
docs := getDocuments(t, ctrdi.DebugInfo.Documents, substr)
|
||||||
|
setBlocks(ctrdi.DebugInfo, docs)
|
||||||
|
for _, iMethod := range c.Methods {
|
||||||
|
countInstructions(ctrdi.DebugInfo.Documents, docs, iMethod.Instructions)
|
||||||
|
}
|
||||||
|
printToFile(t, fileName)
|
||||||
}
|
}
|
||||||
|
|
||||||
// getDocuments returns compiler.DebugInfo.Documents indexes which contain specific substring.
|
// getDocuments returns compiler.DebugInfo.Documents indexes which contain specific substring.
|
||||||
|
@ -50,28 +54,30 @@ func getDocuments(t testing.TB, docs []string, substr string) []int {
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
// getSeqPoints accumulates sequence points from every method in compiler.DebugInfo.Methods which belong to specified documents.
|
// setBlocks extracts sequence points for every specific document
|
||||||
func getSeqPoints(t testing.TB, di *compiler.DebugInfo, docs []int) []coverline {
|
// from compiler.DebugInfo and stores them in testing.Cover format
|
||||||
res := make([]coverline, 0, 10)
|
func setBlocks(di *compiler.DebugInfo, docs []int) {
|
||||||
|
|
||||||
for _, method := range di.Methods {
|
for _, method := range di.Methods {
|
||||||
maxLine := method.Range.End
|
|
||||||
for _, seqPoint := range method.SeqPoints {
|
for _, seqPoint := range method.SeqPoints {
|
||||||
if isValidDocument(seqPoint.Document, docs) && seqPoint.Opcode < int(maxLine) {
|
statements := seqPoint.EndLine - seqPoint.StartLine + 1
|
||||||
res = append(res, coverline{
|
docStr := di.Documents[seqPoint.Document]
|
||||||
Doc: di.Documents[seqPoint.Document],
|
if isValidDocument(seqPoint.Document, docs) {
|
||||||
Opcode: seqPoint.Opcode,
|
cover.Blocks[docStr] = append(cover.Blocks[docStr], testing.CoverBlock{
|
||||||
StartLine: seqPoint.StartLine,
|
Line0: uint32(seqPoint.StartLine),
|
||||||
StartCol: seqPoint.StartCol,
|
Col0: uint16(seqPoint.StartCol),
|
||||||
EndLine: seqPoint.EndLine,
|
Line1: uint32(seqPoint.EndLine),
|
||||||
EndCol: seqPoint.EndCol,
|
Col1: uint16(seqPoint.EndCol),
|
||||||
Statements: 1,
|
Stmts: uint16(statements),
|
||||||
IsCovered: false,
|
|
||||||
})
|
})
|
||||||
|
cover.Opcodes[docStr] = append(cover.Opcodes[docStr], seqPoint.Opcode)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return res
|
for doc, seqPoints := range cover.Blocks {
|
||||||
|
if hasNoCounters(doc) {
|
||||||
|
cover.Counters[doc] = make([]uint32, len(seqPoints))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// isValidDocument checks if document index exists in an array of document indexes.
|
// isValidDocument checks if document index exists in an array of document indexes.
|
||||||
|
@ -84,44 +90,96 @@ func isValidDocument(iDocToCheck int, docs []int) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func hasNoBlocks(doc string) bool {
|
||||||
|
if _, exists := cover.Blocks[doc]; exists {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func hasNoCounters(doc string) bool {
|
||||||
|
if _, exists := cover.Counters[doc]; exists {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
// countInstructions finds for every instruction a corresponding sequence point and sets IsCovered flag to true.
|
// countInstructions finds for every instruction a corresponding sequence point and sets IsCovered flag to true.
|
||||||
func countInstructions(cov []coverline, codes []InstrHash) {
|
func countInstructions(diDocs []string, validDocs []int, instrs []InstrHash) {
|
||||||
for i := range cov {
|
sValidDocs := getValidStrDocs(diDocs, validDocs)
|
||||||
for _, code := range codes {
|
for doc, ops := range cover.Opcodes {
|
||||||
if code.Offset == cov[i].Opcode {
|
if isValidStrDoc(doc, sValidDocs) {
|
||||||
cov[i].IsCovered = true
|
cover.Counters[doc] = getNewCounts(doc, ops, instrs)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// printToFile writes coverlines to file.
|
func getNewCounts(doc string, ops []int, instrs []InstrHash) []uint32 {
|
||||||
func printToFile(t testing.TB, cov []coverline, name string) {
|
counts := cover.Counters[doc]
|
||||||
mu.Lock()
|
for _, instr := range instrs {
|
||||||
defer mu.Unlock()
|
for i, op := range ops {
|
||||||
|
if instr.Offset == op {
|
||||||
|
counts[i]++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return counts
|
||||||
|
}
|
||||||
|
|
||||||
f, err := os.OpenFile(name, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
|
func getValidStrDocs(diDocs []string, validDocs []int) []string {
|
||||||
|
res := make([]string, len(validDocs))
|
||||||
|
for i, iDoc := range validDocs {
|
||||||
|
res[i] = diDocs[iDoc]
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
func isValidStrDoc(docToCheck string, docs []string) bool {
|
||||||
|
for _, doc := range docs {
|
||||||
|
if doc == docToCheck {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// printToFile writes coverage info to file.
|
||||||
|
func printToFile(t testing.TB, name string) {
|
||||||
|
fileName := name
|
||||||
|
if fileName == "" {
|
||||||
|
// if no specific file name was provided, the output file
|
||||||
|
// will have formatted timestamp with no spaces as its name
|
||||||
|
fileName = fmt.Sprintf("%s.out", time.Now().Format(time.RFC3339Nano))
|
||||||
|
}
|
||||||
|
|
||||||
|
f, err := os.OpenFile(fileName, os.O_TRUNC|os.O_WRONLY|os.O_CREATE, 0600)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
||||||
fi, err := os.Stat(name)
|
_, err = f.WriteString("mode: set\n")
|
||||||
require.NoError(t, err)
|
|
||||||
firstToWrite := ""
|
|
||||||
if fi.Size() == 0 {
|
|
||||||
firstToWrite = "mode: set\n"
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = f.WriteString(firstToWrite)
|
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
for _, info := range cov {
|
var count uint32
|
||||||
covered := 0
|
for name, counts := range cover.Counters {
|
||||||
if info.IsCovered {
|
blocks := cover.Blocks[name]
|
||||||
covered++
|
for i := range counts {
|
||||||
|
stmts := int64(blocks[i].Stmts)
|
||||||
|
count = counts[i]
|
||||||
|
|
||||||
|
// mode: set
|
||||||
|
if count != 0 {
|
||||||
|
count = 1
|
||||||
|
}
|
||||||
|
if f != nil && stmts == 1 {
|
||||||
|
_, err := fmt.Fprintf(f, "%s:%d.%d,%d.%d %d %d\n", name,
|
||||||
|
blocks[i].Line0, blocks[i].Col0,
|
||||||
|
blocks[i].Line1, blocks[i].Col1,
|
||||||
|
stmts,
|
||||||
|
count)
|
||||||
|
require.NoError(t, err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
line := fmt.Sprintf("%s:%d.%d,%d.%d %d %d\n", info.Doc, info.StartLine, info.StartCol, info.EndLine, info.EndCol, info.Statements, covered)
|
|
||||||
_, err = f.WriteString(line)
|
|
||||||
require.NoError(t, err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue