aboutsummaryrefslogtreecommitdiffstats
path: root/tests/mason-core/managers/pip3_spec.lua
blob: 1ec2dc284dd4dad028fa3d9939e94a47ce333854 (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
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
local mock = require "luassert.mock"
local spy = require "luassert.spy"
local stub = require "luassert.stub"
local path = require "mason-core.path"

local a = require "mason-core.async"
local pip3 = require "mason-core.managers.pip3"
local installer = require "mason-core.installer"
local Result = require "mason-core.result"
local settings = require "mason.settings"
local spawn = require "mason-core.spawn"
local api = require "mason-registry.api"

describe("pip3 manager", function()
    it("normalizes pip3 packages", function()
        local normalize = pip3.normalize_package
        assert.equals("python-lsp-server", normalize "python-lsp-server[all]")
        assert.equals("python-lsp-server", normalize "python-lsp-server[]")
        assert.equals("python-lsp-server", normalize "python-lsp-server[[]]")
    end)

    it(
        "should create venv and call pip3 install",
        async_test(function()
            local handle = InstallHandleGenerator "dummy"
            local ctx = InstallContextGenerator(handle, { requested_version = "42.13.37" })
            installer.run_installer(ctx, pip3.packages { "main-package", "supporting-package", "supporting-package2" })
            assert.equals(path.package_prefix "dummy", ctx.cwd:get()) -- should've promoted cwd
            assert.spy(ctx.spawn.python3).was_called(1)
            assert.spy(ctx.spawn.python3).was_called_with {
                "-m",
                "venv",
                "venv",
            }
            assert.spy(ctx.spawn.python).was_called(1)
            assert.spy(ctx.spawn.python).was_called_with {
                "-m",
                "pip",
                "--disable-pip-version-check",
                "install",
                "-U",
                {},
                {
                    "main-package==42.13.37",
                    "supporting-package",
                    "supporting-package2",
                },
                with_paths = { path.concat { path.package_prefix "dummy", "venv", "bin" } },
            }
        end)
    )

    it(
        "should exhaust python3 executable candidates if all fail",
        async_test(function()
            vim.g.python3_host_prog = "/my/python3"
            local handle = InstallHandleGenerator "dummy"
            local ctx = InstallContextGenerator(handle)
            ctx.spawn.python3 = spy.new(mockx.throws())
            ctx.spawn.python = spy.new(mockx.throws())
            ctx.spawn[vim.g.python3_host_prog] = spy.new(mockx.throws())
            local err = assert.has_error(function()
                installer.run_installer(ctx, pip3.packages { "package" })
            end)
            vim.g.python3_host_prog = nil

            assert.equals("Unable to create python3 venv environment.", err)
            assert.spy(ctx.spawn["/my/python3"]).was_called(1)
            assert.spy(ctx.spawn.python3).was_called(1)
            assert.spy(ctx.spawn.python).was_called(1)
        end)
    )

    it(
        "should not exhaust python3 executable if one succeeds",
        async_test(function()
            vim.g.python3_host_prog = "/my/python3"
            local handle = InstallHandleGenerator "dummy"
            local ctx = InstallContextGenerator(handle)
            ctx.spawn.python3 = spy.new(mockx.throws())
            ctx.spawn.python = spy.new(mockx.returns {})
            ctx.spawn[vim.g.python3_host_prog] = spy.new(mockx.returns {})

            installer.run_installer(ctx, pip3.packages { "package" })
            vim.g.python3_host_prog = nil
            assert.spy(ctx.spawn.python3).was_called(0)
            assert.spy(ctx.spawn.python).was_called(1)
            assert.spy(ctx.spawn["/my/python3"]).was_called(1)
        end)
    )

    it(
        "should expand python3_host_prog path",
        async_test(function()
            vim.g.python3_host_prog = "~/python3"
            local handle = InstallHandleGenerator "dummy"
            local ctx = InstallContextGenerator(handle)
            ctx.spawn.python = spy.new(mockx.returns {})
            ctx.spawn[vim.env.HOME .. "/python3"] = spy.new(mockx.returns {})

            installer.run_installer(ctx, pip3.packages { "package" })
            a.scheduler()
            vim.g.python3_host_prog = nil
            assert.spy(ctx.spawn[vim.env.HOME .. "/python3"]).was_called(1)
        end)
    )

    it(
        "should use install_args from settings",
        async_test(function()
            settings.set {
                pip = {
                    install_args = { "--proxy", "http://localhost:8080" },
                },
            }
            local handle = InstallHandleGenerator "dummy"
            local ctx = InstallContextGenerator(handle)
            installer.run_installer(ctx, pip3.packages { "package" })
            assert.spy(ctx.spawn.python).was_called_with {
                "-m",
                "pip",
                "--disable-pip-version-check",
                "install",
                "-U",
                { "--proxy", "http://localhost:8080" },
                { "package" },
                with_paths = { path.concat { path.package_prefix "dummy", "venv", "bin" } },
            }
        end)
    )

    it(
        "should provide receipt information",
        async_test(function()
            local handle = InstallHandleGenerator "dummy"
            local ctx = InstallContextGenerator(handle, { requested_version = "42.13.37" })
            installer.run_installer(ctx, pip3.packages { "main-package", "supporting-package", "supporting-package2" })
            assert.same({
                type = "pip3",
                package = "main-package",
            }, ctx.receipt.primary_source)
            assert.same({
                {
                    type = "pip3",
                    package = "supporting-package",
                },
                {
                    type = "pip3",
                    package = "supporting-package2",
                },
            }, ctx.receipt.secondary_sources)
        end)
    )
end)

describe("pip3 version check", function()
    it(
        "should return current version",
        async_test(function()
            spawn.python = spy.new(function()
                return Result.success {
                    stdout = [[
    [{"name": "astroid", "version": "2.9.3"}, {"name": "mccabe", "version": "0.6.1"}, {"name": "python-lsp-server", "version": "1.3.0", "latest_version": "1.4.0", "latest_filetype": "wheel"}, {"name": "wrapt", "version": "1.13.3", "latest_version": "1.14.0", "latest_filetype": "wheel"}]
                    ]],
                }
            end)

            local result = pip3.get_installed_primary_package_version(
                mock.new {
                    primary_source = mock.new {
                        type = "pip3",
                        package = "python-lsp-server",
                    },
                },
                path.package_prefix "dummy"
            )

            assert.spy(spawn.python).was_called(1)
            assert.spy(spawn.python).was_called_with {
                "-m",
                "pip",
                "list",
                "--format=json",
                cwd = path.package_prefix "dummy",
                with_paths = { path.concat { path.package_prefix "dummy", "venv", "bin" } },
            }
            assert.is_true(result:is_success())
            assert.equals("1.3.0", result:get_or_nil())

            spawn.python = nil
        end)
    )

    it(
        "should return outdated primary package",
        async_test(function()
            stub(api, "get")
            api.get.on_call_with("/api/pypi/python-lsp-server/versions/latest").returns(Result.success {
                name = "python-lsp-server",
                version = "1.4.0",
            })
            spawn.python = spy.new(function()
                return Result.success {
                    stdout = [[
    [{"name": "astroid", "version": "2.9.3"}, {"name": "mccabe", "version": "0.6.1"}, {"name": "python-lsp-server", "version": "1.3.0", "latest_version": "1.4.0", "latest_filetype": "wheel"}, {"name": "wrapt", "version": "1.13.3", "latest_version": "1.14.0", "latest_filetype": "wheel"}]
                    ]],
                }
            end)

            local result = pip3.check_outdated_primary_package(
                mock.new {
                    primary_source = mock.new {
                        type = "pip3",
                        package = "python-lsp-server",
                    },
                },
                path.package_prefix "dummy"
            )

            assert.is_true(result:is_success())
            assert.same({
                name = "python-lsp-server",
                current_version = "1.3.0",
                latest_version = "1.4.0",
            }, result:get_or_nil())

            spawn.python = nil
        end)
    )

    it(
        "should return failure if primary package is not outdated",
        async_test(function()
            spawn.python = spy.new(function()
                return Result.success {
                    stdout = [[
    [{"name": "astroid", "version": "2.9.3"}, {"name": "mccabe", "version": "0.6.1"}, {"name": "python-lsp-server", "version": "1.3.0", "latest_version": "1.4.0", "latest_filetype": "wheel"}, {"name": "wrapt", "version": "1.13.3", "latest_version": "1.14.0", "latest_filetype": "wheel"}]
                    ]],
                }
            end)
            stub(api, "get")
            api.get.on_call_with("/api/pypi/python-lsp-server/versions/latest").returns(Result.success {
                name = "python-lsp-server",
                version = "1.3.0",
            })

            local result = pip3.check_outdated_primary_package(
                mock.new {
                    primary_source = mock.new {
                        type = "pip3",
                        package = "python-lsp-server",
                    },
                },
                "/tmp/install/dir"
            )

            assert.is_true(result:is_failure())
            assert.equals("Primary package is not outdated.", result:err_or_nil())
            spawn.python = nil
        end)
    )
end)