Removed iteration from inPath check, the first entry is enough

Added GOOS check for inPath tests
This commit is contained in:
Elliot Morrison-Reed 2016-08-26 08:12:50 +02:00
parent 15e8c75e0b
commit 6296a316b8
2 changed files with 27 additions and 11 deletions

View file

@ -112,11 +112,12 @@ func inPath(srcPath, projectPath string) bool {
if err != nil { if err != nil {
return false return false
} }
for _, d := range filepath.SplitList(relPath) {
if d == ".." || d == "." { splitRelPath := filepath.SplitList(relPath)
return false if splitRelPath[0] == ".." || splitRelPath[0] == "." {
} return false
} }
return true return true
} }

View file

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
"runtime"
"testing" "testing"
) )
@ -39,26 +40,40 @@ func TestProjectPath(t *testing.T) {
checkGuess(t, "github.com/spf13/hugo/../hugo", "", filepath.Join("github.com", "spf13", "hugo")) 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) { func TestInPath(t *testing.T) {
cases := []struct { cases := []inPathTestCase{
Src string
Prj string
InPath bool
}{
{"/bar/foo", "/bar/foo", false}, {"/bar/foo", "/bar/foo", false},
{"/bar/foo", "/bar/foo/baz", true}, {"/bar/foo", "/bar/foo/baz", true},
{"/bar/foo/baz", "/bar/foo", false}, {"/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\\foo", "C:\\bar\\foo", false},
{"c:\\bar\\..\\bar\\foo", "C:\\bar\\foo\\baz", true}, {"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 { for _, tc := range cases {
ip := inPath(tc.Src, tc.Prj) ip := inPath(tc.Src, tc.Prj)
if tc.InPath != ip { if tc.InPath != ip {
if tc.InPath { if tc.InPath {
t.Errorf("Unexpected %s determined as inside %s", tc.Prj, tc.Src) t.Errorf("Unexpected %s determined as inside %s", tc.Prj, tc.Src)
} else { } 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)
} }
} }
} }