2018-01-12 16:30:54 +00:00
|
|
|
package fspath
|
2016-10-23 16:34:17 +00:00
|
|
|
|
|
|
|
import (
|
2017-02-22 21:24:04 +00:00
|
|
|
"fmt"
|
2020-09-01 16:55:06 +00:00
|
|
|
"path/filepath"
|
2019-09-05 10:01:04 +00:00
|
|
|
"runtime"
|
|
|
|
"strings"
|
2016-10-23 16:34:17 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2019-09-05 10:01:04 +00:00
|
|
|
func TestCheckConfigName(t *testing.T) {
|
|
|
|
for _, test := range []struct {
|
|
|
|
in string
|
|
|
|
want error
|
|
|
|
}{
|
|
|
|
{"remote", nil},
|
|
|
|
{"", errInvalidCharacters},
|
|
|
|
{":remote:", errInvalidCharacters},
|
|
|
|
{"remote:", errInvalidCharacters},
|
|
|
|
{"rem:ote", errInvalidCharacters},
|
|
|
|
{"rem/ote", errInvalidCharacters},
|
|
|
|
{"rem\\ote", errInvalidCharacters},
|
|
|
|
{"[remote", errInvalidCharacters},
|
|
|
|
{"*", errInvalidCharacters},
|
2020-06-16 10:34:03 +00:00
|
|
|
{"-remote", errCantStartWithDash},
|
|
|
|
{"r-emote-", nil},
|
|
|
|
{"_rem_ote_", nil},
|
2019-09-05 10:01:04 +00:00
|
|
|
} {
|
|
|
|
got := CheckConfigName(test.in)
|
|
|
|
assert.Equal(t, test.want, got, test.in)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCheckRemoteName(t *testing.T) {
|
|
|
|
for _, test := range []struct {
|
|
|
|
in string
|
|
|
|
want error
|
|
|
|
}{
|
|
|
|
{":remote:", nil},
|
|
|
|
{"remote:", nil},
|
|
|
|
{"", errInvalidCharacters},
|
|
|
|
{"rem:ote", errInvalidCharacters},
|
|
|
|
{"rem:ote:", errInvalidCharacters},
|
|
|
|
{"remote", errInvalidCharacters},
|
|
|
|
{"rem/ote:", errInvalidCharacters},
|
|
|
|
{"rem\\ote:", errInvalidCharacters},
|
|
|
|
{"[remote:", errInvalidCharacters},
|
|
|
|
{"*:", errInvalidCharacters},
|
|
|
|
} {
|
|
|
|
got := CheckRemoteName(test.in)
|
|
|
|
assert.Equal(t, test.want, got, test.in)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-18 09:39:00 +00:00
|
|
|
func TestParse(t *testing.T) {
|
|
|
|
for _, test := range []struct {
|
|
|
|
in, wantConfigName, wantFsPath string
|
2019-09-05 10:01:04 +00:00
|
|
|
wantErr error
|
2018-06-18 09:39:00 +00:00
|
|
|
}{
|
2020-05-19 11:34:23 +00:00
|
|
|
{"", "", "", errCantBeEmpty},
|
2019-09-05 10:01:04 +00:00
|
|
|
{":", "", "", errInvalidCharacters},
|
|
|
|
{"::", ":", "", errInvalidCharacters},
|
|
|
|
{":/:", "", "/:", errInvalidCharacters},
|
|
|
|
{"/:", "", "/:", nil},
|
|
|
|
{"\\backslash:", "", "\\backslash:", nil},
|
|
|
|
{"/slash:", "", "/slash:", nil},
|
|
|
|
{"with\\backslash:", "", "with\\backslash:", nil},
|
|
|
|
{"with/slash:", "", "with/slash:", nil},
|
|
|
|
{"/path/to/file", "", "/path/to/file", nil},
|
|
|
|
{"/path:/to/file", "", "/path:/to/file", nil},
|
|
|
|
{"./path:/to/file", "", "./path:/to/file", nil},
|
|
|
|
{"./:colon.txt", "", "./:colon.txt", nil},
|
|
|
|
{"path/to/file", "", "path/to/file", nil},
|
|
|
|
{"remote:path/to/file", "remote", "path/to/file", nil},
|
|
|
|
{"rem*ote:path/to/file", "rem*ote", "path/to/file", errInvalidCharacters},
|
|
|
|
{"remote:/path/to/file", "remote", "/path/to/file", nil},
|
|
|
|
{"rem.ote:/path/to/file", "rem.ote", "/path/to/file", errInvalidCharacters},
|
|
|
|
{":backend:/path/to/file", ":backend", "/path/to/file", nil},
|
|
|
|
{":bac*kend:/path/to/file", ":bac*kend", "/path/to/file", errInvalidCharacters},
|
2018-06-18 09:39:00 +00:00
|
|
|
} {
|
2019-09-05 10:01:04 +00:00
|
|
|
gotConfigName, gotFsPath, gotErr := Parse(test.in)
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
test.wantFsPath = strings.Replace(test.wantFsPath, `\`, `/`, -1)
|
|
|
|
}
|
|
|
|
assert.Equal(t, test.wantErr, gotErr)
|
2018-06-18 09:39:00 +00:00
|
|
|
assert.Equal(t, test.wantConfigName, gotConfigName)
|
|
|
|
assert.Equal(t, test.wantFsPath, gotFsPath)
|
|
|
|
}
|
|
|
|
}
|
2016-10-23 16:34:17 +00:00
|
|
|
|
2018-06-18 09:39:00 +00:00
|
|
|
func TestSplit(t *testing.T) {
|
2016-10-23 16:34:17 +00:00
|
|
|
for _, test := range []struct {
|
|
|
|
remote, wantParent, wantLeaf string
|
2019-09-05 10:01:04 +00:00
|
|
|
wantErr error
|
2016-10-23 16:34:17 +00:00
|
|
|
}{
|
2020-05-19 11:34:23 +00:00
|
|
|
{"", "", "", errCantBeEmpty},
|
2018-08-07 21:51:06 +00:00
|
|
|
|
2019-09-05 10:01:04 +00:00
|
|
|
{"remote:", "remote:", "", nil},
|
|
|
|
{"remote:potato", "remote:", "potato", nil},
|
|
|
|
{"remote:/", "remote:/", "", nil},
|
|
|
|
{"remote:/potato", "remote:/", "potato", nil},
|
|
|
|
{"remote:/potato/potato", "remote:/potato/", "potato", nil},
|
|
|
|
{"remote:potato/sausage", "remote:potato/", "sausage", nil},
|
|
|
|
{"rem.ote:potato/sausage", "", "", errInvalidCharacters},
|
2018-08-07 21:51:06 +00:00
|
|
|
|
2019-09-05 10:01:04 +00:00
|
|
|
{":remote:", ":remote:", "", nil},
|
|
|
|
{":remote:potato", ":remote:", "potato", nil},
|
|
|
|
{":remote:/", ":remote:/", "", nil},
|
|
|
|
{":remote:/potato", ":remote:/", "potato", nil},
|
|
|
|
{":remote:/potato/potato", ":remote:/potato/", "potato", nil},
|
|
|
|
{":remote:potato/sausage", ":remote:potato/", "sausage", nil},
|
|
|
|
{":rem[ote:potato/sausage", "", "", errInvalidCharacters},
|
2018-08-07 21:51:06 +00:00
|
|
|
|
2019-09-05 10:01:04 +00:00
|
|
|
{"/", "/", "", nil},
|
|
|
|
{"/root", "/", "root", nil},
|
|
|
|
{"/a/b", "/a/", "b", nil},
|
|
|
|
{"root", "", "root", nil},
|
|
|
|
{"a/b", "a/", "b", nil},
|
|
|
|
{"root/", "root/", "", nil},
|
|
|
|
{"a/b/", "a/b/", "", nil},
|
2016-10-23 16:34:17 +00:00
|
|
|
} {
|
2019-09-05 10:01:04 +00:00
|
|
|
gotParent, gotLeaf, gotErr := Split(test.remote)
|
|
|
|
assert.Equal(t, test.wantErr, gotErr)
|
2016-10-23 16:34:17 +00:00
|
|
|
assert.Equal(t, test.wantParent, gotParent, test.remote)
|
|
|
|
assert.Equal(t, test.wantLeaf, gotLeaf, test.remote)
|
2019-09-05 10:01:04 +00:00
|
|
|
if gotErr == nil {
|
|
|
|
assert.Equal(t, test.remote, gotParent+gotLeaf, fmt.Sprintf("%s: %q + %q != %q", test.remote, gotParent, gotLeaf, test.remote))
|
|
|
|
}
|
2016-10-23 16:34:17 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-13 10:26:13 +00:00
|
|
|
|
|
|
|
func TestMakeAbsolute(t *testing.T) {
|
|
|
|
for _, test := range []struct {
|
|
|
|
in string
|
|
|
|
want string
|
|
|
|
}{
|
|
|
|
{"", ""},
|
|
|
|
{".", ""},
|
|
|
|
{"/.", "/"},
|
|
|
|
{"../potato", "potato"},
|
|
|
|
{"/../potato", "/potato"},
|
|
|
|
{"./../potato", "potato"},
|
|
|
|
{"//../potato", "/potato"},
|
|
|
|
{"././../potato", "potato"},
|
|
|
|
{"././potato/../../onion", "onion"},
|
|
|
|
} {
|
|
|
|
got := makeAbsolute(test.in)
|
|
|
|
assert.Equal(t, test.want, got, test)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-09 11:35:27 +00:00
|
|
|
func TestJoinRootPath(t *testing.T) {
|
|
|
|
for _, test := range []struct {
|
2020-12-13 10:26:13 +00:00
|
|
|
remote string
|
|
|
|
filePath string
|
2018-10-09 11:35:27 +00:00
|
|
|
want string
|
|
|
|
}{
|
2020-12-13 10:26:13 +00:00
|
|
|
{"", "", ""},
|
|
|
|
{"", "/", "/"},
|
|
|
|
{"/", "", "/"},
|
|
|
|
{"/", "/", "/"},
|
|
|
|
{"/", "//", "/"},
|
|
|
|
{"/root", "", "/root"},
|
|
|
|
{"/root", "/", "/root"},
|
|
|
|
{"/root", "//", "/root"},
|
|
|
|
{"/a/b", "", "/a/b"},
|
|
|
|
{"//", "/", "//"},
|
|
|
|
{"//server", "path", "//server/path"},
|
|
|
|
{"//server/sub", "path", "//server/sub/path"},
|
|
|
|
{"//server", "//path", "//server/path"},
|
|
|
|
{"//server/sub", "//path", "//server/sub/path"},
|
|
|
|
{"//", "/", "//"},
|
|
|
|
{"//server", "path", "//server/path"},
|
|
|
|
{"//server/sub", "path", "//server/sub/path"},
|
|
|
|
{"//server", "//path", "//server/path"},
|
|
|
|
{"//server/sub", "//path", "//server/sub/path"},
|
|
|
|
{filepath.FromSlash("//server/sub"), filepath.FromSlash("//path"), "//server/sub/path"},
|
|
|
|
{"s3:", "", "s3:"},
|
|
|
|
{"s3:", ".", "s3:"},
|
|
|
|
{"s3:.", ".", "s3:"},
|
|
|
|
{"s3:", "..", "s3:"},
|
|
|
|
{"s3:dir", "sub", "s3:dir/sub"},
|
|
|
|
{"s3:dir", "/sub", "s3:dir/sub"},
|
|
|
|
{"s3:dir", "./sub", "s3:dir/sub"},
|
|
|
|
{"s3:/dir", "/sub/", "s3:/dir/sub"},
|
|
|
|
{"s3:dir", "..", "s3:dir"},
|
|
|
|
{"s3:dir", "/..", "s3:dir"},
|
|
|
|
{"s3:dir", "/../", "s3:dir"},
|
2018-10-09 11:35:27 +00:00
|
|
|
} {
|
2020-12-13 10:26:13 +00:00
|
|
|
got := JoinRootPath(test.remote, test.filePath)
|
|
|
|
assert.Equal(t, test.want, got, test)
|
2018-10-09 11:35:27 +00:00
|
|
|
}
|
|
|
|
}
|