Repository navigation
Expand file tree
/
Copy pathstate_spec.lua
More file actions
308 lines (272 loc) · 11.6 KB
/
Copy pathstate_spec.lua
File metadata and controls
308 lines (272 loc) · 11.6 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
-- Clear any cached modules
package.loaded["pack.state"] = nil
package.loaded["pack.persist"] = nil
local state = require("pack.state")
local persist = require("pack.persist")
describe("pack.state", function()
local tmp_path
before_each(function()
tmp_path = vim.fn.tempname() .. "-pack-disabled.json"
persist._set_path_for_testing(tmp_path)
end)
after_each(function()
if vim.fn.filereadable(tmp_path) == 1 then
vim.fn.delete(tmp_path)
end
persist._set_path_for_testing(nil)
end)
local function config_with(plugins)
return {
install_path = vim.fn.tempname() .. "-pack-install",
plugins = plugins,
}
end
it("normalizes a bare string plugin spec and defaults disabled to false", function()
state.init(config_with({ "user/foo.nvim" }))
local p = state.get_plugins()["foo.nvim"]
assert.is_not_nil(p)
assert.equals("https://github.com/user/foo.nvim", p.url)
assert.is_false(p.disabled)
end)
it("marks plugins disabled from the persisted set", function()
persist.save({ ["foo.nvim"] = true })
state.init(config_with({ "user/foo.nvim", "user/bar.nvim" }))
local plugins = state.get_plugins()
assert.is_true(plugins["foo.nvim"].disabled)
assert.is_false(plugins["bar.nvim"].disabled)
end)
it("set_disabled updates in-memory state and persists", function()
state.init(config_with({ "user/foo.nvim" }))
state.set_disabled("foo.nvim", true)
assert.is_true(state.get_plugins()["foo.nvim"].disabled)
assert.is_true(persist.load()["foo.nvim"])
end)
it("remove_plugin clears plugin from state and removes disabled entry from persist json", function()
state.init(config_with({ "user/foo.nvim" }))
state.set_disabled("foo.nvim", true)
assert.is_true(persist.load()["foo.nvim"])
state.remove_plugin("foo.nvim")
assert.is_nil(state.get_plugins()["foo.nvim"])
assert.is_nil(persist.load()["foo.nvim"])
end)
it("set_behind stores the commit-behind count and a timestamp", function()
state.init(config_with({ "user/foo.nvim" }))
state.set_behind("foo.nvim", 3)
local p = state.get_plugins()["foo.nvim"]
assert.equals(3, p.behind)
assert.is_number(p.checked_at)
end)
it("set_disabled on nonexistent plugin does not error and leaves plugin nil", function()
state.init(config_with({ "user/foo.nvim" }))
local ok = pcall(state.set_disabled, "nonexistent.nvim", true)
assert.is_true(ok)
assert.is_nil(state.get_plugins()["nonexistent.nvim"])
end)
it("set_behind on nonexistent plugin does not error and leaves plugin nil", function()
state.init(config_with({ "user/foo.nvim" }))
local ok = pcall(state.set_behind, "nonexistent.nvim", 3)
assert.is_true(ok)
assert.is_nil(state.get_plugins()["nonexistent.nvim"])
end)
it("set_outdated_detail stores revision/branch/commit fields together", function()
state.init(config_with({ "user/foo.nvim" }))
state.set_outdated_detail("foo.nvim", {
revision_before = "abc123",
revision_after = "def456",
upstream_branch = "main",
pending_commits = { "def456 │ fix: something" },
})
local p = state.get_plugins()["foo.nvim"]
assert.equals("abc123", p.revision_before)
assert.equals("def456", p.revision_after)
assert.equals("main", p.upstream_branch)
assert.same({ "def456 │ fix: something" }, p.pending_commits)
end)
it("set_outdated_detail no-ops for an unknown plugin name", function()
state.init(config_with({ "user/foo.nvim" }))
local ok = pcall(state.set_outdated_detail, "nonexistent.nvim", { revision_before = "x" })
assert.is_true(ok)
assert.is_nil(state.get_plugins()["nonexistent.nvim"])
end)
it("reconcile_from_native promotes installed->loaded when dir is on runtimepath", function()
state.init(config_with({ "user/foo.nvim" }))
local p = state.get_plugins()["foo.nvim"]
-- A plugin native itself packadd-ed (e.g. bootstrapped pack.nvim) is active
-- on rtp but never ran through our load_fn, so it sits at "installed".
local rtp_dir = vim.api.nvim_list_runtime_paths()[1]
p.status = "installed"
local fake_native = {
get = function()
return { { spec = { name = "foo.nvim" }, path = rtp_dir, rev = "deadbee" } }
end,
}
state.reconcile_from_native(fake_native)
assert.equals("loaded", p.status)
end)
it("reconcile_from_native leaves installed when dir is not on runtimepath", function()
state.init(config_with({ "user/foo.nvim" }))
local p = state.get_plugins()["foo.nvim"]
p.status = "installed"
local fake_native = {
get = function()
return { { spec = { name = "foo.nvim" }, path = "/nowhere/not/on/rtp/foo.nvim" } }
end,
}
state.reconcile_from_native(fake_native)
assert.equals("installed", p.status)
end)
it("reconcile_from_native trusts runtimepath membership over native's active flag", function()
state.init(config_with({ "user/foo.nvim" }))
local p = state.get_plugins()["foo.nvim"]
p.status = "installed"
local fake_native = {
get = function()
return {
-- Path is NOT on runtimepath. Even though native claims active = true,
-- runtimepath membership is authoritative -- a plugin absent from rtp
-- is not actually loaded this session, so it stays "installed".
{ spec = { name = "foo.nvim" }, path = "/definitely/not/on/rtp/foo.nvim", rev = "abc", active = true },
}
end,
}
state.reconcile_from_native(fake_native)
assert.equals("installed", p.status)
end)
it("reconcile_from_native adopts an unknown plugin as installed (not loaded) when it is off runtimepath", function()
state.init(config_with({ "user/foo.nvim" }))
local fake_native = {
get = function()
return {
-- Present in native and on disk, but its path is not on runtimepath,
-- so it is installed-but-not-loaded regardless of any active flag.
{ spec = { name = "adopted.nvim", src = "https://github.com/x/adopted.nvim" }, path = "/nowhere/not/on/rtp/adopted.nvim", active = false },
}
end,
}
state.reconcile_from_native(fake_native)
local a = state.get_plugins()["adopted.nvim"]
assert.is_not_nil(a)
assert.equals("installed", a.status)
end)
it("declared plugins are marked managed = true", function()
state.init(config_with({ "user/foo.nvim" }))
assert.is_true(state.get_plugins()["foo.nvim"].managed)
end)
it("reconcile_from_native adopts an unknown native plugin as managed = false", function()
state.init(config_with({ "user/foo.nvim" }))
local rtp_dir = vim.api.nvim_list_runtime_paths()[1]
local fake_native = {
get = function()
return {
{ spec = { name = "foo.nvim" }, path = "/some/foo.nvim", rev = "aaa" },
{ spec = { name = "adopted.nvim", src = "https://github.com/x/adopted.nvim" }, path = rtp_dir, rev = "bbb" },
}
end,
}
state.reconcile_from_native(fake_native)
local a = state.get_plugins()["adopted.nvim"]
assert.is_not_nil(a)
assert.is_false(a.managed)
assert.equals("https://github.com/x/adopted.nvim", a.url)
assert.equals("loaded", a.status) -- path is on runtimepath
assert.equals(50, a.priority)
assert.is_false(a.disabled)
assert.is_false(a.lazy)
assert.same({}, a.log)
assert.same({}, a.dependencies)
assert.is_false(a.is_local)
end)
it("reconcile_from_native does not overwrite a managed entry on name collision", function()
state.init(config_with({ "user/foo.nvim" }))
local before = state.get_plugins()["foo.nvim"]
before.status = "installed"
local fake_native = {
get = function()
return { { spec = { name = "foo.nvim" }, path = "/new/path/foo.nvim", rev = "ccc" } }
end,
}
state.reconcile_from_native(fake_native)
local after = state.get_plugins()["foo.nvim"]
assert.is_true(after.managed) -- still managed, not clobbered
assert.equals("/new/path/foo.nvim", after.dir) -- dir still refreshed
assert.equals("ccc", after.rev)
end)
it("reconcile_from_native bumps generation only when it adopts", function()
state.init(config_with({ "user/foo.nvim" }))
local gen0 = state.generation
state.reconcile_from_native({ get = function()
return { { spec = { name = "foo.nvim" }, path = "/p/foo.nvim" } }
end })
assert.equals(gen0, state.generation) -- no new plugin, no bump
state.reconcile_from_native({ get = function()
return { { spec = { name = "new.nvim", src = "https://github.com/x/new.nvim" }, path = "/p/new.nvim" } }
end })
assert.equals(gen0 + 1, state.generation) -- one adopted
end)
it("a plugin's own full spec upgrades a bare dependency-stub registered first", function()
-- "consumer.nvim" depends on "dep.nvim" via a bare string (no keys/config).
-- If it's added first, "dep.nvim" gets a thin stub. The user's own full
-- declaration of "dep.nvim" (with keys/opts) must still win, not be dropped.
state.init(config_with({}))
state.add_plugin({ "user/consumer.nvim", dependencies = { "user/dep.nvim" } }, {})
local stub = state.get_plugins()["dep.nvim"]
assert.is_not_nil(stub)
assert.is_nil(stub.keys)
state.add_plugin({ "user/dep.nvim", keys = { "<leader>d" }, opts = { x = 1 } }, {})
local dep = state.get_plugins()["dep.nvim"]
assert.same({ "<leader>d" }, dep.keys)
assert.same({ x = 1 }, dep.opts)
assert.is_true(dep.lazy, "keys must still infer lazy=true on the upgraded entry")
end)
it("a bare dependency declared after the full spec does not clobber it", function()
state.init(config_with({}))
state.add_plugin({ "user/dep.nvim", keys = { "<leader>d" }, opts = { x = 1 } }, {})
state.add_plugin({ "user/consumer.nvim", dependencies = { "user/dep.nvim" } }, {})
local dep = state.get_plugins()["dep.nvim"]
assert.same({ "<leader>d" }, dep.keys)
assert.same({ x = 1 }, dep.opts)
end)
it("init resets previously registered plugins", function()
state.init(config_with({ "user/foo.nvim", commit = "abc" }))
-- re-init with a different plugin set must NOT retain the old ones
state.init(config_with({ "user/bar.nvim" }))
local plugins = state.get_plugins()
assert.is_nil(plugins["foo.nvim"], "stale plugin from prior init must be cleared")
assert.is_not_nil(plugins["bar.nvim"])
end)
it("infers lazy = true when event, ft, cmd, or keys are present without explicit lazy", function()
state.init({
plugins = {
{ "user/eventplug.nvim", event = "BufReadPost" },
{ "user/ftplug.nvim", ft = "markdown" },
{ "user/cmdplug.nvim", cmd = "Cmd" },
{ "user/keyplug.nvim", keys = "<leader>k" },
{ "user/eagerplug.nvim", event = "BufReadPost", lazy = false },
},
})
local p = state.get_plugins()
assert.is_true(p["eventplug.nvim"].lazy)
assert.is_true(p["ftplug.nvim"].lazy)
assert.is_true(p["cmdplug.nvim"].lazy)
assert.is_true(p["keyplug.nvim"].lazy)
assert.is_false(p["eagerplug.nvim"].lazy)
end)
it("reconciles plugin loaded status directly when entry.active is present", function()
state.init(config_with({ "user/foo.nvim" }))
local fake_path = vim.fs.normalize(vim.fn.tempname() .. "/foo.nvim")
vim.fn.mkdir(fake_path, "p")
vim.opt.rtp:append(fake_path)
local mock_native = {
get = function()
return {
{ spec = { name = "foo.nvim", src = "https://github.com/user/foo.nvim" }, path = fake_path, active = true }
}
end,
}
state.reconcile_from_native(mock_native)
local p = state.get_plugins()["foo.nvim"]
assert.equals("loaded", p.status)
vim.opt.rtp:remove(fake_path)
vim.fn.delete(fake_path, "rf")
end)
end)