Skip to content

Commit c54f77d

Browse files
fix: plenary.path normalize (#347)
1 parent 470d0e8 commit c54f77d

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

lua/plenary/path.lua

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ local function _normalize_path(filename, cwd)
8888
return filename
8989
end
9090

91+
-- handles redundant `./` in the middle
92+
local redundant = path.sep .. "." .. path.sep
93+
if filename:match(redundant) then
94+
filename = filename:gsub(redundant, path.sep)
95+
end
96+
9197
local out_file = filename
9298

9399
local has = string.find(filename, path.sep .. "..", 1, true) or string.find(filename, ".." .. path.sep, 1, true)
@@ -341,7 +347,8 @@ function Path:normalize(cwd)
341347
self:make_relative(cwd)
342348

343349
-- Substitute home directory w/ "~"
344-
self.filename = self.filename:gsub("^" .. path.home, "~" .. path.sep, 1)
350+
local removed_path_sep = path.home:gsub(path.sep .. "$", "")
351+
self.filename = self.filename:gsub("^" .. removed_path_sep, "~", 1)
345352

346353
return _normalize_path(self.filename, self._cwd)
347354
end

tests/plenary/path_spec.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,15 +211,15 @@ describe("Path", function()
211211
local p = Path:new { home, "./test_file" }
212212
p.path.home = home
213213
p._cwd = "/tmp/lua"
214-
assert.are.same("~/./test_file", p:normalize())
214+
assert.are.same("~/test_file", p:normalize())
215215
end)
216216

217217
it("can normalize ~ when file is within home directory (no trailing slash)", function()
218218
local home = "/home/test"
219219
local p = Path:new { home, "./test_file" }
220220
p.path.home = home
221221
p._cwd = "/tmp/lua"
222-
assert.are.same("~//./test_file", p:normalize())
222+
assert.are.same("~/test_file", p:normalize())
223223
end)
224224
end)
225225

0 commit comments

Comments
 (0)