From 63d2f8acef180fd3eaeb8e9799d834d32caf623a Mon Sep 17 00:00:00 2001 From: Steven Arcangeli Date: Mon, 23 Oct 2023 23:10:49 -0700 Subject: [PATCH] test: more functional tests for trash --- lua/oil/view.lua | 1 + tests/test_util.lua | 12 ++++- tests/tmpdir.lua | 4 +- tests/trash_spec.lua | 123 +++++++++++++++++++++++++++++++++++++++---- 4 files changed, 127 insertions(+), 13 deletions(-) diff --git a/lua/oil/view.lua b/lua/oil/view.lua index aa6252ae..dce59e35 100644 --- a/lua/oil/view.lua +++ b/lua/oil/view.lua @@ -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 } } diff --git a/tests/test_util.lua b/tests/test_util.lua index ba270ff5..53d4bfed 100644 --- a/tests/test_util.lua +++ b/tests/test_util.lua @@ -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, diff --git a/tests/tmpdir.lua b/tests/tmpdir.lua index 79be6c34..bb8a9c0a 100644 --- a/tests/tmpdir.lua +++ b/tests/tmpdir.lua @@ -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 diff --git a/tests/trash_spec.lua b/tests/trash_spec.lua index ec98f777..e74ae77b 100644 --- a/tests/trash_spec.lua +++ b/tests/trash_spec.lua @@ -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()) @@ -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)