restic/internal/fs/path_prefix_test.go

53 lines
1.2 KiB
Go

package fs
import (
"path/filepath"
"runtime"
"testing"
)
func fromSlashAbs(p string) string {
if runtime.GOOS == "windows" {
if len(p) > 0 && p[0] == '/' {
p = "c:" + p
}
}
return filepath.FromSlash(p)
}
func TestHasPathPrefix(t *testing.T) {
var tests = []struct {
base, p string
result bool
}{
{"", "", false},
{"/", "", false},
{"/", "x", false},
{"x", "/", false},
{"/", "/x", true},
{"/x", "/y", false},
{"/home/user/foo", "/home", false},
{"/home/user/foo/", "/home", false},
{"/home/user/foo", "/home/", false},
{"/home/user/foo/", "/home/", false},
{"/home/user/foo", "/home/user/foo/bar", true},
{"/home/user/foo", "/home/user/foo/bar/baz/x/y/z", true},
{"/home/user/foo", "/home/user/foobar", false},
{"/home/user/Foo", "/home/user/foo/bar/baz", false},
{"/home/user/foo", "/home/user/Foo/bar/baz", false},
}
for _, test := range tests {
t.Run("", func(t *testing.T) {
base := fromSlashAbs(test.base)
p := fromSlashAbs(test.p)
result := HasPathPrefix(base, p)
if result != test.result {
t.Fatalf("wrong result for HasPathPrefix(%q, %q): want %v, got %v",
base, p, test.result, result)
}
})
}
}