Skip to content

Commit 0e1c8b7

Browse files
committed
test(integrations): expand on testing for snacks.explorer
1 parent addff9e commit 0e1c8b7

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

tests/unit/snacks_explorer_spec.lua

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,77 @@ describe("snacks.explorer integration", function()
342342
vim.fn.isdirectory = original_isdirectory
343343
package.loaded["snacks"] = nil
344344
end)
345+
346+
it("should protect against root-level files", function()
347+
-- Mock snacks module with root-level and safe files
348+
local mock_explorer = {
349+
selected = function(self, opts)
350+
return {
351+
{ file = "/etc/passwd" }, -- root-level file (dangerous)
352+
{ file = "/home/user/file.lua" }, -- safe file
353+
{ file = "/usr/bin/vim" }, -- root-level file (dangerous)
354+
{ file = "/path/to/directory/" }, -- safe directory
355+
}
356+
end,
357+
current = function(self, opts)
358+
return { file = "/etc/hosts" } -- root-level file
359+
end,
360+
}
361+
362+
local mock_snacks = {
363+
picker = {
364+
get = function(opts)
365+
if opts.source == "explorer" then
366+
return { mock_explorer }
367+
end
368+
return {}
369+
end,
370+
},
371+
}
372+
373+
package.loaded["snacks"] = mock_snacks
374+
375+
-- Test selected items - should filter out root-level files
376+
local files, err = integrations._get_snacks_explorer_selection()
377+
assert.is_nil(err)
378+
assert.are.same({
379+
"/home/user/file.lua",
380+
"/path/to/directory/",
381+
}, files)
382+
383+
package.loaded["snacks"] = nil
384+
end)
385+
386+
it("should return error for root-level current file", function()
387+
-- Mock snacks module with root-level current file and no selection
388+
local mock_explorer = {
389+
selected = function(self, opts)
390+
return {} -- No selection
391+
end,
392+
current = function(self, opts)
393+
return { file = "/etc/passwd" } -- root-level file
394+
end,
395+
}
396+
397+
local mock_snacks = {
398+
picker = {
399+
get = function(opts)
400+
if opts.source == "explorer" then
401+
return { mock_explorer }
402+
end
403+
return {}
404+
end,
405+
},
406+
}
407+
408+
package.loaded["snacks"] = mock_snacks
409+
410+
local files, err = integrations._get_snacks_explorer_selection()
411+
assert.are.same({}, files)
412+
assert.equals("Cannot add root-level file. Please select a file in a subdirectory.", err)
413+
414+
package.loaded["snacks"] = nil
415+
end)
345416
end)
346417

347418
describe("get_selected_files_from_tree", function()

0 commit comments

Comments
 (0)