Skip to content

Commit

Permalink
fix: handle symlinks in allowed/suppressed dirs
Browse files Browse the repository at this point in the history
  • Loading branch information
cameronr committed Aug 22, 2024
1 parent 008b907 commit d845369
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ doc/tags
tests/test_sessions
tests/custom_sessions
tests/test_git
tests/symlink-test
32 changes: 27 additions & 5 deletions lua/auto-session/lib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -491,12 +491,34 @@ end
---@param dirs table
---@param dirToFind string
function Lib.find_matching_directory(dirToFind, dirs)
Lib.logger.debug("find_matching_directory", { dirToFind = dirToFind, dirs = dirs })
for _, s in pairs(dirs) do
local expanded = Lib.expand(s)
-- Lib.logger.debug("find_matching_directory expanded: " .. s)
local dirsToCheck = {}

-- resolve any symlinks and also check those
for _, dir in pairs(dirs) do
-- first expand it
local expanded_dir = Lib.expand(dir)

-- resolve symlinks
local resolved_dir = vim.fn.resolve(expanded_dir)

-- Lib.logger.debug("dir: " .. dir .. " expanded_dir: " .. expanded_dir .. " resolved_dir: " .. resolved_dir)

-- add the base expanded dir first. in theory, we should only need
-- the resolved directory but other systems might behave differently so
-- safer to check both
table.insert(dirsToCheck, expanded_dir)

-- add the resolved dir if it's different (e.g. a symlink)
if resolved_dir ~= expanded_dir then
table.insert(dirsToCheck, resolved_dir)
end
end

Lib.logger.debug("find_matching_directory", { dirToFind = dirToFind, dirsToCheck = dirsToCheck })

for _, dir in pairs(dirsToCheck) do
---@diagnostic disable-next-line: param-type-mismatch
for path in string.gmatch(expanded, "[^\r\n]+") do
for path in string.gmatch(dir, "[^\r\n]+") do
local simplified_path = vim.fn.simplify(path)
local path_without_trailing_slashes = string.gsub(simplified_path, "/+$", "")

Expand Down
23 changes: 23 additions & 0 deletions tests/allowed_dirs_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ describe("The allowed dirs config", function()
local c = require "auto-session.config"
as.setup {
auto_session_allowed_dirs = { "/dummy" },
-- log_level = "debug",
}

TL.clearSessionFilesAndBuffers()
vim.cmd("e " .. TL.test_file)
local cwd = vim.fn.getcwd()

it("doesn't save a session for a non-allowed dir", function()
as.AutoSaveSession()
Expand Down Expand Up @@ -46,4 +48,25 @@ describe("The allowed dirs config", function()
-- Make sure the session was created
assert.equals(1, vim.fn.filereadable(session_path))
end)

if vim.fn.has "win32" == 0 then
it("saves a session for an allowed dir with a symlink", function()
TL.clearSessionFilesAndBuffers()
vim.cmd("cd " .. cwd)

vim.cmd("e " .. TL.test_file)
c.allowed_dirs = { vim.fn.getcwd() .. "/tests/symlink-test" }

vim.fn.system "ln -snf test_files tests/symlink-test"
vim.cmd "cd tests/symlink-test"

local session_path = TL.makeSessionPath(vim.fn.getcwd())
assert.equals(0, vim.fn.filereadable(session_path))

assert.True(as.AutoSaveSession())

-- Make sure the session was created
assert.equals(1, vim.fn.filereadable(session_path))
end)
end
end)

0 comments on commit d845369

Please sign in to comment.