From faad7c73ac96891cf7f06efd358399b322c88fdf Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Tue, 14 Apr 2026 11:37:06 -0400 Subject: [PATCH] test: n.rmdir() save-and-restore CWD #39048 --- test/functional/testnvim.lua | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/test/functional/testnvim.lua b/test/functional/testnvim.lua index 7a83733903..5743331032 100644 --- a/test/functional/testnvim.lua +++ b/test/functional/testnvim.lua @@ -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.