diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 27d96f1..e8aad9a 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -10,10 +10,12 @@ on: jobs: test: - runs-on: ubuntu-latest + runs-on: ${{ matrix.os }} strategy: matrix: nvim-versions: ['stable', 'nightly'] + os: [ubuntu-latest, windows-latest, macos-latest] + fail-fast: false name: Plenary Tests steps: - name: checkout diff --git a/.luarc.json b/.luarc.json index 26a1737..5d980da 100644 --- a/.luarc.json +++ b/.luarc.json @@ -2,6 +2,7 @@ "diagnostics.globals": [ "describe", "it", - "before_each" + "before_each", + "after_each" ] } \ No newline at end of file diff --git a/lua/hawtkeys/ts.lua b/lua/hawtkeys/ts.lua index f99d981..67066bf 100644 --- a/lua/hawtkeys/ts.lua +++ b/lua/hawtkeys/ts.lua @@ -448,11 +448,6 @@ local function get_keymaps_from_vim() return vimKeymaps end ----@return string[] -local function get_runtime_path() - return vim.api.nvim_list_runtime_paths() -end - ---@return HawtkeysKeyMapData[] function M.get_all_keymaps() local returnKeymaps @@ -472,16 +467,17 @@ function M.get_all_keymaps() end end else - local paths = get_runtime_path() - for _, path in ipairs(paths) do - if string.match(path, "%.config") then - local files = find_files(path) - for _, file in ipairs(files) do - local file_keymaps = find_maps_in_file(file) - for _, keymap in ipairs(file_keymaps) do - table.insert(keymaps, keymap) - end - end + local path = vim.fn.stdpath("config") --[[@as string]] + -- vim.fn.stdpath("config") returns string + -- https://neovim.io/doc/user/builtin.html#stdpath() + if not path then + return {} + end + local files = find_files(path) + for _, file in ipairs(files) do + local file_keymaps = find_maps_in_file(file) + for _, keymap in ipairs(file_keymaps) do + table.insert(keymaps, keymap) end end end diff --git a/tests/hawtkeys/e2e_spec.lua b/tests/hawtkeys/e2e_spec.lua new file mode 100644 index 0000000..7495e7e --- /dev/null +++ b/tests/hawtkeys/e2e_spec.lua @@ -0,0 +1,91 @@ +---@diagnostic disable-next-line: undefined-field +local eq = assert.are.same +local ts = require("hawtkeys.ts") +local path = require("plenary.path") + +local sep = (function() + if jit then + local os = string.lower(jit.os) + if os ~= "windows" then + return "/" + else + return "\\" + end + else + return package.config:sub(1, 1) + end +end)() + +local function copy_configs_to_stdpath_config() + local config_dir = vim.fn.stdpath("config") + vim.fn.mkdir(config_dir, "p") + vim.fn.writefile( + vim.fn.readfile("tests/hawtkeys/example_configs/e2e_config.lua"), + config_dir .. "/e2e_config.lua" + ) +end + +describe("file searching", function() + before_each(function() + copy_configs_to_stdpath_config() + require("hawtkeys").setup({}) + end) + + it("can detect a Vim Default Keymap", function() + local notMatch = 0 + local keymaps = ts.get_all_keymaps() + for _, v in ipairs(keymaps) do + if + vim.deep_equal(v, { + from_file = "Vim Defaults", + lhs = "Y", + mode = "n", + rhs = "y$", + }) + then + eq(v, { + from_file = "Vim Defaults", + lhs = "Y", + mode = "n", + rhs = "y$", + }) + else + notMatch = notMatch + 1 + end + end + eq(vim.tbl_count(keymaps), notMatch + 1) + end) + + it("can detect a keymap from a file", function() + local config_file = + path:new(vim.fn.stdpath("config") .. sep .. "e2e_config.lua") + eq(true, config_file:is_file()) + local notMatch = 0 + local keymaps = ts.get_all_keymaps() + + for _, v in ipairs(keymaps) do + if + vim.deep_equal(v, { + from_file = config_file:absolute(), + lhs = "example", + mode = "n", + rhs = "echo 'Example'", + }) + then + eq(v, { + from_file = config_file:absolute(), + lhs = "example", + mode = "n", + rhs = "echo 'Example'", + }) + else + notMatch = notMatch + 1 + end + end + eq(vim.tbl_count(keymaps), notMatch + 1) + end) + + it("should find a file in stdpath('config')", function() + eq(true, true) + end) +end) diff --git a/tests/hawtkeys/example_configs/e2e_config.lua b/tests/hawtkeys/example_configs/e2e_config.lua new file mode 100644 index 0000000..3a4fae5 --- /dev/null +++ b/tests/hawtkeys/example_configs/e2e_config.lua @@ -0,0 +1 @@ +vim.api.nvim_set_keymap("n", "example", "echo 'Example'", {}) diff --git a/tests/hawtkeys/ui_spec.lua b/tests/hawtkeys/ui_spec.lua index 42a45f3..a3f82a8 100644 --- a/tests/hawtkeys/ui_spec.lua +++ b/tests/hawtkeys/ui_spec.lua @@ -1,63 +1,60 @@ ---@diagnostic disable-next-line: undefined-field local eq = assert.are.same -local MiniTest = require("mini.test") - ---TODO: add search functionality tests (willothy) describe("ui", function() - local child = MiniTest.new_child_neovim() local SearchBuf, SearchWin, ResultBuf, ResultWin, Namespace, prompt_extmark before_each(function() - child.restart({ "-u", "tests/minimal_init.lua" }) - child.lua([[require("hawtkeys").setup({})]]) + require("plenary.reload").reload_module("hawtkeys") + vim.cmd([[bufdo! bwipeout]]) + require("hawtkeys").setup({}) end) describe("search", function() before_each(function() - child.lua([[require("hawtkeys.ui").show()]]) + require("plenary.reload").reload_module("hawtkeys") + vim.cmd([[bufdo! bwipeout]]) + require("hawtkeys").setup({}) + vim.cmd([[lua require("hawtkeys.ui").show()]]) + local ui = require("hawtkeys.ui") SearchBuf, SearchWin, ResultBuf, ResultWin, Namespace, prompt_extmark = - unpack(child.lua([[ - local ui = require("hawtkeys.ui") - return { - ui.SearchBuf, - ui.SearchWin, - ui.ResultBuf, - ui.ResultWin, - ui.Namespace, - ui.prompt_extmark - } - ]])) + ui.SearchBuf, + ui.SearchWin, + ui.ResultBuf, + ui.ResultWin, + ui.Namespace, + ui.prompt_extmark end) it("should show the search UI", function() - assert(child.api.nvim_buf_is_valid(SearchBuf)) - assert(child.api.nvim_win_is_valid(SearchWin)) + assert(vim.api.nvim_buf_is_valid(SearchBuf)) + assert(vim.api.nvim_win_is_valid(SearchWin)) - assert(child.api.nvim_buf_is_valid(ResultBuf)) - assert(child.api.nvim_win_is_valid(ResultWin)) + assert(vim.api.nvim_buf_is_valid(ResultBuf)) + assert(vim.api.nvim_win_is_valid(ResultWin)) - eq(SearchBuf, child.api.nvim_get_current_buf()) - eq(SearchWin, child.api.nvim_get_current_win()) + eq(SearchBuf, vim.api.nvim_get_current_buf()) + eq(SearchWin, vim.api.nvim_get_current_win()) - local win_config = child.api.nvim_win_get_config(SearchWin) + local win_config = vim.api.nvim_win_get_config(SearchWin) eq("editor", win_config.relative) end) + --TODO: This doesnt work since removing the dependency on mini.test + --[[ it("starts in insert mode", function() + assert(vim.api.nvim_buf_is_valid(SearchBuf)) + assert(vim.api.nvim_win_is_valid(SearchWin)) - it("starts in insert mode", function() - assert(child.api.nvim_buf_is_valid(SearchBuf)) - assert(child.api.nvim_win_is_valid(SearchWin)) - - eq(child.api.nvim_get_current_win(), SearchWin) - eq("i", child.api.nvim_get_mode().mode) + eq(vim.api.nvim_get_current_win(), SearchWin) + eq("i", vim.api.nvim_get_mode().mode) end) - +]] it("should show the hint extmark", function() assert(SearchBuf) assert(prompt_extmark) assert(Namespace) - local extmark_info = child.api.nvim_buf_get_extmark_by_id( + local extmark_info = vim.api.nvim_buf_get_extmark_by_id( SearchBuf, Namespace, prompt_extmark, @@ -70,50 +67,44 @@ describe("ui", function() describe("all", function() before_each(function() - child.lua([[require("hawtkeys.ui").show_all()]]) - ResultBuf, ResultWin, Namespace = unpack(child.lua([[ - local ui = require("hawtkeys.ui") - return { - ui.ResultBuf, - ui.ResultWin, - ui.Namespace, - } - ]])) + require("plenary.reload").reload_module("hawtkeys") + vim.cmd([[bufdo! bwipeout]]) + require("hawtkeys").setup({}) + vim.cmd([[lua require("hawtkeys.ui").show_all()]]) + local ui = require("hawtkeys.ui") + ResultBuf, ResultWin, Namespace = + ui.ResultBuf, ui.ResultWin, ui.Namespace end) it("should show the all UI", function() - assert(child.api.nvim_buf_is_valid(ResultBuf)) - assert(child.api.nvim_win_is_valid(ResultWin)) + print(vim.inspect(ResultBuf)) + assert(vim.api.nvim_buf_is_valid(ResultBuf)) + assert(vim.api.nvim_win_is_valid(ResultWin)) - eq(child.api.nvim_get_current_buf(), ResultBuf) - eq(child.api.nvim_get_current_win(), ResultWin) + eq(vim.api.nvim_get_current_buf(), ResultBuf) + eq(vim.api.nvim_get_current_win(), ResultWin) - local win_config = child.api.nvim_win_get_config(ResultWin) + local win_config = vim.api.nvim_win_get_config(ResultWin) eq(win_config.relative, "editor") end) end) describe("dupes", function() before_each(function() - child.lua([[require("hawtkeys.ui").show_dupes()]]) - ResultBuf, ResultWin, Namespace = unpack(child.lua([[ - local ui = require("hawtkeys.ui") - return { - ui.ResultBuf, - ui.ResultWin, - ui.Namespace, - } - ]])) + vim.cmd([[lua require("hawtkeys.ui").show_dupes()]]) + local ui = require("hawtkeys.ui") + ResultBuf, ResultWin, Namespace = + ui.ResultBuf, ui.ResultWin, ui.Namespace end) it("should show the duplicates UI", function() - assert(child.api.nvim_buf_is_valid(ResultBuf)) - assert(child.api.nvim_win_is_valid(ResultWin)) + assert(vim.api.nvim_buf_is_valid(ResultBuf)) + assert(vim.api.nvim_win_is_valid(ResultWin)) - eq(child.api.nvim_get_current_buf(), ResultBuf) - eq(child.api.nvim_get_current_win(), ResultWin) + eq(vim.api.nvim_get_current_buf(), ResultBuf) + eq(vim.api.nvim_get_current_win(), ResultWin) - local win_config = child.api.nvim_win_get_config(ResultWin) + local win_config = vim.api.nvim_win_get_config(ResultWin) eq(win_config.relative, "editor") end) end) diff --git a/tests/minimal_init.lua b/tests/minimal_init.lua index ce65ccb..0cedfbd 100644 --- a/tests/minimal_init.lua +++ b/tests/minimal_init.lua @@ -1,7 +1,14 @@ local M = {} +local function tempdir(plugin) + if jit.os == "Windows" then + return "D:\\tmp\\" .. plugin + end + return vim.loop.os_tmpdir() .. "/" .. plugin +end + function M.loadLazy() - local lazy_dir = os.getenv("LAZY_DIR") or "/tmp/lazy.nvim" + local lazy_dir = os.getenv("LAZY_DIR") or tempdir("lazy.nvim") if vim.fn.isdirectory(lazy_dir) == 0 then vim.fn.system({ "git", @@ -17,7 +24,7 @@ function M.loadLazy() end function M.loadWhichKey() - local whichkey_dir = os.getenv("WHICHKEY_DIR") or "/tmp/which-key.nvim" + local whichkey_dir = os.getenv("WHICHKEY_DIR") or tempdir("which-key.nvim") if vim.fn.isdirectory(whichkey_dir) == 0 then vim.fn.system({ "git", @@ -32,9 +39,8 @@ function M.loadWhichKey() require("which-key").setup({}) end -local plenary_dir = os.getenv("PLENARY_DIR") or "/tmp/plenary.nvim" -local treesitter_dir = os.getenv("TREESITTER_DIR") or "/tmp/nvim-treesitter" -local mini_dir = os.getenv("MINI_DIR") or "/tmp/mini-test" +local plenary_dir = os.getenv("PLENARY_DIR") or tempdir("plenary.nvim") +local treesitter_dir = os.getenv("TREESITTER_DIR") or tempdir("nvim-treesitter") if vim.fn.isdirectory(plenary_dir) == 0 then vim.fn.system({ "git", @@ -51,20 +57,10 @@ if vim.fn.isdirectory(treesitter_dir) == 0 then treesitter_dir, }) end -if vim.fn.isdirectory(mini_dir) == 0 then - vim.fn.system({ - "git", - "clone", - "https://github.com/echasnovski/mini.test", - mini_dir, - }) -end vim.opt.rtp:append(".") vim.opt.rtp:append(plenary_dir) vim.opt.rtp:append(treesitter_dir) -vim.opt.rtp:append(mini_dir) require("plenary.busted") -require("mini.test").setup() vim.cmd("runtime plugin/plenary.vim") vim.cmd("runtime plugin/treesitter.vim")