Skip to content

Add support for excoveralls json format #278

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

Merged
merged 3 commits into from
Dec 19, 2017
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
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)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think it'd be worth adding coverage for the could not find any files, could not open coverage file states, too?


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 "[email protected]"
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