From 88e2af3affe49acf40473c5b207f9ff1e7465ca0 Mon Sep 17 00:00:00 2001 From: nikandfor Date: Tue, 9 Aug 2016 21:21:57 +0300 Subject: [PATCH] project path guessing updated --- cobra/cmd/helpers.go | 49 +++++++++++++++------------------------ cobra/cmd/helpers_test.go | 9 ++++--- 2 files changed, 25 insertions(+), 33 deletions(-) diff --git a/cobra/cmd/helpers.go b/cobra/cmd/helpers.go index 7cd3be18..753c077f 100644 --- a/cobra/cmd/helpers.go +++ b/cobra/cmd/helpers.go @@ -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") + 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 { - // hardest case.. just a word. - if projectBase == "" { - x, err := getWd() - if err == nil { - projectPath = filepath.Join(x, inputPath) - return - } - er(err) + // 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 { - projectPath = filepath.Join(srcPath, projectBase, inputPath) - return + base = srcPath } } + projectPath = filepath.Join(base, inputPath) } // isEmpty checks if a given path is empty. diff --git a/cobra/cmd/helpers_test.go b/cobra/cmd/helpers_test.go index bd0f7595..d36d066e 100644 --- a/cobra/cmd/helpers_test.go +++ b/cobra/cmd/helpers_test.go @@ -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"))