Permalink
Browse files

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...
ale7714 committed Dec 19, 2017
1 parent 8484493 commit 07c806d745c43dc7025afd1209ef257ca2ab69e3
@@ -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
@@ -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 \
@@ -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"
@@ -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
@@ -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"`
}
@@ -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]
}
]
}
@@ -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())
}
@@ -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.