test: allow exec_lua to handle functions

Problem:

Tests have lots of exec_lua calls which input blocks of code
provided as unformatted strings.

Solution:

Teach exec_lua how to handle functions.
This commit is contained in:
Lewis Russell
2024-07-29 11:20:15 +01:00
committed by Lewis Russell
parent f32557ca67
commit 7d24c4d6b0
16 changed files with 4122 additions and 3891 deletions

View File

@@ -141,19 +141,14 @@ describe('vim.fs', function()
it('works', function()
eq(
true,
exec_lua(
[[
local dir, nvim = ...
for name, type in vim.fs.dir(dir) do
if name == nvim and type == 'file' then
return true
exec_lua(function(dir, nvim)
for name, type in vim.fs.dir(dir) do
if name == nvim and type == 'file' then
return true
end
end
end
return false
]],
nvim_dir,
nvim_prog_basename
)
return false
end, nvim_dir, nvim_prog_basename)
)
end)
@@ -172,27 +167,21 @@ describe('vim.fs', function()
io.open('testd/a/b/c/c4', 'w'):close()
local function run(dir, depth, skip)
local r = exec_lua(
[[
local dir, depth, skip = ...
local r = exec_lua(function(dir0, depth0, skip0)
local r = {}
local skip_f
if skip then
skip_f = function(n)
if vim.tbl_contains(skip or {}, n) then
if skip0 then
skip_f = function(n0)
if vim.tbl_contains(skip0 or {}, n0) then
return false
end
end
end
for name, type_ in vim.fs.dir(dir, { depth = depth, skip = skip_f }) do
for name, type_ in vim.fs.dir(dir0, { depth = depth0, skip = skip_f }) do
r[name] = type_
end
return r
]],
dir,
depth,
skip
)
end, dir, depth, skip)
return r
end
@@ -263,13 +252,9 @@ describe('vim.fs', function()
opts = { path = test_source_path .. '/contrib', limit = math.huge }
eq(
exec_lua(
[[
local dir = ...
return vim.tbl_map(vim.fs.basename, vim.fn.glob(dir..'/contrib/*', false, true))
]],
test_source_path
),
exec_lua(function(dir)
return vim.tbl_map(vim.fs.basename, vim.fn.glob(dir .. '/contrib/*', false, true))
end, test_source_path),
vim.tbl_map(
vim.fs.basename,
vim.fs.find(function(_, d)
@@ -299,11 +284,11 @@ describe('vim.fs', function()
it('works with a function', function()
---@type string
local result = exec_lua([[
return vim.fs.root(0, function(name, path)
local result = exec_lua(function()
return vim.fs.root(0, function(name, _)
return name:match('%.txt$')
end)
]])
end)
eq(vim.fs.joinpath(test_source_path, 'test/functional/fixtures'), result)
end)
@@ -352,13 +337,10 @@ describe('vim.fs', function()
local xdg_config_home = test_build_dir .. '/.config'
eq(
xdg_config_home .. '/nvim',
exec_lua(
[[
vim.env.XDG_CONFIG_HOME = ...
return vim.fs.normalize('$XDG_CONFIG_HOME/nvim')
]],
xdg_config_home
)
exec_lua(function(...)
vim.env.XDG_CONFIG_HOME = ...
return vim.fs.normalize('$XDG_CONFIG_HOME/nvim')
end, xdg_config_home)
)
end)