fix(fs): make normalize() work with '/' path (#24047)

Problem: Current implementation of "remove trailing /" doesn't
account for the case of literal '/' as path.
Solution: Remove trailing / only if it preceded by something else.

Co-authored by: notomo <notomo.motono@gmail.com>
This commit is contained in:
Evgeni Chasnovski 2023-06-18 14:49:33 +03:00 committed by GitHub
parent 7e301ed5b9
commit 8a7e3353eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 3 deletions

View File

@ -348,9 +348,7 @@ function M.normalize(path, opts)
path = path:gsub('%$([%w_]+)', vim.uv.os_getenv)
end
path = path:gsub('\\', '/'):gsub('/+', '/')
return path:sub(-1) == '/' and path:sub(1, -2) or path
return (path:gsub('\\', '/'):gsub('/+', '/'):gsub('(.)/$', '%1'))
end
return M

View File

@ -281,6 +281,12 @@ describe('vim.fs', function()
it('works with backward slashes', function()
eq('C:/Users/jdoe', exec_lua [[ return vim.fs.normalize('C:\\Users\\jdoe') ]])
end)
it('removes trailing /', function()
eq('/home/user', exec_lua [[ return vim.fs.normalize('/home/user/') ]])
end)
it('works with /', function()
eq('/', exec_lua [[ return vim.fs.normalize('/') ]])
end)
it('works with ~', function()
eq( exec_lua([[
local home = ...