forked from codeclimate/test-reporter
/
clover.go
83 lines (70 loc) · 1.74 KB
/
clover.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
package clover
import (
"encoding/xml"
"os"
"strings"
"github.com/Sirupsen/logrus"
"github.com/codeclimate/test-reporter/env"
"github.com/codeclimate/test-reporter/formatters"
"github.com/markbates/pop/nulls"
"github.com/pkg/errors"
)
var searchPaths = []string{"build/logs/clover.xml", "clover.xml"}
type Formatter struct {
Path string
SourceFiles []formatters.SourceFile
}
func (f *Formatter) Search(paths ...string) (string, error) {
paths = append(paths, searchPaths...)
for _, p := range paths {
logrus.Debugf("checking search path %s for clover formatter", p)
if _, err := os.Stat(p); err == nil {
f.Path = p
return p, nil
}
}
return "", errors.WithStack(errors.Errorf("could not find any files in search paths for clover. search paths were: %s", strings.Join(paths, ", ")))
}
func (f *Formatter) Parse() error {
fx, err := os.Open(f.Path)
if err != nil {
return errors.WithStack(err)
}
c := &xmlFile{}
err = xml.NewDecoder(fx).Decode(c)
if err != nil {
return errors.WithStack(err)
}
gitHead, _ := env.GetHead()
for _, pp := range c.Packages {
for _, pf := range pp.Files {
num := 1
sf, err := formatters.NewSourceFile(pf.Name, gitHead)
if err != nil {
return errors.WithStack(err)
}
for _, l := range pf.Lines {
for num < l.Num {
sf.Coverage = append(sf.Coverage, nulls.Int{})
num++
}
ni := nulls.NewInt(l.Count)
sf.Coverage = append(sf.Coverage, ni)
num++
}
sf.CalcLineCounts()
f.SourceFiles = append(f.SourceFiles, sf)
}
}
return nil
}
func (r Formatter) Format() (formatters.Report, error) {
rep, err := formatters.NewReport()
if err != nil {
return rep, err
}
for _, f := range r.SourceFiles {
rep.AddSourceFile(f)
}
return rep, nil
}