From 98cd3de5121086ac388eadad4294aec8ea8da582 Mon Sep 17 00:00:00 2001 From: olimorris Date: Fri, 9 Aug 2024 08:56:18 +0100 Subject: [PATCH] fix: #146 allowed_dirs and ignored_dirs --- Makefile | 1 + lua/persisted/utils.lua | 40 ++++++++++++++++++++++++++++++---------- tests/dirs_spec.lua | 24 ++++++++++++++++++++++++ tests/minimal.vim | 1 + 4 files changed, 56 insertions(+), 10 deletions(-) create mode 100644 tests/dirs_spec.lua diff --git a/Makefile b/Makefile index 16f8a57..ee1c635 100644 --- a/Makefile +++ b/Makefile @@ -36,6 +36,7 @@ test: $(PLENARY_DIR) nvim --headless --noplugin -u tests/minimal.vim +TestGitBranching nvim --headless --noplugin -u tests/minimal.vim +TestFollowCwd nvim --headless --noplugin -u tests/minimal.vim +TestDefaults + nvim --headless --noplugin -u tests/minimal.vim +TestDirs nvim --headless --noplugin -u tests/minimal.vim +TearDown $(PLENARY_DIR): diff --git a/lua/persisted/utils.lua b/lua/persisted/utils.lua index 7418c9c..b4560f2 100644 --- a/lua/persisted/utils.lua +++ b/lua/persisted/utils.lua @@ -14,7 +14,7 @@ end ---@param input string ---@return string function M.escape_dir_pattern(input) - local magic_chars = { "%", "(", ")", ".", "+", "-", "*", "?", "[", "^", "$" } + local magic_chars = { "%", "(", ")", "+", "-", "*", "?", "[", "^", "$" } for _, char in ipairs(magic_chars) do input = input:gsub("%" .. char, "%%" .. char) @@ -23,18 +23,38 @@ function M.escape_dir_pattern(input) return input end ----Check if a target directory exists in a given table ----@param dir string ----@param dirs_table table +---Check if a directory is a subdirectory of another +---@param parent string +---@param child string ---@return boolean -function M.dirs_match(dir, dirs_table) - dir = vim.fn.expand(dir) +function M.is_subdirectory(parent, child) + return vim.startswith(child, parent) +end - local match = M.in_table(dir, dirs_table, function(pattern) - return M.escape_dir_pattern(vim.fn.expand(pattern)) - end) +---Check if a directory exists in the given table of directories +---@param dir string The directory to check +---@param dirs table The table of directories to search in +---@return boolean +function M.dirs_match(dir, dirs) + dir = M.escape_dir_pattern(vim.fn.expand(dir)) - return match + for _, search in ipairs(dirs) do + if type(search) == "string" then + search = M.escape_dir_pattern(vim.fn.expand(search)) + if M.is_subdirectory(search, dir) then + return true + end + elseif type(search) == "table" then + if search.exact then + search = M.escape_dir_pattern(vim.fn.expand(search[1])) + if dir == search then + return true + end + end + end + end + + return false end ---Check if a string matches and entry in a given table diff --git a/tests/dirs_spec.lua b/tests/dirs_spec.lua new file mode 100644 index 0000000..776c5b7 --- /dev/null +++ b/tests/dirs_spec.lua @@ -0,0 +1,24 @@ +local session_dir = vim.fn.getcwd() .. "/tests/default_data/" +local utils = require("persisted.utils") + +describe("Directory utilities:", function() + it("can match directories", function() + local cwd = "~/Code/Neovim/persisted.nvim" + local allowed_dirs = { "~/Code" } + + local match = utils.dirs_match(cwd, allowed_dirs) + assert.equals(true, match) + end) + + it("can work with exact directories", function() + local cwd = "~/Code/Neovim/persisted.nvim" + local allowed_dirs = { { "~/Code", exact = true } } + local match = utils.dirs_match(cwd, allowed_dirs) + assert.equals(false, match) + + cwd = "~/Code/Neovim/persisted.nvim" + allowed_dirs = { { "~/Code/Neovim/persisted.nvim", exact = true } } + match = utils.dirs_match(cwd, allowed_dirs) + assert.equals(true, match) + end) +end) diff --git a/tests/minimal.vim b/tests/minimal.vim index d49f204..420c98b 100644 --- a/tests/minimal.vim +++ b/tests/minimal.vim @@ -7,4 +7,5 @@ command TestAutoloading PlenaryBustedDirectory tests/autoload {minimal_init = 't command TestGitBranching PlenaryBustedDirectory tests/git_branching {minimal_init = 'tests/minimal.vim'} command TestFollowCwd PlenaryBustedDirectory tests/follow_cwd {minimal_init = 'tests/minimal.vim'} command TestDefaults PlenaryBustedFile tests/default_settings_spec.lua +command TestDirs PlenaryBustedFile tests/dirs_spec.lua command TearDown PlenaryBustedFile tests/teardown/clean_up_dirs.lua