test: simplify vim.fs tests

The exec_lua wrapper is no longer necessary.
This commit is contained in:
dundargoc
2023-12-25 20:52:49 +01:00
committed by dundargoc
parent ec74538044
commit 0009c337bc

View File

@@ -55,17 +55,13 @@ describe('vim.fs', function()
it('works', function() it('works', function()
local test_dir = nvim_dir .. '/test' local test_dir = nvim_dir .. '/test'
mkdir_p(test_dir) mkdir_p(test_dir)
local dirs = exec_lua([[ local dirs = {}
local test_dir, test_build_dir = ... for dir in vim.fs.parents(test_dir .. "/foo.txt") do
local dirs = {} dirs[#dirs + 1] = dir
for dir in vim.fs.parents(test_dir .. "/foo.txt") do if dir == test_build_dir then
dirs[#dirs + 1] = dir break
if dir == test_build_dir then
break
end
end end
return dirs end
]], test_dir, test_build_dir)
eq({test_dir, nvim_dir, test_build_dir}, dirs) eq({test_dir, nvim_dir, test_build_dir}, dirs)
rmdir(test_dir) rmdir(test_dir)
end) end)
@@ -73,10 +69,7 @@ describe('vim.fs', function()
describe('dirname()', function() describe('dirname()', function()
it('works', function() it('works', function()
eq(test_build_dir, exec_lua([[ eq(test_build_dir, vim.fs.dirname(nvim_dir))
local nvim_dir = ...
return vim.fs.dirname(nvim_dir)
]], nvim_dir))
local function test_paths(paths) local function test_paths(paths)
for _, path in ipairs(paths) do for _, path in ipairs(paths) do
@@ -85,11 +78,7 @@ describe('vim.fs', function()
local path = ... local path = ...
return vim.fn.fnamemodify(path,':h'):gsub('\\', '/') return vim.fn.fnamemodify(path,':h'):gsub('\\', '/')
]], path), ]], path),
exec_lua([[ vim.fs.dirname(path), path
local path = ...
return vim.fs.dirname(path)
]], path),
path
) )
end end
end end
@@ -103,11 +92,7 @@ describe('vim.fs', function()
describe('basename()', function() describe('basename()', function()
it('works', function() it('works', function()
eq(nvim_prog_basename, vim.fs.basename(nvim_prog))
eq(nvim_prog_basename, exec_lua([[
local nvim_prog = ...
return vim.fs.basename(nvim_prog)
]], nvim_prog))
local function test_paths(paths) local function test_paths(paths)
for _, path in ipairs(paths) do for _, path in ipairs(paths) do
@@ -115,12 +100,7 @@ describe('vim.fs', function()
exec_lua([[ exec_lua([[
local path = ... local path = ...
return vim.fn.fnamemodify(path,':t'):gsub('\\', '/') return vim.fn.fnamemodify(path,':t'):gsub('\\', '/')
]], path), ]], path), vim.fs.basename(path), path
exec_lua([[
local path = ...
return vim.fs.basename(path)
]], path),
path
) )
end end
end end
@@ -224,77 +204,49 @@ describe('vim.fs', function()
describe('find()', function() describe('find()', function()
it('works', function() it('works', function()
eq({test_build_dir .. "/build"}, exec_lua([[ eq({test_build_dir .. "/build"}, vim.fs.find('build', { path = nvim_dir, upward = true, type = 'directory' }))
local dir = ... eq({nvim_prog}, vim.fs.find(nvim_prog_basename, { path = test_build_dir, type = 'file' }))
return vim.fs.find('build', { path = dir, upward = true, type = 'directory' })
]], nvim_dir)) local parent, name = nvim_dir:match('^(.*/)([^/]+)$')
eq({nvim_prog}, exec_lua([[ eq({nvim_dir}, vim.fs.find(name, { path = parent, upward = true, type = 'directory' }))
local dir, nvim = ...
return vim.fs.find(nvim, { path = dir, type = 'file' })
]], test_build_dir, nvim_prog_basename))
eq({nvim_dir}, exec_lua([[
local dir = ...
local parent, name = dir:match('^(.*/)([^/]+)$')
return vim.fs.find(name, { path = parent, upward = true, type = 'directory' })
]], nvim_dir))
end) end)
it('accepts predicate as names', function() it('accepts predicate as names', function()
eq({test_build_dir .. "/build"}, exec_lua([[ local opts = { path = nvim_dir, upward = true, type = 'directory' }
local dir = ... eq({test_build_dir .. "/build"}, vim.fs.find(function(x) return x == 'build' end, opts))
local opts = { path = dir, upward = true, type = 'directory' } eq({nvim_prog}, vim.fs.find(function(x) return x == nvim_prog_basename end, { path = test_build_dir, type = 'file' }))
return vim.fs.find(function(x) return x == 'build' end, opts) eq({}, vim.fs.find(function(x) return x == 'no-match' end, opts))
]], nvim_dir))
eq({nvim_prog}, exec_lua([[ opts = { path = test_source_path .. "/contrib", limit = math.huge }
local dir, nvim = ...
return vim.fs.find(function(x) return x == nvim end, { path = dir, type = 'file' })
]], test_build_dir, nvim_prog_basename))
eq({}, exec_lua([[
local dir = ...
local opts = { path = dir, upward = true, type = 'directory' }
return vim.fs.find(function(x) return x == 'no-match' end, opts)
]], nvim_dir))
eq( eq(
exec_lua([[ exec_lua([[
local dir = ... local dir = ...
return vim.tbl_map(vim.fs.basename, vim.fn.glob(dir..'/contrib/*', false, true)) return vim.tbl_map(vim.fs.basename, vim.fn.glob(dir..'/contrib/*', false, true))
]], test_source_path), ]], test_source_path),
exec_lua([[ vim.tbl_map(vim.fs.basename, vim.fs.find(function(_, d) return d:match('[\\/]contrib$') end, opts))
local dir = ... )
local opts = { path = dir .. "/contrib", limit = math.huge }
return vim.tbl_map(vim.fs.basename, vim.fs.find(function(_, d) return d:match('[\\/]contrib$') end, opts))
]], test_source_path))
end) end)
end) end)
describe('joinpath()', function() describe('joinpath()', function()
it('works', function() it('works', function()
eq('foo/bar/baz', exec_lua([[ eq('foo/bar/baz', vim.fs.joinpath('foo', 'bar', 'baz'))
return vim.fs.joinpath('foo', 'bar', 'baz') eq('foo/bar/baz', vim.fs.joinpath('foo', '/bar/', '/baz'))
]], nvim_dir))
eq('foo/bar/baz', exec_lua([[
return vim.fs.joinpath('foo', '/bar/', '/baz')
]], nvim_dir))
end) end)
end) end)
describe('normalize()', function() describe('normalize()', function()
it('works with backward slashes', function() it('works with backward slashes', function()
eq('C:/Users/jdoe', exec_lua [[ return vim.fs.normalize('C:\\Users\\jdoe') ]]) eq('C:/Users/jdoe', vim.fs.normalize('C:\\Users\\jdoe'))
end) end)
it('removes trailing /', function() it('removes trailing /', function()
eq('/home/user', exec_lua [[ return vim.fs.normalize('/home/user/') ]]) eq('/home/user', vim.fs.normalize('/home/user/'))
end) end)
it('works with /', function() it('works with /', function()
eq('/', exec_lua [[ return vim.fs.normalize('/') ]]) eq('/', vim.fs.normalize('/'))
end) end)
it('works with ~', function() it('works with ~', function()
eq( eq(vim.fs.normalize(uv.os_homedir()) .. '/src/foo', vim.fs.normalize('~/src/foo'))
exec_lua([[
local home = ...
return home .. '/src/foo'
]], vim.fs.normalize(uv.os_homedir())),
exec_lua [[ return vim.fs.normalize('~/src/foo') ]])
end) end)
it('works with environment variables', function() it('works with environment variables', function()
local xdg_config_home = test_build_dir .. '/.config' local xdg_config_home = test_build_dir .. '/.config'
@@ -305,7 +257,7 @@ describe('vim.fs', function()
end) end)
if is_os('win') then if is_os('win') then
it('Last slash is not truncated from root drive', function() it('Last slash is not truncated from root drive', function()
eq('C:/', exec_lua [[ return vim.fs.normalize('C:/') ]]) eq('C:/', vim.fs.normalize('C:/'))
end) end)
end end
end) end)