Skip to content

Commit d3d488b

Browse files
authored
Add support for simplecov's new expected JSON output (#441)
* Add support for simplecov's new expected output * Rename existent tests * Refactor to include simplecov-latest in simplecov.go * Add tests * Format code * Address Noe's refactoring feedback * Avoid using a variable for each search path * Use only one return statement for returning the analysis result * Avoid unnecesary return of rep on each dedicated formatter * Revert "Avoid unnecesary return of rep on each dedicated formatter" This reverts commit dacd033. * Use better var names for testing
1 parent 4917ad5 commit d3d488b

9 files changed

+885
-59
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package simplecov
2+
3+
import (
4+
"encoding/json"
5+
"os"
6+
7+
"github.com/Sirupsen/logrus"
8+
"github.com/codeclimate/test-reporter/env"
9+
"github.com/codeclimate/test-reporter/formatters"
10+
"github.com/pkg/errors"
11+
)
12+
13+
type branch struct {
14+
Type string `json:"type"`
15+
StartLine int `json:"start_line"`
16+
EndLine int `json:"end_line"`
17+
Coverage interface{} `json:"coverage"`
18+
}
19+
20+
type fileCoverage struct {
21+
LineCoverage []interface{} `json:"lines"`
22+
Branches []branch `json:"branches"`
23+
}
24+
25+
type meta struct {
26+
SimpleCovVersion string `json:"simplecov_version"`
27+
}
28+
29+
type simplecovJsonFormatterReport struct {
30+
Meta meta `json:"meta"`
31+
CoverageType map[string]fileCoverage `json:"coverage"`
32+
}
33+
34+
func transformLineCoverageToCoverage(ln []interface{}) formatters.Coverage {
35+
coverage := make([]formatters.NullInt, len(ln))
36+
ignoredLine := formatters.NullInt{-1, false}
37+
var convertedCoverageValue int
38+
for i := 0; i < len(ln); i++ {
39+
_, ok := ln[i].(string)
40+
if ok {
41+
coverage[i] = ignoredLine
42+
} else {
43+
if ln[i] == nil {
44+
coverage[i] = ignoredLine
45+
} else {
46+
convertedCoverageValue = int(ln[i].(float64))
47+
coverage[i] = formatters.NewNullInt(convertedCoverageValue)
48+
}
49+
}
50+
}
51+
52+
return coverage
53+
}
54+
55+
func jsonFormat(r Formatter, rep formatters.Report) (formatters.Report, error) {
56+
logrus.Debugf("Analyzing simplecov json output from latest format %s", r.Path)
57+
jf, err := os.Open(r.Path)
58+
if err != nil {
59+
return rep, errors.WithStack(errors.Errorf("could not open coverage file %s", r.Path))
60+
}
61+
62+
var m simplecovJsonFormatterReport
63+
decoder := json.NewDecoder(jf)
64+
decoder.DisallowUnknownFields()
65+
66+
err = decoder.Decode(&m)
67+
68+
if err != nil {
69+
return rep, errors.WithStack(err)
70+
}
71+
72+
gitHead, _ := env.GetHead()
73+
for n, ls := range m.CoverageType {
74+
fe, err := formatters.NewSourceFile(n, gitHead)
75+
if err != nil {
76+
return rep, errors.WithStack(err)
77+
}
78+
fe.Coverage = transformLineCoverageToCoverage(ls.LineCoverage)
79+
err = rep.AddSourceFile(fe)
80+
if err != nil {
81+
return rep, errors.WithStack(err)
82+
}
83+
}
84+
85+
return rep, nil
86+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package simplecov
2+
3+
import (
4+
"encoding/json"
5+
"os"
6+
7+
"github.com/Sirupsen/logrus"
8+
"github.com/codeclimate/test-reporter/env"
9+
"github.com/codeclimate/test-reporter/formatters"
10+
"github.com/pkg/errors"
11+
)
12+
13+
type resultSet struct {
14+
Coverage map[string]formatters.Coverage `json:"coverage"`
15+
}
16+
17+
func legacyFormat(r Formatter, rep formatters.Report) (formatters.Report, error) {
18+
logrus.Debugf("Analyzing simplecov json output from legacy format %s", r.Path)
19+
jf, err := os.Open(r.Path)
20+
if err != nil {
21+
return rep, errors.WithStack(errors.Errorf("could not open coverage file %s", r.Path))
22+
}
23+
24+
m := map[string]resultSet{}
25+
err = json.NewDecoder(jf).Decode(&m)
26+
27+
if err != nil {
28+
return rep, errors.WithStack(err)
29+
}
30+
31+
gitHead, _ := env.GetHead()
32+
for _, v := range m {
33+
for n, ls := range v.Coverage {
34+
fe, err := formatters.NewSourceFile(n, gitHead)
35+
if err != nil {
36+
return rep, errors.WithStack(err)
37+
}
38+
fe.Coverage = ls
39+
err = rep.AddSourceFile(fe)
40+
if err != nil {
41+
return rep, errors.WithStack(err)
42+
}
43+
}
44+
}
45+
46+
return rep, nil
47+
}
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
{
2+
"meta": {
3+
"simplecov_version": "0.19.0"
4+
},
5+
"coverage": {
6+
"development/mygem/lib/mygem/errors.rb": {
7+
"lines": [
8+
1,
9+
null,
10+
1,
11+
1,
12+
0,
13+
null,
14+
null,
15+
null,
16+
1,
17+
null,
18+
null,
19+
null,
20+
1,
21+
null,
22+
null,
23+
null,
24+
1,
25+
null,
26+
null,
27+
null,
28+
null
29+
],
30+
"branches": [
31+
]
32+
},
33+
"development/mygem/lib/mygem/definition_manager.rb": {
34+
"lines": [
35+
1,
36+
1,
37+
1,
38+
null,
39+
1,
40+
null,
41+
1,
42+
1,
43+
null,
44+
null,
45+
1,
46+
8,
47+
null,
48+
null,
49+
1,
50+
19,
51+
null,
52+
null,
53+
1,
54+
null,
55+
1,
56+
8,
57+
null,
58+
null,
59+
1,
60+
11,
61+
null,
62+
null,
63+
null,
64+
null,
65+
null,
66+
null
67+
],
68+
"branches": [
69+
{
70+
"type": "then",
71+
"start_line": 14,
72+
"end_line": 14,
73+
"coverage": 0
74+
},
75+
{
76+
"type": "else",
77+
"start_line": 16,
78+
"end_line": 16,
79+
"coverage": 1
80+
},
81+
{
82+
"type": "then",
83+
"start_line": 39,
84+
"end_line": 39,
85+
"coverage": 0
86+
},
87+
{
88+
"type": "else",
89+
"start_line": 41,
90+
"end_line": 41,
91+
"coverage": 0
92+
}
93+
]
94+
},
95+
"development/mygem/lib/mygem/interface.rb": {
96+
"lines": [
97+
1,
98+
null,
99+
1,
100+
1,
101+
null,
102+
1,
103+
8,
104+
null,
105+
null,
106+
null,
107+
null
108+
],
109+
"branches": [
110+
111+
]
112+
},
113+
"development/mygem/lib/mygem/implements.rb": {
114+
"lines": [
115+
1,
116+
null,
117+
1,
118+
9,
119+
null,
120+
null,
121+
null
122+
],
123+
"branches": [
124+
125+
]
126+
},
127+
"development/mygem/lib/mygem/implementation_manager.rb": {
128+
"lines": [
129+
1,
130+
1,
131+
1,
132+
null,
133+
1,
134+
null,
135+
1,
136+
1,
137+
null,
138+
null,
139+
1,
140+
9,
141+
null,
142+
null,
143+
1,
144+
82,
145+
null,
146+
null,
147+
1,
148+
null,
149+
1,
150+
9,
151+
null,
152+
null,
153+
1,
154+
73,
155+
null,
156+
null,
157+
null,
158+
null,
159+
null,
160+
null
161+
],
162+
"branches": [
163+
164+
]
165+
},
166+
"development/mygem/lib/mygem/wrap.rb": {
167+
"lines": [
168+
1,
169+
null,
170+
1,
171+
17,
172+
20,
173+
16,
174+
16,
175+
12,
176+
null,
177+
null
178+
],
179+
"branches": [
180+
181+
]
182+
},
183+
"development/mygem/lib/mygem/type_check.rb": {
184+
"lines": [
185+
1,
186+
null,
187+
1,
188+
7,
189+
7,
190+
7,
191+
null,
192+
null,
193+
null
194+
],
195+
"branches": [
196+
197+
]
198+
}
199+
}
200+
}

0 commit comments

Comments
 (0)