project path guessing updated

This commit is contained in:
nikandfor 2016-08-09 21:21:57 +03:00
parent 806b2bb86a
commit 88e2af3aff
2 changed files with 25 additions and 33 deletions

View file

@ -141,42 +141,31 @@ func guessProjectPath() {
} }
srcPath := getSrcPath() srcPath := getSrcPath()
// if provided, inspect for logical locations
if strings.ContainsRune(inputPath, os.PathSeparator) {
if filepath.IsAbs(inputPath) || filepath.HasPrefix(inputPath, string(os.PathSeparator)) {
// if Absolute, use it
projectPath = filepath.Clean(inputPath)
return
}
// If not absolute but contains slashes,
// assuming it means create it from $GOPATH
count := strings.Count(inputPath, string(os.PathSeparator))
switch count { var base string
// If only one directory deep, assume "github.com" // if provided, inspect for logical locations
case 1: if filepath.IsAbs(inputPath) || filepath.HasPrefix(inputPath, string(os.PathSeparator)) {
projectPath = filepath.Join(srcPath, "github.com", inputPath) // if Absolute, use it.
return } else if projectBase != "" {
case 2: // if projectBase specified any relative path starts with it
projectPath = filepath.Join(srcPath, inputPath) base = filepath.Join(srcPath, projectBase)
return } else if inputPath == "." || strings.HasPrefix(inputPath, "./") || strings.HasPrefix(inputPath, "../") {
default: // relative to cwd like 'go test ./'
er("Unknown directory") var err error
base, err = getWd()
if err != nil {
er(err)
} }
} else { } else {
// hardest case.. just a word. // relative, but not to cwd, so to $GOPATH/src
if projectBase == "" { if dir, _ := filepath.Split(inputPath); dir == "" {
x, err := getWd() // If only one directory deep, assume "github.com"
if err == nil { base = filepath.Join(srcPath, "github.com")
projectPath = filepath.Join(x, inputPath)
return
}
er(err)
} else { } else {
projectPath = filepath.Join(srcPath, projectBase, inputPath) base = srcPath
return
} }
} }
projectPath = filepath.Join(base, inputPath)
} }
// isEmpty checks if a given path is empty. // isEmpty checks if a given path is empty.

View file

@ -16,7 +16,7 @@ func checkGuess(t *testing.T, wd, input, expected string) {
guessProjectPath() guessProjectPath()
if projectPath != expected { if projectPath != expected {
t.Errorf("Unexpected Project Path. \n Got: %q\nExpected: %q\n", projectPath, expected) t.Errorf("Unexpected Project Path. \nGot: %q\nExpected: %q\nArg: %v\nDir: %v", projectPath, expected, input, wd)
} }
reset() reset()
@ -30,9 +30,12 @@ func reset() {
func TestProjectPath(t *testing.T) { func TestProjectPath(t *testing.T) {
checkGuess(t, "", filepath.Join("github.com", "spf13", "hugo"), filepath.Join(getSrcPath(), "github.com", "spf13", "hugo")) checkGuess(t, "", filepath.Join("github.com", "spf13", "hugo"), filepath.Join(getSrcPath(), "github.com", "spf13", "hugo"))
checkGuess(t, "", filepath.Join("spf13", "hugo"), filepath.Join(getSrcPath(), "github.com", "spf13", "hugo")) checkGuess(t, "", filepath.Join("spf13", "hugo"), filepath.Join(getSrcPath(), "spf13", "hugo"))
checkGuess(t, "", filepath.Join("/", "bar", "foo"), filepath.Join("/", "bar", "foo")) checkGuess(t, "", filepath.Join("/", "bar", "foo"), filepath.Join("/", "bar", "foo"))
checkGuess(t, "/bar/foo", "baz", filepath.Join("/", "bar", "foo", "baz")) checkGuess(t, "/bar/foo", "baz", filepath.Join(getSrcPath(), "github.com", "baz"))
checkGuess(t, "/bar/foo", "gopkg.in/baz", filepath.Join(getSrcPath(), "gopkg.in", "baz"))
checkGuess(t, "/bar/foo", "./baz", filepath.Join("/", "bar", "foo", "baz"))
checkGuess(t, "/bar/foo", "./gopkg.in/baz", filepath.Join("/", "bar", "foo", "gopkg.in", "baz"))
checkGuess(t, "/bar/foo/cmd", "", filepath.Join("/", "bar", "foo")) checkGuess(t, "/bar/foo/cmd", "", filepath.Join("/", "bar", "foo"))
checkGuess(t, "/bar/foo/command", "", filepath.Join("/", "bar", "foo")) checkGuess(t, "/bar/foo/command", "", filepath.Join("/", "bar", "foo"))
checkGuess(t, "/bar/foo/commands", "", filepath.Join("/", "bar", "foo")) checkGuess(t, "/bar/foo/commands", "", filepath.Join("/", "bar", "foo"))