Skip to content

Commit

Permalink
Support multiple jacoco source paths. (#348)
Browse files Browse the repository at this point in the history
  • Loading branch information
atoulme authored and Ale Paredes committed Jul 31, 2018
1 parent 8359261 commit b2ae857
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
15 changes: 11 additions & 4 deletions formatters/jacoco/jacoco.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (

var searchPaths = []string{"jacoco.xml"}

func getSourcePath() string {
return envy.Get("JACOCO_SOURCE_PATH", "")
func getSourcePaths() []string {
return strings.Fields(envy.Get("JACOCO_SOURCE_PATH", ""))
}

type Formatter struct {
Expand All @@ -38,7 +38,7 @@ func (f *Formatter) Search(paths ...string) (string, error) {
}

func (r Formatter) Format() (formatters.Report, error) {
sourcePath := getSourcePath()
sourcePaths := getSourcePaths()

rep, err := formatters.NewReport()
if err != nil {
Expand All @@ -61,7 +61,14 @@ func (r Formatter) Format() (formatters.Report, error) {
for _, xmlSF := range xmlPackage.SourceFile {
num := 1
filepath := fmt.Sprintf("%s/%s", xmlPackage.Name, xmlSF.Name)
sf, err := formatters.NewSourceFile(path.Join(sourcePath, filepath), gitHead)
absolutePath := filepath
for _, sourcePath := range sourcePaths {
absolutePath = path.Join(sourcePath, filepath)
if _, err := os.Stat(absolutePath); err == nil {
break
}
}
sf, err := formatters.NewSourceFile(absolutePath, gitHead)
if err != nil {
return rep, errors.WithStack(err)
}
Expand Down
28 changes: 28 additions & 0 deletions formatters/jacoco/jacoco_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,31 @@ func Test_Parse_SourcePath(t *testing.T) {
r.Equal(3, sf.Coverage[6].Int)
r.Equal(0, sf.Coverage[8].Int)
}

func Test_Parse_SourcePaths(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"}
var rep formatters.Report
var err error
envy.Temp(func() {
envy.Set("JACOCO_SOURCE_PATH", "src/test/java src/main/java")
rep, err = f.Format()
})
r.NoError(err)
r.Len(rep.SourceFiles, 3)

sf := rep.SourceFiles["src/main/java/be/apo/basic/Application.java"]
r.InDelta(33.3, sf.CoveredPercent, 1)
r.Len(sf.Coverage, 11)
r.True(sf.Coverage[6].Valid)
r.False(sf.Coverage[8].Valid)
r.Equal(3, sf.Coverage[6].Int)
r.Equal(0, sf.Coverage[8].Int)
}

0 comments on commit b2ae857

Please sign in to comment.