Repository navigation
Expand file tree
/
Copy pathcommands_spec.lua
More file actions
360 lines (303 loc) · 10.8 KB
/
Copy pathcommands_spec.lua
File metadata and controls
360 lines (303 loc) · 10.8 KB
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
local pack = require("pack")
local state = require("pack.state")
local ui = require("pack.ui")
local helpers = require("tests.helpers")
describe("Pack user commands and subcommands", function()
before_each(function()
helpers.reset()
pack.setup({
plugins = {
{ "user/foo.nvim" },
{ "user/bar.nvim" },
},
ui = { auto_open = false },
})
end)
after_each(function()
pcall(ui.close)
local wins = vim.api.nvim_list_wins()
for i = 2, #wins do
pcall(vim.api.nvim_win_close, wins[i], true)
end
end)
it("runs :Pack to open dashboard float", function()
vim.cmd("Pack")
local buf = vim.api.nvim_get_current_buf()
local ft = vim.bo[buf].filetype
assert.equals("pack", ft)
end)
it("runs :Pack sync without throwing errors", function()
local orig_update = require("pack.async").update_plugins
local sync_called = false
require("pack.async").update_plugins = function()
sync_called = true
end
vim.cmd("Pack sync")
require("pack.async").update_plugins = orig_update
end)
it(":Pack sync includes never-checked (behind == nil) installed plugins, not just confirmed-outdated ones", function()
-- Nobody has run an outdated check yet: behind is nil, not 0. Sync must
-- not silently skip these -- that's exactly what made `S`/:Pack sync look
-- like it did nothing for freshly-added or never-checked plugins.
state.get_plugins()["foo.nvim"].status = "loaded"
state.get_plugins()["foo.nvim"].behind = nil
state.get_plugins()["bar.nvim"].status = "installed"
state.get_plugins()["bar.nvim"].behind = nil
local updated_names = nil
local orig_update = require("pack.async").update_plugins
require("pack.async").update_plugins = function(names)
updated_names = names
end
vim.cmd("Pack sync")
require("pack.async").update_plugins = orig_update
table.sort(updated_names or {})
assert.same({ "bar.nvim", "foo.nvim" }, updated_names, "never-checked plugins must still be synced")
end)
it(":Pack sync excludes a plugin already confirmed up to date (behind == 0)", function()
state.get_plugins()["foo.nvim"].status = "loaded"
state.get_plugins()["foo.nvim"].behind = 0
state.get_plugins()["bar.nvim"].status = "loaded"
state.get_plugins()["bar.nvim"].behind = nil
local updated_names = nil
local orig_update = require("pack.async").update_plugins
require("pack.async").update_plugins = function(names)
updated_names = names
end
vim.cmd("Pack sync")
require("pack.async").update_plugins = orig_update
assert.same({ "bar.nvim" }, updated_names, "a plugin confirmed up to date must not be re-synced")
end)
it(":Pack sync excludes a plugin that failed to install, even with behind == nil", function()
state.get_plugins()["foo.nvim"].status = "error"
state.get_plugins()["foo.nvim"].behind = nil
state.get_plugins()["bar.nvim"].status = "loaded"
state.get_plugins()["bar.nvim"].behind = nil
local updated_names = nil
local orig_update = require("pack.async").update_plugins
require("pack.async").update_plugins = function(names)
updated_names = names
end
vim.cmd("Pack sync")
require("pack.async").update_plugins = orig_update
assert.same({ "bar.nvim" }, updated_names, "an errored plugin must not be swept into sync's update batch")
end)
it(":Pack sync notifies instead of silently doing nothing when everything is up to date", function()
state.get_plugins()["foo.nvim"].status = "loaded"
state.get_plugins()["foo.nvim"].behind = 0
state.get_plugins()["bar.nvim"].status = "loaded"
state.get_plugins()["bar.nvim"].behind = 0
local infos = {}
local orig_notify = vim.notify
vim.notify = function(msg, level)
if level == vim.log.levels.INFO then
table.insert(infos, tostring(msg))
end
end
vim.cmd("Pack sync")
vim.notify = orig_notify
local found = false
for _, msg in ipairs(infos) do
if msg:find("up to date", 1, true) then
found = true
end
end
assert.is_true(found, "sync must notify when there is nothing to install or update")
end)
it("runs :Pack update and :Pack update <target>", function()
local updated_names = nil
local orig_native = pack.native_pack.update
pack.native_pack.update = function(names)
updated_names = names
end
vim.cmd("Pack update foo.nvim")
assert.same({ "foo.nvim" }, updated_names)
vim.cmd("Pack update")
pack.native_pack.update = orig_native
end)
it(":Pack update batches multiple names through the shared updater", function()
local updated_names, call_count = nil, 0
local orig_native = pack.native_pack.update
pack.native_pack.update = function(names)
updated_names = names
call_count = call_count + 1
end
-- Two names -> ONE batched native call.
vim.cmd("Pack update foo.nvim bar.nvim")
assert.same({ "foo.nvim", "bar.nvim" }, updated_names)
assert.equals(1, call_count)
-- Three names still land as one call (below the per-call cap).
state.add_plugin({ "user/baz.nvim" }, pack.config)
updated_names, call_count = nil, 0
vim.cmd("Pack update foo.nvim bar.nvim baz.nvim")
table.sort(updated_names or {})
assert.same({ "bar.nvim", "baz.nvim", "foo.nvim" }, updated_names)
assert.equals(1, call_count)
pack.native_pack.update = orig_native
end)
it(":Pack update splits large name lists into <=5-per-call native batches", function()
local calls = {}
local orig_native = pack.native_pack.update
pack.native_pack.update = function(batch)
table.insert(calls, vim.deepcopy(batch))
end
local names = {}
for i = 1, 11 do
local name = "many" .. i .. ".nvim"
names[#names + 1] = name
state.add_plugin({ "user/" .. name }, pack.config)
end
vim.cmd("Pack update " .. table.concat(names, " "))
vim.wait(2000, function()
return #calls == 3
end)
pack.native_pack.update = orig_native
assert.equals(3, #calls, "11 names must split into three capped batches")
assert.equals(5, #calls[1])
assert.equals(5, #calls[2])
assert.equals(1, #calls[3])
local seen = {}
for _, batch in ipairs(calls) do
for _, name in ipairs(batch) do
seen[name] = true
end
end
for _, name in ipairs(names) do
assert.is_true(seen[name], "every requested plugin must be updated exactly where it landed")
end
end)
it(":Pack update reports unknown names but still updates the valid ones", function()
local updated_names = nil
local orig_native = pack.native_pack.update
pack.native_pack.update = function(names)
updated_names = names
end
local warned = nil
local orig_notify = vim.notify
vim.notify = function(msg, level)
if level == vim.log.levels.ERROR then
warned = msg
end
end
vim.cmd("Pack update foo.nvim ghost.nvim")
vim.notify = orig_notify
pack.native_pack.update = orig_native
assert.same({ "foo.nvim" }, updated_names)
assert.truthy(warned and warned:find("ghost.nvim"), "unknown names must be reported")
end)
it("runs :Pack build and :Pack build <target>", function()
local built = {}
local orig_build = require("pack.async").run_build_hook
require("pack.async").run_build_hook = function(p, done)
table.insert(built, p.name)
done()
end
vim.cmd("Pack build foo.nvim")
assert.same({ "foo.nvim" }, built)
built = {}
vim.cmd("Pack build")
assert.is_true(#built > 0)
require("pack.async").run_build_hook = orig_build
end)
it("runs :Pack load <target>", function()
local loaded_target = nil
local orig_load = require("pack.loader").load
require("pack.loader").load = function(name)
loaded_target = name
end
vim.cmd("Pack load foo.nvim")
assert.equals("foo.nvim", loaded_target)
require("pack.loader").load = orig_load
end)
it("runs :Pack delete <target>", function()
local deleted_name = nil
local orig_del = vim.pack.del
vim.pack.del = function(names)
deleted_name = names[1]
end
vim.cmd("Pack delete foo.nvim")
assert.equals("foo.nvim", deleted_name)
vim.pack.del = orig_del
end)
it("runs :Pack clean", function()
local orig_get = pack.native_pack.get
pack.native_pack.get = function()
return {}
end
vim.cmd("Pack clean")
pack.native_pack.get = orig_get
end)
it("runs :Pack restore", function()
local restore_called = false
local orig_update = pack.native_pack.update
pack.native_pack.update = function(names, opts)
if opts and opts.target == "lockfile" then
restore_called = true
end
end
vim.cmd("Pack restore")
assert.is_true(restore_called)
pack.native_pack.update = orig_update
end)
it("runs :Pack repair", function()
local lockfile = require("pack.lockfile")
local orig_repair = lockfile.repair
local repair_called = false
lockfile.repair = function()
repair_called = true
return {}, nil
end
vim.cmd("Pack repair")
assert.is_true(repair_called)
lockfile.repair = orig_repair
end)
it("runs :Pack profile", function()
vim.cmd("Pack profile")
local buf = vim.api.nvim_get_current_buf()
assert.equals("pack_profile", vim.bo[buf].filetype)
end)
it("runs :Pack diff", function()
state.get_plugins()["foo.nvim"].behind = 2
vim.cmd("Pack diff")
local buf = vim.api.nvim_get_current_buf()
assert.equals("pack_diff", vim.bo[buf].filetype)
end)
it("completes :Pack subcommands and targets", function()
local cmd = vim.api.nvim_get_commands({})["Pack"]
assert.is_not_nil(cmd)
local complete_fn = cmd.complete
-- Completion for subcommands
local sub_matches = complete_fn("up", "Pack up", 7)
assert.is_true(vim.tbl_contains(sub_matches, "update"))
-- Completion for plugin target
local plugin_matches = complete_fn("fo", "Pack update fo", 14)
assert.is_true(vim.tbl_contains(plugin_matches, "foo.nvim"))
-- Completion also works for batch targets after the first name.
local batch_matches = complete_fn("ba", "Pack update foo.nvim ba", 24)
assert.is_true(vim.tbl_contains(batch_matches, "bar.nvim"))
end)
it("supports config.ui.filter modes (default, function)", function()
-- Default mode (vim.ui.input)
pack.config.ui.filter = "default"
local ui_input_called = false
local orig_ui_input = vim.ui.input
vim.ui.input = function(opts, cb)
ui_input_called = true
cb("foo")
end
ui.open(pack.config)
ui.filter()
assert.is_true(ui_input_called)
vim.ui.input = orig_ui_input
ui.close()
-- Custom function mode
local custom_called = false
pack.config.ui.filter = function(opts, cb)
custom_called = true
cb("baz")
end
ui.open(pack.config)
ui.filter()
assert.is_true(custom_called)
ui.close()
end)
end)