test: fix flaky tests

This commit is contained in:
Steven Arcangeli 2023-11-19 19:40:20 -08:00
parent af04969c43
commit 4df43ad5f5
5 changed files with 31 additions and 21 deletions

View file

@ -12,14 +12,10 @@ local FIELD_META = constants.FIELD_META
local M = {} local M = {}
local function touch_dir(path)
uv.fs_mkdir(path, 448) -- 0700
end
local function ensure_trash_dir(path) local function ensure_trash_dir(path)
touch_dir(path) local mode = 448 -- 0700
touch_dir(fs.join(path, "info")) fs.mkdirp(fs.join(path, "info"), mode)
touch_dir(fs.join(path, "files")) fs.mkdirp(fs.join(path, "files"), mode)
end end
---Gets the location of the home trash dir, creating it if necessary ---Gets the location of the home trash dir, creating it if necessary

View file

@ -143,7 +143,10 @@ M.shorten_path = function(path, relative_to)
return relpath or path return relpath or path
end end
M.mkdirp = function(dir) ---@param dir string
---@param mode? integer
M.mkdirp = function(dir, mode)
mode = mode or 493
local mod = "" local mod = ""
local path = dir local path = dir
while vim.fn.isdirectory(path) == 0 do while vim.fn.isdirectory(path) == 0 do
@ -153,7 +156,7 @@ M.mkdirp = function(dir)
while mod ~= "" do while mod ~= "" do
mod = mod:sub(3) mod = mod:sub(3)
path = vim.fn.fnamemodify(dir, mod) path = vim.fn.fnamemodify(dir, mod)
uv.fs_mkdir(path, 493) uv.fs_mkdir(path, mode)
end end
end end

View file

@ -536,7 +536,9 @@ M.try_write_changes = function(confirm, cb)
view.unlock_buffers() view.unlock_buffers()
if err then if err then
err = string.format("[oil] Error applying actions: %s", err) 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 else
local current_entry = oil.get_cursor_entry() local current_entry = oil.get_cursor_entry()
if current_entry then 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] vim.split(current_entry.parsed_name or current_entry.name, "/")[1]
) )
end end
view.rerender_all_oil_buffers() view.rerender_all_oil_buffers(nil, function(render_err)
vim.api.nvim_exec_autocmds("User", { pattern = "OilMutationComplete", modeline = false }) vim.api.nvim_exec_autocmds(
"User",
{ pattern = "OilMutationComplete", modeline = false }
)
cb(render_err)
end)
end end
mutation_in_progress = false mutation_in_progress = false
cb(err)
end) end)
) )
end) end)

View file

@ -145,9 +145,10 @@ M.unlock_buffers = function()
end end
---@param opts? table ---@param opts? table
---@param callback? fun(err: nil|string)
---@note ---@note
--- This DISCARDS ALL MODIFICATIONS a user has made to oil buffers --- 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 {} opts = opts or {}
local buffers = M.get_all_buffers() local buffers = M.get_all_buffers()
local hidden_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 hidden_buffers[vim.api.nvim_win_get_buf(winid)] = nil
end end
end end
local cb = util.cb_collect(#buffers, callback or function() end)
for _, bufnr in ipairs(buffers) do for _, bufnr in ipairs(buffers) do
if hidden_buffers[bufnr] then if hidden_buffers[bufnr] then
vim.b[bufnr].oil_dirty = opts vim.b[bufnr].oil_dirty = opts
-- We also need to mark this as nomodified so it doesn't interfere with quitting vim -- We also need to mark this as nomodified so it doesn't interfere with quitting vim
vim.bo[bufnr].modified = false vim.bo[bufnr].modified = false
vim.schedule(cb)
else else
M.render_buffer_async(bufnr, opts) M.render_buffer_async(bufnr, opts, cb)
end end
end end
end end

View file

@ -20,17 +20,23 @@ end
a.describe("freedesktop", function() a.describe("freedesktop", function()
local tmpdir local tmpdir
local tmphome
local home = vim.env.XDG_DATA_HOME
a.before_each(function() a.before_each(function()
require("oil.config").delete_to_trash = true require("oil.config").delete_to_trash = true
tmpdir = TmpDir.new() tmpdir = TmpDir.new()
tmphome = TmpDir.new()
package.loaded["oil.adapters.trash"] = require("oil.adapters.trash.freedesktop") package.loaded["oil.adapters.trash"] = require("oil.adapters.trash.freedesktop")
local trash_dir = string.format(".Trash-%d", uv.getuid()) vim.env.XDG_DATA_HOME = tmphome.path
tmpdir:create({ fs.join(trash_dir, "__dummy__") })
end) end)
a.after_each(function() a.after_each(function()
vim.env.XDG_DATA_HOME = home
if tmpdir then if tmpdir then
tmpdir:dispose() tmpdir:dispose()
end end
if tmphome then
tmphome:dispose()
end
test_util.reset_editor() test_util.reset_editor()
package.loaded["oil.adapters.trash"] = nil package.loaded["oil.adapters.trash"] = nil
end) end)
@ -140,12 +146,8 @@ a.describe("freedesktop", function()
test_util.actions.save() test_util.actions.save()
test_util.actions.reload() test_util.actions.reload()
assert.are.same({ "a.txt" }, parse_entries(0)) assert.are.same({ "a.txt" }, parse_entries(0))
local uid = uv.getuid()
tmpdir:assert_fs({ tmpdir:assert_fs({
["a.txt"] = "a.txt", ["a.txt"] = "a.txt",
[".Trash-" .. uid .. "/__dummy__"] = ".Trash-" .. uid .. "/__dummy__",
[".Trash-" .. uid .. "/files/"] = true,
[".Trash-" .. uid .. "/info/"] = true,
}) })
end) end)