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
|
local stub = require "luassert.stub"
local match = require "luassert.match"
local std = require "mason-core.managers.std"
local path = require "mason-core.path"
local pip3 = require "mason-core.managers.pip3"
local registry = require "mason-registry"
describe("installer", function()
---@module "mason-core.platform"
local platform
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 handle = InstallHandleGenerator "dummy"
local ctx = InstallContextGenerator(handle)
stub(ctx.fs, "write_file")
stub(std, "chmod")
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",
[[#!/bin/bash
export GREETING="Hello World!"
exec bash -c 'echo $GREETING' "$@"]]
)
end)
it("should write shell exec wrapper on Windows", function()
platform.is_mac = false
platform.is_unix = false
platform.is_linux = false
platform.is_win = true
local handle = InstallHandleGenerator "dummy"
local ctx = InstallContextGenerator(handle)
stub(ctx.fs, "write_file")
stub(std, "chmod")
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 write Node exec wrapper", function()
local js_rel_path = path.concat { "some", "obscure", "path", "server.js" }
local dummy = registry.get_package "dummy"
local handle = InstallHandleGenerator "dummy"
local ctx = InstallContextGenerator(handle)
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 { dummy: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 handle = InstallHandleGenerator "dummy"
local ctx = InstallContextGenerator(handle)
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 dummy = registry.get_package "dummy"
local handle = InstallHandleGenerator "dummy"
local ctx = InstallContextGenerator(handle)
stub(ctx.cwd, "get")
ctx.cwd.get.returns "/tmp/placeholder"
stub(ctx, "write_shell_exec_wrapper")
stub(ctx.spawn, "python")
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 { pip3.venv_path(dummy:get_install_path()), "python" })
)
end)
it("should not write Python exec wrapper if module cannot be found", function()
local handle = InstallHandleGenerator "dummy"
local ctx = InstallContextGenerator(handle)
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 dummy = registry.get_package "dummy"
local exec_rel_path = path.concat { "obscure", "path", "to", "server" }
local handle = InstallHandleGenerator "dummy"
local ctx = InstallContextGenerator(handle)
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 { dummy: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 handle = InstallHandleGenerator "dummy"
local ctx = InstallContextGenerator(handle)
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)
end)
|