forked from imsky/cobertool
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
145 lines (116 loc) · 2.89 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"encoding/xml"
"flag"
"fmt"
"os"
"strconv"
)
var (
BuildTime string
BuildSHA string
)
type Line struct {
Hit bool
Number uint64
}
type SourceFile struct {
Lines []Line
}
func GetLines() []Line {
return make([]Line, 0, 100)
}
func ComputeTotalCoverage(sourceFiles map[string][]SourceFile) float64 {
var totalCoverage float64
totalFiles := 0
//todo: validate that each source file list of lines is same length
for _, fileSets := range sourceFiles {
fileLineHitMap := make(map[uint64]bool)
for _, sourceFile := range fileSets {
for _, line := range sourceFile.Lines {
fileLineHitMap[line.Number] = fileLineHitMap[line.Number] || line.Hit
}
}
totalLines := len(fileLineHitMap)
hitLines := 0
for _, hit := range fileLineHitMap {
if hit {
hitLines++
}
}
if totalLines > 0 {
totalFiles++
totalCoverage += float64(hitLines) / float64(totalLines)
}
}
return (totalCoverage / float64(totalFiles)) * 100
}
func Run(reports []string) {
if len(reports) == 0 {
flag.Usage()
return
}
//todo: rework this, it's really a map of source file names to line sets
sourceFiles := make(map[string][]SourceFile)
for _, reportFileName := range reports {
file, err := os.Open(reportFileName)
if err != nil {
panic("Could not open file " + reportFileName)
}
decoder := xml.NewDecoder(file)
var sourceFilename string
var lines []Line = GetLines()
for {
t, _ := decoder.Token()
if t == nil {
break
}
switch se := t.(type) {
case xml.StartElement:
switch se.Name.Local {
case "class":
for _, attr := range se.Attr {
if attr.Name.Local == "filename" {
sourceFilename = attr.Value
if _, exists := sourceFiles[sourceFilename]; !exists {
sourceFiles[sourceFilename] = make([]SourceFile, 0, len(reports))
}
}
}
case "line":
//todo: could move the file line hit map calculation here if the hit count is irrelevant
//todo: check that class file name is set
var line Line
for _, attr := range se.Attr {
if attr.Name.Local == "number" {
line.Number, _ = strconv.ParseUint(attr.Value, 10, 64)
} else if attr.Name.Local == "hits" {
line.Hit = attr.Value != "0"
}
}
lines = append(lines, line)
}
case xml.EndElement:
switch se.Name.Local {
case "class":
if len(lines) > 0 {
class := SourceFile{Lines: lines}
sourceFiles[sourceFilename] = append(sourceFiles[sourceFilename], class)
}
lines = GetLines()
}
}
}
file.Close()
}
fmt.Printf("%.2f\n", ComputeTotalCoverage(sourceFiles))
}
func main() {
flag.Usage = func() {
fmt.Println("Cobertool", BuildSHA, BuildTime)
fmt.Println("Usage: cobertool <file> ...")
fmt.Println("\nCobertool uses one or more Cobertura code coverage reports to compute code coverage statistics.")
}
flag.Parse()
Run(flag.Args())
}