mirror of
https://github.com/restic/restic.git
synced 2025-08-20 21:17:39 +00:00
local: Fix creating data dirs
This commit is contained in:
52
internal/fs/path_prefix_test.go
Normal file
52
internal/fs/path_prefix_test.go
Normal file
@@ -0,0 +1,52 @@
|
||||
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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user