aboutsummaryrefslogtreecommitdiffstats
path: root/tests/mason-core/installer/linker_spec.lua
blob: 2177f6a38266746b086cb39d5c28d1de2280580f (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
local a = require "mason-core.async"
local fs = require "mason-core.fs"
local path = require "mason-core.path"
local registry = require "mason-registry"
local stub = require "luassert.stub"
local test_helpers = require "mason-test.helpers"

local WIN_CMD_SCRIPT = [[@ECHO off
GOTO start
:find_dp0
SET dp0=%%~dp0
EXIT /b
:start
SETLOCAL
CALL :find_dp0

endLocal & goto #_undefined_# 2>NUL || title %%COMSPEC%% & "%%dp0%%\%s" %%*]]

describe("linker", function()
    local snapshot

    before_each(function()
        snapshot = assert.snapshot()
    end)

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

    ---@module "mason-core.installer.linker"
    local linker
    ---@module "mason-core.platform"
    local platform

    before_each(function()
        package.loaded["mason-core.platform"] = nil
        package.loaded["mason-core.installer.linker"] = nil
        platform = require "mason-core.platform"
        linker = require "mason-core.installer.linker"
    end)

    it("should symlink executable on Unix", function()
        local dummy = registry.get_package "dummy"
        local ctx = test_helpers.create_context()

        stub(fs.async, "file_exists")
        stub(fs.async, "symlink")
        stub(fs.async, "write_file")

        fs.async.file_exists.on_call_with(ctx.location:bin "my-executable").returns(false)
        fs.async.file_exists.on_call_with(ctx.location:bin "another-executable").returns(false)
        fs.async.file_exists
            .on_call_with(path.concat { ctx:get_install_path(), "nested", "path", "my-executable" })
            .returns(true)
        fs.async.file_exists.on_call_with(path.concat { ctx:get_install_path(), "another-executable" }).returns(true)

        ctx:link_bin("my-executable", path.concat { "nested", "path", "my-executable" })
        ctx:link_bin("another-executable", "another-executable")
        local result = a.run_blocking(linker.link, ctx)
        assert.is_true(result:is_success())

        assert.spy(fs.async.write_file).was_called(0)
        assert.spy(fs.async.symlink).was_called(2)
        assert
            .spy(fs.async.symlink)
            .was_called_with("../packages/dummy/another-executable", ctx.location:bin "another-executable")
        assert
            .spy(fs.async.symlink)
            .was_called_with("../packages/dummy/nested/path/my-executable", ctx.location:bin "my-executable")
    end)

    it("should write executable wrapper on Windows", function()
        local dummy = registry.get_package "dummy"
        local ctx = test_helpers.create_context()

        platform.is.darwin = false
        platform.is.mac = false
        platform.is.linux = false
        platform.is.unix = false
        platform.is.win = true

        stub(fs.async, "file_exists")
        stub(fs.async, "symlink")
        stub(fs.async, "write_file")

        fs.async.file_exists.on_call_with(ctx.location:bin "my-executable").returns(false)
        fs.async.file_exists.on_call_with(ctx.location:bin "another-executable").returns(false)
        fs.async.file_exists
            .on_call_with(path.concat { ctx:get_install_path(), "nested", "path", "my-executable" })
            .returns(true)
        fs.async.file_exists.on_call_with(path.concat { ctx:get_install_path(), "another-executable" }).returns(true)

        ctx:link_bin("my-executable", path.concat { "nested", "path", "my-executable" })
        ctx:link_bin("another-executable", "another-executable")

        local result = a.run_blocking(linker.link, ctx)
        assert.is_true(result:is_success())

        assert.spy(fs.async.symlink).was_called(0)
        assert.spy(fs.async.write_file).was_called(2)
        assert
            .spy(fs.async.write_file)
            .was_called_with(ctx.location:bin "another-executable.cmd", WIN_CMD_SCRIPT:format "..\\packages\\dummy\\another-executable")
        assert
            .spy(fs.async.write_file)
            .was_called_with(
                ctx.location:bin "my-executable.cmd",
                WIN_CMD_SCRIPT:format "..\\packages\\dummy\\nested\\path\\my-executable"
            )
    end)

    it("should symlink share files", function()
        local dummy = registry.get_package "dummy"
        local ctx = test_helpers.create_context()

        stub(fs.async, "mkdirp")
        stub(fs.async, "dir_exists")
        stub(fs.async, "file_exists")
        stub(fs.async, "symlink")
        stub(fs.async, "write_file")

        -- mock non-existent dest files
        fs.async.file_exists.on_call_with(ctx.location:share "share-file").returns(false)
        fs.async.file_exists.on_call_with(ctx.location:share(path.concat { "nested", "share-file" })).returns(false)

        fs.async.dir_exists.on_call_with(ctx.location:share "nested/path").returns(false)

        -- mock existent source files
        fs.async.file_exists.on_call_with(path.concat { ctx:get_install_path(), "share-file" }).returns(true)
        fs.async.file_exists
            .on_call_with(path.concat { ctx:get_install_path(), "nested", "path", "to", "share-file" })
            .returns(true)

        ctx.links.share["nested/path/share-file"] = path.concat { "nested", "path", "to", "share-file" }
        ctx.links.share["share-file"] = "share-file"

        local result = a.run_blocking(linker.link, ctx)

        assert.is_true(result:is_success())

        assert.spy(fs.async.write_file).was_called(0)
        assert.spy(fs.async.symlink).was_called(2)
        assert.spy(fs.async.symlink).was_called_with("../packages/dummy/share-file", ctx.location:share "share-file")
        assert
            .spy(fs.async.symlink)
            .was_called_with("../../../packages/dummy/nested/path/to/share-file", ctx.location:share "nested/path/share-file")

        assert.spy(fs.async.mkdirp).was_called(2)
        assert.spy(fs.async.mkdirp).was_called_with(ctx.location:share "nested/path")
    end)

    it("should copy share files on Windows", function()
        local dummy = registry.get_package "dummy"
        local ctx = test_helpers.create_context()

        platform.is.darwin = false
        platform.is.mac = false
        platform.is.linux = false
        platform.is.unix = false
        platform.is.win = true

        stub(fs.async, "mkdirp")
        stub(fs.async, "dir_exists")
        stub(fs.async, "file_exists")
        stub(fs.async, "copy_file")

        -- mock non-existent dest files
        fs.async.file_exists.on_call_with(ctx.location:share "share-file").returns(false)
        fs.async.file_exists.on_call_with(ctx.location:share(path.concat { "nested", "share-file" })).returns(false)

        fs.async.dir_exists.on_call_with(ctx.location:share "nested/path").returns(false)

        -- mock existent source files
        fs.async.file_exists.on_call_with(path.concat { ctx:get_install_path(), "share-file" }).returns(true)
        fs.async.file_exists
            .on_call_with(path.concat { ctx:get_install_path(), "nested", "path", "to", "share-file" })
            .returns(true)

        ctx.links.share["nested/path/share-file"] = path.concat { "nested", "path", "to", "share-file" }
        ctx.links.share["share-file"] = "share-file"

        local result = linker.link(ctx)

        assert.is_true(result:is_success())

        assert.spy(fs.async.copy_file).was_called(2)
        assert
            .spy(fs.async.copy_file)
            .was_called_with(path.concat { ctx:get_install_path(), "share-file" }, ctx.location:share "share-file", { excl = true })
        assert.spy(fs.async.copy_file).was_called_with(
            path.concat { ctx:get_install_path(), "nested", "path", "to", "share-file" },
            ctx.location:share "nested/path/share-file",
            { excl = true }
        )

        assert.spy(fs.async.mkdirp).was_called(2)
        assert.spy(fs.async.mkdirp).was_called_with(ctx.location:share "nested/path")
    end)
end)