-
Notifications
You must be signed in to change notification settings - Fork 76
/
format-coverage.go
137 lines (121 loc) · 4.41 KB
/
format-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
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
package cmd
import (
"fmt"
"io"
"os"
"strings"
"github.com/Sirupsen/logrus"
"github.com/codeclimate/test-reporter/formatters"
"github.com/codeclimate/test-reporter/formatters/clover"
"github.com/codeclimate/test-reporter/formatters/cobertura"
"github.com/codeclimate/test-reporter/formatters/coveragepy"
"github.com/codeclimate/test-reporter/formatters/excoveralls"
"github.com/codeclimate/test-reporter/formatters/gcov"
"github.com/codeclimate/test-reporter/formatters/gocov"
"github.com/codeclimate/test-reporter/formatters/jacoco"
"github.com/codeclimate/test-reporter/formatters/lcov"
"github.com/codeclimate/test-reporter/formatters/simplecov"
"github.com/gobuffalo/envy"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
type CoverageFormatter struct {
CoveragePath string
In formatters.Formatter
InputType string
Output string
Prefix string
AddPrefix string
writer io.Writer
}
var formatOptions = CoverageFormatter{}
// a prioritized list of the formatters to use
var formatterList = []string{"clover", "cobertura", "coverage.py", "excoveralls", "gcov", "gocov", "jacoco", "lcov", "simplecov"}
// a map of the formatters to use
var formatterMap = map[string]formatters.Formatter{
"clover": &clover.Formatter{},
"cobertura": &cobertura.Formatter{},
"coverage.py": &coveragepy.Formatter{},
"excoveralls": &excoveralls.Formatter{},
"gcov": &gcov.Formatter{},
"gocov": &gocov.Formatter{},
"jacoco": &jacoco.Formatter{},
"lcov": &lcov.Formatter{},
"simplecov": &simplecov.Formatter{},
}
// formatCoverageCmd represents the format command
var formatCoverageCmd = &cobra.Command{
Use: "format-coverage [coverage file]",
Short: "Locate, parse, and re-format supported coverage sources.",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 0 {
logrus.Debugf("coverage path %s", args[0])
formatOptions.CoveragePath = args[0]
if formatOptions.InputType == "" {
return errors.WithStack(errors.Errorf("please specify the format of the coverage file \"%s\" using the --input-type flag", formatOptions.CoveragePath))
}
}
return runFormatter(formatOptions)
},
}
func runFormatter(formatOptions CoverageFormatter) error {
envy.Set("PREFIX", formatOptions.Prefix)
envy.Set("ADD_PREFIX", formatOptions.AddPrefix)
// if a type is specified use that
if formatOptions.InputType != "" {
if f, ok := formatterMap[formatOptions.InputType]; ok {
logrus.Debugf("using formatter %s", formatOptions.InputType)
_, err := f.Search(formatOptions.CoveragePath)
if err != nil {
logrus.Errorf("could not find coverage file %s\n%s", formatOptions.CoveragePath, err)
return errors.WithStack(err)
}
formatOptions.In = f
} else {
return errors.WithStack(errors.Errorf("could not find a formatter of type %s", formatOptions.InputType))
}
} else {
logrus.Debug("searching for a formatter to use")
// else start searching for files:
for n, f := range formatterMap {
logrus.Debugf("checking %s formatter", n)
if p, err := f.Search(); err == nil {
logrus.Debugf("found file %s for %s formatter", p, n)
formatOptions.In = f
break
}
}
}
if formatOptions.In == nil {
return errors.WithStack(errors.Errorf("could not find any viable formatter. available formatters: %s", strings.Join(formatterList, ", ")))
}
return formatOptions.Save()
}
func (f CoverageFormatter) Save() error {
rep, err := f.In.Format()
if err != nil {
return errors.WithStack(err)
}
if len(rep.SourceFiles) == 0 {
return errors.WithStack(errors.New("could not find coverage info for source files"))
}
if f.writer == nil {
f.writer, err = writer(formatOptions.Output)
if err != nil {
return errors.WithStack(err)
}
}
err = rep.Save(f.writer)
if err != nil {
return errors.WithStack(err)
}
return nil
}
func init() {
pwd, _ := os.Getwd()
formatCoverageCmd.Flags().StringVarP(&formatOptions.Prefix, "prefix", "p", pwd, "the root directory where the coverage analysis was performed")
formatCoverageCmd.Flags().StringVar(&formatOptions.AddPrefix, "add-prefix", "", "add this prefix to file paths")
formatCoverageCmd.Flags().StringVarP(&formatOptions.Output, "output", "o", ccDefaultCoveragePath, "output path")
formatCoverageCmd.Flags().StringVarP(&formatOptions.InputType, "input-type", "t", "", fmt.Sprintf("type of input source to use [%s]", strings.Join(formatterList, ", ")))
RootCmd.AddCommand(formatCoverageCmd)
}