Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make simplecov json formatter more flexible #488

Merged
merged 4 commits into from
Feb 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ linux_image: &linux_image

macos_image: &macos_image
macos:
xcode: "11.3.0"
xcode: "11.4.0"

setup_macos_env: &setup_macos_env

Expand Down
19 changes: 18 additions & 1 deletion formatters/simplecov/json_formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package simplecov
import (
"encoding/json"
"os"
"fmt"

"github.com/Sirupsen/logrus"
"github.com/codeclimate/test-reporter/env"
Expand Down Expand Up @@ -31,6 +32,19 @@ type simplecovJsonFormatterReport struct {
CoverageType map[string]fileCoverage `json:"coverage"`
}

func reportIsOnLegacyFormat(simplecovVersion string) bool {
var major, minor, patch int
fmt.Sscanf(simplecovVersion, "%d.%d.%d", &major, &minor, &patch)

if major < 1 {
if minor < 18 {
return true
}
}

return false
}

func transformLineCoverageToCoverage(ln []interface{}) formatters.Coverage {
coverage := make([]formatters.NullInt, len(ln))
ignoredLine := formatters.NullInt{-1, false}
Expand Down Expand Up @@ -61,14 +75,17 @@ func jsonFormat(r Formatter, rep formatters.Report) (formatters.Report, error) {

var m simplecovJsonFormatterReport
decoder := json.NewDecoder(jf)
decoder.DisallowUnknownFields()

err = decoder.Decode(&m)

if err != nil {
return rep, errors.WithStack(err)
}

if reportIsOnLegacyFormat(m.Meta.SimpleCovVersion) {
return rep, errors.WithStack(errors.Errorf("Simplecov report is on legacy format, falling back to legacy formatter."))
}

gitHead, _ := env.GetHead()
for n, ls := range m.CoverageType {
fe, err := formatters.NewSourceFile(n, gitHead)
Expand Down
158 changes: 158 additions & 0 deletions formatters/simplecov/simplecov-with-groups.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
{
"meta": {
"simplecov_version": "0.19.0"
},
"coverage": {
"development/mygem/lib/mygem/errors.rb": {
"lines": [
1,
null,
1,
1,
0,
null,
null,
null,
1,
null,
null,
null,
1,
null,
null,
null,
1,
null,
null,
null,
null
]
},
"development/mygem/lib/mygem/definition_manager.rb": {
"lines": [
1,
1,
1,
null,
1,
null,
1,
1,
null,
null,
1,
8,
null,
null,
1,
19,
null,
null,
1,
null,
1,
8,
null,
null,
1,
11,
null,
null,
null,
null,
null,
null
]
},
"development/mygem/lib/mygem/interface.rb": {
"lines": [
1,
null,
1,
1,
null,
1,
8,
null,
null,
null,
null
]
},
"development/mygem/lib/mygem/implements.rb": {
"lines": [
1,
null,
1,
9,
null,
null,
null
]
},
"development/mygem/lib/mygem/implementation_manager.rb": {
"lines": [
1,
1,
1,
null,
1,
null,
1,
1,
null,
null,
1,
9,
null,
null,
1,
82,
null,
null,
1,
null,
1,
9,
null,
null,
1,
73,
null,
null,
null,
null,
null,
null
]
},
"development/mygem/lib/mygem/wrap.rb": {
"lines": [
1,
null,
1,
17,
20,
16,
16,
12,
null,
null
]
},
"development/mygem/lib/mygem/type_check.rb": {
"lines": [
1,
null,
1,
7,
7,
7,
null,
null,
null
]
}
},
"groups": {}
}
27 changes: 27 additions & 0 deletions formatters/simplecov/simplecov_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,3 +248,30 @@ func Test_Format_Merged(t *testing.T) {
assert.Equal(lc.Missed, 2)
assert.Equal(lc.Total, 8)
}

func Test_ParseWithGroups(t *testing.T) {
ogb := env.GitBlob
defer func() {
env.GitBlob = ogb
}()
env.GitBlob = func(s string, c *object.Commit) (string, error) {
return s, nil
}

assert := require.New(t)

formatter := Formatter{
Path: "./simplecov-with-groups.json",
}
rep, err := formatter.Format()
assert.NoError(err)

assert.Len(rep.SourceFiles, 7)

cf := rep.SourceFiles["development/mygem/lib/mygem/wrap.rb"]
assert.Len(cf.Coverage, 10)
for i, x := range []interface{}{1, nil, 1, 17, 20, 16, 16, 12, nil, nil} {
l := cf.Coverage[i]
assert.Equal(x, l.Interface())
}
}