| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package filesys |
| 8 |
|
| 9 |
import ( |
| 10 |
"os" |
| 11 |
"path/filepath" |
| 12 |
"reflect" |
| 13 |
"testing" |
| 14 |
) |
| 15 |
|
| 16 |
|
| 17 |
func TestFilePathMatch(t *testing.T) { |
| 18 |
cases := []struct { |
| 19 |
pattern string |
| 20 |
path string |
| 21 |
expected bool |
| 22 |
}{ |
| 23 |
{ |
| 24 |
pattern: "*e*", |
| 25 |
path: "hey", |
| 26 |
expected: true, |
| 27 |
}, |
| 28 |
{ |
| 29 |
pattern: "*e*", |
| 30 |
path: "hay", |
| 31 |
expected: false, |
| 32 |
}, |
| 33 |
{ |
| 34 |
pattern: "*e*", |
| 35 |
path: filepath.Join("h", "e", "y"), |
| 36 |
expected: false, |
| 37 |
}, |
| 38 |
{ |
| 39 |
pattern: "*/e/*", |
| 40 |
path: filepath.Join("h", "e", "y"), |
| 41 |
expected: true, |
| 42 |
}, |
| 43 |
{ |
| 44 |
pattern: "h/e/*", |
| 45 |
path: filepath.Join("h", "e", "y"), |
| 46 |
expected: true, |
| 47 |
}, |
| 48 |
{ |
| 49 |
pattern: "*/e/y", |
| 50 |
path: filepath.Join("h", "e", "y"), |
| 51 |
expected: true, |
| 52 |
}, |
| 53 |
{ |
| 54 |
pattern: "*/*/*", |
| 55 |
path: filepath.Join("h", "e", "y"), |
| 56 |
expected: true, |
| 57 |
}, |
| 58 |
{ |
| 59 |
pattern: "*/*/*", |
| 60 |
path: filepath.Join("h", "e", "y", "there"), |
| 61 |
expected: false, |
| 62 |
}, |
| 63 |
{ |
| 64 |
pattern: "*/*/*/t*e", |
| 65 |
path: filepath.Join("h", "e", "y", "there"), |
| 66 |
expected: true, |
| 67 |
}, |
| 68 |
} |
| 69 |
for _, item := range cases { |
| 70 |
match, err := filepath.Match(item.pattern, item.path) |
| 71 |
if err != nil { |
| 72 |
t.Fatalf("unexpected err: %v", err) |
| 73 |
} |
| 74 |
if match != item.expected { |
| 75 |
t.Fatalf("'%s' '%s' %v\n", item.pattern, item.path, match) |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
func TestFilePathSplit(t *testing.T) { |
| 82 |
cases := []struct { |
| 83 |
full string |
| 84 |
dir string |
| 85 |
file string |
| 86 |
}{ |
| 87 |
{ |
| 88 |
full: "", |
| 89 |
dir: "", |
| 90 |
file: "", |
| 91 |
}, |
| 92 |
{ |
| 93 |
full: SelfDir, |
| 94 |
dir: "", |
| 95 |
file: SelfDir, |
| 96 |
}, |
| 97 |
{ |
| 98 |
full: "rabbit.jpg", |
| 99 |
dir: "", |
| 100 |
file: "rabbit.jpg", |
| 101 |
}, |
| 102 |
{ |
| 103 |
full: "/", |
| 104 |
dir: "/", |
| 105 |
file: "", |
| 106 |
}, |
| 107 |
{ |
| 108 |
full: "/beans", |
| 109 |
dir: "/", |
| 110 |
file: "beans", |
| 111 |
}, |
| 112 |
{ |
| 113 |
full: "/home/foo/bar", |
| 114 |
dir: "/home/foo/", |
| 115 |
file: "bar", |
| 116 |
}, |
| 117 |
{ |
| 118 |
full: "/usr/local/", |
| 119 |
dir: "/usr/local/", |
| 120 |
file: "", |
| 121 |
}, |
| 122 |
{ |
| 123 |
full: "/usr//local//go", |
| 124 |
dir: "/usr//local//", |
| 125 |
file: "go", |
| 126 |
}, |
| 127 |
} |
| 128 |
for _, p := range cases { |
| 129 |
dir, file := filepath.Split(p.full) |
| 130 |
if dir != p.dir || file != p.file { |
| 131 |
t.Fatalf( |
| 132 |
"in '%s',\ngot dir='%s' (expected '%s'),\n got file='%s' (expected %s).", |
| 133 |
p.full, dir, p.dir, file, p.file) |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
func TestPathSplitAndJoin(t *testing.T) { |
| 139 |
cases := map[string]struct { |
| 140 |
original string |
| 141 |
expected []string |
| 142 |
}{ |
| 143 |
"Empty": { |
| 144 |
original: "", |
| 145 |
expected: []string{}, |
| 146 |
}, |
| 147 |
"One": { |
| 148 |
original: "hello", |
| 149 |
expected: []string{"hello"}, |
| 150 |
}, |
| 151 |
"Two": { |
| 152 |
original: "hello/there", |
| 153 |
expected: []string{"hello", "there"}, |
| 154 |
}, |
| 155 |
"Three": { |
| 156 |
original: "hello/my/friend", |
| 157 |
expected: []string{"hello", "my", "friend"}, |
| 158 |
}, |
| 159 |
} |
| 160 |
for n, c := range cases { |
| 161 |
f := func(t *testing.T, original string, expected []string) { |
| 162 |
t.Helper() |
| 163 |
actual := PathSplit(original) |
| 164 |
if len(actual) != len(expected) { |
| 165 |
t.Fatalf( |
| 166 |
"expected len %d, got len %d", |
| 167 |
len(expected), len(actual)) |
| 168 |
} |
| 169 |
for i := range expected { |
| 170 |
if expected[i] != actual[i] { |
| 171 |
t.Fatalf( |
| 172 |
"at i=%d, expected '%s', got '%s'", |
| 173 |
i, expected[i], actual[i]) |
| 174 |
} |
| 175 |
} |
| 176 |
joined := PathJoin(actual) |
| 177 |
if joined != original { |
| 178 |
t.Fatalf( |
| 179 |
"when rejoining, expected '%s', got '%s'", |
| 180 |
original, joined) |
| 181 |
} |
| 182 |
} |
| 183 |
t.Run("relative"+n, func(t *testing.T) { |
| 184 |
f(t, c.original, c.expected) |
| 185 |
}) |
| 186 |
t.Run("absolute"+n, func(t *testing.T) { |
| 187 |
f(t, |
| 188 |
string(os.PathSeparator)+c.original, |
| 189 |
append([]string{""}, c.expected...)) |
| 190 |
}) |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
func TestInsertPathPart(t *testing.T) { |
| 195 |
cases := map[string]struct { |
| 196 |
original string |
| 197 |
pos int |
| 198 |
part string |
| 199 |
expected string |
| 200 |
}{ |
| 201 |
"rootOne": { |
| 202 |
original: "/", |
| 203 |
pos: 0, |
| 204 |
part: "___", |
| 205 |
expected: "/___", |
| 206 |
}, |
| 207 |
"rootTwo": { |
| 208 |
original: "/", |
| 209 |
pos: 444, |
| 210 |
part: "___", |
| 211 |
expected: "/___", |
| 212 |
}, |
| 213 |
"rootedFirst": { |
| 214 |
original: "/apple", |
| 215 |
pos: 0, |
| 216 |
part: "___", |
| 217 |
expected: "/___/apple", |
| 218 |
}, |
| 219 |
"rootedSecond": { |
| 220 |
original: "/apple", |
| 221 |
pos: 444, |
| 222 |
part: "___", |
| 223 |
expected: "/apple/___", |
| 224 |
}, |
| 225 |
"rootedThird": { |
| 226 |
original: "/apple/banana", |
| 227 |
pos: 444, |
| 228 |
part: "___", |
| 229 |
expected: "/apple/banana/___", |
| 230 |
}, |
| 231 |
"emptyLow": { |
| 232 |
original: "", |
| 233 |
pos: -3, |
| 234 |
part: "___", |
| 235 |
expected: "___", |
| 236 |
}, |
| 237 |
"emptyHigh": { |
| 238 |
original: "", |
| 239 |
pos: 444, |
| 240 |
part: "___", |
| 241 |
expected: "___", |
| 242 |
}, |
| 243 |
"peachPie": { |
| 244 |
original: "a/nice/warm/pie", |
| 245 |
pos: 3, |
| 246 |
part: "PEACH", |
| 247 |
expected: "a/nice/warm/PEACH/pie", |
| 248 |
}, |
| 249 |
"rootedPeachPie": { |
| 250 |
original: "/a/nice/warm/pie", |
| 251 |
pos: 3, |
| 252 |
part: "PEACH", |
| 253 |
expected: "/a/nice/warm/PEACH/pie", |
| 254 |
}, |
| 255 |
"longStart": { |
| 256 |
original: "a/b/c/d/e/f", |
| 257 |
pos: 0, |
| 258 |
part: "___", |
| 259 |
expected: "___/a/b/c/d/e/f", |
| 260 |
}, |
| 261 |
"rootedLongStart": { |
| 262 |
original: "/a/b/c/d/e/f", |
| 263 |
pos: 0, |
| 264 |
part: "___", |
| 265 |
expected: "/___/a/b/c/d/e/f", |
| 266 |
}, |
| 267 |
"longMiddle": { |
| 268 |
original: "a/b/c/d/e/f", |
| 269 |
pos: 3, |
| 270 |
part: "___", |
| 271 |
expected: "a/b/c/___/d/e/f", |
| 272 |
}, |
| 273 |
"rootedLongMiddle": { |
| 274 |
original: "/a/b/c/d/e/f", |
| 275 |
pos: 3, |
| 276 |
part: "___", |
| 277 |
expected: "/a/b/c/___/d/e/f", |
| 278 |
}, |
| 279 |
"longEnd": { |
| 280 |
original: "a/b/c/d/e/f", |
| 281 |
pos: 444, |
| 282 |
part: "___", |
| 283 |
expected: "a/b/c/d/e/f/___", |
| 284 |
}, |
| 285 |
"rootedLongEnd": { |
| 286 |
original: "/a/b/c/d/e/f", |
| 287 |
pos: 444, |
| 288 |
part: "___", |
| 289 |
expected: "/a/b/c/d/e/f/___", |
| 290 |
}, |
| 291 |
} |
| 292 |
for n, c := range cases { |
| 293 |
t.Run(n, func(t *testing.T) { |
| 294 |
actual := InsertPathPart(c.original, c.pos, c.part) |
| 295 |
if actual != c.expected { |
| 296 |
t.Fatalf("expected '%s', got '%s'", c.expected, actual) |
| 297 |
} |
| 298 |
}) |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
func TestStripTrailingSeps(t *testing.T) { |
| 303 |
cases := []struct { |
| 304 |
full string |
| 305 |
rem string |
| 306 |
}{ |
| 307 |
{ |
| 308 |
full: "foo", |
| 309 |
rem: "foo", |
| 310 |
}, |
| 311 |
{ |
| 312 |
full: "", |
| 313 |
rem: "", |
| 314 |
}, |
| 315 |
{ |
| 316 |
full: "foo/", |
| 317 |
rem: "foo", |
| 318 |
}, |
| 319 |
{ |
| 320 |
full: "foo///bar///", |
| 321 |
rem: "foo///bar", |
| 322 |
}, |
| 323 |
{ |
| 324 |
full: "/////", |
| 325 |
rem: "", |
| 326 |
}, |
| 327 |
{ |
| 328 |
full: "/", |
| 329 |
rem: "", |
| 330 |
}, |
| 331 |
} |
| 332 |
for _, p := range cases { |
| 333 |
dir := StripTrailingSeps(p.full) |
| 334 |
if dir != p.rem { |
| 335 |
t.Fatalf( |
| 336 |
"in '%s', got dir='%s' (expected '%s')", |
| 337 |
p.full, dir, p.rem) |
| 338 |
} |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
func TestStripLeadingSeps(t *testing.T) { |
| 343 |
cases := []struct { |
| 344 |
full string |
| 345 |
rem string |
| 346 |
}{ |
| 347 |
{ |
| 348 |
full: "foo", |
| 349 |
rem: "foo", |
| 350 |
}, |
| 351 |
{ |
| 352 |
full: "", |
| 353 |
rem: "", |
| 354 |
}, |
| 355 |
{ |
| 356 |
full: "/foo", |
| 357 |
rem: "foo", |
| 358 |
}, |
| 359 |
{ |
| 360 |
full: "///foo///bar///", |
| 361 |
rem: "foo///bar///", |
| 362 |
}, |
| 363 |
{ |
| 364 |
full: "/////", |
| 365 |
rem: "", |
| 366 |
}, |
| 367 |
{ |
| 368 |
full: "/", |
| 369 |
rem: "", |
| 370 |
}, |
| 371 |
} |
| 372 |
for _, p := range cases { |
| 373 |
dir := StripLeadingSeps(p.full) |
| 374 |
if dir != p.rem { |
| 375 |
t.Fatalf( |
| 376 |
"in '%s', got dir='%s' (expected '%s')", |
| 377 |
p.full, dir, p.rem) |
| 378 |
} |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
func TestIsHiddenFilePath(t *testing.T) { |
| 383 |
tests := map[string]struct { |
| 384 |
paths []string |
| 385 |
expectHidden bool |
| 386 |
}{ |
| 387 |
"hiddenGlobs": { |
| 388 |
expectHidden: true, |
| 389 |
paths: []string{ |
| 390 |
".*", |
| 391 |
"/.*", |
| 392 |
"dir/.*", |
| 393 |
"dir1/dir2/dir3/.*", |
| 394 |
"../../.*", |
| 395 |
"../../dir/.*", |
| 396 |
}, |
| 397 |
}, |
| 398 |
"visibleGlobes": { |
| 399 |
expectHidden: false, |
| 400 |
paths: []string{ |
| 401 |
"*", |
| 402 |
"/*", |
| 403 |
"dir/*", |
| 404 |
"dir1/dir2/dir3/*", |
| 405 |
"../../*", |
| 406 |
"../../dir/*", |
| 407 |
}, |
| 408 |
}, |
| 409 |
"hiddenFiles": { |
| 410 |
expectHidden: true, |
| 411 |
paths: []string{ |
| 412 |
".root_file.xtn", |
| 413 |
"/.file_1.xtn", |
| 414 |
"dir/.file_2.xtn", |
| 415 |
"dir1/dir2/dir3/.file_3.xtn", |
| 416 |
"../../.file_4.xtn", |
| 417 |
"../../dir/.file_5.xtn", |
| 418 |
}, |
| 419 |
}, |
| 420 |
"visibleFiles": { |
| 421 |
expectHidden: false, |
| 422 |
paths: []string{ |
| 423 |
"root_file.xtn", |
| 424 |
"/file_1.xtn", |
| 425 |
"dir/file_2.xtn", |
| 426 |
"dir1/dir2/dir3/file_3.xtn", |
| 427 |
"../../file_4.xtn", |
| 428 |
"../../dir/file_5.xtn", |
| 429 |
}, |
| 430 |
}, |
| 431 |
} |
| 432 |
for n, c := range tests { |
| 433 |
t.Run(n, func(t *testing.T) { |
| 434 |
for _, path := range c.paths { |
| 435 |
actual := IsHiddenFilePath(path) |
| 436 |
if actual != c.expectHidden { |
| 437 |
t.Fatalf("For file path %q, expected hidden: %v, got hidden: %v", path, c.expectHidden, actual) |
| 438 |
} |
| 439 |
} |
| 440 |
}) |
| 441 |
} |
| 442 |
} |
| 443 |
|
| 444 |
func TestRemoveHiddenFiles(t *testing.T) { |
| 445 |
paths := []string{ |
| 446 |
"file1.xtn", |
| 447 |
".file2.xtn", |
| 448 |
"dir/fa1", |
| 449 |
"dir/fa2", |
| 450 |
"dir/.fa3", |
| 451 |
"../../.fa4", |
| 452 |
"../../fa5", |
| 453 |
"../../dir/fa6", |
| 454 |
"../../dir/.fa7", |
| 455 |
} |
| 456 |
result := RemoveHiddenFiles(paths) |
| 457 |
expected := []string{ |
| 458 |
"file1.xtn", |
| 459 |
"dir/fa1", |
| 460 |
"dir/fa2", |
| 461 |
"../../fa5", |
| 462 |
"../../dir/fa6", |
| 463 |
} |
| 464 |
if !reflect.DeepEqual(result, expected) { |
| 465 |
t.Fatalf("Hidden dirs not correctly removed, expected %v but got %v\n", expected, result) |
| 466 |
} |
| 467 |
} |