From 23fb0002cf466c8a4af96ffb7d8aed584114e97e Mon Sep 17 00:00:00 2001 From: Michael Paulson Date: Fri, 1 Dec 2023 13:44:59 -0700 Subject: [PATCH] fix: broken unit tests --- lua/harpoon/buffer.lua | 2 +- lua/harpoon/config.lua | 2 +- lua/harpoon/test/ui_spec.lua | 16 ++++++++++++++++ lua/harpoon/test/utils.lua | 21 +++++++++++++++++++-- 4 files changed, 37 insertions(+), 4 deletions(-) diff --git a/lua/harpoon/buffer.lua b/lua/harpoon/buffer.lua index c224594..1a59bcd 100644 --- a/lua/harpoon/buffer.lua +++ b/lua/harpoon/buffer.lua @@ -8,7 +8,7 @@ local HARPOON_MENU = "__harpoon-menu__" -- simple reason here is that if we are deving harpoon, we will create several -- ui objects, each with their own buffer, which will cause the name to be -- duplicated and then we will get a vim error on nvim_buf_set_name -local harpoon_menu_id = 0 +local harpoon_menu_id = math.random(1000000) local function get_harpoon_menu_name() harpoon_menu_id = harpoon_menu_id + 1 diff --git a/lua/harpoon/config.lua b/lua/harpoon/config.lua index d09b8d1..a761742 100644 --- a/lua/harpoon/config.lua +++ b/lua/harpoon/config.lua @@ -57,7 +57,7 @@ function M.get_default_config() return { settings = { - save_on_toggle = true, + save_on_toggle = false, jump_to_file_location = true, key = function() return vim.loop.cwd() diff --git a/lua/harpoon/test/ui_spec.lua b/lua/harpoon/test/ui_spec.lua index db84591..39212a1 100644 --- a/lua/harpoon/test/ui_spec.lua +++ b/lua/harpoon/test/ui_spec.lua @@ -40,6 +40,7 @@ describe("harpoon", function() table.remove(created_files, 2) Buffer.set_contents(harpoon.ui.bufnr, created_files) harpoon.ui:save() + harpoon.ui:toggle_quick_menu() eq(harpoon:list():length(), 2) eq(harpoon:list():display(), created_files) @@ -55,6 +56,7 @@ describe("harpoon", function() harpoon.ui:toggle_quick_menu(list) Buffer.set_contents(harpoon.ui.bufnr, created_files) harpoon.ui:save() + harpoon.ui:toggle_quick_menu() eq(list:length(), 4) eq(list:display(), created_files) @@ -91,4 +93,18 @@ describe("harpoon", function() eq(harpoon.ui.bufnr, nil) eq(harpoon.ui.win_id, nil) end) + + it("closing toggle_quick_menu with save_on_toggle should save contents", function() + harpoon:setup({ settings = { save_on_toggle = true }}) + local list = harpoon:list() + local created_files = utils.fill_list_with_files(3, list) + + harpoon.ui:toggle_quick_menu(list) + table.remove(created_files, 2) + Buffer.set_contents(harpoon.ui.bufnr, created_files) + harpoon.ui:toggle_quick_menu() + + eq(list:length(), 2) + eq(list:display(), created_files) + end) end) diff --git a/lua/harpoon/test/utils.lua b/lua/harpoon/test/utils.lua index 0cf284a..25bbe0d 100644 --- a/lua/harpoon/test/utils.lua +++ b/lua/harpoon/test/utils.lua @@ -4,6 +4,22 @@ local M = {} M.created_files = {} +local checkpoint_file = nil +local checkpoint_file_bufnr = nil +function M.create_checkpoint_file() + checkpoint_file = os.tmpname() + checkpoint_file_bufnr = M.create_file(checkpoint_file, { "test" }) +end + +function M.return_to_checkpoint() + if checkpoint_file_bufnr == nil then + return + end + + vim.api.nvim_set_current_buf(checkpoint_file_bufnr) + M.clean_files() +end + ---@param name string function M.before_each(name) return function() @@ -15,7 +31,7 @@ function M.before_each(name) Data.set_data_path(name) local harpoon = require("harpoon") - M.clean_files() + M.return_to_checkpoint() harpoon:setup({ settings = { @@ -39,6 +55,7 @@ end ---@param contents string[] function M.create_file(name, contents, row, col) local bufnr = vim.fn.bufnr(name, true) + vim.api.nvim_buf_set_option(bufnr, "bufhidden", "hide") vim.api.nvim_set_current_buf(bufnr) vim.api.nvim_buf_set_text(0, 0, 0, 0, 0, contents) if row then @@ -54,7 +71,7 @@ end function M.fill_list_with_files(count, list) local files = {} - for _ = 1, count do + for i = 1, count do local name = os.tmpname() table.insert(files, name) M.create_file(name, { "test" })