-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for excoveralls json format (#278)
* Add support for excoveralls json format * Coverage tool https://github.com/parroty/excoveralls using coveralls.json format * Move QA repo to codeclimate-testing org * Add test for when file is missing
- Loading branch information
Ale Paredes
committed
Dec 19, 2017
1 parent
8484493
commit 07c806d
Showing
6 changed files
with
188 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package excoveralls | ||
|
||
import ( | ||
"encoding/json" | ||
"os" | ||
"strings" | ||
|
||
"github.com/Sirupsen/logrus" | ||
"github.com/codeclimate/test-reporter/env" | ||
"github.com/codeclimate/test-reporter/formatters" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
var searchPaths = []string{"cover/excoveralls.json"} | ||
|
||
type Formatter struct { | ||
Path string | ||
} | ||
|
||
func (f *Formatter) Search(paths ...string) (string, error) { | ||
paths = append(paths, searchPaths...) | ||
for _, p := range paths { | ||
logrus.Debugf("checking search path %s for excoveralls 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 excoveralls. search paths were: %s", strings.Join(paths, ", "))) | ||
} | ||
|
||
func (r Formatter) Format() (formatters.Report, error) { | ||
report, err := formatters.NewReport() | ||
if err != nil { | ||
return report, err | ||
} | ||
|
||
inputFile, err := os.Open(r.Path) | ||
if err != nil { | ||
return report, errors.WithStack(errors.Errorf("could not open coverage file %s", r.Path)) | ||
} | ||
|
||
coverageInput := &jsonExcoveralls{} | ||
err = json.NewDecoder(inputFile).Decode(&coverageInput) | ||
if err != nil { | ||
return report, errors.WithStack(err) | ||
} | ||
|
||
gitHead, _ := env.GetHead() | ||
for _, file := range coverageInput.Files { | ||
sourceFile, err := formatters.NewSourceFile(file.Name, gitHead) | ||
if err != nil { | ||
return report, errors.WithStack(err) | ||
} | ||
sourceFile.Coverage = file.Coverage | ||
err = report.AddSourceFile(sourceFile) | ||
if err != nil { | ||
return report, errors.WithStack(err) | ||
} | ||
} | ||
|
||
return report, nil | ||
} | ||
|
||
type jsonSourceFile struct { | ||
Name string `json:"name,attr"` | ||
Coverage []formatters.NullInt `json:"coverage,attr"` | ||
} | ||
|
||
type jsonExcoveralls struct { | ||
Files []jsonSourceFile `json:"source_files"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"source_files":[ | ||
{ | ||
"name":"demo-app/services/create_user.ex", | ||
"coverage":[null,null,null,1,null,null,null,null,null,1,null,1,null,null,1,null,null,1,null,0,null,null] | ||
}, | ||
{ | ||
"name":"demo-app/services/update_user_info.ex", | ||
"coverage":[null,null,null,1,null,null,1,1,null,1,null,null,1,null,null,1,1,null,null,1,null,1,null,null] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package excoveralls | ||
|
||
import ( | ||
"testing" | ||
|
||
"gopkg.in/src-d/go-git.v4/plumbing/object" | ||
|
||
"github.com/codeclimate/test-reporter/env" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_Format(t *testing.T) { | ||
gb := env.GitBlob | ||
defer func() { env.GitBlob = gb }() | ||
env.GitBlob = func(s string, c *object.Commit) (string, error) { | ||
return s, nil | ||
} | ||
|
||
r := require.New(t) | ||
|
||
rb := Formatter{ | ||
Path: "./excoveralls_example.json", | ||
} | ||
rep, err := rb.Format() | ||
r.NoError(err) | ||
|
||
r.InDelta(93.3, rep.CoveredPercent, 1) | ||
|
||
sf := rep.SourceFiles["demo-app/services/update_user_info.ex"] | ||
r.InDelta(100, sf.CoveredPercent, 1) | ||
sfLc := sf.LineCounts | ||
r.Equal(sfLc.Covered, 9) | ||
r.Equal(sfLc.Missed, 0) | ||
r.Equal(sfLc.Total, 9) | ||
|
||
lc := rep.LineCounts | ||
r.Equal(lc.Covered, 14) | ||
r.Equal(lc.Missed, 1) | ||
r.Equal(lc.Total, 15) | ||
} | ||
|
||
func Test_Format_MissingFile(t *testing.T) { | ||
gb := env.GitBlob | ||
defer func() { env.GitBlob = gb }() | ||
env.GitBlob = func(s string, c *object.Commit) (string, error) { | ||
return s, nil | ||
} | ||
|
||
r := require.New(t) | ||
|
||
rb := Formatter{ | ||
Path: "./not_real.json", | ||
} | ||
_, err := rb.Format() | ||
r.Error(err) | ||
r.Equal("could not open coverage file ./not_real.json", err.Error()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
FROM elixir:1.4.2 | ||
|
||
RUN curl -O https://storage.googleapis.com/golang/go1.8.linux-amd64.tar.gz | ||
RUN tar -xvf go1.8.linux-amd64.tar.gz | ||
RUN mv go /usr/local | ||
|
||
ENV PATH $PATH:/usr/local/go/bin | ||
RUN go version | ||
|
||
ENV GOPATH /go | ||
RUN mkdir $GOPATH | ||
ENV PATH $PATH:/go/bin | ||
|
||
ENV CCTR=$GOPATH/src/github.com/codeclimate/test-reporter | ||
RUN mkdir -p $CCTR | ||
WORKDIR $CCTR | ||
COPY . . | ||
RUN go install -v | ||
|
||
# RUN git clone https://github.com/ale7714/excoveralls.git | ||
RUN git clone https://github.com/codeclimate-testing/excoveralls.git | ||
WORKDIR excoveralls | ||
|
||
RUN echo "testing" > ignore.me | ||
RUN git config --global user.email "you@example.com" | ||
RUN git config --global user.name "Your Name" | ||
RUN git add ignore.me | ||
RUN git commit -m "testing" | ||
ENV MIX_ENV=test | ||
RUN mix do local.hex --force, local.rebar --force, deps.get, compile, coveralls.json | ||
|
||
|
||
ENV CC_TEST_REPORTER_ID=f611556edda9a27a3faace9b837185944ada203dfca1ec3242a4d0a35162f9fc | ||
|
||
RUN test-reporter after-build -s 2 -d |