From 05cb8257cb9257144e63f41ccfe5a41ba3d1003c Mon Sep 17 00:00:00 2001 From: Steven Arcangeli Date: Sun, 19 Nov 2023 23:58:40 -0800 Subject: [PATCH] fix: bug copying file multiple times --- lua/oil/mutator/init.lua | 14 +++++++++----- tests/regression_spec.lua | 16 +++++++++++++++- tests/test_util.lua | 14 ++++++++++++++ tests/trash_spec.lua | 32 +++++++++----------------------- 4 files changed, 47 insertions(+), 29 deletions(-) diff --git a/lua/oil/mutator/init.lua b/lua/oil/mutator/init.lua index 9857b59d..60a6ba67 100644 --- a/lua/oil/mutator/init.lua +++ b/lua/oil/mutator/init.lua @@ -68,8 +68,6 @@ M.create_actions_from_diffs = function(all_diffs) table.insert(actions, action) end end - ---@type table - local dest_by_id = {} for bufnr, diffs in pairs(all_diffs) do local adapter = util.get_adapter(bufnr) if not adapter then @@ -80,7 +78,9 @@ M.create_actions_from_diffs = function(all_diffs) if diff.type == "new" then if diff.id then local by_id = diff_by_id[diff.id] - dest_by_id[diff.id] = parent_url .. diff.name + ---HACK: set the destination on this diff for use later + ---@diagnostic disable-next-line: inject-field + diff.dest = parent_url .. diff.name table.insert(by_id, diff) else -- Parse nested files like foo/bar/baz @@ -145,7 +145,9 @@ M.create_actions_from_diffs = function(all_diffs) add_action({ type = i == #diffs and "move" or "copy", entry_type = entry[FIELD_TYPE], - dest_url = dest_by_id[diff.id], + ---HACK: access the dest field we set above + ---@diagnostic disable-next-line: undefined-field + dest_url = diff.dest, src_url = cache.get_parent_url(id) .. entry[FIELD_NAME], }) end @@ -164,7 +166,9 @@ M.create_actions_from_diffs = function(all_diffs) type = "copy", entry_type = entry[FIELD_TYPE], src_url = cache.get_parent_url(id) .. entry[FIELD_NAME], - dest_url = dest_by_id[diff.id], + ---HACK: access the dest field we set above + ---@diagnostic disable-next-line: undefined-field + dest_url = diff.dest, }) end end diff --git a/tests/regression_spec.lua b/tests/regression_spec.lua index f730ee5e..20da321e 100644 --- a/tests/regression_spec.lua +++ b/tests/regression_spec.lua @@ -12,7 +12,6 @@ a.describe("regression tests", function() a.after_each(function() if tmpdir then tmpdir:dispose() - a.util.scheduler() tmpdir = nil end test_util.reset_editor() @@ -117,4 +116,19 @@ a.describe("regression tests", function() test_util.wait_for_autocmd({ "User", pattern = "OilEnter" }) assert.are.same({ bufnr }, require("oil.view").get_all_buffers()) end) + + a.it("can copy a file multiple times", function() + test_util.actions.open({ tmpdir.path }) + vim.api.nvim_feedkeys("ifoo.txt", "x", true) + test_util.actions.save() + vim.api.nvim_feedkeys("yyp$ciWbar.txt", "x", true) + vim.api.nvim_feedkeys("yyp$ciWbaz.txt", "x", true) + test_util.actions.save() + assert.are.same({ "bar.txt", "baz.txt", "foo.txt" }, test_util.parse_entries(0)) + tmpdir:assert_fs({ + ["foo.txt"] = "", + ["bar.txt"] = "", + ["baz.txt"] = "", + }) + end) end) diff --git a/tests/test_util.lua b/tests/test_util.lua index 53d4bfed..00766893 100644 --- a/tests/test_util.lua +++ b/tests/test_util.lua @@ -114,4 +114,18 @@ M.actions = { end, } +---Get the raw list of filenames from an unmodified oil buffer +---@param bufnr? integer +---@return string[] +M.parse_entries = function(bufnr) + bufnr = bufnr or 0 + if vim.bo[bufnr].modified then + error("parse_entries doesn't work on a modified oil buffer") + end + local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, true) + return vim.tbl_map(function(line) + return line:match("^/%d+ +(.+)$") + end, lines) +end + return M diff --git a/tests/trash_spec.lua b/tests/trash_spec.lua index cf6b8359..d09a57f9 100644 --- a/tests/trash_spec.lua +++ b/tests/trash_spec.lua @@ -2,20 +2,6 @@ require("plenary.async").tests.add_to_env() local TmpDir = require("tests.tmpdir") local test_util = require("tests.test_util") ----Get the raw list of filenames from an unmodified oil buffer ----@param bufnr? integer ----@return string[] -local function parse_entries(bufnr) - bufnr = bufnr or 0 - if vim.bo[bufnr].modified then - error("parse_entries doesn't work on a modified oil buffer") - end - local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, true) - return vim.tbl_map(function(line) - return line:match("^/%d+ +(.+)$") - end, lines) -end - a.describe("freedesktop", function() local tmpdir local tmphome @@ -50,7 +36,7 @@ a.describe("freedesktop", function() tmpdir:assert_not_exists("a.txt") tmpdir:assert_exists("foo/b.txt") test_util.actions.reload() - assert.are.same({ "a.txt" }, parse_entries(0)) + assert.are.same({ "a.txt" }, test_util.parse_entries(0)) end) a.it("deleting a file moves it to trash", function() @@ -62,7 +48,7 @@ a.describe("freedesktop", function() 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)) + assert.are.same({ "a.txt" }, test_util.parse_entries(0)) end) a.it("deleting a directory moves it to trash", function() @@ -74,7 +60,7 @@ a.describe("freedesktop", function() tmpdir:assert_not_exists("foo") tmpdir:assert_exists("a.txt") test_util.actions.open({ "--trash", tmpdir.path }) - assert.are.same({ "foo/" }, parse_entries(0)) + assert.are.same({ "foo/" }, test_util.parse_entries(0)) end) a.it("deleting a file from trash deletes it permanently", function() @@ -89,7 +75,7 @@ a.describe("freedesktop", function() test_util.actions.save() test_util.actions.reload() tmpdir:assert_not_exists("a.txt") - assert.are.same({}, parse_entries(0)) + assert.are.same({}, test_util.parse_entries(0)) end) a.it("cannot create files in the trash", function() @@ -102,7 +88,7 @@ a.describe("freedesktop", function() 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)) + assert.are.same({ "a.txt" }, test_util.parse_entries(0)) end) a.it("cannot rename files in the trash", function() @@ -115,7 +101,7 @@ a.describe("freedesktop", function() vim.api.nvim_feedkeys("0facwnew_name", "x", true) test_util.actions.save() test_util.actions.reload() - assert.are.same({ "a.txt" }, parse_entries(0)) + assert.are.same({ "a.txt" }, test_util.parse_entries(0)) end) a.it("cannot copy files in the trash", function() @@ -128,7 +114,7 @@ a.describe("freedesktop", function() vim.api.nvim_feedkeys("yypp", "x", true) test_util.actions.save() test_util.actions.reload() - assert.are.same({ "a.txt" }, parse_entries(0)) + assert.are.same({ "a.txt" }, test_util.parse_entries(0)) end) a.it("can restore files from trash", function() @@ -143,7 +129,7 @@ a.describe("freedesktop", function() vim.api.nvim_feedkeys("p", "x", true) test_util.actions.save() test_util.actions.reload() - assert.are.same({ "a.txt" }, parse_entries(0)) + assert.are.same({ "a.txt" }, test_util.parse_entries(0)) tmpdir:assert_fs({ ["a.txt"] = "a.txt", }) @@ -159,6 +145,6 @@ a.describe("freedesktop", function() 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)) + assert.are.same({ "a.txt", "a.txt" }, test_util.parse_entries(0)) end) end)