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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
local a = require "mason-core.async"
local match = require "luassert.match"
local path = require "mason-core.path"
local pypi = require "mason-core.installer.managers.pypi"
local registry = require "mason-registry"
local spy = require "luassert.spy"
local stub = require "luassert.stub"
local test_helpers = require "mason-test.helpers"
describe("installer", function()
---@module "mason-core.platform"
local platform
local snapshot
before_each(function()
snapshot = assert.snapshot()
end)
after_each(function()
snapshot:revert()
end)
before_each(function()
package.loaded["mason-core.installer.platform"] = nil
package.loaded["mason-core.installer.context"] = nil
platform = require "mason-core.platform"
end)
it("should write shell exec wrapper on Unix", function()
local ctx = test_helpers.create_context()
stub(ctx.fs, "write_file")
stub(ctx.fs, "file_exists")
stub(ctx.fs, "dir_exists")
stub(ctx.fs, "chmod_exec")
ctx.fs.file_exists.on_call_with(match.is_ref(ctx.fs), "my-executable").returns(false)
ctx.fs.dir_exists.on_call_with(match.is_ref(ctx.fs), "my-executable").returns(false)
ctx:write_shell_exec_wrapper("my-executable", "bash -c 'echo $GREETING'", {
GREETING = "Hello World!",
})
assert.spy(ctx.fs.write_file).was_called(1)
assert.spy(ctx.fs.write_file).was_called_with(
match.is_ref(ctx.fs),
"my-executable",
[[#!/usr/bin/env bash
export GREETING="Hello World!"
exec bash -c 'echo $GREETING' "$@"]]
)
end)
it("should write shell exec wrapper on Windows", function()
platform.is.darwin = false
platform.is.mac = false
platform.is.unix = false
platform.is.linux = false
platform.is.win = true
local ctx = test_helpers.create_context()
stub(ctx.fs, "write_file")
stub(ctx.fs, "file_exists")
stub(ctx.fs, "dir_exists")
ctx.fs.file_exists.on_call_with(match.is_ref(ctx.fs), "my-executable").returns(false)
ctx.fs.dir_exists.on_call_with(match.is_ref(ctx.fs), "my-executable").returns(false)
ctx:write_shell_exec_wrapper("my-executable", "cmd.exe /C echo %GREETING%", {
GREETING = "Hello World!",
})
assert.spy(ctx.fs.write_file).was_called(1)
assert.spy(ctx.fs.write_file).was_called_with(
match.is_ref(ctx.fs),
"my-executable.cmd",
[[@ECHO off
SET GREETING=Hello World!
cmd.exe /C echo %GREETING% %*]]
)
end)
it("should not write shell exec wrapper if new executable path already exists", function()
local exec_rel_path = path.concat { "obscure", "path", "to", "server" }
local ctx = test_helpers.create_context()
stub(ctx.fs, "file_exists")
stub(ctx.fs, "dir_exists")
ctx.fs.file_exists.on_call_with(match.is_ref(ctx.fs), exec_rel_path).returns(true)
ctx.fs.file_exists.on_call_with(match.is_ref(ctx.fs), "my-wrapper-script").returns(true)
ctx.fs.dir_exists.on_call_with(match.is_ref(ctx.fs), "my-wrapper-script").returns(true)
local err = assert.has_error(function()
ctx:write_shell_exec_wrapper("my-wrapper-script", "contents")
end)
assert.equals([[Cannot write exec wrapper to "my-wrapper-script" because the file already exists.]], err)
end)
it("should write Node exec wrapper", function()
local js_rel_path = path.concat { "some", "obscure", "path", "server.js" }
local ctx = test_helpers.create_context()
stub(ctx, "write_shell_exec_wrapper")
stub(ctx.fs, "file_exists")
ctx.fs.file_exists.on_call_with(match.is_ref(ctx.fs), js_rel_path).returns(true)
ctx:write_node_exec_wrapper("my-wrapper-script", js_rel_path)
assert.spy(ctx.write_shell_exec_wrapper).was_called(1)
assert.spy(ctx.write_shell_exec_wrapper).was_called_with(
match.is_ref(ctx),
"my-wrapper-script",
("node %q"):format(path.concat { ctx:get_install_path(), js_rel_path })
)
end)
it("should write Ruby exec wrapper", function()
local js_rel_path = path.concat { "some", "obscure", "path", "server.js" }
local ctx = test_helpers.create_context()
stub(ctx, "write_shell_exec_wrapper")
stub(ctx.fs, "file_exists")
ctx.fs.file_exists.on_call_with(match.is_ref(ctx.fs), js_rel_path).returns(true)
ctx:write_ruby_exec_wrapper("my-wrapper-script", js_rel_path)
assert.spy(ctx.write_shell_exec_wrapper).was_called(1)
assert.spy(ctx.write_shell_exec_wrapper).was_called_with(
match.is_ref(ctx),
"my-wrapper-script",
("ruby %q"):format(path.concat { ctx:get_install_path(), js_rel_path })
)
end)
it("should not write Node exec wrapper if the target script doesn't exist", function()
local js_rel_path = path.concat { "some", "obscure", "path", "server.js" }
local ctx = test_helpers.create_context()
stub(ctx, "write_shell_exec_wrapper")
stub(ctx.fs, "file_exists")
ctx.fs.file_exists.on_call_with(match.is_ref(ctx.fs), js_rel_path).returns(false)
local err = assert.has_error(function()
ctx:write_node_exec_wrapper("my-wrapper-script", js_rel_path)
end)
assert.equals(
[[Cannot write Node exec wrapper for path "some/obscure/path/server.js" as it doesn't exist.]],
err
)
assert.spy(ctx.write_shell_exec_wrapper).was_called(0)
end)
it("should write Python exec wrapper", function()
local ctx = test_helpers.create_context()
stub(ctx.cwd, "get")
ctx.cwd.get.returns "/tmp/placeholder"
stub(ctx, "write_shell_exec_wrapper")
ctx:write_pyvenv_exec_wrapper("my-wrapper-script", "my-module")
assert.spy(ctx.write_shell_exec_wrapper).was_called(1)
assert.spy(ctx.write_shell_exec_wrapper).was_called_with(
match.is_ref(ctx),
"my-wrapper-script",
("%q -m my-module"):format(path.concat { pypi.venv_path(ctx:get_install_path()), "python" })
)
end)
it("should not write Python exec wrapper if module cannot be found", function()
local ctx = test_helpers.create_context()
stub(ctx.cwd, "get")
ctx.cwd.get.returns "/tmp/placeholder"
stub(ctx, "write_shell_exec_wrapper")
stub(ctx.spawn, "python")
ctx.spawn.python.invokes(function()
error ""
end)
local err = assert.has_error(function()
ctx:write_pyvenv_exec_wrapper("my-wrapper-script", "my-module")
end)
assert.equals([[Cannot write Python exec wrapper for module "my-module" as it doesn't exist.]], err)
assert.spy(ctx.write_shell_exec_wrapper).was_called(0)
end)
it("should write exec wrapper", function()
local exec_rel_path = path.concat { "obscure", "path", "to", "server" }
local ctx = test_helpers.create_context()
stub(ctx, "write_shell_exec_wrapper")
stub(ctx.fs, "file_exists")
ctx.fs.file_exists.on_call_with(match.is_ref(ctx.fs), exec_rel_path).returns(true)
ctx:write_exec_wrapper("my-wrapper-script", exec_rel_path)
assert.spy(ctx.write_shell_exec_wrapper).was_called(1)
assert
.spy(ctx.write_shell_exec_wrapper)
.was_called_with(
match.is_ref(ctx),
"my-wrapper-script",
("%q"):format(path.concat { ctx:get_install_path(), exec_rel_path })
)
end)
it("should not write exec wrapper if target executable doesn't exist", function()
local exec_rel_path = path.concat { "obscure", "path", "to", "server" }
local ctx = test_helpers.create_context()
stub(ctx, "write_shell_exec_wrapper")
stub(ctx.fs, "file_exists")
ctx.fs.file_exists.on_call_with(match.is_ref(ctx.fs), exec_rel_path).returns(false)
local err = assert.has_error(function()
ctx:write_exec_wrapper("my-wrapper-script", exec_rel_path)
end)
assert.equals([[Cannot write exec wrapper for path "obscure/path/to/server" as it doesn't exist.]], err)
assert.spy(ctx.write_shell_exec_wrapper).was_called(0)
end)
it("should write PHP exec wrapper", function()
local php_rel_path = path.concat { "some", "obscure", "path", "cli.php" }
local ctx = test_helpers.create_context()
stub(ctx, "write_shell_exec_wrapper")
stub(ctx.fs, "file_exists")
ctx.fs.file_exists.on_call_with(match.is_ref(ctx.fs), php_rel_path).returns(true)
ctx:write_php_exec_wrapper("my-wrapper-script", php_rel_path)
assert.spy(ctx.write_shell_exec_wrapper).was_called(1)
assert.spy(ctx.write_shell_exec_wrapper).was_called_with(
match.is_ref(ctx),
"my-wrapper-script",
("php %q"):format(path.concat { ctx:get_install_path(), php_rel_path })
)
end)
it("should not write PHP exec wrapper if the target script doesn't exist", function()
local php_rel_path = path.concat { "some", "obscure", "path", "cli.php" }
local ctx = test_helpers.create_context()
stub(ctx, "write_shell_exec_wrapper")
stub(ctx.fs, "file_exists")
ctx.fs.file_exists.on_call_with(match.is_ref(ctx.fs), php_rel_path).returns(false)
local err = assert.has_error(function()
ctx:write_php_exec_wrapper("my-wrapper-script", php_rel_path)
end)
assert.equals([[Cannot write PHP exec wrapper for path "some/obscure/path/cli.php" as it doesn't exist.]], err)
assert.spy(ctx.write_shell_exec_wrapper).was_called(0)
end)
it("should await callback-style async function", function()
local value = a.run_blocking(function()
local ctx = test_helpers.create_context()
return ctx:execute(function()
return ctx:await(function(resolve, reject)
vim.defer_fn(function()
resolve "Value!"
end, 500)
end)
end)
end)
assert.equals("Value!", value)
end)
it("should propagate errors in callback-style async function", function()
local guard = spy.new()
local error = assert.has_error(function()
a.run_blocking(function()
local ctx = test_helpers.create_context()
return ctx:execute(function()
ctx:await(function(resolve, reject)
vim.defer_fn(function()
reject "Error!"
end, 500)
end)
guard()
end)
end)
end)
assert.equals("Error!", error)
assert.spy(guard).was_called(0)
end)
end)
|