Golang coverage summary updated to version 2.0.1 (#5346)

This commit is contained in:
Catena cyber 2021-03-17 14:53:19 +01:00 committed by GitHub
parent d3d1d4c190
commit e71e49d721
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 46 additions and 19 deletions

View File

@ -24,10 +24,18 @@ type CoverageTotals struct {
Functions CoverageTotal `json:"functions,omitempty"` Functions CoverageTotal `json:"functions,omitempty"`
Lines CoverageTotal `json:"lines,omitempty"` Lines CoverageTotal `json:"lines,omitempty"`
Regions CoverageTotal `json:"regions,omitempty"` Regions CoverageTotal `json:"regions,omitempty"`
Instantiations CoverageTotal `json:"instantiations,omitempty"`
Branches CoverageTotal `json:"branches,omitempty"`
}
type CoverageFile struct {
Summary CoverageTotals `json:"summary,omitempty"`
Filename string `json:"filename,omitempty"`
} }
type CoverageData struct { type CoverageData struct {
Totals CoverageTotals `json:"totals,omitempty"` Totals CoverageTotals `json:"totals,omitempty"`
Files []CoverageFile `json:"files,omitempty"`
} }
type PositionInterval struct { type PositionInterval struct {
@ -52,6 +60,12 @@ func isFunctionCovered(s token.Position, e token.Position, blocks []cover.Profil
return false return false
} }
func computePercent(s *CoverageTotals) {
s.Regions.Percent = float64(100*s.Regions.Covered) / float64(s.Regions.Count)
s.Lines.Percent = float64(100*s.Lines.Covered) / float64(s.Lines.Count)
s.Functions.Percent = float64(100*s.Functions.Covered) / float64(s.Functions.Count)
}
func main() { func main() {
flag.Parse() flag.Parse()
@ -64,7 +78,7 @@ func main() {
} }
r := CoverageSummary{} r := CoverageSummary{}
r.Type = "oss-fuzz.go.coverage.json.export" r.Type = "oss-fuzz.go.coverage.json.export"
r.Version = "1.0.0" r.Version = "2.0.1"
r.Data = make([]CoverageData, 1) r.Data = make([]CoverageData, 1)
for _, p := range profiles { for _, p := range profiles {
fset := token.NewFileSet() // positions are relative to fset fset := token.NewFileSet() // positions are relative to fset
@ -72,49 +86,62 @@ func main() {
if err != nil { if err != nil {
panic(err) panic(err)
} }
fileCov := CoverageFile{}
fileCov.Filename = p.FileName
ast.Inspect(f, func(n ast.Node) bool { ast.Inspect(f, func(n ast.Node) bool {
switch x := n.(type) { switch x := n.(type) {
case *ast.FuncLit: case *ast.FuncLit:
startf := fset.Position(x.Pos()) startf := fset.Position(x.Pos())
endf := fset.Position(x.End()) endf := fset.Position(x.End())
r.Data[0].Totals.Functions.Count++ fileCov.Summary.Functions.Count++
if isFunctionCovered(startf, endf, p.Blocks) { if isFunctionCovered(startf, endf, p.Blocks) {
r.Data[0].Totals.Functions.Covered++ fileCov.Summary.Functions.Covered++
} else { } else {
r.Data[0].Totals.Functions.Uncovered++ fileCov.Summary.Functions.Uncovered++
} }
case *ast.FuncDecl: case *ast.FuncDecl:
startf := fset.Position(x.Pos()) startf := fset.Position(x.Pos())
endf := fset.Position(x.End()) endf := fset.Position(x.End())
r.Data[0].Totals.Functions.Count++ fileCov.Summary.Functions.Count++
if isFunctionCovered(startf, endf, p.Blocks) { if isFunctionCovered(startf, endf, p.Blocks) {
r.Data[0].Totals.Functions.Covered++ fileCov.Summary.Functions.Covered++
} else { } else {
r.Data[0].Totals.Functions.Uncovered++ fileCov.Summary.Functions.Uncovered++
} }
} }
return true return true
}) })
for _, b := range p.Blocks { for _, b := range p.Blocks {
r.Data[0].Totals.Regions.Count++ fileCov.Summary.Regions.Count++
if b.Count > 0 { if b.Count > 0 {
r.Data[0].Totals.Regions.Covered++ fileCov.Summary.Regions.Covered++
} else { } else {
r.Data[0].Totals.Regions.Uncovered++ fileCov.Summary.Regions.Uncovered++
} }
r.Data[0].Totals.Lines.Count += b.NumStmt fileCov.Summary.Lines.Count += b.NumStmt
if b.Count > 0 { if b.Count > 0 {
r.Data[0].Totals.Lines.Covered += b.NumStmt fileCov.Summary.Lines.Covered += b.NumStmt
} else { } else {
r.Data[0].Totals.Lines.Uncovered += b.NumStmt fileCov.Summary.Lines.Uncovered += b.NumStmt
} }
} }
r.Data[0].Totals.Regions.Count += fileCov.Summary.Regions.Count
r.Data[0].Totals.Regions.Covered += fileCov.Summary.Regions.Covered
r.Data[0].Totals.Regions.Uncovered += fileCov.Summary.Regions.Uncovered
r.Data[0].Totals.Lines.Count += fileCov.Summary.Lines.Count
r.Data[0].Totals.Lines.Covered += fileCov.Summary.Lines.Covered
r.Data[0].Totals.Lines.Uncovered += fileCov.Summary.Lines.Uncovered
r.Data[0].Totals.Functions.Count += fileCov.Summary.Functions.Count
r.Data[0].Totals.Functions.Covered += fileCov.Summary.Functions.Covered
r.Data[0].Totals.Functions.Uncovered += fileCov.Summary.Functions.Uncovered
computePercent(&fileCov.Summary)
r.Data[0].Files = append(r.Data[0].Files, fileCov)
} }
r.Data[0].Totals.Regions.Percent = float64(100*r.Data[0].Totals.Regions.Covered) / float64(r.Data[0].Totals.Regions.Count)
r.Data[0].Totals.Lines.Percent = float64(100*r.Data[0].Totals.Lines.Covered) / float64(r.Data[0].Totals.Lines.Count) computePercent(&r.Data[0].Totals)
r.Data[0].Totals.Functions.Percent = float64(100*r.Data[0].Totals.Functions.Covered) / float64(r.Data[0].Totals.Functions.Count)
o, _ := json.Marshal(r) o, _ := json.Marshal(r)
fmt.Printf(string(o)) fmt.Printf(string(o))
} }