From 67cbaf58c41a3db19c5014587e72d06be9e3d58e Mon Sep 17 00:00:00 2001 From: Gregory Anders Date: Sun, 15 May 2022 14:38:19 -0600 Subject: feat(fs): add vim.fs.parents() vim.fs.parents() is a Lua iterator that returns the next parent directory of the given file or directory on each iteration. --- test/functional/lua/fs_spec.lua | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 test/functional/lua/fs_spec.lua (limited to 'test/functional') diff --git a/test/functional/lua/fs_spec.lua b/test/functional/lua/fs_spec.lua new file mode 100644 index 0000000000..69eb8cd539 --- /dev/null +++ b/test/functional/lua/fs_spec.lua @@ -0,0 +1,33 @@ +local helpers = require('test.functional.helpers')(after_each) + +local clear = helpers.clear +local exec_lua = helpers.exec_lua +local eq = helpers.eq +local mkdir_p = helpers.mkdir_p +local rmdir = helpers.rmdir +local nvim_dir = helpers.nvim_dir +local test_build_dir = helpers.test_build_dir + +before_each(clear) + +describe('vim.fs', function() + describe('parents()', function() + it('works', function() + local test_dir = nvim_dir .. '/test' + mkdir_p(test_dir) + local dirs = exec_lua([[ + local test_dir, test_build_dir = ... + local dirs = {} + for dir in vim.fs.parents(test_dir .. "/foo.txt") do + dirs[#dirs + 1] = dir + if dir == test_build_dir then + break + end + end + return dirs + ]], test_dir, test_build_dir) + eq({test_dir, nvim_dir, test_build_dir}, dirs) + rmdir(test_dir) + end) + end) +end) -- cgit From c5526a27c3b61acb33b7c3c3fe518d8f1e0b602f Mon Sep 17 00:00:00 2001 From: Gregory Anders Date: Sun, 15 May 2022 19:53:23 -0600 Subject: feat(fs): add vim.fs.dirname() --- test/functional/lua/fs_spec.lua | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'test/functional') diff --git a/test/functional/lua/fs_spec.lua b/test/functional/lua/fs_spec.lua index 69eb8cd539..23ffb1e607 100644 --- a/test/functional/lua/fs_spec.lua +++ b/test/functional/lua/fs_spec.lua @@ -30,4 +30,13 @@ describe('vim.fs', function() rmdir(test_dir) end) end) + + describe('dirname()', function() + it('works', function() + eq(test_build_dir, exec_lua([[ + local nvim_dir = ... + return vim.fs.dirname(nvim_dir) + ]], nvim_dir)) + end) + end) end) -- cgit From b740709431f5e68dac5238d455f9f86d5a564f36 Mon Sep 17 00:00:00 2001 From: Gregory Anders Date: Sun, 15 May 2022 19:55:18 -0600 Subject: feat(fs): add vim.fs.basename() --- test/functional/lua/fs_spec.lua | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'test/functional') diff --git a/test/functional/lua/fs_spec.lua b/test/functional/lua/fs_spec.lua index 23ffb1e607..674a7f6957 100644 --- a/test/functional/lua/fs_spec.lua +++ b/test/functional/lua/fs_spec.lua @@ -7,6 +7,10 @@ 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 nvim_prog_basename = iswin() and 'nvim.exe' or 'nvim' before_each(clear) @@ -39,4 +43,13 @@ describe('vim.fs', function() ]], nvim_dir)) 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)) + end) + end) end) -- cgit From 2a62bec37ced51678ff914700d7165605d5a0d53 Mon Sep 17 00:00:00 2001 From: Gregory Anders Date: Sun, 15 May 2022 20:10:12 -0600 Subject: feat(fs): add vim.fs.dir() This function is modeled after the path.dir() function from Penlight and the luafilesystem module. --- test/functional/lua/fs_spec.lua | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'test/functional') diff --git a/test/functional/lua/fs_spec.lua b/test/functional/lua/fs_spec.lua index 674a7f6957..6f1f1df012 100644 --- a/test/functional/lua/fs_spec.lua +++ b/test/functional/lua/fs_spec.lua @@ -52,4 +52,18 @@ describe('vim.fs', function() ]], nvim_prog)) end) end) + + describe('dir()', 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 + end + end + return false + ]], nvim_dir, nvim_prog_basename)) + end) + end) end) -- cgit From f271d706611049bc53a6a439b310fe60bf0fab13 Mon Sep 17 00:00:00 2001 From: Gregory Anders Date: Sun, 15 May 2022 20:37:35 -0600 Subject: feat(fs): add vim.fs.find() This is a pure Lua implementation of the Vim findfile() and finddir() functions without the special syntax. --- test/functional/lua/fs_spec.lua | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'test/functional') diff --git a/test/functional/lua/fs_spec.lua b/test/functional/lua/fs_spec.lua index 6f1f1df012..204bdc1567 100644 --- a/test/functional/lua/fs_spec.lua +++ b/test/functional/lua/fs_spec.lua @@ -66,4 +66,17 @@ describe('vim.fs', function() ]], nvim_dir, nvim_prog_basename)) end) end) + + describe('find()', function() + it('works', function() + eq({test_build_dir}, exec_lua([[ + local dir = ... + return vim.fs.find('build', { path = dir, upward = true, type = 'directory' }) + ]], nvim_dir)) + eq({nvim_prog}, exec_lua([[ + local dir, nvim = ... + return vim.fs.find(nvim, { path = dir, type = 'file' }) + ]], test_build_dir, nvim_prog_basename)) + end) + end) end) -- cgit From 046b4ed461cb78b8b302a6403cc7ea64ad6b6085 Mon Sep 17 00:00:00 2001 From: Gregory Anders Date: Tue, 17 May 2022 08:49:33 -0600 Subject: feat(fs): add vim.fs.normalize() --- test/functional/lua/fs_spec.lua | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'test/functional') diff --git a/test/functional/lua/fs_spec.lua b/test/functional/lua/fs_spec.lua index 204bdc1567..2bcc84db0f 100644 --- a/test/functional/lua/fs_spec.lua +++ b/test/functional/lua/fs_spec.lua @@ -79,4 +79,23 @@ describe('vim.fs', function() ]], test_build_dir, nvim_prog_basename)) end) end) + + describe('normalize()', function() + it('works with backward slashes', function() + eq('C:/Users/jdoe', exec_lua [[ return vim.fs.normalize('C:\\Users\\jdoe') ]]) + end) + it('works with ~', function() + if iswin() then + pending([[$HOME does not exist on Windows ¯\_(ツ)_/¯]]) + end + eq(os.getenv('HOME') .. '/src/foo', exec_lua [[ return vim.fs.normalize('~/src/foo') ]]) + end) + it('works with environment variables', 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)) + end) + end) end) -- cgit