Skip to content

Commit 34c4f55

Browse files
committed
minor fixes
1 parent 030ee62 commit 34c4f55

File tree

2 files changed

+29
-32
lines changed

2 files changed

+29
-32
lines changed

lua/plenary/path2.lua

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ function _PosixPath:join(path, ...)
222222
for _, p in ipairs(paths) do
223223
if p:sub(1, 1) == self.sep then
224224
parts = { p } -- is absolute, ignore previous path, later paths take precedence
225-
elseif path == "" or path:sub(-1) == self.sep then
225+
elseif #parts > 1 and parts[#parts]:sub(-1) == self.sep then
226226
table.insert(parts, p)
227227
else
228228
table.insert(parts, self.sep .. p)
@@ -231,17 +231,6 @@ function _PosixPath:join(path, ...)
231231
return table.concat(parts)
232232
end
233233

234-
--[[
235-
236-
for b in map(os.fspath, p):
237-
if b.startswith(sep):
238-
path = b
239-
elif not path or path.endswith(sep):
240-
path += b
241-
else:
242-
path += sep + b
243-
]]
244-
245234
local S_IF = {
246235
-- S_IFDIR = 0o040000 # directory
247236
DIR = 0x4000,
@@ -736,7 +725,7 @@ function Path:make_relative(to, walk_up)
736725
end
737726

738727
if self:is_relative(to) then
739-
return Path:new(abs:sub(#to:absolute() + 1)).filename
728+
return Path:new((abs:sub(#to:absolute() + 1):gsub("^" .. self.sep, ""))).filename
740729
end
741730

742731
if not walk_up then

tests/plenary/path2_spec.lua

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ describe("Path2", function()
5555
{ { "lua/../README.md" }, "lua/../README.md" },
5656
{ { "./lua/../README.md" }, "lua/../README.md" },
5757
{ "./lua//..//README.md", "lua/../README.md" },
58+
{ { "foo", "bar", "baz" }, "foo/bar/baz" },
5859
{ "foo/bar/", "foo/bar" },
5960
{ { readme_path }, readme_path },
6061
{ { readme_path, license_path }, license_path }, -- takes only the last abs path
@@ -269,7 +270,16 @@ describe("Path2", function()
269270
end)
270271

271272
describe(":make_relative", function()
272-
local root = iswin and "c:\\" or "/"
273+
local root = function()
274+
if not iswin then
275+
return "/"
276+
end
277+
if hasshellslash and vim.o.shellslash then
278+
return "C:/"
279+
end
280+
return "C:\\"
281+
end
282+
273283
it_cross_plat("can take absolute paths and make them relative to the cwd", function()
274284
local p = Path:new { "lua", "plenary", "path.lua" }
275285
local absolute = vim.fn.getcwd() .. path.sep .. p.filename
@@ -278,7 +288,7 @@ describe("Path2", function()
278288
end)
279289

280290
it_cross_plat("can take absolute paths and make them relative to a given path", function()
281-
local r = Path:new { root, "home", "prime" }
291+
local r = Path:new { root(), "home", "prime" }
282292
local p = Path:new { "aoeu", "agen.lua" }
283293
local absolute = r.filename .. path.sep .. p.filename
284294
local relative = Path:new(absolute):make_relative(r.filename)
@@ -293,30 +303,29 @@ describe("Path2", function()
293303
end)
294304

295305
it_cross_plat("can take double separator absolute paths and make them relative to a given path", function()
296-
local r = Path:new { root, "home", "prime" }
306+
local r = Path:new { root(), "home", "prime" }
297307
local p = Path:new { "aoeu", "agen.lua" }
298308
local absolute = r.filename .. path.sep .. path.sep .. p.filename
299309
local relative = Path:new(absolute):make_relative(r.filename)
300310
assert.are.same(p.filename, relative)
301311
end)
302312

303313
it_cross_plat("can take absolute paths and make them relative to a given path with trailing separator", function()
304-
local r = Path:new { root, "home", "prime" }
314+
local r = Path:new { root(), "home", "prime" }
305315
local p = Path:new { "aoeu", "agen.lua" }
306316
local absolute = r.filename .. path.sep .. p.filename
307317
local relative = Path:new(absolute):make_relative(r.filename .. path.sep)
308318
assert.are.same(p.filename, relative)
309319
end)
310320

311321
it_cross_plat("can take absolute paths and make them relative to the root directory", function()
312-
local p = Path:new { root, "prime", "aoeu", "agen.lua" }
313-
local absolute = root .. p.filename
314-
local relative = Path:new(absolute):make_relative(root)
315-
assert.are.same(p.filename, relative)
322+
local p = Path:new { root(), "prime", "aoeu", "agen.lua" }
323+
local relative = Path:new(p:absolute()):make_relative(root())
324+
assert.are.same((p.filename:gsub(root(), "")), relative)
316325
end)
317326

318327
it_cross_plat("can take absolute paths and make them relative to themselves", function()
319-
local p = Path:new { root, "home", "prime", "aoeu", "agen.lua" }
328+
local p = Path:new { root(), "home", "prime", "aoeu", "agen.lua" }
320329
local relative = Path:new(p.filename):make_relative(p.filename)
321330
assert.are.same(".", relative)
322331
end)
@@ -445,17 +454,16 @@ describe("Path2", function()
445454
assert.is_false(Path:new("impossible"):exists())
446455
end)
447456

448-
-- it_cross_plat("can set different modes", function()
449-
-- local p = Path:new "_dir_not_exist"
450-
-- assert.has_no_error(function()
451-
-- p:mkdir { mode = 0755 }
452-
-- end)
453-
-- print(vim.uv.fs_stat(p:absolute()).mode)
454-
-- assert_permission(0755, p:permission())
457+
it_cross_plat("can set different modes", function()
458+
local p = Path:new "_dir_not_exist"
459+
assert.has_no_error(function()
460+
p:mkdir { mode = 0755 }
461+
end)
462+
assert_permission(0755, p:permission())
455463

456-
-- p:rmdir()
457-
-- assert.is_false(p:exists())
458-
-- end)
464+
p:rmdir()
465+
assert.is_false(p:exists())
466+
end)
459467
end)
460468

461469
describe("parents", function()

0 commit comments

Comments
 (0)