Skip to content

Commit e52848d

Browse files
committed
test: add tests for ZClean and registered plugins cache
- ZClean detects orphan plugins not in spec - ZClean does not delete plugins that are in spec - ZClean detects multiple orphan plugins - registered_plugin_names is alphabetically sorted - registered_plugins orders startup before lazy - registered_plugins sorts by priority within each category - registered_plugins has orphans after lazy plugins - registered_plugin_names includes orphan plugins
1 parent 1bb6c1d commit e52848d

File tree

2 files changed

+252
-0
lines changed

2 files changed

+252
-0
lines changed

tests/run_all.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ local test_modules = {
2525
'dependencies_test',
2626
'module_loader_test',
2727
'zdelete_test',
28+
'zclean_test',
2829
}
2930

3031
print("\n" .. string.rep("=", 60))

tests/zclean_test.lua

Lines changed: 251 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,251 @@
1+
local helpers = require('helpers')
2+
3+
return function()
4+
helpers.describe("ZClean Command", function()
5+
helpers.test("ZClean detects orphan plugins not in spec", function()
6+
helpers.setup_test_env()
7+
8+
require('zpack').setup({
9+
spec = {
10+
{ 'test/plugin-a' },
11+
{ 'test/plugin-b' },
12+
},
13+
defaults = { confirm = false },
14+
})
15+
16+
helpers.flush_pending()
17+
18+
_G.test_state.registered_pack_specs['orphan-plugin'] = {
19+
src = 'test/orphan-plugin',
20+
name = 'orphan-plugin',
21+
}
22+
23+
require('zpack.commands').update_registered_plugins_cache()
24+
25+
vim.cmd('ZClean')
26+
helpers.flush_pending()
27+
28+
helpers.assert_equal(#_G.test_state.vim_pack_del_calls, 1, "vim.pack.del should be called")
29+
local call = _G.test_state.vim_pack_del_calls[1]
30+
helpers.assert_table_contains(call.names, 'orphan-plugin', "orphan plugin should be in delete list")
31+
helpers.assert_equal(#call.names, 1, "only orphan plugin should be deleted")
32+
33+
helpers.cleanup_test_env()
34+
helpers.delete_zpack_commands()
35+
end)
36+
37+
helpers.test("ZClean does not delete plugins in spec", function()
38+
helpers.setup_test_env()
39+
40+
require('zpack').setup({
41+
spec = {
42+
{ 'test/plugin-a' },
43+
{ 'test/plugin-b' },
44+
},
45+
defaults = { confirm = false },
46+
})
47+
48+
helpers.flush_pending()
49+
50+
vim.cmd('ZClean')
51+
helpers.flush_pending()
52+
53+
helpers.assert_equal(#_G.test_state.vim_pack_del_calls, 0, "vim.pack.del should not be called")
54+
55+
local found_info = false
56+
for _, notif in ipairs(_G.test_state.notifications) do
57+
if notif.msg:find('No unused plugins') and notif.level == vim.log.levels.INFO then
58+
found_info = true
59+
break
60+
end
61+
end
62+
helpers.assert_true(found_info, "Should show info that no unused plugins exist")
63+
64+
helpers.cleanup_test_env()
65+
helpers.delete_zpack_commands()
66+
end)
67+
68+
helpers.test("ZClean detects multiple orphan plugins", function()
69+
helpers.setup_test_env()
70+
71+
require('zpack').setup({
72+
spec = {
73+
{ 'test/plugin-a' },
74+
},
75+
defaults = { confirm = false },
76+
})
77+
78+
helpers.flush_pending()
79+
80+
_G.test_state.registered_pack_specs['orphan-1'] = {
81+
src = 'test/orphan-1',
82+
name = 'orphan-1',
83+
}
84+
_G.test_state.registered_pack_specs['orphan-2'] = {
85+
src = 'test/orphan-2',
86+
name = 'orphan-2',
87+
}
88+
89+
require('zpack.commands').update_registered_plugins_cache()
90+
91+
vim.cmd('ZClean')
92+
helpers.flush_pending()
93+
94+
helpers.assert_equal(#_G.test_state.vim_pack_del_calls, 1, "vim.pack.del should be called once")
95+
local call = _G.test_state.vim_pack_del_calls[1]
96+
helpers.assert_equal(#call.names, 2, "both orphan plugins should be deleted")
97+
98+
helpers.cleanup_test_env()
99+
helpers.delete_zpack_commands()
100+
end)
101+
end)
102+
103+
helpers.describe("Registered Plugins Cache", function()
104+
helpers.test("registered_plugin_names is alphabetically sorted", function()
105+
helpers.setup_test_env()
106+
local state = require('zpack.state')
107+
108+
require('zpack').setup({
109+
spec = {
110+
{ 'test/zebra-plugin' },
111+
{ 'test/alpha-plugin' },
112+
{ 'test/middle-plugin' },
113+
},
114+
defaults = { confirm = false },
115+
})
116+
117+
helpers.flush_pending()
118+
119+
helpers.assert_equal(state.registered_plugin_names[1], 'alpha-plugin', "first should be alpha")
120+
helpers.assert_equal(state.registered_plugin_names[2], 'middle-plugin', "second should be middle")
121+
helpers.assert_equal(state.registered_plugin_names[3], 'zebra-plugin', "third should be zebra")
122+
123+
helpers.cleanup_test_env()
124+
helpers.delete_zpack_commands()
125+
end)
126+
127+
helpers.test("registered_plugins has startup before lazy", function()
128+
helpers.setup_test_env()
129+
local state = require('zpack.state')
130+
131+
require('zpack').setup({
132+
spec = {
133+
{ 'test/lazy-plugin', cmd = 'LazyCmd' },
134+
{ 'test/startup-plugin', config = function() end },
135+
},
136+
defaults = { confirm = false },
137+
})
138+
139+
helpers.flush_pending()
140+
141+
helpers.assert_equal(#state.registered_plugins, 2, "should have 2 plugins")
142+
helpers.assert_equal(state.registered_plugins[1].name, 'startup-plugin', "startup should be first")
143+
helpers.assert_equal(state.registered_plugins[2].name, 'lazy-plugin', "lazy should be second")
144+
145+
helpers.cleanup_test_env()
146+
helpers.delete_zpack_commands()
147+
end)
148+
149+
helpers.test("registered_plugins sorts startup by priority", function()
150+
helpers.setup_test_env()
151+
local state = require('zpack.state')
152+
153+
require('zpack').setup({
154+
spec = {
155+
{ 'test/low-priority', priority = 10, config = function() end },
156+
{ 'test/high-priority', priority = 100, config = function() end },
157+
{ 'test/default-priority', config = function() end },
158+
},
159+
defaults = { confirm = false },
160+
})
161+
162+
helpers.flush_pending()
163+
164+
helpers.assert_equal(state.registered_plugins[1].name, 'high-priority', "high priority should be first")
165+
helpers.assert_equal(state.registered_plugins[2].name, 'default-priority', "default (50) should be second")
166+
helpers.assert_equal(state.registered_plugins[3].name, 'low-priority', "low priority should be third")
167+
168+
helpers.cleanup_test_env()
169+
helpers.delete_zpack_commands()
170+
end)
171+
172+
helpers.test("registered_plugins sorts lazy by priority", function()
173+
helpers.setup_test_env()
174+
local state = require('zpack.state')
175+
176+
require('zpack').setup({
177+
spec = {
178+
{ 'test/lazy-low', priority = 10, cmd = 'LowCmd' },
179+
{ 'test/lazy-high', priority = 100, cmd = 'HighCmd' },
180+
},
181+
defaults = { confirm = false },
182+
})
183+
184+
helpers.flush_pending()
185+
186+
helpers.assert_equal(state.registered_plugins[1].name, 'lazy-high', "high priority lazy should be first")
187+
helpers.assert_equal(state.registered_plugins[2].name, 'lazy-low', "low priority lazy should be second")
188+
189+
helpers.cleanup_test_env()
190+
helpers.delete_zpack_commands()
191+
end)
192+
193+
helpers.test("registered_plugins has orphans after lazy", function()
194+
helpers.setup_test_env()
195+
local state = require('zpack.state')
196+
197+
require('zpack').setup({
198+
spec = {
199+
{ 'test/startup-plugin', config = function() end },
200+
{ 'test/lazy-plugin', cmd = 'LazyCmd' },
201+
},
202+
defaults = { confirm = false },
203+
})
204+
205+
helpers.flush_pending()
206+
207+
_G.test_state.registered_pack_specs['orphan-plugin'] = {
208+
src = 'test/orphan-plugin',
209+
name = 'orphan-plugin',
210+
}
211+
212+
require('zpack.commands').update_registered_plugins_cache()
213+
214+
helpers.assert_equal(#state.registered_plugins, 3, "should have 3 plugins")
215+
helpers.assert_equal(state.registered_plugins[1].name, 'startup-plugin', "startup should be first")
216+
helpers.assert_equal(state.registered_plugins[2].name, 'lazy-plugin', "lazy should be second")
217+
helpers.assert_equal(state.registered_plugins[3].name, 'orphan-plugin', "orphan should be last")
218+
219+
helpers.cleanup_test_env()
220+
helpers.delete_zpack_commands()
221+
end)
222+
223+
helpers.test("registered_plugin_names includes orphans", function()
224+
helpers.setup_test_env()
225+
local state = require('zpack.state')
226+
227+
require('zpack').setup({
228+
spec = {
229+
{ 'test/plugin-a' },
230+
},
231+
defaults = { confirm = false },
232+
})
233+
234+
helpers.flush_pending()
235+
236+
_G.test_state.registered_pack_specs['orphan-plugin'] = {
237+
src = 'test/orphan-plugin',
238+
name = 'orphan-plugin',
239+
}
240+
241+
require('zpack.commands').update_registered_plugins_cache()
242+
243+
helpers.assert_equal(#state.registered_plugin_names, 2, "should have 2 names")
244+
helpers.assert_table_contains(state.registered_plugin_names, 'orphan-plugin', "orphan should be in names")
245+
helpers.assert_table_contains(state.registered_plugin_names, 'plugin-a', "spec plugin should be in names")
246+
247+
helpers.cleanup_test_env()
248+
helpers.delete_zpack_commands()
249+
end)
250+
end)
251+
end

0 commit comments

Comments
 (0)