From 4df43ad5f50901d8a97ff77a62bdab7585338401 Mon Sep 17 00:00:00 2001 From: Steven Arcangeli Date: Sun, 19 Nov 2023 19:40:20 -0800 Subject: [PATCH] test: fix flaky tests --- lua/oil/adapters/trash/freedesktop.lua | 10 +++------- lua/oil/fs.lua | 7 +++++-- lua/oil/mutator/init.lua | 14 ++++++++++---- lua/oil/view.lua | 7 +++++-- tests/trash_spec.lua | 14 ++++++++------ 5 files changed, 31 insertions(+), 21 deletions(-) diff --git a/lua/oil/adapters/trash/freedesktop.lua b/lua/oil/adapters/trash/freedesktop.lua index 3ba211f..d4a949a 100644 --- a/lua/oil/adapters/trash/freedesktop.lua +++ b/lua/oil/adapters/trash/freedesktop.lua @@ -12,14 +12,10 @@ local FIELD_META = constants.FIELD_META local M = {} -local function touch_dir(path) - uv.fs_mkdir(path, 448) -- 0700 -end - local function ensure_trash_dir(path) - touch_dir(path) - touch_dir(fs.join(path, "info")) - touch_dir(fs.join(path, "files")) + local mode = 448 -- 0700 + fs.mkdirp(fs.join(path, "info"), mode) + fs.mkdirp(fs.join(path, "files"), mode) end ---Gets the location of the home trash dir, creating it if necessary diff --git a/lua/oil/fs.lua b/lua/oil/fs.lua index 3de1acd..e0b11dd 100644 --- a/lua/oil/fs.lua +++ b/lua/oil/fs.lua @@ -143,7 +143,10 @@ M.shorten_path = function(path, relative_to) return relpath or path end -M.mkdirp = function(dir) +---@param dir string +---@param mode? integer +M.mkdirp = function(dir, mode) + mode = mode or 493 local mod = "" local path = dir while vim.fn.isdirectory(path) == 0 do @@ -153,7 +156,7 @@ M.mkdirp = function(dir) while mod ~= "" do mod = mod:sub(3) path = vim.fn.fnamemodify(dir, mod) - uv.fs_mkdir(path, 493) + uv.fs_mkdir(path, mode) end end diff --git a/lua/oil/mutator/init.lua b/lua/oil/mutator/init.lua index eb9aeef..9857b59 100644 --- a/lua/oil/mutator/init.lua +++ b/lua/oil/mutator/init.lua @@ -536,7 +536,9 @@ M.try_write_changes = function(confirm, cb) view.unlock_buffers() if err then err = string.format("[oil] Error applying actions: %s", err) - view.rerender_all_oil_buffers() + view.rerender_all_oil_buffers(nil, function() + cb(err) + end) else local current_entry = oil.get_cursor_entry() if current_entry then @@ -546,11 +548,15 @@ M.try_write_changes = function(confirm, cb) vim.split(current_entry.parsed_name or current_entry.name, "/")[1] ) end - view.rerender_all_oil_buffers() - vim.api.nvim_exec_autocmds("User", { pattern = "OilMutationComplete", modeline = false }) + view.rerender_all_oil_buffers(nil, function(render_err) + vim.api.nvim_exec_autocmds( + "User", + { pattern = "OilMutationComplete", modeline = false } + ) + cb(render_err) + end) end mutation_in_progress = false - cb(err) end) ) end) diff --git a/lua/oil/view.lua b/lua/oil/view.lua index 8fd423e..e3a4b49 100644 --- a/lua/oil/view.lua +++ b/lua/oil/view.lua @@ -145,9 +145,10 @@ M.unlock_buffers = function() end ---@param opts? table +---@param callback? fun(err: nil|string) ---@note --- This DISCARDS ALL MODIFICATIONS a user has made to oil buffers -M.rerender_all_oil_buffers = function(opts) +M.rerender_all_oil_buffers = function(opts, callback) opts = opts or {} local buffers = M.get_all_buffers() local hidden_buffers = {} @@ -159,13 +160,15 @@ M.rerender_all_oil_buffers = function(opts) hidden_buffers[vim.api.nvim_win_get_buf(winid)] = nil end end + local cb = util.cb_collect(#buffers, callback or function() end) for _, bufnr in ipairs(buffers) do if hidden_buffers[bufnr] then vim.b[bufnr].oil_dirty = opts -- We also need to mark this as nomodified so it doesn't interfere with quitting vim vim.bo[bufnr].modified = false + vim.schedule(cb) else - M.render_buffer_async(bufnr, opts) + M.render_buffer_async(bufnr, opts, cb) end end end diff --git a/tests/trash_spec.lua b/tests/trash_spec.lua index e74ae77..bedd076 100644 --- a/tests/trash_spec.lua +++ b/tests/trash_spec.lua @@ -20,17 +20,23 @@ end a.describe("freedesktop", function() local tmpdir + local tmphome + local home = vim.env.XDG_DATA_HOME a.before_each(function() require("oil.config").delete_to_trash = true tmpdir = TmpDir.new() + tmphome = TmpDir.new() package.loaded["oil.adapters.trash"] = require("oil.adapters.trash.freedesktop") - local trash_dir = string.format(".Trash-%d", uv.getuid()) - tmpdir:create({ fs.join(trash_dir, "__dummy__") }) + vim.env.XDG_DATA_HOME = tmphome.path end) a.after_each(function() + vim.env.XDG_DATA_HOME = home if tmpdir then tmpdir:dispose() end + if tmphome then + tmphome:dispose() + end test_util.reset_editor() package.loaded["oil.adapters.trash"] = nil end) @@ -140,12 +146,8 @@ a.describe("freedesktop", function() test_util.actions.save() test_util.actions.reload() assert.are.same({ "a.txt" }, parse_entries(0)) - local uid = uv.getuid() tmpdir:assert_fs({ ["a.txt"] = "a.txt", - [".Trash-" .. uid .. "/__dummy__"] = ".Trash-" .. uid .. "/__dummy__", - [".Trash-" .. uid .. "/files/"] = true, - [".Trash-" .. uid .. "/info/"] = true, }) end)