aboutsummaryrefslogtreecommitdiffstats
path: root/tests/mason-core/installer_spec.lua
blob: 8dc9b516abdc3852debec91860e2a68145a89479 (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
local spy = require "luassert.spy"
local match = require "luassert.match"
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"
            handler.package.spec.install = installer_fn
            local result = installer.execute(handler, {})
            assert.spy(installer_fn).was_called(1)
            assert.is_true(result:is_failure())
            assert.is_true(match.has_match "^.*: 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"
            installer.execute(handle, {})
            assert.spy(fs.async.write_file).was_called(1)
            assert
                .spy(fs.async.write_file)
                .was_called_with(("%s/mason-receipt.json"):format(handle.package:get_install_path()), match.is_string())
        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)
    )
end)