diff --git a/cobra/cmd/helpers.go b/cobra/cmd/helpers.go index 3f6357f0..d5c276f9 100644 --- a/cobra/cmd/helpers.go +++ b/cobra/cmd/helpers.go @@ -104,7 +104,7 @@ func guessImportPath() string { er("Cobra only supports project within $GOPATH") } - return filepath.Clean(strings.TrimPrefix(projectPath, getSrcPath())) + return filepath.ToSlash(filepath.Clean(strings.TrimPrefix(projectPath, getSrcPath()))) } func getSrcPath() string { diff --git a/cobra/cmd/helpers_test.go b/cobra/cmd/helpers_test.go index 564ddbe5..c3f990bb 100644 --- a/cobra/cmd/helpers_test.go +++ b/cobra/cmd/helpers_test.go @@ -3,6 +3,7 @@ package cmd import ( "fmt" "os" + "path/filepath" "testing" ) @@ -28,12 +29,14 @@ func reset() { } func TestProjectPath(t *testing.T) { - checkGuess(t, "", "github.com/spf13/hugo", getSrcPath()+"github.com/spf13/hugo") - checkGuess(t, "", "spf13/hugo", getSrcPath()+"github.com/spf13/hugo") - checkGuess(t, "", "/bar/foo", "/bar/foo") - checkGuess(t, "/bar/foo", "baz", "/bar/foo/baz") - checkGuess(t, "/bar/foo/cmd", "", "/bar/foo") - checkGuess(t, "/bar/foo/command", "", "/bar/foo") - checkGuess(t, "/bar/foo/commands", "", "/bar/foo") - checkGuess(t, "github.com/spf13/hugo/../hugo", "", "github.com/spf13/hugo") + wd, _ := os.Getwd() + + checkGuess(t, "", "github.com/spf13/hugo", filepath.Join(wd, "github.com", "spf13", "hugo")) + checkGuess(t, "", "spf13/hugo", filepath.Join(wd, "spf13", "hugo")) + checkGuess(t, "", "/bar/foo", filepath.Join(wd, "bar", "foo")) + checkGuess(t, "/bar/foo", "baz", filepath.Join("/", "bar", "foo", "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")) + checkGuess(t, "github.com/spf13/hugo/../hugo", "", filepath.Join("github.com", "spf13", "hugo")) }