fix(vim.fs.normalize): normalize case for windows drive letter

Also add tests for the current path casing behavior so it doesn't get
accidentally changed.
This commit is contained in:
dundargoc
2025-01-02 16:29:00 +01:00
committed by dundargoc
parent 69aa33d890
commit a8ace2c58a
2 changed files with 28 additions and 2 deletions

View File

@@ -629,8 +629,8 @@ function M.normalize(path, opts)
return prefix .. path
end
-- Remove extraneous slashes from the prefix
prefix = prefix:gsub('/+', '/')
-- Ensure capital drive and remove extraneous slashes from the prefix
prefix = prefix:gsub('^%a:', string.upper):gsub('/+', '/')
end
if not opts._fast then

View File

@@ -340,6 +340,9 @@ describe('vim.fs', function()
end)
describe('normalize()', function()
-- local function vim.fs.normalize(path, opts)
-- return exec_lua([[return vim.fs.vim.fs.normalize(...)]], path, opts)
-- end
it('removes trailing /', function()
eq('/home/user', vim.fs.normalize('/home/user/'))
end)
@@ -373,6 +376,29 @@ describe('vim.fs', function()
eq('/foo/bar', vim.fs.normalize('/foo//bar////', posix_opts))
end)
it('normalizes drive letter', function()
eq('C:/', vim.fs.normalize('C:/', win_opts))
eq('C:/', vim.fs.normalize('c:/', win_opts))
eq('D:/', vim.fs.normalize('d:/', win_opts))
eq('C:', vim.fs.normalize('C:', win_opts))
eq('C:', vim.fs.normalize('c:', win_opts))
eq('D:', vim.fs.normalize('d:', win_opts))
eq('C:/foo/test', vim.fs.normalize('C:/foo/test/', win_opts))
eq('C:/foo/test', vim.fs.normalize('c:/foo/test/', win_opts))
eq('D:foo/test', vim.fs.normalize('D:foo/test/', win_opts))
eq('D:foo/test', vim.fs.normalize('d:foo/test/', win_opts))
end)
it('does not change case on paths, see #31833', function()
eq('TEST', vim.fs.normalize('TEST', win_opts))
eq('test', vim.fs.normalize('test', win_opts))
eq('C:/FOO/test', vim.fs.normalize('C:/FOO/test', win_opts))
eq('C:/foo/test', vim.fs.normalize('C:/foo/test', win_opts))
eq('//SERVER/SHARE/FOO/BAR', vim.fs.normalize('//SERVER/SHARE/FOO/BAR', win_opts))
eq('//server/share/foo/bar', vim.fs.normalize('//server/share/foo/bar', win_opts))
eq('C:/FOO/test', vim.fs.normalize('c:/FOO/test', win_opts))
end)
it('allows backslashes on unix-based os', function()
eq('/home/user/hello\\world', vim.fs.normalize('/home/user/hello\\world', posix_opts))
end)