Skip to content
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*~
/goveralls
/vendor
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/mattn/goveralls

go 1.11

require golang.org/x/tools v0.0.0-20200522201501-cb1345f3a375
require (
golang.org/x/mod v0.2.0
golang.org/x/tools v0.0.0-20200522201501-cb1345f3a375
)
25 changes: 23 additions & 2 deletions gocover.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,24 @@ import (
"fmt"
"go/build"
"io/ioutil"
"os"
"path/filepath"
"strings"

"golang.org/x/mod/modfile"
"golang.org/x/tools/cover"
)

func findFile(file string) (string, error) {
func findFile(rootPackage string, rootDir string, file string) (string, error) {
// If we find a file that is inside the root package, we already know
// where it should be!
if rootPackage != "" {
if relPath, _ := filepath.Rel(rootPackage, file); !strings.HasPrefix(relPath, "..") {
// The file is inside the root package...
return filepath.Join(rootDir, relPath), nil
}
}

dir, file := filepath.Split(file)
pkg, err := build.Import(dir, ".", build.FindOnly)
if err != nil {
Expand Down Expand Up @@ -102,9 +113,19 @@ func mergeTwoProfBlock(left, right []cover.ProfileBlock) []cover.ProfileBlock {

// toSF converts profiles to sourcefiles for coveralls.
func toSF(profs []*cover.Profile) ([]*SourceFile, error) {
rootDirectory, err := os.Getwd()
if err != nil {
return nil, fmt.Errorf("get working dir: %w", err)
}
modPath := filepath.Join(rootDirectory, "go.mod")
rootPackage := ""
if content, err := ioutil.ReadFile(modPath); err == nil {
rootPackage = modfile.ModulePath(content)
}

var rv []*SourceFile
for _, prof := range profs {
path, err := findFile(prof.FileName)
path, err := findFile(rootPackage, rootDirectory, prof.FileName)
if err != nil {
return nil, fmt.Errorf("cannot find file %q: %v", prof.FileName, err)
}
Expand Down