aboutsummaryrefslogtreecommitdiffstats
path: root/tests/helpers
diff options
context:
space:
mode:
authorWilliam Boman <william@redwill.se>2022-03-26 13:41:50 +0100
committerGitHub <noreply@github.com>2022-03-26 13:41:50 +0100
commit212d17a039da449043b67529c29851db37acc236 (patch)
tree38411b14487895cef0d7648e198b79fd28793fe6 /tests/helpers
parentrun autogen_metadata.lua (diff)
downloadmason-212d17a039da449043b67529c29851db37acc236.tar
mason-212d17a039da449043b67529c29851db37acc236.tar.gz
mason-212d17a039da449043b67529c29851db37acc236.tar.bz2
mason-212d17a039da449043b67529c29851db37acc236.tar.lz
mason-212d17a039da449043b67529c29851db37acc236.tar.xz
mason-212d17a039da449043b67529c29851db37acc236.tar.zst
mason-212d17a039da449043b67529c29851db37acc236.zip
add async managers (#536)
Diffstat (limited to 'tests/helpers')
-rw-r--r--tests/helpers/lua/luassertx.lua62
-rw-r--r--tests/helpers/lua/test_helpers.lua75
2 files changed, 137 insertions, 0 deletions
diff --git a/tests/helpers/lua/luassertx.lua b/tests/helpers/lua/luassertx.lua
new file mode 100644
index 00000000..06d08ede
--- /dev/null
+++ b/tests/helpers/lua/luassertx.lua
@@ -0,0 +1,62 @@
+local assert = require "luassert"
+local match = require "luassert.match"
+local a = require "nvim-lsp-installer.core.async"
+
+local function wait_for(_, arguments)
+ ---@type fun() @Function to execute until it does not error.
+ local assertions_fn = arguments[1]
+ ---@type number @Timeout in milliseconds. Defaults to 5000.
+ local timeout = arguments[2]
+ timeout = timeout or 15000
+
+ local start = vim.loop.hrtime()
+ local is_ok, err
+ repeat
+ is_ok, err = pcall(assertions_fn)
+ if not is_ok then
+ a.sleep(math.min(timeout, 100))
+ end
+ until is_ok or ((vim.loop.hrtime() - start) / 1e6) > timeout
+
+ if not is_ok then
+ error(err)
+ end
+
+ return is_ok
+end
+
+local function tbl_containing(_, arguments, _)
+ return function(value)
+ local expected = arguments[1]
+ for key, val in pairs(expected) do
+ if match.is_matcher(val) then
+ if not val(value[key]) then
+ return false
+ end
+ elseif value[key] ~= val then
+ return false
+ end
+ end
+ return true
+ end
+end
+
+local function list_containing(_, arguments, _)
+ return function(value)
+ local expected = arguments[1]
+ for _, val in pairs(value) do
+ if match.is_matcher(expected) then
+ if expected(val) then
+ return true
+ end
+ elseif expected == val then
+ return true
+ end
+ end
+ return false
+ end
+end
+
+assert:register("matcher", "tbl_containing", tbl_containing)
+assert:register("matcher", "list_containing", list_containing)
+assert:register("assertion", "wait_for", wait_for)
diff --git a/tests/helpers/lua/test_helpers.lua b/tests/helpers/lua/test_helpers.lua
new file mode 100644
index 00000000..23353fcb
--- /dev/null
+++ b/tests/helpers/lua/test_helpers.lua
@@ -0,0 +1,75 @@
+---@diagnostic disable: lowercase-global
+local mock = require "luassert.mock"
+local util = require "luassert.util"
+
+local a = require "nvim-lsp-installer.core.async"
+local process = require "nvim-lsp-installer.process"
+local server = require "nvim-lsp-installer.server"
+local Optional = require "nvim-lsp-installer.core.optional"
+local Result = require "nvim-lsp-installer.core.result"
+local receipt = require "nvim-lsp-installer.core.receipt"
+
+function async_test(suspend_fn)
+ return function()
+ local ok, err = pcall(a.run_blocking, suspend_fn)
+ if not ok then
+ error(err, util.errorlevel())
+ end
+ end
+end
+
+mockx = {
+ just_runs = function() end,
+ returns = function(val)
+ return function()
+ return val
+ end
+ end,
+ throws = function(exception)
+ return function()
+ error(exception, 2)
+ end
+ end,
+}
+
+function ServerGenerator(opts)
+ return server.Server:new(vim.tbl_deep_extend("force", {
+ name = "dummy",
+ languages = { "dummylang" },
+ root_dir = server.get_server_root_path "dummy",
+ homepage = "https://dummylang.org",
+ installer = function(_, callback, ctx)
+ ctx.stdio_sink.stdout "Installing dummy!\n"
+ callback(true)
+ end,
+ }, opts))
+end
+
+function FailingServerGenerator(opts)
+ return ServerGenerator(vim.tbl_deep_extend("force", {
+ installer = function(_, callback, ctx)
+ ctx.stdio_sink.stdout "Installing failing dummy!\n"
+ callback(false)
+ end,
+ }, opts))
+end
+
+function InstallContextGenerator(opts)
+ ---@type InstallContext
+ local default_opts = {
+ fs = mock.new {
+ append_file = mockx.just_runs,
+ dir_exists = mockx.returns(true),
+ file_exists = mockx.returns(true),
+ },
+ spawn = mock.new {},
+ cwd = function()
+ return "/tmp/install-dir"
+ end,
+ promote_cwd = mockx.returns(Result.success()),
+ receipt = receipt.InstallReceiptBuilder.new(),
+ requested_version = Optional.empty(),
+ }
+ local merged_opts = vim.tbl_deep_extend("force", default_opts, opts)
+ return mock.new(merged_opts)
+end