2018-01-13 00:30:54 +08:00
|
|
|
package fspath
|
2016-10-24 00:34:17 +08:00
|
|
|
|
|
|
|
import (
|
2017-02-23 05:24:04 +08:00
|
|
|
"fmt"
|
2016-10-24 00:34:17 +08:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2018-06-18 17:39:00 +08:00
|
|
|
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"},
|
2018-08-08 05:51:06 +08:00
|
|
|
{":backend:/path/to/file", ":backend", "/path/to/file"},
|
2018-06-18 17:39:00 +08:00
|
|
|
} {
|
|
|
|
gotConfigName, gotFsPath := Parse(test.in)
|
|
|
|
assert.Equal(t, test.wantConfigName, gotConfigName)
|
|
|
|
assert.Equal(t, test.wantFsPath, gotFsPath)
|
|
|
|
}
|
|
|
|
}
|
2016-10-24 00:34:17 +08:00
|
|
|
|
2018-06-18 17:39:00 +08:00
|
|
|
func TestSplit(t *testing.T) {
|
2016-10-24 00:34:17 +08:00
|
|
|
for _, test := range []struct {
|
|
|
|
remote, wantParent, wantLeaf string
|
|
|
|
}{
|
|
|
|
{"", "", ""},
|
2018-08-08 05:51:06 +08:00
|
|
|
|
2017-02-23 05:24:04 +08:00
|
|
|
{"remote:", "remote:", ""},
|
2016-10-24 00:34:17 +08:00
|
|
|
{"remote:potato", "remote:", "potato"},
|
2017-02-23 05:24:04 +08:00
|
|
|
{"remote:/", "remote:/", ""},
|
|
|
|
{"remote:/potato", "remote:/", "potato"},
|
|
|
|
{"remote:/potato/potato", "remote:/potato/", "potato"},
|
|
|
|
{"remote:potato/sausage", "remote:potato/", "sausage"},
|
2018-08-08 05:51:06 +08:00
|
|
|
|
|
|
|
{":remote:", ":remote:", ""},
|
|
|
|
{":remote:potato", ":remote:", "potato"},
|
|
|
|
{":remote:/", ":remote:/", ""},
|
|
|
|
{":remote:/potato", ":remote:/", "potato"},
|
|
|
|
{":remote:/potato/potato", ":remote:/potato/", "potato"},
|
|
|
|
{":remote:potato/sausage", ":remote:potato/", "sausage"},
|
|
|
|
|
2017-02-23 05:24:04 +08:00
|
|
|
{"/", "/", ""},
|
2016-10-24 00:34:17 +08:00
|
|
|
{"/root", "/", "root"},
|
2017-02-23 05:24:04 +08:00
|
|
|
{"/a/b", "/a/", "b"},
|
|
|
|
{"root", "", "root"},
|
|
|
|
{"a/b", "a/", "b"},
|
|
|
|
{"root/", "root/", ""},
|
|
|
|
{"a/b/", "a/b/", ""},
|
2016-10-24 00:34:17 +08:00
|
|
|
} {
|
2018-06-18 17:39:00 +08:00
|
|
|
gotParent, gotLeaf := Split(test.remote)
|
2016-10-24 00:34:17 +08:00
|
|
|
assert.Equal(t, test.wantParent, gotParent, test.remote)
|
|
|
|
assert.Equal(t, test.wantLeaf, gotLeaf, test.remote)
|
2017-02-23 05:24:04 +08:00
|
|
|
assert.Equal(t, test.remote, gotParent+gotLeaf, fmt.Sprintf("%s: %q + %q != %q", test.remote, gotParent, gotLeaf, test.remote))
|
2016-10-24 00:34:17 +08:00
|
|
|
}
|
|
|
|
}
|
2018-10-09 19:35:27 +08:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|