@@ -46,6 +46,16 @@ local function plat_path(p)
4646 return p :gsub (" /" , " \\ " )
4747end
4848
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+
4959-- set up mock file with consistent eol regardless of system (git autocrlf settings)
5060-- simplifies reading tests
5161local licence_lines = {
@@ -301,16 +311,6 @@ describe("Path2", function()
301311 end )
302312
303313 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-
314314 it_cross_plat (" can take absolute paths and make them relative to the cwd" , function ()
315315 local p = Path :new { " lua" , " plenary" , " path.lua" }
316316 local absolute = vim .fn .getcwd () .. path .sep .. p .filename
@@ -380,6 +380,57 @@ describe("Path2", function()
380380 local expect = Path :new { " .." , " foo" , " bar" , " baz" }
381381 assert .are .same (expect .filename , p :make_relative (cwd , true ))
382382 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 )
383434 end )
384435
385436 describe (" :shorten" , function ()
@@ -441,13 +492,6 @@ describe("Path2", function()
441492 end )
442493 end )
443494
444- local function assert_permission (expect , actual )
445- if iswin then
446- return
447- end
448- assert .equal (expect , actual )
449- end
450-
451495 describe (" mkdir / rmdir" , function ()
452496 after_each (function ()
453497 uv .fs_rmdir " _dir_not_exist"
@@ -464,7 +508,6 @@ describe("Path2", function()
464508 p :mkdir ()
465509 assert .is_true (p :exists ())
466510 assert .is_true (p :is_dir ())
467- assert_permission (755 , p :permission ()) -- umask dependent, probably bad test
468511
469512 p :rmdir ()
470513 assert .is_false (p :exists ())
@@ -497,17 +540,6 @@ describe("Path2", function()
497540 assert .is_false (p :exists ())
498541 assert .is_false (Path :new (" impossible" ):exists ())
499542 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 )
511543 end )
512544
513545 describe (" touch/rm" , function ()
@@ -764,17 +796,6 @@ describe("Path2", function()
764796 table.insert (trg_dirs , trg_dir :joinpath (dir ))
765797 end
766798
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-
778799 before_each (function ()
779800 -- generate {file}_{level}.lua on every directory level in src
780801 -- src
@@ -1198,14 +1219,14 @@ SOFTWARE.]]
11981219 local p = Path :new " lua/plenary"
11991220 local res = assert (p :find_upwards " busted.lua" )
12001221 local expect = Path :new " lua/plenary/busted.lua"
1201- assert .are .same (expect , res )
1222+ assert .are .same (expect . filename , res . filename )
12021223 end )
12031224
12041225 it_cross_plat (" finds file in parent dir" , function ()
12051226 local p = Path :new " lua/plenary"
12061227 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 )
12091230 end )
12101231
12111232 it_cross_plat (" doesn't find file" , function ()
0 commit comments