-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Adam Scarr
committed
Aug 23, 2018
1 parent
4a6f505
commit b07736e
Showing
6 changed files
with
116 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package gopath | ||
|
||
import ( | ||
"fmt" | ||
"go/build" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
var NotFound = fmt.Errorf("not on GOPATH") | ||
|
||
// Contains returns true if the given directory is in the GOPATH | ||
func Contains(dir string) bool { | ||
_, err := Dir2Import(dir) | ||
return err == nil | ||
} | ||
|
||
// Dir2Import takes an *absolute* path and returns a golang import path for the package, and returns an error if it isn't on the gopath | ||
func Dir2Import(dir string) (string, error) { | ||
dir = filepath.ToSlash(dir) | ||
for _, gopath := range filepath.SplitList(build.Default.GOPATH) { | ||
gopath = filepath.ToSlash(filepath.Join(gopath, "src")) | ||
if len(gopath) < len(dir) && strings.EqualFold(gopath, dir[0:len(gopath)]) { | ||
return dir[len(gopath)+1:], nil | ||
} | ||
} | ||
return "", NotFound | ||
} | ||
|
||
// MustDir2Import takes an *absolute* path and returns a golang import path for the package, and panics if it isn't on the gopath | ||
func MustDir2Import(dir string) string { | ||
pkg, err := Dir2Import(dir) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return pkg | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package gopath | ||
|
||
import ( | ||
"go/build" | ||
"runtime" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestContains(t *testing.T) { | ||
origBuildContext := build.Default | ||
defer func() { build.Default = origBuildContext }() | ||
|
||
if runtime.GOOS == "windows" { | ||
build.Default.GOPATH = `C:\go;C:\Users\user\go` | ||
|
||
assert.True(t, Contains(`C:\go\src\github.com\vektah\gqlgen`)) | ||
assert.True(t, Contains(`C:\go\src\fpp`)) | ||
assert.True(t, Contains(`C:/go/src/github.com/vektah/gqlgen`)) | ||
assert.True(t, Contains(`C:\Users\user\go\src\foo`)) | ||
assert.False(t, Contains(`C:\tmp`)) | ||
assert.False(t, Contains(`C:\Users\user`)) | ||
assert.False(t, Contains(`C:\Users\another\go`)) | ||
} else { | ||
build.Default.GOPATH = "/go:/home/user/go" | ||
|
||
assert.True(t, Contains("/go/src/github.com/vektah/gqlgen")) | ||
assert.True(t, Contains("/go/src/foo")) | ||
assert.True(t, Contains("/home/user/go/src/foo")) | ||
assert.False(t, Contains("/tmp")) | ||
assert.False(t, Contains("/home/user")) | ||
assert.False(t, Contains("/home/another/go")) | ||
} | ||
} | ||
|
||
func TestDir2Package(t *testing.T) { | ||
origBuildContext := build.Default | ||
defer func() { build.Default = origBuildContext }() | ||
|
||
if runtime.GOOS == "windows" { | ||
build.Default.GOPATH = "C:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx;C:/a/y;C:/b/" | ||
|
||
assert.Equal(t, "foo/bar", MustDir2Import("C:/a/y/src/foo/bar")) | ||
assert.Equal(t, "foo/bar", MustDir2Import(`C:\a\y\src\foo\bar`)) | ||
assert.Equal(t, "foo/bar", MustDir2Import("C:/b/src/foo/bar")) | ||
assert.Equal(t, "foo/bar", MustDir2Import(`C:\b\src\foo\bar`)) | ||
|
||
assert.PanicsWithValue(t, NotFound, func() { | ||
MustDir2Import("C:/tmp/foo") | ||
}) | ||
} else { | ||
build.Default.GOPATH = "/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:/a/y:/b/" | ||
|
||
assert.Equal(t, "foo/bar", MustDir2Import("/a/y/src/foo/bar")) | ||
assert.Equal(t, "foo/bar", MustDir2Import("/b/src/foo/bar")) | ||
|
||
assert.PanicsWithValue(t, NotFound, func() { | ||
MustDir2Import("/tmp/foo") | ||
}) | ||
} | ||
} |