Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
haya14busa committed Sep 12, 2016
1 parent de5a843 commit 1bd374c
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 8 deletions.
55 changes: 51 additions & 4 deletions gocover.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,51 @@ func findFile(file string) (string, error) {
return filepath.Join(pkg.Dir, file), nil
}

func parseCover(fn string) ([]*SourceFile, error) {
profs, err := cover.ParseProfiles(fn)
if err != nil {
return nil, fmt.Errorf("Error parsing coverage: %v", err)
// mergeProfs merges profiles for same target packages.
// It assumes each profiles have same sorted FileName and Blocks.
func mergeProfs(pfss [][]*cover.Profile) []*cover.Profile {
if len(pfss) < 1 {
return nil
} else if len(pfss) == 1 {
return pfss[0]
}
head, rest := pfss[0], pfss[1:]
ret := make([]*cover.Profile, 0, len(head))
for i, profile := range head {
for _, ps := range rest {
if len(ps) < i+1 {
log.Fatal("Profile length is different")
}
if ps[i].FileName != profile.FileName {
log.Fatal("Profile FileName is different")
}
profile.Blocks = mergeProfBlocks(profile.Blocks, ps[i].Blocks)
}
ret = append(ret, profile)
}
return ret
}

func mergeProfBlocks(as, bs []cover.ProfileBlock) []cover.ProfileBlock {
if len(as) != len(bs) {
log.Fatal("Two block length should be same")
}
// cover.ProfileBlock genereated by cover.ParseProfiles() is sorted by
// StartLine and StartCol, so we can use index.
ret := make([]cover.ProfileBlock, 0, len(as))
for i, a := range as {
b := bs[i]
if a.StartLine != b.StartLine || a.StartCol != b.StartCol {
log.Fatal("Blocks are not sorted")
}
a.Count += b.Count
ret = append(ret, a)
}
return ret
}

// toSF converts profiles to sourcefiles for coveralls.
func toSF(profs []*cover.Profile) ([]*SourceFile, error) {
var rv []*SourceFile
for _, prof := range profs {
path, err := findFile(prof.FileName)
Expand Down Expand Up @@ -62,3 +101,11 @@ func parseCover(fn string) ([]*SourceFile, error) {

return rv, nil
}

func parseCover(fn string) ([]*SourceFile, error) {
profs, err := cover.ParseProfiles(fn)
if err != nil {
return nil, fmt.Errorf("Error parsing coverage: %v", err)
}
return toSF(profs)
}
16 changes: 12 additions & 4 deletions goveralls.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"strings"
"time"

"golang.org/x/tools/cover"

"github.com/pborman/uuid"
)

Expand Down Expand Up @@ -89,11 +91,11 @@ func getCoverage() ([]*SourceFile, error) {
return parseCover(*coverprof)
}

var sourceFiles []*SourceFile
lines, err := getList()
if err != nil {
return nil, err
}
var pfss [][]*cover.Profile
for _, line := range lines {
f, err := ioutil.TempFile("", "goveralls")
if err != nil {
Expand All @@ -102,7 +104,7 @@ func getCoverage() ([]*SourceFile, error) {
f.Close()

cmd := exec.Command("go")
args := []string{"go", "test", "-covermode", *covermode, "-coverprofile", f.Name()}
args := []string{"go", "test", "-covermode", *covermode, "-coverprofile", f.Name(), "-coverpkg=./..."}
if *verbose {
args = append(args, "-v")
}
Expand All @@ -116,16 +118,22 @@ func getCoverage() ([]*SourceFile, error) {
if err != nil {
return nil, fmt.Errorf("%v: %v", err, string(b))
}
sf, err := parseCover(f.Name())
pfs, err := cover.ParseProfiles(f.Name())
if err != nil {
return nil, err
}
err = os.Remove(f.Name())
if err != nil {
return nil, err
}
sourceFiles = append(sourceFiles, sf...)
pfss = append(pfss, pfs)
}

sourceFiles, err := toSF(mergeProfs(pfss))
if err != nil {
return nil, err
}

return sourceFiles, nil
}

Expand Down

0 comments on commit 1bd374c

Please sign in to comment.