fix: #146 allowed_dirs and ignored_dirs
parent
970f68938a
commit
98cd3de512
1
Makefile
1
Makefile
|
|
@ -36,6 +36,7 @@ test: $(PLENARY_DIR)
|
||||||
nvim --headless --noplugin -u tests/minimal.vim +TestGitBranching
|
nvim --headless --noplugin -u tests/minimal.vim +TestGitBranching
|
||||||
nvim --headless --noplugin -u tests/minimal.vim +TestFollowCwd
|
nvim --headless --noplugin -u tests/minimal.vim +TestFollowCwd
|
||||||
nvim --headless --noplugin -u tests/minimal.vim +TestDefaults
|
nvim --headless --noplugin -u tests/minimal.vim +TestDefaults
|
||||||
|
nvim --headless --noplugin -u tests/minimal.vim +TestDirs
|
||||||
nvim --headless --noplugin -u tests/minimal.vim +TearDown
|
nvim --headless --noplugin -u tests/minimal.vim +TearDown
|
||||||
|
|
||||||
$(PLENARY_DIR):
|
$(PLENARY_DIR):
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@ end
|
||||||
---@param input string
|
---@param input string
|
||||||
---@return string
|
---@return string
|
||||||
function M.escape_dir_pattern(input)
|
function M.escape_dir_pattern(input)
|
||||||
local magic_chars = { "%", "(", ")", ".", "+", "-", "*", "?", "[", "^", "$" }
|
local magic_chars = { "%", "(", ")", "+", "-", "*", "?", "[", "^", "$" }
|
||||||
|
|
||||||
for _, char in ipairs(magic_chars) do
|
for _, char in ipairs(magic_chars) do
|
||||||
input = input:gsub("%" .. char, "%%" .. char)
|
input = input:gsub("%" .. char, "%%" .. char)
|
||||||
|
|
@ -23,18 +23,38 @@ function M.escape_dir_pattern(input)
|
||||||
return input
|
return input
|
||||||
end
|
end
|
||||||
|
|
||||||
---Check if a target directory exists in a given table
|
---Check if a directory is a subdirectory of another
|
||||||
---@param dir string
|
---@param parent string
|
||||||
---@param dirs_table table
|
---@param child string
|
||||||
---@return boolean
|
---@return boolean
|
||||||
function M.dirs_match(dir, dirs_table)
|
function M.is_subdirectory(parent, child)
|
||||||
dir = vim.fn.expand(dir)
|
return vim.startswith(child, parent)
|
||||||
|
end
|
||||||
|
|
||||||
local match = M.in_table(dir, dirs_table, function(pattern)
|
---Check if a directory exists in the given table of directories
|
||||||
return M.escape_dir_pattern(vim.fn.expand(pattern))
|
---@param dir string The directory to check
|
||||||
end)
|
---@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
|
end
|
||||||
|
|
||||||
---Check if a string matches and entry in a given table
|
---Check if a string matches and entry in a given table
|
||||||
|
|
|
||||||
|
|
@ -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)
|
||||||
|
|
@ -7,4 +7,5 @@ command TestAutoloading PlenaryBustedDirectory tests/autoload {minimal_init = 't
|
||||||
command TestGitBranching PlenaryBustedDirectory tests/git_branching {minimal_init = 'tests/minimal.vim'}
|
command TestGitBranching PlenaryBustedDirectory tests/git_branching {minimal_init = 'tests/minimal.vim'}
|
||||||
command TestFollowCwd PlenaryBustedDirectory tests/follow_cwd {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 TestDefaults PlenaryBustedFile tests/default_settings_spec.lua
|
||||||
|
command TestDirs PlenaryBustedFile tests/dirs_spec.lua
|
||||||
command TearDown PlenaryBustedFile tests/teardown/clean_up_dirs.lua
|
command TearDown PlenaryBustedFile tests/teardown/clean_up_dirs.lua
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue