mirror of
https://github.com/spf13/cobra
synced 2024-11-24 22:57:12 +00:00
project path guessing updated
This commit is contained in:
parent
806b2bb86a
commit
88e2af3aff
2 changed files with 25 additions and 33 deletions
|
@ -141,42 +141,31 @@ func guessProjectPath() {
|
|||
}
|
||||
|
||||
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 {
|
||||
// If only one directory deep, assume "github.com"
|
||||
case 1:
|
||||
projectPath = filepath.Join(srcPath, "github.com", inputPath)
|
||||
return
|
||||
case 2:
|
||||
projectPath = filepath.Join(srcPath, inputPath)
|
||||
return
|
||||
default:
|
||||
er("Unknown directory")
|
||||
}
|
||||
} else {
|
||||
// hardest case.. just a word.
|
||||
if projectBase == "" {
|
||||
x, err := getWd()
|
||||
if err == nil {
|
||||
projectPath = filepath.Join(x, inputPath)
|
||||
return
|
||||
}
|
||||
var base string
|
||||
// if provided, inspect for logical locations
|
||||
if filepath.IsAbs(inputPath) || filepath.HasPrefix(inputPath, string(os.PathSeparator)) {
|
||||
// if Absolute, use it.
|
||||
} else if projectBase != "" {
|
||||
// if projectBase specified any relative path starts with it
|
||||
base = filepath.Join(srcPath, projectBase)
|
||||
} else if inputPath == "." || strings.HasPrefix(inputPath, "./") || strings.HasPrefix(inputPath, "../") {
|
||||
// relative to cwd like 'go test ./'
|
||||
var err error
|
||||
base, err = getWd()
|
||||
if err != nil {
|
||||
er(err)
|
||||
}
|
||||
} else {
|
||||
projectPath = filepath.Join(srcPath, projectBase, inputPath)
|
||||
return
|
||||
// relative, but not to cwd, so to $GOPATH/src
|
||||
if dir, _ := filepath.Split(inputPath); dir == "" {
|
||||
// If only one directory deep, assume "github.com"
|
||||
base = filepath.Join(srcPath, "github.com")
|
||||
} else {
|
||||
base = srcPath
|
||||
}
|
||||
}
|
||||
projectPath = filepath.Join(base, inputPath)
|
||||
}
|
||||
|
||||
// isEmpty checks if a given path is empty.
|
||||
|
|
|
@ -16,7 +16,7 @@ func checkGuess(t *testing.T, wd, input, expected string) {
|
|||
guessProjectPath()
|
||||
|
||||
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()
|
||||
|
@ -30,9 +30,12 @@ func reset() {
|
|||
|
||||
func TestProjectPath(t *testing.T) {
|
||||
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, "/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/command", "", filepath.Join("/", "bar", "foo"))
|
||||
checkGuess(t, "/bar/foo/commands", "", filepath.Join("/", "bar", "foo"))
|
||||
|
|
Loading…
Reference in a new issue