Skip to content

Commit

Permalink
fix: #146 better handling for empty tables
Browse files Browse the repository at this point in the history
PR (#152)
  • Loading branch information
miversen33 authored Aug 30, 2024
1 parent 53ee4a4 commit c9c11ee
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
7 changes: 2 additions & 5 deletions lua/persisted/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -190,14 +190,11 @@ end
---@param opts? {dir?: string}
---@return boolean
function M.allowed_dir(opts)
if next(config.allowed_dirs) == nil and next(config.ignored_dirs) == nil then
return true
end

opts = opts or {}
local dir = opts.dir or vim.fn.getcwd()

return utils.dirs_match(dir, config.allowed_dirs) and not utils.dirs_match(dir, config.ignored_dirs)
return (next(config.allowed_dirs) and utils.dirs_match(dir, config.allowed_dirs) or true)
and not (next(config.ignored_dirs) and utils.dirs_match(dir, config.ignored_dirs) or false)
end

---Get an ordered list of sessions, sorted by modified time
Expand Down
14 changes: 14 additions & 0 deletions tests/dirs_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,18 @@ describe("Directory utilities:", function()
match = utils.dirs_match(cwd, allowed_dirs)
assert.equals(true, match)
end)

it("can handle only ignore directories", function()
local cwd = "~/Code/Neovim/persisted.nvim"
local allowed_dirs = {}
local ignored_dirs = { { "/tmp" } }
local allowed_match = utils.dirs_match(cwd, allowed_dirs)
local ignored_match = utils.dirs_match(cwd, ignored_dirs)
-- This looks weird, I know. That is because we expect dirs_match to return
-- false for allowed_dirs since allowed dirs is empty.
-- Therefore this is actually testing to ensure we are getting false and false
-- This test specifically addresses the change added in
-- https://github.com/olimorris/persisted.nvim/pull/152
assert.equals(true, not allowed_match and not ignored_match)
end)
end)

0 comments on commit c9c11ee

Please sign in to comment.