fix: #146 allowed_dirs and ignored_dirs

main
olimorris 2024-08-09 08:56:18 +01:00
parent 970f68938a
commit 98cd3de512
4 changed files with 56 additions and 10 deletions

View File

@ -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):

View File

@ -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

24
tests/dirs_spec.lua Normal file
View File

@ -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)

View File

@ -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