Skip to content

Commit def1b84

Browse files
committed
Added support GOPATH with multiple path
fixes #4
1 parent 2bd9bd2 commit def1b84

File tree

3 files changed

+69
-2
lines changed

3 files changed

+69
-2
lines changed

go-carpet.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,26 @@ func runGoTest(path string, coverFileName string, hideStderr bool) error {
121121
return nil
122122
}
123123

124+
func guessAbsPathInGOPATH(GOPATH, relPath string) (absPath string, err error) {
125+
if GOPATH == "" {
126+
return "", fmt.Errorf("GOPATH is not set")
127+
}
128+
129+
gopathChunks := strings.Split(GOPATH, string(os.PathListSeparator))
130+
for _, gopathChunk := range gopathChunks {
131+
guessAbsPath := strings.Join([]string{gopathChunk, "src", relPath}, string(os.PathSeparator))
132+
if _, err = os.Stat(guessAbsPath); err == nil {
133+
absPath = guessAbsPath
134+
break
135+
}
136+
}
137+
138+
if absPath == "" {
139+
return "", fmt.Errorf("File '%s' not found in GOPATH", relPath)
140+
}
141+
return absPath, err
142+
}
143+
124144
func getCoverForDir(path string, coverFileName string, filesFilter []string, colors256 bool) (result []byte, err error) {
125145
coverProfile, err := cover.ParseProfiles(coverFileName)
126146
if err != nil {
@@ -133,8 +153,11 @@ func getCoverForDir(path string, coverFileName string, filesFilter []string, col
133153
// absolute path (or relative in tests)
134154
fileName = strings.TrimLeft(fileProfile.FileName, "_")
135155
} else {
136-
// file in GOPATH
137-
fileName = os.Getenv("GOPATH") + "/src/" + fileProfile.FileName
156+
// file in one dir in GOPATH
157+
fileName, err = guessAbsPathInGOPATH(os.Getenv("GOPATH"), fileProfile.FileName)
158+
if err != nil {
159+
return result, err
160+
}
138161
}
139162

140163
if len(filesFilter) > 0 && !isSliceInString(fileName, filesFilter) {

go-carpet_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,3 +342,38 @@ func Test_runGoTest(t *testing.T) {
342342
t.Errorf("runGoTest() error failed")
343343
}
344344
}
345+
346+
func Test_guessAbsPathInGOPATH(t *testing.T) {
347+
GOPATH := ""
348+
absPath, err := guessAbsPathInGOPATH(GOPATH, "file.golang")
349+
if absPath != "" || err == nil {
350+
t.Errorf("1. guessAbsPathInGOPATH() empty GOPATH")
351+
}
352+
353+
sep := string(os.PathSeparator)
354+
cwd, _ := os.Getwd()
355+
356+
GOPATH = cwd + sep + "test_data"
357+
absPath, err = guessAbsPathInGOPATH(GOPATH, "file.golang")
358+
if err != nil {
359+
t.Errorf("2. guessAbsPathInGOPATH() error: %s", err)
360+
}
361+
if absPath != cwd+sep+"test_data"+sep+"src"+sep+"file.golang" {
362+
t.Errorf("3. guessAbsPathInGOPATH() empty GOPATH")
363+
}
364+
365+
GOPATH = cwd + sep + "test_data" + string(os.PathListSeparator) + "/tmp"
366+
absPath, err = guessAbsPathInGOPATH(GOPATH, "file.golang")
367+
if err != nil {
368+
t.Errorf("4. guessAbsPathInGOPATH() error: %s", err)
369+
}
370+
if absPath != cwd+sep+"test_data"+sep+"src"+sep+"file.golang" {
371+
t.Errorf("5. guessAbsPathInGOPATH() empty GOPATH")
372+
}
373+
374+
GOPATH = "/tmp" + string(os.PathListSeparator) + "/"
375+
absPath, err = guessAbsPathInGOPATH(GOPATH, "file.golang")
376+
if absPath != "" || err == nil {
377+
t.Errorf("6. guessAbsPathInGOPATH() file not in GOPATH")
378+
}
379+
}

test_data/src/file.golang

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
func main() {
8+
fmt.Println("Hello world")
9+
}

0 commit comments

Comments
 (0)