forked from codeclimate/test-reporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsum-coverage.go
81 lines (67 loc) · 1.76 KB
/
sum-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
package cmd
import (
"encoding/json"
"os"
"github.com/codeclimate/test-reporter/formatters"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
type CoverageSummer struct {
Output string
Parts int
}
var summerOptions = CoverageSummer{}
var sumCoverageCmd = &cobra.Command{
Use: "sum-coverage",
Short: "Combine (sum) multiple pre-formatted coverage payloads into one.",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return errors.New("you must pass in one or more files to be summarized")
}
if summerOptions.Parts != 0 && len(args) != summerOptions.Parts {
return errors.Errorf("expected %d parts, received %d parts", summerOptions.Parts, len(args))
}
rep := formatters.Report{
SourceFiles: formatters.SourceFiles{},
}
f, err := os.Open(args[0])
if err != nil {
return errors.WithStack(err)
}
err = json.NewDecoder(f).Decode(&rep)
if err != nil {
return errors.WithStack(err)
}
for _, n := range args[1:] {
f, err := os.Open(n)
if err != nil {
return errors.WithStack(err)
}
rr := formatters.Report{
SourceFiles: formatters.SourceFiles{},
}
err = json.NewDecoder(f).Decode(&rr)
if err != nil {
return errors.WithStack(err)
}
err = rep.Merge(&rr)
if err != nil {
return errors.WithStack(err)
}
}
out, err := writer(summerOptions.Output)
if err != nil {
return errors.WithStack(err)
}
err = rep.Save(out)
if err != nil {
return errors.WithStack(err)
}
return nil
},
}
func init() {
sumCoverageCmd.Flags().IntVarP(&summerOptions.Parts, "parts", "p", 0, "total number of parts to sum")
sumCoverageCmd.Flags().StringVarP(&summerOptions.Output, "output", "o", ccDefaultCoveragePath, "output path")
RootCmd.AddCommand(sumCoverageCmd)
}