Skip to content

Commit

Permalink
Added support GOPATH with multiple path
Browse files Browse the repository at this point in the history
fixes #4
  • Loading branch information
msoap committed Mar 4, 2016
1 parent 2bd9bd2 commit def1b84
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
27 changes: 25 additions & 2 deletions go-carpet.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,26 @@ func runGoTest(path string, coverFileName string, hideStderr bool) error {
return nil
}

func guessAbsPathInGOPATH(GOPATH, relPath string) (absPath string, err error) {
if GOPATH == "" {
return "", fmt.Errorf("GOPATH is not set")
}

gopathChunks := strings.Split(GOPATH, string(os.PathListSeparator))
for _, gopathChunk := range gopathChunks {
guessAbsPath := strings.Join([]string{gopathChunk, "src", relPath}, string(os.PathSeparator))
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
}

func getCoverForDir(path string, coverFileName string, filesFilter []string, colors256 bool) (result []byte, err error) {
coverProfile, err := cover.ParseProfiles(coverFileName)
if err != nil {
Expand All @@ -133,8 +153,11 @@ func getCoverForDir(path string, coverFileName string, filesFilter []string, col
// absolute path (or relative in tests)
fileName = strings.TrimLeft(fileProfile.FileName, "_")
} else {
// file in GOPATH
fileName = os.Getenv("GOPATH") + "/src/" + fileProfile.FileName
// file in one dir in GOPATH
fileName, err = guessAbsPathInGOPATH(os.Getenv("GOPATH"), fileProfile.FileName)
if err != nil {
return result, err
}
}

if len(filesFilter) > 0 && !isSliceInString(fileName, filesFilter) {
Expand Down
35 changes: 35 additions & 0 deletions go-carpet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,3 +342,38 @@ func Test_runGoTest(t *testing.T) {
t.Errorf("runGoTest() error failed")
}
}

func Test_guessAbsPathInGOPATH(t *testing.T) {
GOPATH := ""
absPath, err := guessAbsPathInGOPATH(GOPATH, "file.golang")
if absPath != "" || err == nil {
t.Errorf("1. guessAbsPathInGOPATH() empty GOPATH")
}

sep := string(os.PathSeparator)
cwd, _ := os.Getwd()

GOPATH = cwd + sep + "test_data"
absPath, err = guessAbsPathInGOPATH(GOPATH, "file.golang")
if err != nil {
t.Errorf("2. guessAbsPathInGOPATH() error: %s", err)
}
if absPath != cwd+sep+"test_data"+sep+"src"+sep+"file.golang" {
t.Errorf("3. guessAbsPathInGOPATH() empty GOPATH")
}

GOPATH = cwd + sep + "test_data" + string(os.PathListSeparator) + "/tmp"
absPath, err = guessAbsPathInGOPATH(GOPATH, "file.golang")
if err != nil {
t.Errorf("4. guessAbsPathInGOPATH() error: %s", err)
}
if absPath != cwd+sep+"test_data"+sep+"src"+sep+"file.golang" {
t.Errorf("5. guessAbsPathInGOPATH() empty GOPATH")
}

GOPATH = "/tmp" + string(os.PathListSeparator) + "/"
absPath, err = guessAbsPathInGOPATH(GOPATH, "file.golang")
if absPath != "" || err == nil {
t.Errorf("6. guessAbsPathInGOPATH() file not in GOPATH")
}
}
9 changes: 9 additions & 0 deletions test_data/src/file.golang
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

import (
"fmt"
)

func main() {
fmt.Println("Hello world")
}

0 comments on commit def1b84

Please # to comment.