Skip to content

Commit d90956b

Browse files
authored
Fix ~ normalization in Path:normalize() (#279)
Co-authored-by: fenuks <fenuks>
1 parent b5fd5ca commit d90956b

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

lua/plenary/path.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ function Path:normalize(cwd)
340340
self:make_relative(cwd)
341341

342342
-- Substitute home directory w/ "~"
343-
self.filename = self.filename:gsub("^" .. path.home, "~", 1)
343+
self.filename = self.filename:gsub("^" .. path.home, "~" .. path.sep, 1)
344344

345345
return _normalize_path(self.filename, self._cwd)
346346
end

tests/plenary/path_spec.lua

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,22 @@ describe("Path", function()
205205
p._cwd = "/tmp/lua"
206206
assert.are.same("/tmp/lua/plenary/path.lua", p:normalize())
207207
end)
208+
209+
it("can normalize ~ when file is within home directory (traling slash)", function()
210+
local home = "/home/test/"
211+
local p = Path:new { home, "./test_file" }
212+
p.path.home = home
213+
p._cwd = "/tmp/lua"
214+
assert.are.same("~/./test_file", p:normalize())
215+
end)
216+
217+
it("can normalize ~ when file is within home directory (no trailing slash)", function()
218+
local home = "/home/test"
219+
local p = Path:new { home, "./test_file" }
220+
p.path.home = home
221+
p._cwd = "/tmp/lua"
222+
assert.are.same("~//./test_file", p:normalize())
223+
end)
208224
end)
209225

210226
describe(":shorten", function()

0 commit comments

Comments
 (0)