aboutsummaryrefslogtreecommitdiff
path: root/test/functional/lua/fs_spec.lua
diff options
context:
space:
mode:
authorFamiu Haque <famiuhaque@proton.me>2024-04-05 14:48:13 +0600
committerdundargoc <33953936+dundargoc@users.noreply.github.com>2024-12-28 11:40:39 +0100
commit518070731003e30ea7eee96e76ccdf7b950c90da (patch)
tree71f361f6ef8de2799ba290d112ada0597a0606df /test/functional/lua/fs_spec.lua
parent48c09ed4d9edd92a7c665a62aed04e8597088e60 (diff)
downloadrneovim-518070731003e30ea7eee96e76ccdf7b950c90da.tar.gz
rneovim-518070731003e30ea7eee96e76ccdf7b950c90da.tar.bz2
rneovim-518070731003e30ea7eee96e76ccdf7b950c90da.zip
feat(lua): add `vim.fs.abspath`
Problem: There is currently no way to check if a given path is absolute or convert a relative path to an absolute path through the Lua stdlib. `vim.fs.joinpath` does not work when the path is absolute. There is also currently no way to resolve `C:foo\bar` style paths in Windows. Solution: Add `vim.fs.abspath`, which allows converting any path to an absolute path. This also allows checking if current path is absolute by doing `vim.fs.abspath(path) == path`. It also has support for `C:foo\bar` style paths in Windows.
Diffstat (limited to 'test/functional/lua/fs_spec.lua')
-rw-r--r--test/functional/lua/fs_spec.lua34
1 files changed, 34 insertions, 0 deletions
diff --git a/test/functional/lua/fs_spec.lua b/test/functional/lua/fs_spec.lua
index 89f6ad6a0e..63444f5ba1 100644
--- a/test/functional/lua/fs_spec.lua
+++ b/test/functional/lua/fs_spec.lua
@@ -454,4 +454,38 @@ describe('vim.fs', function()
end)
end)
end)
+
+ describe('abspath', function()
+ local cwd = is_os('win') and vim.uv.cwd():gsub('\\', '/') or vim.uv.cwd()
+ local home = is_os('win') and vim.uv.os_homedir():gsub('\\', '/') or vim.uv.os_homedir()
+
+ it('works', function()
+ eq(cwd .. '/foo', vim.fs.abspath('foo'))
+ eq(cwd .. '/././foo', vim.fs.abspath('././foo'))
+ eq(cwd .. '/.././../foo', vim.fs.abspath('.././../foo'))
+ end)
+
+ it('works with absolute paths', function()
+ if is_os('win') then
+ eq([[C:/foo]], vim.fs.abspath([[C:\foo]]))
+ eq([[C:/foo/../.]], vim.fs.abspath([[C:\foo\..\.]]))
+ else
+ eq('/foo/../.', vim.fs.abspath('/foo/../.'))
+ eq('/foo/bar', vim.fs.abspath('/foo/bar'))
+ end
+ end)
+
+ it('expands ~', function()
+ eq(home .. '/foo', vim.fs.abspath('~/foo'))
+ eq(home .. '/./.././foo', vim.fs.abspath('~/./.././foo'))
+ end)
+
+ if is_os('win') then
+ it('works with drive-specific cwd on Windows', function()
+ local cwd_drive = cwd:match('^%w:')
+
+ eq(cwd .. '/foo', vim.fs.abspath(cwd_drive .. 'foo'))
+ end)
+ end
+ end)
end)