forked from AlekSi/gocov-xml
-
Notifications
You must be signed in to change notification settings - Fork 2
/
gocov-xml.go
163 lines (143 loc) · 4.89 KB
/
gocov-xml.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package main
import (
"encoding/json"
"encoding/xml"
"fmt"
"go/token"
"io/ioutil"
"os"
"strings"
"time"
"github.com/axw/gocov"
)
type Coverage struct {
XMLName xml.Name `xml:"coverage"`
LineRate float32 `xml:"line-rate,attr"`
BranchRate float32 `xml:"branch-rate,attr"`
Version string `xml:"version,attr"`
Timestamp int64 `xml:"timestamp,attr"`
Packages []Package `xml:"packages>package"`
}
type Package struct {
Name string `xml:"name,attr"`
LineRate float32 `xml:"line-rate,attr"`
BranchRate float32 `xml:"branch-rate,attr"`
Complexity float32 `xml:"complexity,attr"`
Classes []Class `xml:"classes>class"`
LineCount int64 `xml:"line-count,attr"`
LineHits int64 `xml:"line-hits,attr"`
}
type Class struct {
Name string `xml:"name,attr"`
Filename string `xml:"filename,attr"`
LineRate float32 `xml:"line-rate,attr"`
BranchRate float32 `xml:"branch-rate,attr"`
Complexity float32 `xml:"complexity,attr"`
Methods []Method `xml:"methods>method"`
Lines []Line `xml:"lines>line"`
LineCount int64 `xml:"line-count,attr"`
LineHits int64 `xml:"line-hits,attr"`
}
type Method struct {
Name string `xml:"name,attr"`
Signature string `xml:"signature,attr"`
LineRate float32 `xml:"line-rate,attr"`
BranchRate float32 `xml:"branch-rate,attr"`
Lines []Line `xml:"lines>line"`
LineCount int64 `xml:"line-count,attr"`
LineHits int64 `xml:"line-hits,attr"`
}
type Line struct {
Number int `xml:"number,attr"`
Hits int64 `xml:"hits,attr"`
}
func main() {
var r struct{ Packages []gocov.Package }
var total_lines, total_hits int64 = 0, 0
err := json.NewDecoder(os.Stdin).Decode(&r)
if err != nil {
panic(err)
}
fset := token.NewFileSet()
tokenFiles := make(map[string]*token.File)
// convert packages
packages := make([]Package, len(r.Packages))
for i, gPackage := range r.Packages {
// group functions by filename and "class" (type)
files := make(map[string]map[string]*Class)
for _, gFunction := range gPackage.Functions {
classes := files[gFunction.File]
if classes == nil {
// group functions by "class" (type) in a File
classes = make(map[string]*Class)
files[gFunction.File] = classes
}
s := strings.Split("-."+gFunction.Name, ".") // className is "-" for package-level functions
className, methodName := s[len(s)-2], s[len(s)-1]
class := classes[className]
if class == nil {
class = &Class{Name: className, Filename: gFunction.File, Methods: []Method{}, Lines: []Line{}, LineCount: 0, LineHits: 0}
classes[className] = class
}
// from github.com/axw/gocov /gocov/annotate.go#printFunctionSource
// Load the file for line information. Probably overkill, maybe
// just compute the lines from offsets in here.
setContent := false
tokenFile := tokenFiles[gFunction.File]
if tokenFile == nil {
info, err := os.Stat(gFunction.File)
if err != nil {
panic(err)
}
tokenFile = fset.AddFile(gFunction.File, fset.Base(), int(info.Size()))
setContent = true
}
tokenData, err := ioutil.ReadFile(gFunction.File)
if err != nil {
panic(err)
}
if setContent {
// This processes the content and records line number info.
tokenFile.SetLinesForContent(tokenData)
}
// convert statements to lines
lines := make([]Line, len(gFunction.Statements))
func_hits := 0
for i, s := range gFunction.Statements {
lineno := tokenFile.Line(tokenFile.Pos(s.Start))
line := Line{Number: lineno, Hits: s.Reached}
func_hits += int(s.Reached)
lines[i] = line
class.Lines = append(class.Lines, line)
}
line_rate := float32(func_hits) / float32(len(gFunction.Statements))
class.Methods = append(class.Methods, Method{Name: methodName, Lines: lines, LineRate: line_rate})
class.LineCount += int64(len(gFunction.Statements))
class.LineHits += int64(func_hits)
}
// fill package with "classes"
p := Package{Name: gPackage.Name, Classes: []Class{}, LineCount: 0, LineHits: 0}
for _, classes := range files {
for _, class := range classes {
p.LineCount += class.LineCount
p.LineHits += class.LineHits
class.LineRate = float32(class.LineHits) / float32(class.LineCount)
p.Classes = append(p.Classes, *class)
}
p.LineRate = float32(p.LineHits) / float32(p.LineCount)
total_lines += p.LineCount
total_hits += p.LineHits
}
packages[i] = p
}
coverage := Coverage{Packages: packages, Timestamp: time.Now().UnixNano() / int64(time.Millisecond), LineRate: float32(total_hits) / float32(total_lines)}
fmt.Printf(xml.Header)
fmt.Printf("<!DOCTYPE coverage SYSTEM \"http://cobertura.sourceforge.net/xml/coverage-03.dtd\">\n")
encoder := xml.NewEncoder(os.Stdout)
encoder.Indent("", "\t")
err = encoder.Encode(coverage)
if err != nil {
panic(err)
}
fmt.Println()
}