-
Notifications
You must be signed in to change notification settings - Fork 75
/
gocov.go
88 lines (74 loc) · 1.93 KB
/
gocov.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
package gocov
import (
"os"
"path/filepath"
"strings"
"golang.org/x/tools/cover"
"github.com/Sirupsen/logrus"
"github.com/codeclimate/test-reporter/env"
"github.com/codeclimate/test-reporter/formatters"
"github.com/gobuffalo/envy"
"github.com/markbates/pop/nulls"
"github.com/pkg/errors"
)
var basePackage string
func init() {
basePackage, _ = os.Getwd()
for _, gp := range envy.GoPaths() {
basePackage = strings.TrimPrefix(basePackage, filepath.Join(gp, "src")+string(os.PathSeparator))
}
}
var searchPaths = []string{"c.out"}
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 gocov 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 gocov. search paths were: %s", strings.Join(paths, ", ")))
}
func (f *Formatter) Parse() error {
profiles, err := cover.ParseProfiles(f.Path)
if err != nil {
return errors.WithStack(err)
}
gitHead, _ := env.GetHead()
for _, p := range profiles {
n := strings.TrimPrefix(p.FileName, basePackage+string(os.PathSeparator))
sf, err := formatters.NewSourceFile(n, gitHead)
if err != nil {
return errors.WithStack(err)
}
num := 0
for _, b := range p.Blocks {
for num < b.StartLine {
sf.Coverage = append(sf.Coverage, nulls.Int{})
num++
}
for i := 0; i < b.NumStmt; i++ {
sf.Coverage = append(sf.Coverage, nulls.NewInt(b.Count))
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
}