aboutsummaryrefslogtreecommitdiff
path: root/test/functional/lua/fs_spec.lua
diff options
context:
space:
mode:
authorMike <4576770+mike325@users.noreply.github.com>2025-01-15 01:39:17 +0100
committerGitHub <noreply@github.com>2025-01-14 16:39:17 -0800
commit611ef354919f1c6564efd2ff8074545941458ccc (patch)
treee4fe577188f7e05f8c618c3a9f271a48b0ef2f7c /test/functional/lua/fs_spec.lua
parente8a6c1b02122852da83dc52184e78369598d8240 (diff)
downloadrneovim-611ef354919f1c6564efd2ff8074545941458ccc.tar.gz
rneovim-611ef354919f1c6564efd2ff8074545941458ccc.tar.bz2
rneovim-611ef354919f1c6564efd2ff8074545941458ccc.zip
feat(vim.fs): find(), dir() can "follow" symlinks #31551
Problem: vim.fs.dir(), vim.fs.find() do not follow symlinks. Solution: - Add "follow" flag. - Enable it by default.
Diffstat (limited to 'test/functional/lua/fs_spec.lua')
-rw-r--r--test/functional/lua/fs_spec.lua83
1 files changed, 78 insertions, 5 deletions
diff --git a/test/functional/lua/fs_spec.lua b/test/functional/lua/fs_spec.lua
index 0ba0948eee..33af25f629 100644
--- a/test/functional/lua/fs_spec.lua
+++ b/test/functional/lua/fs_spec.lua
@@ -17,6 +17,8 @@ local mkdir = t.mkdir
local nvim_prog_basename = is_os('win') and 'nvim.exe' or 'nvim'
+local link_limit = is_os('win') and 64 or (is_os('mac') or is_os('bsd')) and 33 or 41
+
local test_basename_dirname_eq = {
'~/foo/',
'~/foo',
@@ -152,7 +154,7 @@ describe('vim.fs', function()
)
end)
- it('works with opts.depth and opts.skip', function()
+ it('works with opts.depth, opts.skip and opts.follow', function()
io.open('testd/a1', 'w'):close()
io.open('testd/b1', 'w'):close()
io.open('testd/c1', 'w'):close()
@@ -166,8 +168,8 @@ describe('vim.fs', function()
io.open('testd/a/b/c/b4', 'w'):close()
io.open('testd/a/b/c/c4', 'w'):close()
- local function run(dir, depth, skip)
- return exec_lua(function()
+ local function run(dir, depth, skip, follow)
+ return exec_lua(function(follow_)
local r = {} --- @type table<string, string>
local skip_f --- @type function
if skip then
@@ -177,11 +179,11 @@ describe('vim.fs', function()
end
end
end
- for name, type_ in vim.fs.dir(dir, { depth = depth, skip = skip_f }) do
+ for name, type_ in vim.fs.dir(dir, { depth = depth, skip = skip_f, follow = follow_ }) do
r[name] = type_
end
return r
- end)
+ end, follow)
end
local exp = {}
@@ -197,6 +199,7 @@ describe('vim.fs', function()
exp['a/b2'] = 'file'
exp['a/c2'] = 'file'
exp['a/b'] = 'directory'
+ local lexp = vim.deepcopy(exp)
eq(exp, run('testd', 2))
@@ -213,6 +216,29 @@ describe('vim.fs', function()
exp['a/b/c/c4'] = 'file'
eq(exp, run('testd', 999))
+
+ vim.uv.fs_symlink(vim.uv.fs_realpath('testd/a'), 'testd/l', { junction = true, dir = true })
+ lexp['l'] = 'link'
+ eq(lexp, run('testd', 2, nil, false))
+
+ lexp['l/a2'] = 'file'
+ lexp['l/b2'] = 'file'
+ lexp['l/c2'] = 'file'
+ lexp['l/b'] = 'directory'
+ eq(lexp, run('testd', 2, nil, true))
+ end)
+
+ it('follow=true handles symlink loop', function()
+ local cwd = 'testd/a/b/c'
+ local symlink = cwd .. '/link_loop' ---@type string
+ vim.uv.fs_symlink(vim.uv.fs_realpath(cwd), symlink, { junction = true, dir = true })
+
+ eq(
+ link_limit,
+ exec_lua(function()
+ return #vim.iter(vim.fs.dir(cwd, { depth = math.huge, follow = true })):totable()
+ end)
+ )
end)
end)
@@ -228,6 +254,53 @@ describe('vim.fs', function()
eq({ nvim_dir }, vim.fs.find(name, { path = parent, upward = true, type = 'directory' }))
end)
+ it('follows symlinks', function()
+ local build_dir = test_source_path .. '/build' ---@type string
+ local symlink = test_source_path .. '/build_link' ---@type string
+ vim.uv.fs_symlink(build_dir, symlink, { junction = true, dir = true })
+
+ finally(function()
+ vim.uv.fs_unlink(symlink)
+ end)
+
+ eq(
+ { nvim_prog, symlink .. '/bin/' .. nvim_prog_basename },
+ vim.fs.find(nvim_prog_basename, {
+ path = test_source_path,
+ type = 'file',
+ limit = 2,
+ follow = true,
+ })
+ )
+
+ eq(
+ { nvim_prog },
+ vim.fs.find(nvim_prog_basename, {
+ path = test_source_path,
+ type = 'file',
+ limit = 2,
+ follow = false,
+ })
+ )
+ end)
+
+ it('follow=true handles symlink loop', function()
+ local cwd = test_source_path ---@type string
+ local symlink = test_source_path .. '/loop_link' ---@type string
+ vim.uv.fs_symlink(cwd, symlink, { junction = true, dir = true })
+
+ finally(function()
+ vim.uv.fs_unlink(symlink)
+ end)
+
+ eq(link_limit, #vim.fs.find(nvim_prog_basename, {
+ path = test_source_path,
+ type = 'file',
+ limit = math.huge,
+ follow = true,
+ }))
+ end)
+
it('accepts predicate as names', function()
local opts = { path = nvim_dir, upward = true, type = 'directory' }
eq(