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
|
local stub = require "luassert.stub"
local fs = require "mason-core.fs"
local path = require "mason-core.path"
local registry = require "mason-registry"
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%% & "%s" %%*]]
describe("linker", function()
---@module "mason-core.installer.linker"
local linker
---@module "mason-core.platform"
local platform
before_each(function()
package.loaded["mason-core.installer.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",
async_test(function()
local dummy = registry.get_package "dummy"
stub(fs.async, "file_exists")
stub(fs.async, "symlink")
stub(fs.async, "write_file")
fs.async.file_exists.on_call_with(path.bin_prefix "my-executable").returns(false)
fs.async.file_exists.on_call_with(path.bin_prefix "another-executable").returns(false)
fs.async.file_exists
.on_call_with(path.concat { dummy:get_install_path(), "nested", "path", "my-executable" })
.returns(true)
fs.async.file_exists
.on_call_with(path.concat { dummy:get_install_path(), "another-executable" })
.returns(true)
local handle = InstallHandleGenerator "dummy"
local ctx = InstallContextGenerator(handle)
ctx:link_bin("my-executable", path.concat { "nested", "path", "my-executable" })
ctx:link_bin("another-executable", "another-executable")
assert.is_true(linker.link(ctx):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(path.concat { dummy:get_install_path(), "another-executable" }, path.bin_prefix "another-executable")
assert.spy(fs.async.symlink).was_called_with(
path.concat { dummy:get_install_path(), "nested", "path", "my-executable" },
path.bin_prefix "my-executable"
)
end)
)
it(
"should write executable wrapper on Windows",
async_test(function()
platform.is.darwin = false
platform.is.mac = false
platform.is.linux = false
platform.is.unix = false
platform.is.win = true
local dummy = registry.get_package "dummy"
stub(fs.async, "file_exists")
stub(fs.async, "symlink")
stub(fs.async, "write_file")
fs.async.file_exists.on_call_with(path.bin_prefix "my-executable").returns(false)
fs.async.file_exists.on_call_with(path.bin_prefix "another-executable").returns(false)
fs.async.file_exists
.on_call_with(path.concat { dummy:get_install_path(), "nested", "path", "my-executable" })
.returns(true)
fs.async.file_exists
.on_call_with(path.concat { dummy:get_install_path(), "another-executable" })
.returns(true)
local handle = InstallHandleGenerator "dummy"
local ctx = InstallContextGenerator(handle)
ctx:link_bin("my-executable", path.concat { "nested", "path", "my-executable" })
ctx:link_bin("another-executable", "another-executable")
assert.is_true(linker.link(ctx):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(
path.bin_prefix "another-executable.cmd",
WIN_CMD_SCRIPT:format(path.concat { dummy:get_install_path(), "another-executable" })
)
assert.spy(fs.async.write_file).was_called_with(
path.bin_prefix "my-executable.cmd",
WIN_CMD_SCRIPT:format(path.concat { dummy:get_install_path(), "nested", "path", "my-executable" })
)
end)
)
it(
"should symlink share files",
async_test(function()
local dummy = registry.get_package "dummy"
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(path.share_prefix "share-file").returns(false)
fs.async.file_exists.on_call_with(path.share_prefix(path.concat { "nested", "share-file" })).returns(false)
fs.async.dir_exists.on_call_with(path.share_prefix()).returns(false)
fs.async.dir_exists.on_call_with(path.share_prefix "nested/path").returns(false)
-- mock existent source files
fs.async.file_exists.on_call_with(path.concat { dummy:get_install_path(), "share-file" }).returns(true)
fs.async.file_exists
.on_call_with(path.concat { dummy:get_install_path(), "nested", "path", "to", "share-file" })
.returns(true)
local handle = InstallHandleGenerator "dummy"
local ctx = InstallContextGenerator(handle)
ctx:link_share("nested/path/share-file", path.concat { "nested", "path", "to", "share-file" })
ctx:link_share("share-file", "share-file")
assert.is_true(linker.link(ctx):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(path.concat { dummy:get_install_path(), "share-file" }, path.share_prefix "share-file")
assert.spy(fs.async.symlink).was_called_with(
path.concat { dummy:get_install_path(), "nested", "path", "to", "share-file" },
path.share_prefix "nested/path/share-file"
)
assert.spy(fs.async.mkdirp).was_called(2)
assert.spy(fs.async.mkdirp).was_called_with(path.share_prefix())
assert.spy(fs.async.mkdirp).was_called_with(path.share_prefix "nested/path")
end)
)
end)
|