lib/fs: Improve IsParent (#5658)

This commit is contained in:
Simon Frei
2019-04-22 11:12:32 +02:00
committed by GitHub
parent 926e9228ed
commit e302ccf4b4
2 changed files with 81 additions and 3 deletions

View File

@@ -8,6 +8,7 @@ package fs
import (
"path/filepath"
"runtime"
"testing"
)
@@ -106,3 +107,66 @@ func TestFileModeString(t *testing.T) {
t.Fatalf("Got %v, expected %v", fm.String(), exp)
}
}
func TestIsParent(t *testing.T) {
test := func(path, parent string, expected bool) {
t.Helper()
path = filepath.FromSlash(path)
parent = filepath.FromSlash(parent)
if res := IsParent(path, parent); res != expected {
t.Errorf(`Unexpected result: IsParent("%v", "%v"): %v should be %v`, path, parent, res, expected)
}
}
testBoth := func(path, parent string, expected bool) {
t.Helper()
test(path, parent, expected)
if runtime.GOOS == "windows" {
test("C:/"+path, "C:/"+parent, expected)
} else {
test("/"+path, "/"+parent, expected)
}
}
// rel - abs
for _, parent := range []string{"/", "/foo", "/foo/bar"} {
for _, path := range []string{"", ".", "foo", "foo/bar", "bas", "bas/baz"} {
if runtime.GOOS == "windows" {
parent = "C:/" + parent
}
test(parent, path, false)
test(path, parent, false)
}
}
// equal
for i, path := range []string{"/", "/foo", "/foo/bar", "", ".", "foo", "foo/bar"} {
if i < 3 && runtime.GOOS == "windows" {
path = "C:" + path
}
test(path, path, false)
}
test("", ".", false)
test(".", "", false)
for _, parent := range []string{"", "."} {
for _, path := range []string{"foo", "foo/bar"} {
test(path, parent, true)
test(parent, path, false)
}
}
for _, parent := range []string{"foo", "foo/bar"} {
for _, path := range []string{"bar", "bar/foo"} {
testBoth(path, parent, false)
testBoth(parent, path, false)
}
}
for _, parent := range []string{"foo", "foo/bar"} {
for _, path := range []string{"foo/bar/baz", "foo/bar/baz/bas"} {
testBoth(path, parent, true)
testBoth(parent, path, false)
if runtime.GOOS == "windows" {
test("C:/"+path, "D:/"+parent, false)
}
}
}
}