Skip to content

Commit

Permalink
Merge pull request #16 from msoap/gomod_fixes
Browse files Browse the repository at this point in the history
Added Go modules support
  • Loading branch information
msoap committed Aug 24, 2019
2 parents 7e3c7ae + c81a384 commit d224cde
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 24 deletions.
35 changes: 11 additions & 24 deletions go-carpet.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"bufio"
"flag"
"fmt"
"go/build"
Expand Down Expand Up @@ -39,6 +38,8 @@ var (

// directories for skip
skipDirs = []string{"testdata"}

errIsNotInGoMod = fmt.Errorf("is not in go modules")
)

func getDirsWithTests(includeVendor bool, roots ...string) (result []string, err error) {
Expand Down Expand Up @@ -145,31 +146,10 @@ func guessAbsPathInGOPATH(GOPATH, relPath string) (absPath string, err error) {
}
}

if absPath == "" {
var cwd string
if cwd, err = os.Getwd(); err == nil {
var f *os.File
if f, err = os.Open("go.mod"); err == nil {
// The func wrap with the _ = is just to quiet errcheck; probably better some other way?
defer func() { _ = f.Close() }()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "module ") {
guessAbsPath := filepath.Join(cwd, strings.TrimPrefix(relPath, strings.TrimSpace(strings.SplitN(line, " ", 2)[1])))
if _, err = os.Stat(guessAbsPath); err == nil {
absPath = guessAbsPath
}
break
}
}
}
}
}

if absPath == "" {
return "", fmt.Errorf("file '%s' not found in GOPATH", relPath)
}

return absPath, err
}

Expand All @@ -181,14 +161,21 @@ func getCoverForDir(coverFileName string, filesFilter []string, config Config) (

for _, fileProfile := range coverProfile {
var fileName string
if strings.HasPrefix(fileProfile.FileName, "_") {
if strings.HasPrefix(fileProfile.FileName, "/") {
// TODO: what about windows?
fileName = fileProfile.FileName
} else if strings.HasPrefix(fileProfile.FileName, "_") {
// absolute path (or relative in tests)
if runtime.GOOS != "windows" {
fileName = strings.TrimLeft(fileProfile.FileName, "_")
} else {
// "_\C_\Users\..." -> "C:\Users\..."
fileName = reWindowsPathFix.ReplaceAllString(fileProfile.FileName, "$1:")
}
} else if fileName, err = guessAbsPathInGoMod(fileProfile.FileName); err != errIsNotInGoMod {
if err != nil {
return result, profileBlocks, err
}
} else {
// file in one dir in GOPATH
fileName, err = guessAbsPathInGOPATH(os.Getenv("GOPATH"), fileProfile.FileName)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ require (
github.com/mattn/go-isatty v0.0.4 // indirect
github.com/mattn/go-shellwords v1.0.3
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b
github.com/msoap/byline v1.1.1
golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8 // indirect
golang.org/x/tools v0.0.0-20181109202920-92d8274bd7b8
)
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRUIY4=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-isatty v0.0.4 h1:bnP0vzxcAdeI1zdubAl5PjU6zsERjGZb7raWodagDYs=
Expand All @@ -6,6 +8,13 @@ github.com/mattn/go-shellwords v1.0.3 h1:K/VxK7SZ+cvuPgFSLKi5QPI9Vr/ipOf4C1gN+nt
github.com/mattn/go-shellwords v1.0.3/go.mod h1:3xCvwCdWdlDJUrvuMn7Wuy9eWs4pE8vqg+NOMyg4B2o=
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b h1:j7+1HpAFS1zy5+Q4qx1fWh90gTKwiN4QCGoY9TWyyO4=
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE=
github.com/msoap/byline v1.1.1 h1:imxWvm9wIHNGePF/peiOxcL1vgVLK3/qKsMW75XZn9c=
github.com/msoap/byline v1.1.1/go.mod h1:E2oCrXddpzrmu4NmrwEv4Qiyweo62Yp3+w3IN3X2sq8=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8 h1:YoY1wS6JYVRpIfFngRf2HHo9R9dAne3xbkGOQ5rJXjU=
golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/tools v0.0.0-20181109202920-92d8274bd7b8 h1:VIvcx91Sk7S/d0CZtYIbqRQSTs0PQsHUcIJTHECUaY4=
Expand Down
75 changes: 75 additions & 0 deletions mod.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package main

import (
"fmt"
"io"
"log"
"os"
"os/exec"
"path"
"strings"

"github.com/msoap/byline"
)

var goModFilename *string

func getGoModFilename() string {
if goModFilename != nil {
return *goModFilename
}

file := ""
out, err := exec.Command("go", "env", "GOMOD").Output()
if err != nil {
log.Printf("failed to load 'go env GOMOD' content: %s", err)
goModFilename = &file
return ""
}

file = strings.TrimSpace(string(out))
goModFilename = &file

return file
}

func guessAbsPathInGoMod(relPath string) (string, error) {
modFilename := getGoModFilename()
if modFilename == "" {
return "", errIsNotInGoMod
}

modFile, err := os.Open(modFilename)
if err != nil {
return "", err
}
defer func() {
if err := modFile.Close(); err != nil {
log.Printf("failed to close %s file: %s", modFilename, err)
}
}()

moduleName := ""
if err := byline.NewReader(modFile).AWKMode(func(line string, fields []string, vars byline.AWKVars) (string, error) {
if vars.NF == 2 && fields[0] == "module" && fields[1] != "" {
moduleName = fields[1]
return "", io.EOF
}

return "", nil
}).Discard(); err != nil {
return "", err
}
if moduleName == "" {
return "", errIsNotInGoMod
}

absPath := path.Dir(modFilename) + strings.TrimPrefix(relPath, moduleName)
if stat, err := os.Stat(absPath); err != nil {
return "", err
} else if !stat.Mode().IsRegular() {
return "", fmt.Errorf("%s is not regular file", absPath)
}

return absPath, nil
}
36 changes: 36 additions & 0 deletions mod_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import (
"os"
"testing"
)

func Test_guessAbsPathInGoMod(t *testing.T) {
if err := os.Setenv("GO111MODULE", "on"); err != nil {
t.Fatalf("failed to set env: %s", err)
}

t.Run("empty", func(t *testing.T) {
if _, err := guessAbsPathInGoMod(""); err == nil {
t.Errorf("failed to test empty file")
}
})

t.Run("real", func(t *testing.T) {
gotAbsPath, err := guessAbsPathInGoMod("github.com/msoap/go-carpet/terminal_posix.go")
if err != nil {
t.Errorf("failed to test real file: %s", err)
}

if _, err := os.Stat(gotAbsPath); err != nil {
t.Errorf("failed to test real file: %s", err)
}
})

t.Run("not exists", func(t *testing.T) {
_, err := guessAbsPathInGoMod("github.com/msoap/go-carpet/terminal_posix_another_file.go")
if err == nil {
t.Errorf("failed to test not exists file")
}
})
}

0 comments on commit d224cde

Please sign in to comment.