test: n.rmdir() save-and-restore CWD #39048

This commit is contained in:
Justin M. Keyes
2026-04-14 11:37:06 -04:00
committed by GitHub
parent c0f33c9a86
commit faad7c73ac

View File

@@ -837,17 +837,19 @@ function M.assert_visible(bufnr, visible)
end
end
local start_dir = uv.cwd()
local start_dir = assert(uv.cwd())
function M.rmdir(path)
local ret, _ = pcall(vim.fs.rm, path, { recursive = true, force = true })
local did_cd = false
if not ret and is_os('win') then
-- Maybe "Permission denied"; try again after changing the nvim
-- process to the top-level directory.
ret, _ = pcall(function()
M.command([[exe 'cd '.fnameescape(']] .. start_dir .. "')")
M.fn.chdir(start_dir)
vim.fs.rm(path, { recursive = true, force = true })
end)
did_cd = true
end
-- During teardown, the nvim process may not exit quickly enough, then rmdir()
-- will fail (on Windows).
@@ -855,6 +857,10 @@ function M.rmdir(path)
sleep(1000)
vim.fs.rm(path, { recursive = true, force = true })
end
if did_cd then
-- Try to restore CWD in case rmdir() is used within a test. Needs pcall: #38278
pcall(M.command, 'cd -')
end
end
--- @deprecated Use `t.pcall_err()` to check failure, or `n.command()` to check success.