diff options
author | kylo252 <[email protected]> | 2022-01-02 14:53:01 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2022-01-02 14:53:01 +0100 |
commit | b3cfd165fbca4c8b595ed577027a5171e33a00e9 (patch) | |
tree | bfc6526edf613b294733d46ff823d69d6e9a58df /tests | |
parent | 73bf039c6333ba9cb3af93437b26c41e14566c47 (diff) |
refactor(test): cleanup test utilities (#2132)
Diffstat (limited to 'tests')
-rw-r--r-- | tests/helpers.lua | 51 | ||||
-rw-r--r-- | tests/minimal_init.lua | 9 | ||||
-rw-r--r-- | tests/minimal_lsp.lua | 7 | ||||
-rw-r--r-- | tests/specs/bootstrap_spec.lua (renamed from tests/bootstrap_spec.lua) | 0 | ||||
-rw-r--r-- | tests/specs/config_loader_spec.lua (renamed from tests/config_loader_spec.lua) | 0 | ||||
-rw-r--r-- | tests/specs/lsp_spec.lua (renamed from tests/lsp_spec.lua) | 8 | ||||
-rw-r--r-- | tests/specs/plugins_load_spec.lua (renamed from tests/plugins_load_spec.lua) | 0 |
7 files changed, 65 insertions, 10 deletions
diff --git a/tests/helpers.lua b/tests/helpers.lua new file mode 100644 index 00000000..ada83267 --- /dev/null +++ b/tests/helpers.lua @@ -0,0 +1,51 @@ +local M = {} + +function M.search_file(file, args) + local Job = require "plenary.job" + local stderr = {} + local stdout, ret = Job + :new({ + command = "grep", + args = { args, file }, + cwd = get_cache_dir(), + on_stderr = function(_, data) + table.insert(stderr, data) + end, + }) + :sync() + return stdout, ret, stderr +end + +function M.file_contains(file, query) + local stdout, ret, stderr = M.search_file(file, query) + if ret == 0 then + return true + end + if not vim.tbl_isempty(stderr) then + error(vim.inspect(stderr)) + end + if not vim.tbl_isempty(stdout) then + error(vim.inspect(stdout)) + end + return false +end + +function M.log_contains(query) + local logfile = require("lvim.core.log"):get_path() + local stdout, ret, stderr = M.search_file(logfile, query) + if ret == 0 then + return true + end + if not vim.tbl_isempty(stderr) then + error(vim.inspect(stderr)) + end + if not vim.tbl_isempty(stdout) then + error(vim.inspect(stdout)) + end + if not vim.tbl_isempty(stderr) then + error(vim.inspect(stderr)) + end + return false +end + +return M diff --git a/tests/minimal_init.lua b/tests/minimal_init.lua index 126f8e6e..0178514b 100644 --- a/tests/minimal_init.lua +++ b/tests/minimal_init.lua @@ -1,5 +1,12 @@ local path_sep = vim.loop.os_uname().version:match "Windows" and "\\" or "/" +local base_dir = os.getenv "LUNARVIM_RUNTIME_DIR" .. path_sep .. "lvim" +local tests_dir = base_dir .. path_sep .. "tests" -vim.opt.rtp:append(os.getenv "LUNARVIM_RUNTIME_DIR" .. path_sep .. "lvim") +vim.opt.rtp = { base_dir, tests_dir, os.getenv "VIMRUNTIME" } + +vim.opt.swapfile = false + +-- load helper functions before any other plugin to avoid name-collisions +pcall(require, "tests.helpers") require("lvim.bootstrap"):init() diff --git a/tests/minimal_lsp.lua b/tests/minimal_lsp.lua index 12a4a6a9..a4ecd4fb 100644 --- a/tests/minimal_lsp.lua +++ b/tests/minimal_lsp.lua @@ -8,12 +8,7 @@ end vim.cmd [[set runtimepath=$VIMRUNTIME]] -local temp_dir -if on_windows then - temp_dir = vim.loop.os_getenv "TEMP" -else - temp_dir = "/tmp" -end +local temp_dir = vim.loop.os_getenv "TEMP" or "/tmp" vim.cmd("set packpath=" .. join_paths(temp_dir, "nvim", "site")) diff --git a/tests/bootstrap_spec.lua b/tests/specs/bootstrap_spec.lua index c86d22d4..c86d22d4 100644 --- a/tests/bootstrap_spec.lua +++ b/tests/specs/bootstrap_spec.lua diff --git a/tests/config_loader_spec.lua b/tests/specs/config_loader_spec.lua index 1aef0974..1aef0974 100644 --- a/tests/config_loader_spec.lua +++ b/tests/specs/config_loader_spec.lua diff --git a/tests/lsp_spec.lua b/tests/specs/lsp_spec.lua index 17e72577..633aa17c 100644 --- a/tests/lsp_spec.lua +++ b/tests/specs/lsp_spec.lua @@ -1,6 +1,8 @@ local a = require "plenary.async_lib.tests" local utils = require "lvim.utils" -lvim.lsp.templates_dir = join_paths(get_runtime_dir(), "lvim", "tests", "artifacts") +local helpers = require "tests.helpers" +local temp_dir = vim.loop.os_getenv "TEMP" or "/tmp" +lvim.lsp.templates_dir = join_paths(temp_dir, "lvim", "tests", "artifacts") a.describe("lsp workflow", function() local Log = require "lvim.core.log" @@ -40,7 +42,7 @@ a.describe("lsp workflow", function() -- we need to delay this check until the log gets populated vim.schedule(function() - assert.False(utils.log_contains "templates") + assert.False(helpers.log_contains "templates") end) end) @@ -50,7 +52,7 @@ a.describe("lsp workflow", function() for _, file in ipairs(vim.fn.glob(lvim.lsp.templates_dir .. "/*.lua", 1, 1)) do for _, server in ipairs(lvim.lsp.override) do - assert.False(utils.file_contains(file, server)) + assert.False(helpers.file_contains(file, server)) end end end) diff --git a/tests/plugins_load_spec.lua b/tests/specs/plugins_load_spec.lua index 08c96c12..08c96c12 100644 --- a/tests/plugins_load_spec.lua +++ b/tests/specs/plugins_load_spec.lua |