github.com/xhghs/rclone@v1.51.1-0.20200430155106-e186a28cced8/fs/fspath/path_test.go (about) 1 package fspath 2 3 import ( 4 "fmt" 5 "runtime" 6 "strings" 7 "testing" 8 9 "github.com/stretchr/testify/assert" 10 ) 11 12 func TestCheckConfigName(t *testing.T) { 13 for _, test := range []struct { 14 in string 15 want error 16 }{ 17 {"remote", nil}, 18 {"", errInvalidCharacters}, 19 {":remote:", errInvalidCharacters}, 20 {"remote:", errInvalidCharacters}, 21 {"rem:ote", errInvalidCharacters}, 22 {"rem/ote", errInvalidCharacters}, 23 {"rem\\ote", errInvalidCharacters}, 24 {"[remote", errInvalidCharacters}, 25 {"*", errInvalidCharacters}, 26 } { 27 got := CheckConfigName(test.in) 28 assert.Equal(t, test.want, got, test.in) 29 } 30 } 31 32 func TestCheckRemoteName(t *testing.T) { 33 for _, test := range []struct { 34 in string 35 want error 36 }{ 37 {":remote:", nil}, 38 {"remote:", nil}, 39 {"", errInvalidCharacters}, 40 {"rem:ote", errInvalidCharacters}, 41 {"rem:ote:", errInvalidCharacters}, 42 {"remote", errInvalidCharacters}, 43 {"rem/ote:", errInvalidCharacters}, 44 {"rem\\ote:", errInvalidCharacters}, 45 {"[remote:", errInvalidCharacters}, 46 {"*:", errInvalidCharacters}, 47 } { 48 got := CheckRemoteName(test.in) 49 assert.Equal(t, test.want, got, test.in) 50 } 51 } 52 53 func TestParse(t *testing.T) { 54 for _, test := range []struct { 55 in, wantConfigName, wantFsPath string 56 wantErr error 57 }{ 58 {"", "", "", nil}, 59 {":", "", "", errInvalidCharacters}, 60 {"::", ":", "", errInvalidCharacters}, 61 {":/:", "", "/:", errInvalidCharacters}, 62 {"/:", "", "/:", nil}, 63 {"\\backslash:", "", "\\backslash:", nil}, 64 {"/slash:", "", "/slash:", nil}, 65 {"with\\backslash:", "", "with\\backslash:", nil}, 66 {"with/slash:", "", "with/slash:", nil}, 67 {"/path/to/file", "", "/path/to/file", nil}, 68 {"/path:/to/file", "", "/path:/to/file", nil}, 69 {"./path:/to/file", "", "./path:/to/file", nil}, 70 {"./:colon.txt", "", "./:colon.txt", nil}, 71 {"path/to/file", "", "path/to/file", nil}, 72 {"remote:path/to/file", "remote", "path/to/file", nil}, 73 {"rem*ote:path/to/file", "rem*ote", "path/to/file", errInvalidCharacters}, 74 {"remote:/path/to/file", "remote", "/path/to/file", nil}, 75 {"rem.ote:/path/to/file", "rem.ote", "/path/to/file", errInvalidCharacters}, 76 {":backend:/path/to/file", ":backend", "/path/to/file", nil}, 77 {":bac*kend:/path/to/file", ":bac*kend", "/path/to/file", errInvalidCharacters}, 78 } { 79 gotConfigName, gotFsPath, gotErr := Parse(test.in) 80 if runtime.GOOS == "windows" { 81 test.wantFsPath = strings.Replace(test.wantFsPath, `\`, `/`, -1) 82 } 83 assert.Equal(t, test.wantErr, gotErr) 84 assert.Equal(t, test.wantConfigName, gotConfigName) 85 assert.Equal(t, test.wantFsPath, gotFsPath) 86 } 87 } 88 89 func TestSplit(t *testing.T) { 90 for _, test := range []struct { 91 remote, wantParent, wantLeaf string 92 wantErr error 93 }{ 94 {"", "", "", nil}, 95 96 {"remote:", "remote:", "", nil}, 97 {"remote:potato", "remote:", "potato", nil}, 98 {"remote:/", "remote:/", "", nil}, 99 {"remote:/potato", "remote:/", "potato", nil}, 100 {"remote:/potato/potato", "remote:/potato/", "potato", nil}, 101 {"remote:potato/sausage", "remote:potato/", "sausage", nil}, 102 {"rem.ote:potato/sausage", "", "", errInvalidCharacters}, 103 104 {":remote:", ":remote:", "", nil}, 105 {":remote:potato", ":remote:", "potato", nil}, 106 {":remote:/", ":remote:/", "", nil}, 107 {":remote:/potato", ":remote:/", "potato", nil}, 108 {":remote:/potato/potato", ":remote:/potato/", "potato", nil}, 109 {":remote:potato/sausage", ":remote:potato/", "sausage", nil}, 110 {":rem[ote:potato/sausage", "", "", errInvalidCharacters}, 111 112 {"/", "/", "", nil}, 113 {"/root", "/", "root", nil}, 114 {"/a/b", "/a/", "b", nil}, 115 {"root", "", "root", nil}, 116 {"a/b", "a/", "b", nil}, 117 {"root/", "root/", "", nil}, 118 {"a/b/", "a/b/", "", nil}, 119 } { 120 gotParent, gotLeaf, gotErr := Split(test.remote) 121 assert.Equal(t, test.wantErr, gotErr) 122 assert.Equal(t, test.wantParent, gotParent, test.remote) 123 assert.Equal(t, test.wantLeaf, gotLeaf, test.remote) 124 if gotErr == nil { 125 assert.Equal(t, test.remote, gotParent+gotLeaf, fmt.Sprintf("%s: %q + %q != %q", test.remote, gotParent, gotLeaf, test.remote)) 126 } 127 } 128 } 129 func TestJoinRootPath(t *testing.T) { 130 for _, test := range []struct { 131 elements []string 132 want string 133 }{ 134 {nil, ""}, 135 {[]string{""}, ""}, 136 {[]string{"/"}, "/"}, 137 {[]string{"/", "/"}, "/"}, 138 {[]string{"/", "//"}, "/"}, 139 {[]string{"/root", ""}, "/root"}, 140 {[]string{"/root", "/"}, "/root"}, 141 {[]string{"/root", "//"}, "/root"}, 142 {[]string{"/a/b"}, "/a/b"}, 143 {[]string{"//", "/"}, "//"}, 144 {[]string{"//server", "path"}, "//server/path"}, 145 {[]string{"//server/sub", "path"}, "//server/sub/path"}, 146 {[]string{"//server", "//path"}, "//server/path"}, 147 {[]string{"//server/sub", "//path"}, "//server/sub/path"}, 148 {[]string{"", "//", "/"}, "//"}, 149 {[]string{"", "//server", "path"}, "//server/path"}, 150 {[]string{"", "//server/sub", "path"}, "//server/sub/path"}, 151 {[]string{"", "//server", "//path"}, "//server/path"}, 152 {[]string{"", "//server/sub", "//path"}, "//server/sub/path"}, 153 } { 154 got := JoinRootPath(test.elements...) 155 assert.Equal(t, test.want, got) 156 } 157 }