From 15e8c75e0b21dcbc26057dc71196ab13150d4295 Mon Sep 17 00:00:00 2001 From: Elliot Morrison-Reed Date: Thu, 25 Aug 2016 23:06:56 +0200 Subject: [PATCH 1/3] Changed string comparison for child path to use filepath.Rel --- cobra/cmd/helpers.go | 15 ++++++++++++++- cobra/cmd/helpers_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/cobra/cmd/helpers.go b/cobra/cmd/helpers.go index 7cd3be18..2c08dc1b 100644 --- a/cobra/cmd/helpers.go +++ b/cobra/cmd/helpers.go @@ -100,13 +100,26 @@ func guessCmdDir() string { func guessImportPath() string { guessProjectPath() - if !strings.HasPrefix(projectPath, getSrcPath()) { + if !inPath(getSrcPath(), projectPath) { er("Cobra only supports project within $GOPATH") } return filepath.ToSlash(filepath.Clean(strings.TrimPrefix(projectPath, getSrcPath()))) } +func inPath(srcPath, projectPath string) bool { + relPath, err := filepath.Rel(srcPath, projectPath) + if err != nil { + return false + } + for _, d := range filepath.SplitList(relPath) { + if d == ".." || d == "." { + return false + } + } + return true +} + func getSrcPath() string { return filepath.Join(os.Getenv("GOPATH"), "src") + string(os.PathSeparator) } diff --git a/cobra/cmd/helpers_test.go b/cobra/cmd/helpers_test.go index bd0f7595..668850f1 100644 --- a/cobra/cmd/helpers_test.go +++ b/cobra/cmd/helpers_test.go @@ -38,3 +38,28 @@ func TestProjectPath(t *testing.T) { checkGuess(t, "/bar/foo/commands", "", filepath.Join("/", "bar", "foo")) checkGuess(t, "github.com/spf13/hugo/../hugo", "", filepath.Join("github.com", "spf13", "hugo")) } + +func TestInPath(t *testing.T) { + cases := []struct { + Src string + Prj string + InPath bool + }{ + {"/bar/foo", "/bar/foo", false}, + {"/bar/foo", "/bar/foo/baz", true}, + {"/bar/foo/baz", "/bar/foo", false}, + {"C:/bar/foo", "c:/bar/foo/baz", true}, + {"c:\\bar\\foo", "C:\\bar\\foo", false}, + {"c:\\bar\\..\\bar\\foo", "C:\\bar\\foo\\baz", true}, + } + for _, tc := range cases { + ip := inPath(tc.Src, tc.Prj) + if tc.InPath != ip { + if tc.InPath { + t.Errorf("Unexpected %s determined as inside %s", tc.Prj, tc.Src) + } else { + t.Errorf("Unexpected %s not determined as inside %s", tc.Prj, tc.Src) + } + } + } +} From 6296a316b80182355227043df7a9cf7087c5c1ea Mon Sep 17 00:00:00 2001 From: Elliot Morrison-Reed Date: Fri, 26 Aug 2016 08:12:50 +0200 Subject: [PATCH 2/3] Removed iteration from inPath check, the first entry is enough Added GOOS check for inPath tests --- cobra/cmd/helpers.go | 9 +++++---- cobra/cmd/helpers_test.go | 29 ++++++++++++++++++++++------- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/cobra/cmd/helpers.go b/cobra/cmd/helpers.go index 2c08dc1b..31002fb8 100644 --- a/cobra/cmd/helpers.go +++ b/cobra/cmd/helpers.go @@ -112,11 +112,12 @@ func inPath(srcPath, projectPath string) bool { if err != nil { return false } - for _, d := range filepath.SplitList(relPath) { - if d == ".." || d == "." { - return false - } + + splitRelPath := filepath.SplitList(relPath) + if splitRelPath[0] == ".." || splitRelPath[0] == "." { + return false } + return true } diff --git a/cobra/cmd/helpers_test.go b/cobra/cmd/helpers_test.go index 668850f1..437f9860 100644 --- a/cobra/cmd/helpers_test.go +++ b/cobra/cmd/helpers_test.go @@ -4,6 +4,7 @@ import ( "fmt" "os" "path/filepath" + "runtime" "testing" ) @@ -39,26 +40,40 @@ func TestProjectPath(t *testing.T) { checkGuess(t, "github.com/spf13/hugo/../hugo", "", filepath.Join("github.com", "spf13", "hugo")) } +type inPathTestCase struct { + Src string + Prj string + InPath bool +} + func TestInPath(t *testing.T) { - cases := []struct { - Src string - Prj string - InPath bool - }{ + cases := []inPathTestCase{ {"/bar/foo", "/bar/foo", false}, {"/bar/foo", "/bar/foo/baz", true}, {"/bar/foo/baz", "/bar/foo", false}, - {"C:/bar/foo", "c:/bar/foo/baz", true}, + {"/bar/foo", "/bar/foo/.wierd..dirname/", true}, {"c:\\bar\\foo", "C:\\bar\\foo", false}, {"c:\\bar\\..\\bar\\foo", "C:\\bar\\foo\\baz", true}, } + if runtime.GOOS == "windows" { + cases = append( + cases, + inPathTestCase{"C:/Bar/foo", "c:/bar/foo/baz", true}, + ) + } else { + cases = append( + cases, + inPathTestCase{"C:/Bar/foo", "c:/bar/foo/baz", false}, + ) + } + for _, tc := range cases { ip := inPath(tc.Src, tc.Prj) if tc.InPath != ip { if tc.InPath { t.Errorf("Unexpected %s determined as inside %s", tc.Prj, tc.Src) } else { - t.Errorf("Unexpected %s not determined as inside %s", tc.Prj, tc.Src) + t.Errorf("Unexpected %s determined as not inside %s", tc.Prj, tc.Src) } } } From 895387ad4b2436395dbbe94ed3d48b6ab4dcf564 Mon Sep 17 00:00:00 2001 From: Elliot Morrison-Reed Date: Fri, 26 Aug 2016 18:11:33 +0200 Subject: [PATCH 3/3] Run windows valid path test cases only if runtime.GOOS == "windows" --- cobra/cmd/helpers_test.go | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/cobra/cmd/helpers_test.go b/cobra/cmd/helpers_test.go index 437f9860..f929e19f 100644 --- a/cobra/cmd/helpers_test.go +++ b/cobra/cmd/helpers_test.go @@ -52,28 +52,23 @@ func TestInPath(t *testing.T) { {"/bar/foo", "/bar/foo/baz", true}, {"/bar/foo/baz", "/bar/foo", false}, {"/bar/foo", "/bar/foo/.wierd..dirname/", true}, - {"c:\\bar\\foo", "C:\\bar\\foo", false}, - {"c:\\bar\\..\\bar\\foo", "C:\\bar\\foo\\baz", true}, } if runtime.GOOS == "windows" { cases = append( cases, inPathTestCase{"C:/Bar/foo", "c:/bar/foo/baz", true}, - ) - } else { - cases = append( - cases, - inPathTestCase{"C:/Bar/foo", "c:/bar/foo/baz", false}, + inPathTestCase{"c:\\bar\\foo", "C:\\bar\\foo", false}, + inPathTestCase{"c:\\bar\\..\\bar\\foo", "C:\\bar\\foo\\baz", true}, ) } for _, tc := range cases { ip := inPath(tc.Src, tc.Prj) if tc.InPath != ip { - if tc.InPath { - t.Errorf("Unexpected %s determined as inside %s", tc.Prj, tc.Src) - } else { + if ip { t.Errorf("Unexpected %s determined as not inside %s", tc.Prj, tc.Src) + } else { + t.Errorf("Unexpected %s determined as inside %s", tc.Prj, tc.Src) } } }