forked from thought-machine/please
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxml_coverage.go
More file actions
215 lines (186 loc) · 6.47 KB
/
Copy pathxml_coverage.go
File metadata and controls
215 lines (186 loc) · 6.47 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Code for parsing XML coverage output (eg. Java or Python).
package test
import (
"encoding/xml"
"github.com/thought-machine/please/src/cli"
"github.com/thought-machine/please/src/core"
"math"
"path"
"strings"
"time"
)
func parseXMLCoverageResults(target *core.BuildTarget, coverage *core.TestCoverage, data []byte) error {
xcoverage := xmlCoverage{}
if err := xml.Unmarshal(data, &xcoverage); err != nil {
return err
}
for _, pkg := range xcoverage.Packages.Package {
for _, cls := range pkg.Classes.Class {
filename := strings.TrimPrefix(cls.Filename, core.RepoRoot)
// There can be multiple classes per file so we must merge here, not overwrite.
coverage.Files[filename] = core.MergeCoverageLines(coverage.Files[filename], parseXMLLines(cls.Lines.Line))
}
}
coverage.Tests[target.Label] = coverage.Files
return nil
}
func parseXMLLines(lines []xmlCoverageLine) []core.LineCoverage {
ret := []core.LineCoverage{}
for _, line := range lines {
for i := len(ret) + 1; i < line.Number; i++ {
ret = append(ret, core.NotExecutable)
}
if line.Hits > 0 {
ret = append(ret, core.Covered)
} else {
ret = append(ret, core.Uncovered)
}
}
return ret
}
// Covert the Coverage result to XML bytes, ready to be write to file
func coverageResultToXML(sources []core.BuildLabel, coverage core.TestCoverage) []byte {
linesValid := 0
linesCovered := 0
validFiles := coverage.OrderedFiles()
// get the string representative of sources
sourcesAsStr := make([]string, len(sources))
for i, source := range sources {
sourcesAsStr[i] = path.Join(core.RepoRoot, source.PackageName)
}
// Get the list of packages for <package> tag in the coverage xml file
var packages []pkg
for label, coverage := range coverage.Tests {
packageName := label.String()
// Get the list of classes for <class> tag in the coverage xml file
var classes []class
classLineRateTotal := 0.0
for className, lineCover := range coverage {
// Do not include files in coverage report if its not valid
if !cli.ContainsString(className, validFiles) {
continue
}
name := strings.Replace(className, label.PackageName+"/", "", -1)
lines, covered, total := getLineCoverageInfo(lineCover)
classLineRate := float64(covered) / float64(total)
cls := class{Name: name, Filename: name,
Lines: lines, LineRate: formatFloatPrecision(classLineRate, 4)}
classes = append(classes, cls)
classLineRateTotal += classLineRate
linesValid += total
linesCovered += covered
}
pkgLineRate := float64(classLineRateTotal) / float64(len(classes))
if len(classes) != 0 {
pkg := pkg{Name: packageName, Classes: classes, LineRate: formatFloatPrecision(pkgLineRate, 4)}
packages = append(packages, pkg)
}
}
topLevelLineRate := float64(linesCovered) / float64(linesValid)
// Create the coverage object based on the data collected
coverageObj := coverageType{Packages: packages, LineRate: formatFloatPrecision(topLevelLineRate, 4),
LinesCovered: int(linesCovered), LinesValid: int(linesValid),
Timestamp: int(time.Now().UnixNano()) / int(time.Millisecond),
Sources: sourcesAsStr}
// Serialise struct to xml bytes
xmlBytes, err := xml.MarshalIndent(coverageObj, "", " ")
if err != nil {
log.Fatalf("Failed to parse to xml: %s", err)
}
covReport := []byte(xml.Header + string(xmlBytes))
return covReport
}
// Get the line coverage info, returns: list of lines covered, num of covered lines, and total valid lines
func getLineCoverageInfo(lineCover []core.LineCoverage) ([]line, int, int) {
var lines []line
covered := 0
total := 0
for index, status := range lineCover {
if status == core.Covered {
line := line{Hits: 1, Number: index}
lines = append(lines, line)
covered++
total++
} else if status == core.Uncovered {
line := line{Hits: 0, Number: index}
lines = append(lines, line)
total++
}
}
return lines, covered, total
}
// format the float64 numbers to a specific precision
func formatFloatPrecision(val float64, precision int) float64 {
unit := math.Pow10(precision)
newFloat := math.Round(val*unit) / unit
return float64(newFloat)
}
// Note that this is based off coverage.py's format, which is originally a Java format
// so some of the structures are a little awkward (eg. 'classes' actually refer to Python modules, not classes).
type xmlCoverage struct {
Packages struct {
Package []struct {
Classes struct {
Class []struct {
LineRate float64 `xml:"line-rate,attr"`
Filename string `xml:"filename,attr"`
Name string `xml:"name,attr"`
Lines struct {
Line []xmlCoverageLine `xml:"line"`
} `xml:"lines"`
} `xml:"class"`
} `xml:"classes"`
} `xml:"package"`
} `xml:"packages"`
}
type xmlCoverageLine struct {
Hits int `xml:"hits,attr"`
Number int `xml:"number,attr"`
}
// Coverage struct for writing to xml file
type coverageType struct {
XMLName xml.Name `xml:"coverage"`
LineRate float64 `xml:"line-rate,attr"`
BranchRate float64 `xml:"branch-rate,attr"`
LinesCovered int `xml:"lines-covered,attr"`
LinesValid int `xml:"lines-valid,attr"`
BranchesCovered int `xml:"branches-covered,attr"`
BranchesValid int `xml:"branches-valid,attr"`
Complexity float64 `xml:"complexity,attr"`
Version string `xml:"version,attr"`
Timestamp int `xml:"timestamp,attr"`
Sources []string `xml:"sources>source"`
Packages []pkg `xml:"packages>package"`
}
type pkg struct {
Name string `xml:"name,attr"`
LineRate float64 `xml:"line-rate,attr"`
BranchRate float64 `xml:"branch-rate,attr"`
Complexity float64 `xml:"complexity,attr"`
Classes []class `xml:"classes>class"`
LineCount int `xml:"line-count,attr"`
LineHits int `xml:"line-hits,attr"`
}
type class struct {
Name string `xml:"name,attr"`
Filename string `xml:"filename,attr"`
LineRate float64 `xml:"line-rate,attr"`
BranchRate float64 `xml:"branch-rate,attr"`
Complexity float64 `xml:"complexity,attr"`
Methods []method `xml:"methods>method"`
Lines []line `xml:"lines>line"`
}
type method struct {
Name string `xml:"name,attr"`
Signature string `xml:"signature,attr"`
LineRate float64 `xml:"line-rate,attr"`
BranchRate float64 `xml:"branch-rate,attr"`
Complexity float64 `xml:"complexity,attr"`
Lines []line `xml:"lines>line"`
LineCount int `xml:"line-count,attr"`
LineHits int `xml:"line-hits,attr"`
}
type line struct {
Number int `xml:"number,attr"`
Hits int `xml:"hits,attr"`
}