aboutsummaryrefslogtreecommitdiffstats
path: root/tests/mason-core/installer/installer_spec.lua
blob: 3e29130848ca3da7f06b824c1743b337607d67a0 (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
local InstallContext = require "mason-core.installer.context"
local Result = require "mason-core.result"
local a = require "mason-core.async"
local fs = require "mason-core.fs"
local installer = require "mason-core.installer"
local match = require "luassert.match"
local path = require "mason-core.path"
local spy = require "luassert.spy"
local stub = require "luassert.stub"

local function timestamp()
    local seconds, microseconds = vim.loop.gettimeofday()
    return (seconds * 1000) + math.floor(microseconds / 1000)
end

describe("installer", function()
    before_each(function()
        package.loaded["dummy_package"] = nil
    end)

    it(
        "should call installer",
        async_test(function()
            spy.on(fs.async, "mkdirp")
            spy.on(fs.async, "rename")

            local handle = InstallHandleGenerator "dummy"
            spy.on(handle.package.spec.source, "install")
            local result = installer.execute(handle, {})

            assert.is_nil(result:err_or_nil())
            assert.spy(handle.package.spec.source.install).was_called(1)
            assert
                .spy(handle.package.spec.source.install)
                .was_called_with(match.instanceof(InstallContext), match.is_table())
            assert.spy(fs.async.mkdirp).was_called_with(path.package_build_prefix "dummy")
            assert.spy(fs.async.rename).was_called_with(path.package_build_prefix "dummy", path.package_prefix "dummy")
        end)
    )

    it(
        "should return failure if installer errors",
        async_test(function()
            spy.on(fs.async, "rmrf")
            spy.on(fs.async, "rename")
            local installer_fn = spy.new(function()
                error("something went wrong. don't try again.", 0)
            end)
            local handler = InstallHandleGenerator "dummy"
            stub(handler.package.spec.source, "install", installer_fn)
            local result = installer.execute(handler, {})
            assert.spy(installer_fn).was_called(1)
            assert.is_true(result:is_failure())
            assert.equals("something went wrong. don't try again.", result:err_or_nil())
            assert.spy(fs.async.rmrf).was_called_with(path.package_build_prefix "dummy")
            assert.spy(fs.async.rename).was_not_called()
        end)
    )

    it(
        "should write receipt",
        async_test(function()
            spy.on(fs.async, "write_file")
            local handle = InstallHandleGenerator "dummy"
            stub(handle.package.spec.source, "install", function(ctx)
                ctx.fs:write_file("target", "")
                ctx.fs:write_file("file.jar", "")
                ctx.fs:write_file("opt-cmd", "")
            end)
            handle.package.spec.bin = {
                ["executable"] = "target",
            }
            handle.package.spec.share = {
                ["package/file.jar"] = "file.jar",
            }
            handle.package.spec.opt = {
                ["package/bin/opt-cmd"] = "opt-cmd",
            }
            installer.execute(handle, {})
            handle.package.spec.bin = {}
            handle.package.spec.share = {}
            handle.package.spec.opt = {}
            assert.spy(fs.async.write_file).was_called_with(
                ("%s/mason-receipt.json"):format(handle.package:get_install_path()),
                match.capture(function(arg)
                    ---@type InstallReceipt
                    local receipt = vim.json.decode(arg)
                    assert.is_true(match.tbl_containing {
                        name = "dummy",
                        source = match.same {
                            type = handle.package.spec.schema,
                            id = handle.package.spec.source.id,
                        },
                        schema_version = "1.2",
                        metrics = match.is_table(),
                        links = match.same {
                            bin = { executable = "target" },
                            share = { ["package/file.jar"] = "file.jar" },
                            opt = { ["package/bin/opt-cmd"] = "opt-cmd" },
                        },
                    }(receipt))
                end)
            )
        end)
    )

    it(
        "should run async functions concurrently",
        async_test(function()
            spy.on(fs.async, "write_file")
            local capture = spy.new()
            local start = timestamp()
            local handle = InstallHandleGenerator "dummy"
            stub(handle.package.spec.source, "install", function(ctx)
                capture(installer.run_concurrently {
                    function()
                        a.sleep(100)
                        return installer.context()
                    end,
                    function()
                        a.sleep(100)
                        return "two"
                    end,
                    function()
                        a.sleep(100)
                        return "three"
                    end,
                })
            end)
            installer.execute(handle, {})
            local stop = timestamp()
            local grace_ms = 25
            assert.is_true((stop - start) >= (100 - grace_ms))
            assert.spy(capture).was_called_with(match.instanceof(InstallContext), "two", "three")
        end)
    )

    it(
        "should write log files if debug is true",
        async_test(function()
            spy.on(fs.async, "write_file")
            local handle = InstallHandleGenerator "dummy"
            stub(handle.package.spec.source, "install", function(ctx)
                ctx.stdio_sink.stdout "Hello stdout!\n"
                ctx.stdio_sink.stderr "Hello "
                ctx.stdio_sink.stderr "stderr!"
            end)
            installer.execute(handle, { debug = true })
            assert
                .spy(fs.async.write_file)
                .was_called_with(path.package_prefix "dummy/mason-debug.log", "Hello stdout!\nHello stderr!")
        end)
    )

    it(
        "should raise spawn errors in strict mode",
        async_test(function()
            local handle = InstallHandleGenerator "dummy"
            stub(handle.package.spec.source, "install", function(ctx)
                ctx.spawn.bash { "-c", "exit 42" }
            end)
            local result = installer.execute(handle, { debug = true })
            assert.same(
                Result.failure {
                    exit_code = 42,
                    signal = 0,
                },
                result
            )
            assert.equals("spawn: bash failed with exit code 42 and signal 0. ", tostring(result:err_or_nil()))
        end)
    )

    it(
        "should lock package",
        async_test(function()
            local handle = InstallHandleGenerator "dummy"
            local callback = spy.new()
            stub(handle.package.spec.source, "install", function()
                a.sleep(3000)
            end)

            a.run(function()
                return installer.execute(handle, { debug = true })
            end, callback)

            assert.wait_for(function()
                assert.is_true(fs.sync.file_exists(path.package_lock "dummy"))
            end)
            handle:terminate()
            assert.wait_for(function()
                assert.spy(callback).was_called(1)
            end)
            assert.is_false(fs.sync.file_exists(path.package_lock "dummy"))
        end)
    )

    it(
        "should not run installer if package lock exists",
        async_test(function()
            local handle = InstallHandleGenerator "dummy"
            local install = spy.new()
            stub(handle.package.spec.source, "install", install)

            fs.sync.write_file(path.package_lock "dummy", "dummypid")
            local result = installer.execute(handle, { debug = true })
            assert.is_true(fs.sync.file_exists(path.package_lock "dummy"))
            fs.sync.unlink(path.package_lock "dummy")

            assert.spy(install).was_not_called()
            assert.equals(
                "Lockfile exists, installation is already running in another process (pid: dummypid). Run with :MasonInstall --force to bypass.",
                result:err_or_nil()
            )
        end)
    )
end)