Skip to content

Commit

Permalink
test: more functional tests for trash
Browse files Browse the repository at this point in the history
  • Loading branch information
stevearc committed Oct 24, 2023
1 parent 3cc9a21 commit 63d2f8a
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 13 deletions.
1 change: 1 addition & 0 deletions lua/oil/view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ M.initialize = function(bufnr)
vim.log.levels.ERROR
)
else
vim.b[bufnr].oil_ready = true
vim.api.nvim_exec_autocmds(
"User",
{ pattern = "OilEnter", modeline = false, data = { buf = bufnr } }
Expand Down
12 changes: 11 additions & 1 deletion tests/test_util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,17 @@ M.actions = {
---Open oil and wait for it to finish rendering
---@param args string[]
open = function(args)
vim.schedule_wrap(vim.cmd.Oil)({ args = args })
vim.schedule(function()
vim.cmd.Oil({ args = args })
-- If this buffer was already open, manually dispatch the autocmd to finish the wait
if vim.b.oil_ready then
vim.api.nvim_exec_autocmds("User", {
pattern = "OilEnter",
modeline = false,
data = { buf = vim.api.nvim_get_current_buf() },
})
end
end)
M.wait_for_autocmd({ "User", pattern = "OilEnter" })
end,

Expand Down
4 changes: 2 additions & 2 deletions tests/tmpdir.lua
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ local assert_fs = function(root, paths)
local pieces = vim.split(k, "/")
local partial_path = ""
for i, piece in ipairs(pieces) do
partial_path = fs.join(partial_path, piece) .. "/"
partial_path = partial_path .. piece .. "/"
if i ~= #pieces then
unlisted_dirs[partial_path:sub(2)] = true
unlisted_dirs[partial_path] = true
end
end
end
Expand Down
123 changes: 113 additions & 10 deletions tests/trash_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ end
a.describe("freedesktop", function()
local tmpdir
a.before_each(function()
require("oil.config").delete_to_trash = true
tmpdir = TmpDir.new()
package.loaded["oil.adapters.trash"] = require("oil.adapters.trash.freedesktop")
local trash_dir = string.format(".Trash-%d", uv.getuid())
Expand Down Expand Up @@ -48,14 +49,116 @@ a.describe("freedesktop", function()
assert.are.same({ "a.txt" }, parse_entries(0))
end)

-- TODO
-- deleting a file moves it to the trash
-- can't create files in trash
-- can't rename files in trash
-- can't copy files in trash
-- pasting a file multiple times into the trash only deletes to trash once
-- restore from trash
-- restore from trash to multiple locations
-- can have multiple files with the same name in trash
-- can delete directories to trash
a.it("deleting a file moves it to trash", function()
tmpdir:create({ "a.txt", "foo/b.txt" })
test_util.actions.open({ tmpdir.path })
test_util.actions.focus("a.txt")
vim.api.nvim_feedkeys("dd", "x", true)
test_util.actions.save()
tmpdir:assert_not_exists("a.txt")
tmpdir:assert_exists("foo/b.txt")
test_util.actions.open({ "--trash", tmpdir.path })
assert.are.same({ "a.txt" }, parse_entries(0))
end)

a.it("deleting a directory moves it to trash", function()
tmpdir:create({ "a.txt", "foo/b.txt" })
test_util.actions.open({ tmpdir.path })
test_util.actions.focus("foo/")
vim.api.nvim_feedkeys("dd", "x", true)
test_util.actions.save()
tmpdir:assert_not_exists("foo")
tmpdir:assert_exists("a.txt")
test_util.actions.open({ "--trash", tmpdir.path })
assert.are.same({ "foo/" }, parse_entries(0))
end)

a.it("deleting a file from trash deletes it permanently", function()
tmpdir:create({ "a.txt" })
test_util.actions.open({ tmpdir.path })
test_util.actions.focus("a.txt")
vim.api.nvim_feedkeys("dd", "x", true)
test_util.actions.save()
test_util.actions.open({ "--trash", tmpdir.path })
test_util.actions.focus("a.txt")
vim.api.nvim_feedkeys("dd", "x", true)
test_util.actions.save()
test_util.actions.reload()
tmpdir:assert_not_exists("a.txt")
assert.are.same({}, parse_entries(0))
end)

a.it("cannot create files in the trash", function()
tmpdir:create({ "a.txt" })
test_util.actions.open({ tmpdir.path })
test_util.actions.focus("a.txt")
vim.api.nvim_feedkeys("dd", "x", true)
test_util.actions.save()
test_util.actions.open({ "--trash", tmpdir.path })
vim.api.nvim_feedkeys("onew_file.txt", "x", true)
test_util.actions.save()
test_util.actions.reload()
assert.are.same({ "a.txt" }, parse_entries(0))
end)

a.it("cannot rename files in the trash", function()
tmpdir:create({ "a.txt" })
test_util.actions.open({ tmpdir.path })
test_util.actions.focus("a.txt")
vim.api.nvim_feedkeys("dd", "x", true)
test_util.actions.save()
test_util.actions.open({ "--trash", tmpdir.path })
vim.api.nvim_feedkeys("0facwnew_name", "x", true)
test_util.actions.save()
test_util.actions.reload()
assert.are.same({ "a.txt" }, parse_entries(0))
end)

a.it("cannot copy files in the trash", function()
tmpdir:create({ "a.txt" })
test_util.actions.open({ tmpdir.path })
test_util.actions.focus("a.txt")
vim.api.nvim_feedkeys("dd", "x", true)
test_util.actions.save()
test_util.actions.open({ "--trash", tmpdir.path })
vim.api.nvim_feedkeys("yypp", "x", true)
test_util.actions.save()
test_util.actions.reload()
assert.are.same({ "a.txt" }, parse_entries(0))
end)

a.it("can restore files from trash", function()
tmpdir:create({ "a.txt" })
test_util.actions.open({ tmpdir.path })
test_util.actions.focus("a.txt")
vim.api.nvim_feedkeys("dd", "x", true)
test_util.actions.save()
test_util.actions.open({ "--trash", tmpdir.path })
vim.api.nvim_feedkeys("dd", "x", true)
test_util.actions.open({ tmpdir.path })
vim.api.nvim_feedkeys("p", "x", true)
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)

a.it("can have multiple files with the same name in trash", function()
tmpdir:create({ "a.txt" })
test_util.actions.open({ tmpdir.path })
vim.api.nvim_feedkeys("dd", "x", true)
test_util.actions.save()
tmpdir:create({ "a.txt" })
test_util.actions.reload()
vim.api.nvim_feedkeys("dd", "x", true)
test_util.actions.save()
test_util.actions.open({ "--trash", tmpdir.path })
assert.are.same({ "a.txt", "a.txt" }, parse_entries(0))
end)
end)

0 comments on commit 63d2f8a

Please sign in to comment.