aboutsummaryrefslogtreecommitdiff
path: root/test/functional/lua/fs_spec.lua
diff options
context:
space:
mode:
authorJosh Rahm <joshuarahm@gmail.com>2023-01-25 18:23:01 +0000
committerJosh Rahm <joshuarahm@gmail.com>2023-01-25 18:23:01 +0000
commit142d9041391780ac15b89886a54015fdc5c73995 (patch)
tree0f6b5cac1a60810a03f52826c9e67c9e2780b033 /test/functional/lua/fs_spec.lua
parentad86b5db74922285699ab2a1dbb2ff20e6268a33 (diff)
parent3c48d3c83fc21dbc0841f9210f04bdb073d73cd1 (diff)
downloadrneovim-142d9041391780ac15b89886a54015fdc5c73995.tar.gz
rneovim-142d9041391780ac15b89886a54015fdc5c73995.tar.bz2
rneovim-142d9041391780ac15b89886a54015fdc5c73995.zip
Merge remote-tracking branch 'upstream/master' into userreg
Diffstat (limited to 'test/functional/lua/fs_spec.lua')
-rw-r--r--test/functional/lua/fs_spec.lua164
1 files changed, 161 insertions, 3 deletions
diff --git a/test/functional/lua/fs_spec.lua b/test/functional/lua/fs_spec.lua
index 3123ec324c..642d36f63a 100644
--- a/test/functional/lua/fs_spec.lua
+++ b/test/functional/lua/fs_spec.lua
@@ -1,4 +1,5 @@
local helpers = require('test.functional.helpers')(after_each)
+local lfs = require('lfs')
local clear = helpers.clear
local exec_lua = helpers.exec_lua
@@ -7,10 +8,43 @@ local mkdir_p = helpers.mkdir_p
local rmdir = helpers.rmdir
local nvim_dir = helpers.nvim_dir
local test_build_dir = helpers.test_build_dir
-local iswin = helpers.iswin
local nvim_prog = helpers.nvim_prog
+local is_os = helpers.is_os
-local nvim_prog_basename = iswin() and 'nvim.exe' or 'nvim'
+local nvim_prog_basename = is_os('win') and 'nvim.exe' or 'nvim'
+
+local test_basename_dirname_eq = {
+ '~/foo/',
+ '~/foo',
+ '~/foo/bar.lua',
+ 'foo.lua',
+ ' ',
+ '',
+ '.',
+ '..',
+ '../',
+ '~',
+ '/usr/bin',
+ '/usr/bin/gcc',
+ '/',
+ '/usr/',
+ '/usr',
+ 'c:/usr',
+ 'c:/',
+ 'c:',
+ 'c:/users/foo',
+ 'c:/users/foo/bar.lua',
+ 'c:/users/foo/bar/../',
+}
+
+local tests_windows_paths = {
+ 'c:\\usr',
+ 'c:\\',
+ 'c:',
+ 'c:\\users\\foo',
+ 'c:\\users\\foo\\bar.lua',
+ 'c:\\users\\foo\\bar\\..\\',
+}
before_each(clear)
@@ -41,19 +75,73 @@ describe('vim.fs', function()
local nvim_dir = ...
return vim.fs.dirname(nvim_dir)
]], nvim_dir))
+
+ local function test_paths(paths)
+ for _, path in ipairs(paths) do
+ eq(
+ exec_lua([[
+ local path = ...
+ return vim.fn.fnamemodify(path,':h'):gsub('\\', '/')
+ ]], path),
+ exec_lua([[
+ local path = ...
+ return vim.fs.dirname(path)
+ ]], path),
+ path
+ )
+ end
+ end
+
+ test_paths(test_basename_dirname_eq)
+ if is_os('win') then
+ test_paths(tests_windows_paths)
+ end
end)
end)
describe('basename()', function()
it('works', function()
+
eq(nvim_prog_basename, exec_lua([[
local nvim_prog = ...
return vim.fs.basename(nvim_prog)
]], nvim_prog))
+
+ local function test_paths(paths)
+ for _, path in ipairs(paths) do
+ eq(
+ exec_lua([[
+ local path = ...
+ return vim.fn.fnamemodify(path,':t'):gsub('\\', '/')
+ ]], path),
+ exec_lua([[
+ local path = ...
+ return vim.fs.basename(path)
+ ]], path),
+ path
+ )
+ end
+ end
+
+ test_paths(test_basename_dirname_eq)
+ if is_os('win') then
+ test_paths(tests_windows_paths)
+ end
end)
end)
describe('dir()', function()
+ before_each(function()
+ lfs.mkdir('testd')
+ lfs.mkdir('testd/a')
+ lfs.mkdir('testd/a/b')
+ lfs.mkdir('testd/a/b/c')
+ end)
+
+ after_each(function()
+ rmdir('testd')
+ end)
+
it('works', function()
eq(true, exec_lua([[
local dir, nvim = ...
@@ -65,6 +153,71 @@ describe('vim.fs', function()
return false
]], nvim_dir, nvim_prog_basename))
end)
+
+ it('works with opts.depth and opts.skip', function()
+ io.open('testd/a1', 'w'):close()
+ io.open('testd/b1', 'w'):close()
+ io.open('testd/c1', 'w'):close()
+ io.open('testd/a/a2', 'w'):close()
+ io.open('testd/a/b2', 'w'):close()
+ io.open('testd/a/c2', 'w'):close()
+ io.open('testd/a/b/a3', 'w'):close()
+ io.open('testd/a/b/b3', 'w'):close()
+ io.open('testd/a/b/c3', 'w'):close()
+ io.open('testd/a/b/c/a4', 'w'):close()
+ io.open('testd/a/b/c/b4', 'w'):close()
+ 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 = {}
+ local skip_f
+ if skip then
+ skip_f = function(n)
+ if vim.tbl_contains(skip or {}, n) then
+ return false
+ end
+ end
+ end
+ for name, type_ in vim.fs.dir(dir, { depth = depth, skip = skip_f }) do
+ r[name] = type_
+ end
+ return r
+ ]], dir, depth, skip)
+ return r
+ end
+
+ local exp = {}
+
+ exp['a1'] = 'file'
+ exp['b1'] = 'file'
+ exp['c1'] = 'file'
+ exp['a'] = 'directory'
+
+ eq(exp, run('testd', 1))
+
+ exp['a/a2'] = 'file'
+ exp['a/b2'] = 'file'
+ exp['a/c2'] = 'file'
+ exp['a/b'] = 'directory'
+
+ eq(exp, run('testd', 2))
+
+ exp['a/b/a3'] = 'file'
+ exp['a/b/b3'] = 'file'
+ exp['a/b/c3'] = 'file'
+ exp['a/b/c'] = 'directory'
+
+ eq(exp, run('testd', 3))
+ eq(exp, run('testd', 999, {'a/b/c'}))
+
+ exp['a/b/c/a4'] = 'file'
+ exp['a/b/c/b4'] = 'file'
+ exp['a/b/c/c4'] = 'file'
+
+ eq(exp, run('testd', 999))
+ end)
end)
describe('find()', function()
@@ -77,6 +230,11 @@ describe('vim.fs', function()
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)
it('accepts predicate as names', function()
@@ -102,7 +260,7 @@ describe('vim.fs', function()
eq('C:/Users/jdoe', exec_lua [[ return vim.fs.normalize('C:\\Users\\jdoe') ]])
end)
it('works with ~', function()
- if iswin() then
+ if is_os('win') then
pending([[$HOME does not exist on Windows ¯\_(ツ)_/¯]])
end
eq(os.getenv('HOME') .. '/src/foo', exec_lua [[ return vim.fs.normalize('~/src/foo') ]])