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

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, "install")
            local result = installer.execute(handle, {})

            assert.is_nil(result:err_or_nil())
            assert.spy(handle.package.spec.install).was_called(1)
            assert.spy(handle.package.spec.install).was_called_with(match.instanceof(InstallContext))
            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, "install")
            handler.package.spec.install.invokes(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 handler = InstallHandleGenerator "dummy"
            ---@param ctx InstallContext
            handler.package.spec.install = function(ctx)
                ctx.receipt:with_primary_source { type = "source", metadata = {} }
                ctx.fs:write_file("target", "script-contents")
                ctx:link_bin("executable", "target")
            end
            installer.execute(handler, {})
            assert.spy(fs.async.write_file).was_called_with(
                ("%s/mason-receipt.json"):format(handler.package:get_install_path()),
                match.capture(function(arg)
                    ---@type InstallReceipt
                    local receipt = vim.json.decode(arg)
                    assert.equals("dummy", receipt.name)
                    assert.same({ type = "source", metadata = {} }, receipt.primary_source)
                    assert.same({}, receipt.secondary_sources)
                    assert.same("1.1", receipt.schema_version)
                    assert.same({ bin = { executable = "target" }, share = {} }, receipt.links)
                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"
            handle.package.spec.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,
                })
                ctx.receipt:with_primary_source { type = "dummy" }
            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, "install", function(ctx)
                ctx.stdio_sink.stdout "Hello stdout!\n"
                ctx.stdio_sink.stderr "Hello "
                ctx.stdio_sink.stderr "stderr!"
                ctx.receipt:with_primary_source { type = "unmanaged" }
            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)
    )
end)