forked from AlekSi/gocov-xml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgocov-xml.go
189 lines (174 loc) · 5.72 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package gocovxml
import (
"encoding/json"
"encoding/xml"
"fmt"
"go/token"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
"github.com/axw/gocov"
)
// Coverage information
type Coverage struct {
XMLName xml.Name `xml:"coverage"`
LineRate float32 `xml:"line-rate,attr"`
BranchRate float32 `xml:"branch-rate,attr"`
LinesCovered float32 `xml:"lines-covered,attr"`
LinesValid int64 `xml:"lines-valid,attr"`
BranchesCovered int64 `xml:"branches-covered,attr"`
BranchesValid int64 `xml:"branches-valid,attr"`
Complexity float32 `xml:"complexity,attr"`
Version string `xml:"version,attr"`
Timestamp int64 `xml:"timestamp,attr"`
Packages []Package `xml:"packages>package"`
Sources []string `xml:"sources>source"`
}
// Package information
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"`
}
// Class information
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"`
}
// Method information
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"`
Complexity float32 `xml:"complexity,attr"`
Lines []Line `xml:"lines>line"`
LineCount int64 `xml:"line-count,attr"`
LineHits int64 `xml:"line-hits,attr"`
}
// Line information
type Line struct {
Number int `xml:"number,attr"`
Hits int64 `xml:"hits,attr"`
}
func Parse(sourcePath string) error {
err := ParseWithOutput(os.Stdin, sourcePath, os.Stdout)
if err != nil {
return err
}
fmt.Println()
return nil
}
func ParseWithOutput(input io.Reader, sourcePath string, output io.Writer) error {
sources := make([]string, 1)
sources[0] = sourcePath
var r struct{ Packages []gocov.Package }
var totalLines, totalHits int64
err := json.NewDecoder(input).Decode(&r)
if err != nil {
return 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 {
// get the releative path by base path.
fpath, err := filepath.Rel(sourcePath, gFunction.File)
if err != nil {
return err
}
classes := files[fpath]
if classes == nil {
// group functions by "class" (type) in a File
classes = make(map[string]*Class)
files[fpath] = 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: fpath, Methods: []Method{}, Lines: []Line{}}
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 {
return 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))
var funcHits int
for i, s := range gFunction.Statements {
lineno := tokenFile.Line(tokenFile.Pos(s.Start))
line := Line{Number: lineno, Hits: s.Reached}
if int(s.Reached) > 0 {
funcHits++
}
lines[i] = line
class.Lines = append(class.Lines, line)
}
lineRate := float32(funcHits) / float32(len(gFunction.Statements))
class.Methods = append(class.Methods, Method{Name: methodName, Lines: lines, LineRate: lineRate})
class.LineCount += int64(len(gFunction.Statements))
class.LineHits += int64(funcHits)
}
// fill package with "classes"
p := Package{Name: gPackage.Name, Classes: []Class{}}
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)
}
packages[i] = p
totalLines += p.LineCount
totalHits += p.LineHits
}
coverage := Coverage{Sources: sources, Packages: packages, Timestamp: time.Now().UnixNano() / int64(time.Millisecond), LinesCovered: float32(totalHits), LinesValid: int64(totalLines), LineRate: float32(totalHits) / float32(totalLines)}
fmt.Fprintf(output, xml.Header)
fmt.Fprintf(output, "<!DOCTYPE coverage SYSTEM \"http://cobertura.sourceforge.net/xml/coverage-04.dtd\">\n")
encoder := xml.NewEncoder(output)
encoder.Indent("", "\t")
err = encoder.Encode(coverage)
if err != nil {
return err
}
return nil
}