Skip to content

Commit

Permalink
add fallback for Go 1.7
Browse files Browse the repository at this point in the history
x/mod/modfile is available from Go 1.8
  • Loading branch information
shogo82148 committed May 25, 2021
1 parent e0fd538 commit b20196b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
9 changes: 3 additions & 6 deletions gocover.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"path/filepath"
"strings"

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

Expand Down Expand Up @@ -113,15 +112,13 @@ func mergeTwoProfBlock(left, right []cover.ProfileBlock) []cover.ProfileBlock {

// toSF converts profiles to sourcefiles for coveralls.
func toSF(profs []*cover.Profile) ([]*SourceFile, error) {
// find root package to reduce build.Import calls when importing files from relative root
// https://github.com/mattn/goveralls/pull/195
rootDirectory, err := os.Getwd()
if err != nil {
return nil, fmt.Errorf("get working dir: %v", err)
}
modPath := filepath.Join(rootDirectory, "go.mod")
rootPackage := ""
if content, err := ioutil.ReadFile(modPath); err == nil {
rootPackage = modfile.ModulePath(content)
}
rootPackage := findRootPackage(rootDirectory)

var rv []*SourceFile
for _, prof := range profs {
Expand Down
19 changes: 19 additions & 0 deletions gocover_ge1.8.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// +build go1.8

package main

import (
"io/ioutil"
"path/filepath"

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

func findRootPackage(rootDirectory string) string {
modPath := filepath.Join(rootDirectory, "go.mod")
content, err := ioutil.ReadFile(modPath)
if err != nil {
return ""
}
return modfile.ModulePath(content)
}
14 changes: 14 additions & 0 deletions gocover_lt1.8.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// +build !go1.8

package main

import (
"io/ioutil"
"path/filepath"

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

func findRootPackage(rootDirectory string) string {
return ""
}

0 comments on commit b20196b

Please sign in to comment.