1
mirror of https://github.com/rclone/rclone synced 2024-12-02 11:53:45 +01:00
rclone/fs/fspath/path_test.go
Fabian Möller 1a40bceb1d backend: unify NewFs path handling for wrapping remotes
Use the same function to join the root paths for the wrapping remotes
alias, cache and crypt.
The new function fspath.JoinRootPath is equivalent to path.Join, but if
the first non empty element starts with "//", this is preserved to allow
Windows network path to be used in these remotes.
2018-10-10 17:50:27 +01:00

90 lines
2.7 KiB
Go

package fspath
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestParse(t *testing.T) {
for _, test := range []struct {
in, wantConfigName, wantFsPath string
}{
{"", "", ""},
{"/path/to/file", "", "/path/to/file"},
{"path/to/file", "", "path/to/file"},
{"remote:path/to/file", "remote", "path/to/file"},
{"remote:/path/to/file", "remote", "/path/to/file"},
{":backend:/path/to/file", ":backend", "/path/to/file"},
} {
gotConfigName, gotFsPath := Parse(test.in)
assert.Equal(t, test.wantConfigName, gotConfigName)
assert.Equal(t, test.wantFsPath, gotFsPath)
}
}
func TestSplit(t *testing.T) {
for _, test := range []struct {
remote, wantParent, wantLeaf string
}{
{"", "", ""},
{"remote:", "remote:", ""},
{"remote:potato", "remote:", "potato"},
{"remote:/", "remote:/", ""},
{"remote:/potato", "remote:/", "potato"},
{"remote:/potato/potato", "remote:/potato/", "potato"},
{"remote:potato/sausage", "remote:potato/", "sausage"},
{":remote:", ":remote:", ""},
{":remote:potato", ":remote:", "potato"},
{":remote:/", ":remote:/", ""},
{":remote:/potato", ":remote:/", "potato"},
{":remote:/potato/potato", ":remote:/potato/", "potato"},
{":remote:potato/sausage", ":remote:potato/", "sausage"},
{"/", "/", ""},
{"/root", "/", "root"},
{"/a/b", "/a/", "b"},
{"root", "", "root"},
{"a/b", "a/", "b"},
{"root/", "root/", ""},
{"a/b/", "a/b/", ""},
} {
gotParent, gotLeaf := Split(test.remote)
assert.Equal(t, test.wantParent, gotParent, test.remote)
assert.Equal(t, test.wantLeaf, gotLeaf, test.remote)
assert.Equal(t, test.remote, gotParent+gotLeaf, fmt.Sprintf("%s: %q + %q != %q", test.remote, gotParent, gotLeaf, test.remote))
}
}
func TestJoinRootPath(t *testing.T) {
for _, test := range []struct {
elements []string
want string
}{
{nil, ""},
{[]string{""}, ""},
{[]string{"/"}, "/"},
{[]string{"/", "/"}, "/"},
{[]string{"/", "//"}, "/"},
{[]string{"/root", ""}, "/root"},
{[]string{"/root", "/"}, "/root"},
{[]string{"/root", "//"}, "/root"},
{[]string{"/a/b"}, "/a/b"},
{[]string{"//", "/"}, "//"},
{[]string{"//server", "path"}, "//server/path"},
{[]string{"//server/sub", "path"}, "//server/sub/path"},
{[]string{"//server", "//path"}, "//server/path"},
{[]string{"//server/sub", "//path"}, "//server/sub/path"},
{[]string{"", "//", "/"}, "//"},
{[]string{"", "//server", "path"}, "//server/path"},
{[]string{"", "//server/sub", "path"}, "//server/sub/path"},
{[]string{"", "//server", "//path"}, "//server/path"},
{[]string{"", "//server/sub", "//path"}, "//server/sub/path"},
} {
got := JoinRootPath(test.elements...)
assert.Equal(t, test.want, got)
}
}