aboutsummaryrefslogtreecommitdiffstats
path: root/tests/mason-core/package/package_spec.lua
blob: 8d1929d8281440c7163d8585392da763b7dbb4bf (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
local Pkg = require "mason-core.package"
local a = require "mason-core.async"
local match = require "luassert.match"
local mock = require "luassert.mock"
local receipt = require "mason-core.receipt"
local registry = require "mason-registry"
local spy = require "luassert.spy"
local stub = require "luassert.stub"
local test_helpers = require "mason-test.helpers"

describe("Package ::", function()
    local snapshot

    before_each(function()
        snapshot = assert.snapshot()
        local dummy = registry.get_package "dummy"
        if dummy:is_installed() then
            test_helpers.sync_uninstall(dummy)
        end
    end)

    after_each(function()
        snapshot:revert()
    end)

    it("should parse package specifiers", function()
        local function parse(str)
            local name, version = Pkg.Parse(str)
            return { name, version }
        end

        assert.same({ "rust-analyzer", nil }, parse "rust-analyzer")
        assert.same({ "rust-analyzer", "" }, parse "rust-analyzer@")
        assert.same({ "rust-analyzer", "nightly" }, parse "rust-analyzer@nightly")
    end)

    if vim.fn.has "nvim-0.11" == 1 then
        it("should validate spec", function()
            ---@type RegistryPackageSpec
            local valid_spec = {
                schema = "registry+v1",
                name = "Package name",
                description = "Package description",
                homepage = "https://example.com",
                categories = { "LSP" },
                languages = { "Rust" },
                licenses = {},
                source = {
                    id = "pkg:mason/package@1",
                    install = function() end,
                },
            }
            local function spec(fields)
                return setmetatable(fields, { __index = valid_spec })
            end
            assert.equals(
                "name: expected string, got number",
                assert.has_error(function()
                    Pkg:new(spec { name = 23 })
                end)
            )

            assert.equals(
                "description: expected string, got number",
                assert.has_error(function()
                    Pkg:new(spec { description = 23 })
                end)
            )

            assert.equals(
                "homepage: expected string, got number",
                assert.has_error(function()
                    Pkg:new(spec { homepage = 23 })
                end)
            )

            assert.equals(
                "categories: expected table, got number",
                assert.has_error(function()
                    Pkg:new(spec { categories = 23 })
                end)
            )

            assert.equals(
                "languages: expected table, got number",
                assert.has_error(function()
                    Pkg:new(spec { languages = 23 })
                end)
            )
        end)
    end

    it("should create new handle", function()
        local dummy = registry.get_package "dummy"
        local callback = spy.new()
        dummy:once("install:handle", callback)
        local handle = dummy:new_install_handle()
        assert.spy(callback).was_called(1)
        assert.spy(callback).was_called_with(match.ref(handle))
        handle:close()
    end)

    it("should not create new handle if one already exists", function()
        local dummy = registry.get_package "dummy"
        dummy.install_handle = mock.new {
            is_closed = mockx.returns(false),
        }
        local handle_handler = spy.new()
        dummy:once("install:handle", handle_handler)
        local err = assert.has_error(function()
            dummy:new_install_handle()
        end)
        assert.equals("Cannot create new install handle because existing handle is not closed.", err)
        assert.spy(handle_handler).was_called(0)
        dummy.install_handle = nil
    end)

    it("should successfully install package", function()
        local dummy = registry.get_package "dummy"
        local package_install_success_handler = spy.new()
        local package_install_failed_handler = spy.new()
        local install_success_handler = spy.new()
        local install_failed_handler = spy.new()
        registry:once("package:install:success", package_install_success_handler)
        registry:once("package:install:failed", package_install_failed_handler)
        dummy:once("install:success", install_success_handler)
        dummy:once("install:failed", install_failed_handler)

        local handle = dummy:install { version = "1337" }

        assert.wait(function()
            assert.is_true(handle:is_closed())
            assert.is_true(dummy:is_installed())
        end)

        assert.wait(function()
            assert.spy(install_success_handler).was_called(1)
            assert.spy(install_success_handler).was_called_with(match.instanceof(receipt.InstallReceipt))
            assert.spy(package_install_success_handler).was_called(1)
            assert
                .spy(package_install_success_handler)
                .was_called_with(match.is_ref(dummy), match.instanceof(receipt.InstallReceipt))
            assert.spy(package_install_failed_handler).was_called(0)
            assert.spy(install_failed_handler).was_called(0)
        end)
    end)

    it("should fail to install package", function()
        local dummy = registry.get_package "dummy"
        stub(dummy.spec.source, "install", function()
            error("I simply refuse to be installed.", 0)
        end)
        local package_install_success_handler = spy.new()
        local package_install_failed_handler = spy.new()
        local install_success_handler = spy.new()
        local install_failed_handler = spy.new()
        registry:once("package:install:success", package_install_success_handler)
        registry:once("package:install:failed", package_install_failed_handler)
        dummy:once("install:success", install_success_handler)
        dummy:once("install:failed", install_failed_handler)

        local handle = dummy:install { version = "1337" }

        assert.wait(function()
            assert.is_true(handle:is_closed())
            assert.is_false(dummy:is_installed())
        end)

        assert.wait(function()
            assert.spy(install_failed_handler).was_called(1)
            assert.spy(install_failed_handler).was_called_with "I simply refuse to be installed."
            assert.spy(package_install_failed_handler).was_called(1)
            assert
                .spy(package_install_failed_handler)
                .was_called_with(match.is_ref(dummy), "I simply refuse to be installed.")
            assert.spy(package_install_success_handler).was_called(0)
            assert.spy(install_success_handler).was_called(0)
        end)
    end)

    it("should be able to start package installation outside of main loop", function()
        local dummy = registry.get_package "dummy"

        local handle = a.run_blocking(function()
            -- Move outside the main loop
            a.wait(function(resolve)
                local timer = vim.loop.new_timer()
                timer:start(0, 0, function()
                    timer:close()
                    resolve()
                end)
            end)
            assert.is_true(vim.in_fast_event())

            return assert.is_not.has_error(function()
                return dummy:install()
            end)
        end)
    end)

    it("should be able to instantiate package outside of main loop", function()
        local dummy = registry.get_package "registry"

        -- Move outside the main loop
        a.run_blocking(function()
            a.wait(function(resolve)
                local timer = vim.loop.new_timer()
                timer:start(0, 0, function()
                    timer:close()
                    resolve()
                end)
            end)

            assert.is_true(vim.in_fast_event())
            local pkg = assert.is_not.has_error(function()
                return Pkg:new(dummy.spec)
            end)
            assert.same(dummy.spec, pkg.spec)
        end)
    end)
end)