aboutsummaryrefslogtreecommitdiffstats
path: root/tests/mason-lspconfig/api/command_spec.lua
blob: 0551c77782c12f31c223003546d01320dcb17e39 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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)