Skip to content

Commit

Permalink
Add support for excoveralls json format (#278)
Browse files Browse the repository at this point in the history
* 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
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 6 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: test-docker build-docker build-all build-all-latest release
.PHONY: test-docker build-docker build-all build-all-latest release test-excoveralls

AWS ?= $(shell which aws)
DOCKER_RUN ?= $(shell which docker) run --rm
Expand Down Expand Up @@ -71,6 +71,9 @@ test-clover:
test-cobertura:
docker build -f integration-tests/cobertura/Dockerfile .

test-excoveralls:
docker build -f integration-tests/excoveralls/Dockerfile .

publish-head:
$(AWS) s3 cp \
--acl public-read \
Expand Down
12 changes: 7 additions & 5 deletions cmd/format-coverage.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"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"
Expand All @@ -34,18 +35,19 @@ type CoverageFormatter struct {
var formatOptions = CoverageFormatter{}

// a prioritized list of the formatters to use
var formatterList = []string{"simplecov", "lcov", "coverage.py", "clover", "gocov", "gcov", "cobertura", "jacoco"}
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{
"simplecov": &simplecov.Formatter{},
"lcov": &lcov.Formatter{},
"coverage.py": &coveragepy.Formatter{},
"gocov": &gocov.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
Expand Down
73 changes: 73 additions & 0 deletions formatters/excoveralls/excoveralls.go
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"`
}
12 changes: 12 additions & 0 deletions formatters/excoveralls/excoveralls_example.json
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]
}
]
}
57 changes: 57 additions & 0 deletions formatters/excoveralls/excoveralls_test.go
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())
}
35 changes: 35 additions & 0 deletions integration-tests/excoveralls/Dockerfile
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

0 comments on commit 07c806d

Please sign in to comment.