aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorWilliam Boman <william@redwill.se>2022-07-21 14:15:11 +0200
committerWilliam Boman <william@redwill.se>2022-07-22 03:03:23 +0200
commit1d459b6d19118b9944d5313e4439cb361d607366 (patch)
treed48a07eaa5fddebc75ade8bb1d3861992e0c11a3 /tests
downloadmason-lspconfig-1d459b6d19118b9944d5313e4439cb361d607366.tar
mason-lspconfig-1d459b6d19118b9944d5313e4439cb361d607366.tar.gz
mason-lspconfig-1d459b6d19118b9944d5313e4439cb361d607366.tar.bz2
mason-lspconfig-1d459b6d19118b9944d5313e4439cb361d607366.tar.lz
mason-lspconfig-1d459b6d19118b9944d5313e4439cb361d607366.tar.xz
mason-lspconfig-1d459b6d19118b9944d5313e4439cb361d607366.tar.zst
mason-lspconfig-1d459b6d19118b9944d5313e4439cb361d607366.zip
init
Diffstat (limited to 'tests')
-rw-r--r--tests/helpers/lua/dummy2_package.lua14
-rw-r--r--tests/helpers/lua/dummy_package.lua14
-rw-r--r--tests/helpers/lua/luassertx.lua70
-rw-r--r--tests/helpers/lua/test_helpers.lua49
-rw-r--r--tests/mason-lspconfig/api/command_spec.lua71
-rw-r--r--tests/mason-lspconfig/setup_spec.lua141
-rw-r--r--tests/minimal_init.vim39
7 files changed, 398 insertions, 0 deletions
diff --git a/tests/helpers/lua/dummy2_package.lua b/tests/helpers/lua/dummy2_package.lua
new file mode 100644
index 0000000..424e47d
--- /dev/null
+++ b/tests/helpers/lua/dummy2_package.lua
@@ -0,0 +1,14 @@
+local Pkg = require "mason-core.package"
+
+return Pkg.new {
+ name = "dummy2",
+ desc = [[This is a dummy2 package.]],
+ categories = { Pkg.Cat.LSP },
+ languages = { Pkg.Lang.Dummy2Lang },
+ homepage = "https://example.com",
+ ---@async
+ ---@param ctx InstallContext
+ install = function(ctx)
+ ctx.receipt:with_primary_source { type = "dummy2" }
+ end,
+}
diff --git a/tests/helpers/lua/dummy_package.lua b/tests/helpers/lua/dummy_package.lua
new file mode 100644
index 0000000..b38d1cd
--- /dev/null
+++ b/tests/helpers/lua/dummy_package.lua
@@ -0,0 +1,14 @@
+local Pkg = require "mason-core.package"
+
+return Pkg.new {
+ name = "dummy",
+ desc = [[This is a dummy package.]],
+ categories = { Pkg.Cat.LSP },
+ languages = { Pkg.Lang.DummyLang },
+ homepage = "https://example.com",
+ ---@async
+ ---@param ctx InstallContext
+ install = function(ctx)
+ ctx.receipt:with_primary_source { type = "dummy" }
+ end,
+}
diff --git a/tests/helpers/lua/luassertx.lua b/tests/helpers/lua/luassertx.lua
new file mode 100644
index 0000000..55ea0d7
--- /dev/null
+++ b/tests/helpers/lua/luassertx.lua
@@ -0,0 +1,70 @@
+local assert = require "luassert"
+local match = require "luassert.match"
+local a = require "mason-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
+
+local function instanceof(_, arguments, _)
+ return function(value)
+ local expected_mt = arguments[1]
+ return getmetatable(value) == expected_mt
+ end
+end
+
+assert:register("matcher", "tbl_containing", tbl_containing)
+assert:register("matcher", "list_containing", list_containing)
+assert:register("matcher", "instanceof", instanceof)
+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 0000000..57c0b4f
--- /dev/null
+++ b/tests/helpers/lua/test_helpers.lua
@@ -0,0 +1,49 @@
+---@diagnostic disable: lowercase-global
+local util = require "luassert.util"
+local spy = require "luassert.spy"
+
+local a = require "mason-core.async"
+local InstallHandle = require "mason-core.installer.handle"
+local InstallContext = require "mason-core.installer.context"
+local registry = require "mason-registry"
+
+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,
+}
+
+---@param package_name string
+function InstallHandleGenerator(package_name)
+ return InstallHandle.new(registry.get_package(package_name))
+end
+
+---@param handle InstallHandle
+---@param opts InstallContextOpts | nil
+function InstallContextGenerator(handle, opts)
+ local context = InstallContext.new(handle, opts or {})
+ context.spawn = setmetatable({}, {
+ __index = function(s, cmd)
+ s[cmd] = spy.new(mockx.just_runs())
+ return s[cmd]
+ end,
+ })
+ return context
+end
diff --git a/tests/mason-lspconfig/api/command_spec.lua b/tests/mason-lspconfig/api/command_spec.lua
new file mode 100644
index 0000000..0551c77
--- /dev/null
+++ b/tests/mason-lspconfig/api/command_spec.lua
@@ -0,0 +1,71 @@
+local spy = require "luassert.spy"
+local stub = require "luassert.stub"
+local match = require "luassert.match"
+
+local server_mappings = require "mason-lspconfig.mappings.server"
+local filetype_mappings = require "mason-lspconfig.mappings.filetype"
+local api = require "mason-lspconfig.api.command"
+local registry = require "mason-registry"
+local Pkg = require "mason-core.package"
+
+describe(":LspInstall", function()
+ server_mappings.lspconfig_to_package["dummylsp"] = "dummy"
+ server_mappings.package_to_lspconfig["dummy"] = "dummylsp"
+ filetype_mappings.dummylang = { "dummylsp" }
+
+ it("should install the provided servers", function()
+ local dummy = registry.get_package "dummy"
+ spy.on(Pkg, "install")
+ api.LspInstall { "dummylsp@1.0.0" }
+ assert.spy(Pkg.install).was_called(1)
+ assert.spy(Pkg.install).was_called_with(match.ref(dummy), {
+ version = "1.0.0",
+ })
+ end)
+
+ it(
+ "should prompt user for server to install based on filetype",
+ async_test(function()
+ local dummy = registry.get_package "dummy"
+ spy.on(Pkg, "install")
+ stub(vim.ui, "select")
+ vim.ui.select.invokes(function(items, opts, callback)
+ callback "dummylsp"
+ end)
+ vim.cmd [[new | setf dummylang]]
+ api.LspInstall {}
+ assert.spy(Pkg.install).was_called(1)
+ assert.spy(Pkg.install).was_called_with(match.ref(dummy), {
+ version = nil,
+ })
+ assert.spy(vim.ui.select).was_called(1)
+ assert.spy(vim.ui.select).was_called_with({ "dummylsp" }, match.is_table(), match.is_function())
+ end)
+ )
+
+ it(
+ "should not prompt user for server to install if no filetype match exists",
+ async_test(function()
+ spy.on(Pkg, "install")
+ stub(vim.ui, "select")
+ vim.cmd [[new | setf nolsplang]]
+ api.LspInstall {}
+ assert.spy(Pkg.install).was_called(0)
+ assert.spy(vim.ui.select).was_called(0)
+ end)
+ )
+end)
+
+describe(":LspUninstall", function()
+ server_mappings.lspconfig_to_package["dummylsp"] = "dummy"
+ server_mappings.package_to_lspconfig["dummy"] = "dummylsp"
+ filetype_mappings.dummylang = { "dummylsp" }
+
+ it("should uninstall the provided servers", function()
+ local dummy = registry.get_package "dummy"
+ spy.on(Pkg, "uninstall")
+ api.LspUninstall { "dummylsp" }
+ assert.spy(Pkg.uninstall).was_called(1)
+ assert.spy(Pkg.uninstall).was_called_with(match.ref(dummy))
+ end)
+end)
diff --git a/tests/mason-lspconfig/setup_spec.lua b/tests/mason-lspconfig/setup_spec.lua
new file mode 100644
index 0000000..9b960c0
--- /dev/null
+++ b/tests/mason-lspconfig/setup_spec.lua
@@ -0,0 +1,141 @@
+local match = require "luassert.match"
+local stub = require "luassert.stub"
+local spy = require "luassert.spy"
+
+local Pkg = require "mason-core.package"
+local mason_lspconfig = require "mason-lspconfig"
+local server_mappings = require "mason-lspconfig.mappings.server"
+local registry = require "mason-registry"
+local filetype_mappings = require "mason-lspconfig.mappings.filetype"
+
+describe("mason-lspconfig setup", function()
+ before_each(function()
+ server_mappings.lspconfig_to_package["dummylsp"] = "dummy"
+ server_mappings.lspconfig_to_package["dummy2lsp"] = "dummy2"
+ server_mappings.package_to_lspconfig["dummy"] = "dummylsp"
+ server_mappings.package_to_lspconfig["dummy2"] = "dummy2lsp"
+ filetype_mappings.dummylang = { "dummylsp", "dummy2lsp" }
+ require("lspconfig.util").on_setup = nil
+ local settings = require "mason-lspconfig.settings"
+ settings.set(settings._DEFAULT_SETTINGS)
+ vim.fn.delete(vim.env.INSTALL_ROOT_DIR, "rf")
+ end)
+
+ it("should set up user commands", function()
+ mason_lspconfig.setup()
+ local user_commands = vim.api.nvim_get_commands {}
+
+ assert.is_true(match.tbl_containing {
+ bang = false,
+ bar = false,
+ nargs = "*",
+ complete = "custom",
+ definition = "Install one or more LSP servers.",
+ }(user_commands["LspInstall"]))
+
+ assert.is_true(match.tbl_containing {
+ bang = false,
+ bar = false,
+ definition = "Uninstall one or more LSP servers.",
+ nargs = "+",
+ complete = "custom",
+ }(user_commands["LspUninstall"]))
+ end)
+
+ it(
+ "should install servers listed in ensure_installed",
+ async_test(function()
+ local dummy = registry.get_package "dummy"
+ spy.on(Pkg, "install")
+
+ mason_lspconfig.setup { ensure_installed = { "dummylsp@1.0.0" } }
+
+ assert.spy(Pkg.install).was_called(1)
+ assert.spy(Pkg.install).was_called_with(match.ref(dummy), {
+ version = "1.0.0",
+ })
+ assert.wait_for(function()
+ assert.is_true(dummy.handle:is_closed())
+ end)
+ end)
+ )
+
+ it(
+ "should automatically install servers",
+ async_test(function()
+ local dummy = registry.get_package "dummy"
+ local dummy2 = registry.get_package "dummy2"
+ spy.on(Pkg, "install")
+
+ mason_lspconfig.setup { automatic_installation = true }
+ local lspconfig = require "lspconfig"
+ spy.on(lspconfig.dummylsp, "setup")
+ spy.on(lspconfig.dummy2lsp, "setup")
+ lspconfig.dummylsp.setup {}
+ lspconfig.dummy2lsp.setup {}
+
+ assert.spy(Pkg.install).was_called(2)
+ assert.spy(Pkg.install).was_called_with(match.ref(dummy))
+ assert.spy(Pkg.install).was_called_with(match.ref(dummy2))
+
+ assert.wait_for(function()
+ assert.is_true(dummy.handle:is_closed())
+ assert.is_true(dummy2.handle:is_closed())
+ assert.spy(lspconfig.dummylsp.setup).was_called(2)
+ assert.spy(lspconfig.dummy2lsp.setup).was_called(2)
+ end)
+ end)
+ )
+end)
+
+describe("mason-lspconfig setup_handlers", function()
+ before_each(function()
+ server_mappings.lspconfig_to_package["dummylsp"] = "dummy"
+ server_mappings.lspconfig_to_package["dummy2lsp"] = "dummy2"
+ server_mappings.package_to_lspconfig["dummy"] = "dummylsp"
+ server_mappings.package_to_lspconfig["dummy2"] = "dummy2lsp"
+ filetype_mappings.dummylang = { "dummylsp", "dummy2lsp" }
+ require("lspconfig.util").on_setup = nil
+ local settings = require "mason-lspconfig.settings"
+ settings.set(settings._DEFAULT_SETTINGS)
+ end)
+
+ it("should call default handler", function()
+ stub(registry, "get_installed_package_names")
+ registry.get_installed_package_names.returns { "dummy" }
+ local default_handler = spy.new()
+
+ mason_lspconfig.setup_handlers { default_handler }
+
+ assert.spy(default_handler).was_called(1)
+ assert.spy(default_handler).was_called_with "dummylsp"
+ end)
+
+ it("should call dedicated handler", function()
+ stub(registry, "get_installed_package_names")
+ registry.get_installed_package_names.returns { "dummy" }
+ local dedicated_handler = spy.new()
+ local default_handler = spy.new()
+
+ mason_lspconfig.setup_handlers {
+ default_handler,
+ ["dummylsp"] = dedicated_handler,
+ }
+
+ assert.spy(default_handler).was_called(0)
+ assert.spy(dedicated_handler).was_called(1)
+ assert.spy(dedicated_handler).was_called_with "dummylsp"
+ end)
+
+ it("should print warning if registering handler for non-existent server name", function()
+ spy.on(vim, "notify")
+ mason_lspconfig.setup_handlers {
+ doesnt_exist_server = spy.new(),
+ }
+ assert.spy(vim.notify).was_called(1)
+ assert.spy(vim.notify).was_called_with(
+ "[mason.nvim] mason-lspconfig.setup_handlers: Received handler for unknown lspconfig server name: doesnt_exist_server.",
+ vim.log.levels.WARN
+ )
+ end)
+end)
diff --git a/tests/minimal_init.vim b/tests/minimal_init.vim
new file mode 100644
index 0000000..c3ffb58
--- /dev/null
+++ b/tests/minimal_init.vim
@@ -0,0 +1,39 @@
+" Avoid neovim/neovim#11362
+set display=lastline
+set directory=""
+set noswapfile
+
+let $mason = getcwd()
+let $test_helpers = getcwd() .. "/tests/helpers"
+let $dependencies = getcwd() .. "/dependencies"
+
+set rtp^=$mason,$test_helpers
+set packpath=$dependencies
+
+packloadall
+
+lua require("luassertx")
+lua require("test_helpers")
+
+lua <<EOF
+local index = require "mason-registry.index"
+index["dummy"] = "dummy_package"
+index["dummy2"] = "dummy2_package"
+
+local configs = require 'lspconfig.configs'
+configs.dummylsp = { default_config = {} }
+configs.dummy2lsp = { default_config = {} }
+
+require("mason").setup {
+ install_root_dir = vim.env.INSTALL_ROOT_DIR,
+}
+EOF
+
+function! RunTests() abort
+ lua <<EOF
+ require("plenary.test_harness").test_directory(os.getenv("FILE") or "./tests", {
+ minimal_init = vim.fn.getcwd() .. "/tests/minimal_init.vim",
+ sequential = true,
+ })
+EOF
+endfunction