Skip to content

Commit 15199f8

Browse files
authored
fix: path normalization works with esoteric usernames (#290)
* fix: path normalization works with esoteric usernames Usernames with dashes at the end are perfectly valid unix user names, but they're seen as a regular expression pattern in Lua. Switch to using string.find instead of string.gsub to avoid the pattern matching issue. * Fix syntax error Using end here is no good, even though it worked in the luajit repl. 😆 * Fix typo in spec name * Clean the path we send to _normalize_path Also fix the specs to match the new behavior.
1 parent 78dde9b commit 15199f8

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

lua/plenary/path.lua

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -347,10 +347,14 @@ function Path:normalize(cwd)
347347
self:make_relative(cwd)
348348

349349
-- Substitute home directory w/ "~"
350-
local removed_path_sep = path.home:gsub(path.sep .. "$", "")
351-
self.filename = self.filename:gsub("^" .. removed_path_sep, "~", 1)
350+
-- string.gsub is not useful here because usernames with dashes at the end
351+
-- will be seen as a regexp pattern rather than a raw string
352+
local start, finish = string.find(self.filename, path.home, 1, true)
353+
if start == 1 then
354+
self.filename = "~" .. path.sep .. string.sub(self.filename, (finish + 1), -1)
355+
end
352356

353-
return _normalize_path(self.filename, self._cwd)
357+
return _normalize_path(clean(self.filename), self._cwd)
354358
end
355359

356360
local function shorten_len(filename, len, exclude)

tests/plenary/path_spec.lua

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ describe("Path", function()
212212
assert.are.same("/tmp/lua/plenary/path.lua", p:normalize())
213213
end)
214214

215-
it("can normalize ~ when file is within home directory (traling slash)", function()
215+
it("can normalize ~ when file is within home directory (trailing slash)", function()
216216
local home = "/home/test/"
217217
local p = Path:new { home, "./test_file" }
218218
p.path.home = home
@@ -227,6 +227,14 @@ describe("Path", function()
227227
p._cwd = "/tmp/lua"
228228
assert.are.same("~/test_file", p:normalize())
229229
end)
230+
231+
it("handles usernames with a dash at the end", function()
232+
local home = "/home/mattr-"
233+
local p = Path:new { home, "test_file" }
234+
p.path.home = home
235+
p._cwd = "/tmp/lua"
236+
assert.are.same("~/test_file", p:normalize())
237+
end)
230238
end)
231239

232240
describe(":shorten", function()

0 commit comments

Comments
 (0)