Skip to content

Commit

Permalink
Drop formatters dependency to env package
Browse files Browse the repository at this point in the history
  • Loading branch information
glkz committed May 13, 2024
1 parent ade6529 commit 42fdec6
Show file tree
Hide file tree
Showing 29 changed files with 30 additions and 448 deletions.
5 changes: 1 addition & 4 deletions formatters/clover/clover.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"os"
"strings"

"github.com/codeclimate/test-reporter/env"
"github.com/codeclimate/test-reporter/formatters"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -47,8 +46,6 @@ func (r Formatter) Format() (formatters.Report, error) {
return rep, errors.WithStack(err)
}

gitHead, _ := env.GetHead()

files := c.Files
for _, pp := range c.Packages {
files = append(files, pp.Files...)
Expand All @@ -62,7 +59,7 @@ func (r Formatter) Format() (formatters.Report, error) {
path = pf.Name
}

sf, err := formatters.NewSourceFile(path, gitHead)
sf, err := formatters.NewSourceFile(path, nil)
if err != nil {
return rep, errors.WithStack(err)
}
Expand Down
15 changes: 0 additions & 15 deletions formatters/clover/clover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,10 @@ package clover
import (
"testing"

"gopkg.in/src-d/go-git.v4/plumbing/object"

"github.com/codeclimate/test-reporter/env"
"github.com/stretchr/testify/require"
)

func Test_Parse(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)

f := &Formatter{Path: "./example.xml"}
Expand All @@ -33,12 +24,6 @@ func Test_Parse(t *testing.T) {
}

func Test_Parse_Without_Package(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)

f := &Formatter{Path: "./example_without_package.xml"}
Expand Down
4 changes: 1 addition & 3 deletions formatters/cobertura/cobertura.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"sort"
"strings"

"github.com/codeclimate/test-reporter/env"
"github.com/codeclimate/test-reporter/formatters"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -49,7 +48,6 @@ func (r Formatter) Format() (formatters.Report, error) {
return rep, errors.WithStack(err)
}

gitHead, _ := env.GetHead()
for _, pp := range coberturaFile.Packages {
mergedClasses := make(map[string]*xmlClass)
// merge Classes by filename
Expand All @@ -68,7 +66,7 @@ func (r Formatter) Format() (formatters.Report, error) {
num := 1
fileName := coberturaFile.getFullFilePath(pf.FileName)
logrus.Debugf("creating test file report for %s", fileName)
sf, err := formatters.NewSourceFile(fileName, gitHead)
sf, err := formatters.NewSourceFile(fileName, nil)
if err != nil {
return rep, errors.WithStack(err)
}
Expand Down
8 changes: 0 additions & 8 deletions formatters/cobertura/cobertura_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,10 @@ package cobertura
import (
"testing"

"gopkg.in/src-d/go-git.v4/plumbing/object"

"github.com/codeclimate/test-reporter/env"
"github.com/stretchr/testify/require"
)

func Test_Parse(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)

Expand Down
4 changes: 1 addition & 3 deletions formatters/coveragepy/coveragepy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"os"
"strings"

"github.com/codeclimate/test-reporter/env"
"github.com/codeclimate/test-reporter/formatters"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -47,12 +46,11 @@ func (r *Formatter) Format() (formatters.Report, error) {
return rep, errors.WithStack(err)
}

gitHead, _ := env.GetHead()
for _, xmlPackage := range coverageFile.Packages {
for _, xmlClass := range xmlPackage.Classes {
fileName := coverageFile.getFullFilePath(xmlClass.FileName)
logrus.Debugf("creating test file report for %s", fileName)
sourceFile, err := formatters.NewSourceFile(fileName, gitHead)
sourceFile, err := formatters.NewSourceFile(fileName, nil)
if err != nil {
return rep, errors.WithStack(err)
}
Expand Down
8 changes: 0 additions & 8 deletions formatters/coveragepy/coveragepy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,10 @@ package coveragepy
import (
"testing"

"gopkg.in/src-d/go-git.v4/plumbing/object"

"github.com/codeclimate/test-reporter/env"
"github.com/stretchr/testify/require"
)

func Test_Parse(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)

Expand Down
5 changes: 1 addition & 4 deletions formatters/dotcover/dotcover.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"os"
"strings"

"github.com/codeclimate/test-reporter/env"
"github.com/codeclimate/test-reporter/formatters"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -44,10 +43,8 @@ func (f Formatter) Format() (formatters.Report, error) {
return rep, err
}

gitHead, _ := env.GetHead()

for _, file := range c.Files {
sf, err := formatters.NewSourceFile(file.Path, gitHead)
sf, err := formatters.NewSourceFile(file.Path, nil)
if err != nil {
err = errors.WithStack(err)
break
Expand Down
11 changes: 0 additions & 11 deletions formatters/dotcover/dotcover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,10 @@ package dotcover
import (
"testing"

"gopkg.in/src-d/go-git.v4/plumbing/object"

"github.com/codeclimate/test-reporter/env"
"github.com/stretchr/testify/require"
)

func Test_Parse(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{
Expand Down
4 changes: 1 addition & 3 deletions formatters/excoveralls/excoveralls.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"os"
"strings"

"github.com/codeclimate/test-reporter/env"
"github.com/codeclimate/test-reporter/formatters"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -47,9 +46,8 @@ func (r Formatter) Format() (formatters.Report, error) {
return report, errors.WithStack(err)
}

gitHead, _ := env.GetHead()
for _, file := range coverageInput.Files {
sourceFile, err := formatters.NewSourceFile(file.Name, gitHead)
sourceFile, err := formatters.NewSourceFile(file.Name, nil)
if err != nil {
return report, errors.WithStack(err)
}
Expand Down
13 changes: 0 additions & 13 deletions formatters/excoveralls/excoveralls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,10 @@ 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)

Expand All @@ -40,11 +32,6 @@ func Test_Format(t *testing.T) {
}

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)

Expand Down
4 changes: 1 addition & 3 deletions formatters/gcov/gcov.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"strconv"
"strings"

"github.com/codeclimate/test-reporter/env"
"github.com/codeclimate/test-reporter/formatters"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -56,9 +55,8 @@ func (f *Formatter) Format() (formatters.Report, error) {
return rep, err
}

gitHead, _ := env.GetHead()
for _, file := range f.FileNames {
sf, err := parseSourceFile(file, gitHead)
sf, err := parseSourceFile(file, nil)
if err != nil {
return rep, errors.WithStack(err)
}
Expand Down
19 changes: 4 additions & 15 deletions formatters/gocov/gocov.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,16 @@ import (

"golang.org/x/tools/cover"

"github.com/codeclimate/test-reporter/env"
"github.com/codeclimate/test-reporter/formatters"
"github.com/gobuffalo/envy"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

var basePackage string

func init() {
basePackage, _ = os.Getwd()
for _, gp := range envy.GoPaths() {
basePackage = strings.TrimPrefix(basePackage, filepath.Join(gp, "src")+string(os.PathSeparator))
}
}

var searchPaths = []string{"c.out"}

type Formatter struct {
Path string
Path string
GoModuleName string
}

func (f *Formatter) Search(paths ...string) (string, error) {
Expand All @@ -53,10 +43,9 @@ func (r Formatter) Format() (formatters.Report, error) {
return rep, errors.WithStack(err)
}

gitHead, _ := env.GetHead()
for _, p := range profiles {
n := strings.TrimPrefix(filepath.FromSlash(p.FileName), basePackage+string(os.PathSeparator))
sf, err := formatters.NewSourceFile(n, gitHead)
n := strings.TrimPrefix(filepath.FromSlash(p.FileName), r.GoModuleName+string(os.PathSeparator))
sf, err := formatters.NewSourceFile(n, nil)
if err != nil {
return rep, errors.WithStack(err)
}
Expand Down
24 changes: 7 additions & 17 deletions formatters/gocov/gocov_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,11 @@ import (

"path/filepath"

"gopkg.in/src-d/go-git.v4/plumbing/object"

"github.com/codeclimate/test-reporter/env"
"github.com/stretchr/testify/require"
)

func Test_Parse(t *testing.T) {
t.Run("should parse report from single package", func(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)

Expand All @@ -27,7 +19,8 @@ func Test_Parse(t *testing.T) {

r.Len(rep.SourceFiles, 4)

sf := rep.SourceFiles[filepath.FromSlash("github.com/codeclimate/test-reporter/formatters/source_file.go")]
sf, fok := rep.SourceFiles[filepath.FromSlash("github.com/codeclimate/test-reporter/formatters/source_file.go")]
r.True(fok)

r.InDelta(75.8, sf.CoveredPercent, 1)
r.Len(sf.Coverage, 115)
Expand All @@ -46,22 +39,19 @@ func Test_Parse(t *testing.T) {
// ./...
//
t.Run("should parse coverage report from multiple packages", func(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)

f := &Formatter{Path: filepath.Join("example", "foobar_test.out")}
f := &Formatter{Path: filepath.Join("example", "foobar_test.out"), GoModuleName: "github.com/codeclimate/test-reporter"}
rep, err := f.Format()
r.NoError(err)

r.Len(rep.SourceFiles, 2)

sfFoo := rep.SourceFiles[filepath.Join("example","foo","foo.go")]
sfBar := rep.SourceFiles[filepath.Join("example","bar","bar.go")]
sfFoo, okFoo := rep.SourceFiles[filepath.Join("formatters", "gocov", "example", "foo", "foo.go")]
r.True(okFoo)
sfBar, okBar := rep.SourceFiles[filepath.Join("formatters", "gocov", "example", "bar", "bar.go")]
r.True(okBar)

r.EqualValues(85, rep.CoveredPercent)
r.EqualValues(100, sfFoo.CoveredPercent)
Expand Down
4 changes: 1 addition & 3 deletions formatters/jacoco/jacoco.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"path"
"strings"

"github.com/codeclimate/test-reporter/env"
"github.com/codeclimate/test-reporter/formatters"
"github.com/gobuffalo/envy"
"github.com/pkg/errors"
Expand Down Expand Up @@ -56,7 +55,6 @@ func (r Formatter) Format() (formatters.Report, error) {
return rep, errors.WithStack(err)
}

gitHead, _ := env.GetHead()
for _, xmlPackage := range xmlJacoco.Packages {
for _, xmlSF := range xmlPackage.SourceFile {
num := 1
Expand All @@ -68,7 +66,7 @@ func (r Formatter) Format() (formatters.Report, error) {
break
}
}
sf, err := formatters.NewSourceFile(absolutePath, gitHead)
sf, err := formatters.NewSourceFile(absolutePath, nil)
if err != nil {
logrus.Warnf("Couldn't find file for path \"%s\" from %s coverage data. Ignore if the path doesn't correspond to an existent file in your repo.", absolutePath, r.Path)
continue
Expand Down
Loading

0 comments on commit 42fdec6

Please sign in to comment.