Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added better handling for empty tables #152

Merged
merged 3 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)