/
go_coverage.go
46 lines (40 loc) · 1.21 KB
/
go_coverage.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
// Code for parsing Go's coverage output.
//
// Go comes with a built-in coverage tool and a package to parse its output format. <3
// Its format is actually rather richer than ours and can handle sub-line coverage etc.
// We may look into taking more advantage of that later...
package test
import "bytes"
import "golang.org/x/tools/cover"
import "core"
func looksLikeGoCoverageResults(results []byte) bool {
return bytes.HasPrefix(results, []byte("mode: "))
}
func parseGoCoverageResults(target *core.BuildTarget, coverage *core.TestCoverage, filename string) error {
profiles, err := cover.ParseProfiles(filename)
if err != nil {
return err
}
for _, profile := range profiles {
coverage.Files[profile.FileName] = parseBlocks(profile.Blocks)
}
coverage.Tests[target.Label] = coverage.Files
return nil
}
func parseBlocks(blocks []cover.ProfileBlock) []core.LineCoverage {
if len(blocks) == 0 {
return nil
}
lastLine := blocks[len(blocks)-1].EndLine
ret := make([]core.LineCoverage, lastLine)
for _, block := range blocks {
for line := block.StartLine - 1; line < block.EndLine; line++ {
if block.Count > 0 {
ret[line] = core.Covered
} else {
ret[line] = core.Uncovered
}
}
}
return ret
}