Skip to content

Commit

Permalink
fix: bug copying file multiple times
Browse files Browse the repository at this point in the history
  • Loading branch information
stevearc committed Nov 20, 2023
1 parent 8f0bf37 commit 05cb825
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 29 deletions.
14 changes: 9 additions & 5 deletions lua/oil/mutator/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ M.create_actions_from_diffs = function(all_diffs)
table.insert(actions, action)
end
end
---@type table<integer, string>
local dest_by_id = {}
for bufnr, diffs in pairs(all_diffs) do
local adapter = util.get_adapter(bufnr)
if not adapter then
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
16 changes: 15 additions & 1 deletion tests/regression_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
14 changes: 14 additions & 0 deletions tests/test_util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
32 changes: 9 additions & 23 deletions tests/trash_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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",
})
Expand All @@ -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)

0 comments on commit 05cb825

Please sign in to comment.