aboutsummaryrefslogtreecommitdiffstats
path: root/tests/mason-core/managers/cargo_spec.lua
blob: a938452b1ad17da772768066fb404c332298c873 (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
local spy = require "luassert.spy"
local match = require "luassert.match"
local mock = require "luassert.mock"
local Optional = require "mason-core.optional"
local installer = require "mason-core.installer"
local cargo = require "mason-core.managers.cargo"
local Result = require "mason-core.result"
local spawn = require "mason-core.spawn"
local path = require "mason-core.path"

describe("cargo manager", function()
    it(
        "should call cargo install",
        async_test(function()
            local handle = InstallHandleGenerator "dummy"
            local ctx = InstallContextGenerator(handle, { requested_version = "42.13.37" })
            installer.run_installer(ctx, cargo.crate "my-crate")
            assert.spy(ctx.spawn.cargo).was_called(1)
            assert.spy(ctx.spawn.cargo).was_called_with {
                "install",
                "--root",
                ".",
                "--locked",
                { "--version", "42.13.37" },
                vim.NIL, -- --features
                "my-crate",
            }
        end)
    )

    it(
        "should call cargo install with git source",
        async_test(function()
            local handle = InstallHandleGenerator "dummy"
            local ctx = InstallContextGenerator(handle)
            installer.run_installer(ctx, cargo.crate("https://my-crate.git", { git = true }))
            assert.spy(ctx.spawn.cargo).was_called(1)
            assert.spy(ctx.spawn.cargo).was_called_with {
                "install",
                "--root",
                ".",
                "--locked",
                vim.NIL,
                vim.NIL, -- --features
                { "--git", "https://my-crate.git" },
            }
        end)
    )

    it(
        "should call cargo install with git source and a specific crate",
        async_test(function()
            local handle = InstallHandleGenerator "dummy"
            local ctx = InstallContextGenerator(handle)
            installer.run_installer(ctx, cargo.crate("crate-name", { git = "https://my-crate.git" }))
            assert.spy(ctx.spawn.cargo).was_called(1)
            assert.spy(ctx.spawn.cargo).was_called_with {
                "install",
                "--root",
                ".",
                "--locked",
                vim.NIL,
                vim.NIL, -- --features
                { "--git", "https://my-crate.git", "crate-name" },
            }
        end)
    )

    it(
        "should respect options",
        async_test(function()
            local handle = InstallHandleGenerator "dummy"
            local ctx = InstallContextGenerator(handle, { requested_version = "42.13.37" })
            installer.run_installer(ctx, cargo.crate("my-crate", { features = "lsp" }))
            assert.spy(ctx.spawn.cargo).was_called(1)
            assert.spy(ctx.spawn.cargo).was_called_with {
                "install",
                "--root",
                ".",
                "--locked",
                { "--version", "42.13.37" },
                { "--features", "lsp" },
                "my-crate",
            }
        end)
    )

    it(
        "should not allow combining version with git crate",
        async_test(function()
            local handle = InstallHandleGenerator "dummy"
            local ctx = InstallContextGenerator(handle, { requested_version = "42.13.37" })
            local err = assert.has_error(function()
                installer.run_installer(
                    ctx,
                    cargo.crate("my-crate", {
                        git = true,
                    })
                )
            end)
            assert.equals("Providing a version when installing a git crate is not allowed.", err)
            assert.spy(ctx.spawn.cargo).was_called(0)
        end)
    )

    it(
        "should provide receipt information",
        async_test(function()
            local handle = InstallHandleGenerator "dummy"
            local ctx = InstallContextGenerator(handle)
            installer.run_installer(ctx, cargo.crate "main-package")
            assert.same({
                type = "cargo",
                package = "main-package",
            }, ctx.receipt.primary_source)
        end)
    )
end)

describe("cargo version check", function()
    it("parses cargo installed packages output", function()
        assert.same(
            {
                ["bat"] = "0.18.3",
                ["exa"] = "0.10.1",
                ["git-select-branch"] = "0.1.1",
                ["hello_world"] = "0.0.1",
                ["rust-analyzer"] = "0.0.0",
                ["stylua"] = "0.11.2",
                ["zoxide"] = "0.5.0",
            },
            cargo.parse_installed_crates [[bat v0.18.3:
    bat
exa v0.10.1:
    exa
git-select-branch v0.1.1:
    git-select-branch
hello_world v0.0.1 (/private/var/folders/ky/s6yyhm_d24d0jsrql4t8k4p40000gn/T/tmp.LGbguATJHj):
    hello_world
rust-analyzer v0.0.0 (/private/var/folders/ky/s6yyhm_d24d0jsrql4t8k4p40000gn/T/tmp.YlsHeA9JVL/crates/rust-analyzer):
    rust-analyzer
stylua v0.11.2:
    stylua
zoxide v0.5.0:
    zoxide
]]
        )
    end)

    it(
        "should return current version",
        async_test(function()
            spawn.cargo = spy.new(function()
                return Result.success {
                    stdout = [[flux-lsp v0.8.8 (https://github.com/influxdata/flux-lsp#4e452f07):
    flux-lsp
]],
                }
            end)

            local result = cargo.get_installed_primary_package_version(
                mock.new {
                    primary_source = mock.new {
                        type = "cargo",
                        package = "https://github.com/influxdata/flux-lsp",
                    },
                },
                path.package_prefix "dummy"
            )

            assert.spy(spawn.cargo).was_called(1)
            assert.spy(spawn.cargo).was_called_with(match.tbl_containing {
                "install",
                "--list",
                "--root",
                ".",
                cwd = path.package_prefix "dummy",
            })
            assert.is_true(result:is_success())
            assert.equals("0.8.8", result:get_or_nil())

            spawn.cargo = nil
        end)
    )

    -- XXX: This test will actually send http request to crates.io's API. It's not mocked.
    it(
        "should return outdated primary package",
        async_test(function()
            spawn.cargo = spy.new(function()
                return Result.success {
                    stdout = [[lelwel v0.4.0:
    lelwel-ls
]],
                }
            end)

            local result = cargo.check_outdated_primary_package(
                mock.new {
                    primary_source = mock.new {
                        type = "cargo",
                        package = "lelwel",
                    },
                },
                path.package_prefix "dummy"
            )

            assert.spy(spawn.cargo).was_called(1)
            assert.spy(spawn.cargo).was_called_with(match.tbl_containing {
                "install",
                "--list",
                "--root",
                ".",
                cwd = path.package_prefix "dummy",
            })
            assert.is_true(result:is_success())
            assert.is_true(match.tbl_containing {
                current_version = "0.4.0",
                latest_version = match.matches "%d.%d.%d",
                name = "lelwel",
            }(result:get_or_nil()))

            spawn.cargo = nil
        end)
    )
end)