@@ -46,6 +46,16 @@ local function plat_path(p)
46
46
return p :gsub (" /" , " \\ " )
47
47
end
48
48
49
+ local function root ()
50
+ if not iswin then
51
+ return " /"
52
+ end
53
+ if hasshellslash and vim .o .shellslash then
54
+ return " C:/"
55
+ end
56
+ return " C:\\ "
57
+ end
58
+
49
59
-- set up mock file with consistent eol regardless of system (git autocrlf settings)
50
60
-- simplifies reading tests
51
61
local licence_lines = {
@@ -301,16 +311,6 @@ describe("Path2", function()
301
311
end )
302
312
303
313
describe (" :make_relative" , function ()
304
- local root = function ()
305
- if not iswin then
306
- return " /"
307
- end
308
- if hasshellslash and vim .o .shellslash then
309
- return " C:/"
310
- end
311
- return " C:\\ "
312
- end
313
-
314
314
it_cross_plat (" can take absolute paths and make them relative to the cwd" , function ()
315
315
local p = Path :new { " lua" , " plenary" , " path.lua" }
316
316
local absolute = vim .fn .getcwd () .. path .sep .. p .filename
@@ -380,6 +380,57 @@ describe("Path2", function()
380
380
local expect = Path :new { " .." , " foo" , " bar" , " baz" }
381
381
assert .are .same (expect .filename , p :make_relative (cwd , true ))
382
382
end )
383
+
384
+ it_win (" handles drive letters case insensitively" , function ()
385
+ local p = Path :new { " C:/" , " foo" , " bar" , " baz" }
386
+ local cwd = Path :new { " c:/" , " foo" }
387
+ local expect = Path :new { " bar" , " baz" }
388
+ assert .are .same (expect .filename , p :make_relative (cwd ))
389
+ end )
390
+ end )
391
+
392
+ describe (" normalize" , function ()
393
+ it_cross_plat (" handles empty path" , function ()
394
+ local p = Path :new " "
395
+ assert .are .same (" ." , p :normalize ())
396
+ end )
397
+
398
+ it_cross_plat (" removes middle .." , function ()
399
+ local p = Path :new " lua/../lua/say.lua"
400
+ local expect = Path :new { " lua" , " say.lua" }
401
+ assert .are .same (expect .filename , p :normalize ())
402
+ end )
403
+
404
+ it_cross_plat (" walk up relative path" , function ()
405
+ local p = Path :new " async/../../lua/say.lua"
406
+ local expect = Path :new { " .." , " lua" , " say.lua" }
407
+ assert .are .same (expect .filename , p :normalize ())
408
+ end )
409
+
410
+ it_cross_plat (" handles absolute path" , function ()
411
+ local p = Path :new { root (), " a" , " .." , " a" , " b" }
412
+ local expect = Path :new { root (), " a" , " b" }
413
+ assert .are .same (expect .filename , p :normalize ())
414
+ end )
415
+
416
+ it_cross_plat (" makes relative" , function ()
417
+ local p = Path :new { path .home , " a" , " .." , " " , " a" , " b" }
418
+ local expect = Path :new { " a" , " b" }
419
+ assert .are .same (expect .filename , p :normalize (path .home ))
420
+ end )
421
+
422
+ it_cross_plat (" make relative walk_up" , function ()
423
+ local p = Path :new { path .home , " a" , " .." , " " , " a" , " b" }
424
+ local cwd = Path :new { path .home , " c" }
425
+ local expect = Path :new { " .." , " a" , " b" }
426
+ assert .are .same (expect .filename , p :normalize (cwd , true ))
427
+ end )
428
+
429
+ it_win (" windows drive relative paths" , function ()
430
+ local p = Path :new { " C:" , " a" , " .." , " " , " a" , " b" }
431
+ local expect = Path :new { " C:" , " a" , " b" }
432
+ assert .are .same (expect .filename , p :normalize ())
433
+ end )
383
434
end )
384
435
385
436
describe (" :shorten" , function ()
@@ -441,13 +492,6 @@ describe("Path2", function()
441
492
end )
442
493
end )
443
494
444
- local function assert_permission (expect , actual )
445
- if iswin then
446
- return
447
- end
448
- assert .equal (expect , actual )
449
- end
450
-
451
495
describe (" mkdir / rmdir" , function ()
452
496
after_each (function ()
453
497
uv .fs_rmdir " _dir_not_exist"
@@ -464,7 +508,6 @@ describe("Path2", function()
464
508
p :mkdir ()
465
509
assert .is_true (p :exists ())
466
510
assert .is_true (p :is_dir ())
467
- assert_permission (755 , p :permission ()) -- umask dependent, probably bad test
468
511
469
512
p :rmdir ()
470
513
assert .is_false (p :exists ())
@@ -497,17 +540,6 @@ describe("Path2", function()
497
540
assert .is_false (p :exists ())
498
541
assert .is_false (Path :new (" impossible" ):exists ())
499
542
end )
500
-
501
- it_cross_plat (" can set different modes" , function ()
502
- local p = Path :new " _dir_not_exist"
503
- assert .has_no_error (function ()
504
- p :mkdir { mode = 292 } -- o444
505
- end )
506
- assert_permission (444 , p :permission ())
507
-
508
- p :rmdir ()
509
- assert .is_false (p :exists ())
510
- end )
511
543
end )
512
544
513
545
describe (" touch/rm" , function ()
@@ -764,17 +796,6 @@ describe("Path2", function()
764
796
table.insert (trg_dirs , trg_dir :joinpath (dir ))
765
797
end
766
798
767
- -- vim.tbl_flatten doesn't work here as copy doesn't return a list
768
- local function flatten (ret , t )
769
- for _ , v in pairs (t ) do
770
- if type (v ) == " table" then
771
- flatten (ret , v )
772
- else
773
- table.insert (ret , v )
774
- end
775
- end
776
- end
777
-
778
799
before_each (function ()
779
800
-- generate {file}_{level}.lua on every directory level in src
780
801
-- src
@@ -1198,14 +1219,14 @@ SOFTWARE.]]
1198
1219
local p = Path :new " lua/plenary"
1199
1220
local res = assert (p :find_upwards " busted.lua" )
1200
1221
local expect = Path :new " lua/plenary/busted.lua"
1201
- assert .are .same (expect , res )
1222
+ assert .are .same (expect . filename , res . filename )
1202
1223
end )
1203
1224
1204
1225
it_cross_plat (" finds file in parent dir" , function ()
1205
1226
local p = Path :new " lua/plenary"
1206
1227
local res = assert (p :find_upwards " say.lua" )
1207
- local expect = Path : new " lua/say.lua"
1208
- assert .are .same (expect , res )
1228
+ local expect = vim . fn . fnamemodify ( " lua/say.lua" , " :p " )
1229
+ assert .are .same (expect , res . filename )
1209
1230
end )
1210
1231
1211
1232
it_cross_plat (" doesn't find file" , function ()
0 commit comments