Skip to content

Commit 4c136ff

Browse files
authored
feat(tests): Add baseline testing using Plenary's Busted hooks (#106)
1 parent 901a3ec commit 4c136ff

File tree

4 files changed

+155
-0
lines changed

4 files changed

+155
-0
lines changed

Makefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.PHONY: test
2+
test:
3+
nvim --headless --noplugin -u tests/mininit.lua -c "PlenaryBustedDirectory tests/neo-tree/ { minimal_init = 'tests/mininit.lua' }"
4+
5+
.PHONY: format
6+
format:
7+
stylua ./lua ./tests

tests/helpers/util.lua

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
local utils = {}
2+
3+
local Path = require("plenary.path")
4+
local testdir = Path:new(vim.env.TMPDIR or "/tmp", "neo-tree-testing"):absolute()
5+
6+
local function rm_test_dir()
7+
if vim.fn.isdirectory(testdir) == 1 then
8+
vim.fn.delete(testdir, "rf")
9+
end
10+
end
11+
12+
utils.setup_test_fs = function()
13+
rm_test_dir()
14+
15+
-- Need a list-style map here to ensure that things happen in the correct order.
16+
--
17+
-- When/if editing this, be cautious as (for now) other tests might be accessing
18+
-- files from within this array by index
19+
local fs = {
20+
basedir = testdir,
21+
content = {
22+
{
23+
name = "foo",
24+
type = "dir",
25+
content = {
26+
{ name = "foofile1.txt", type = "file" },
27+
{ name = "foofile2.txt", type = "file" },
28+
},
29+
},
30+
{ name = "bar", type = "dir" },
31+
{ name = "topfile1.txt", type = "file" },
32+
},
33+
}
34+
local function makefs(content, basedir)
35+
for _, info in ipairs(content) do
36+
if info.type == "dir" then
37+
info.abspath = Path:new(basedir, info.name):absolute()
38+
vim.fn.mkdir(info.abspath, "p")
39+
if info.content then
40+
makefs(info.content, info.abspath)
41+
end
42+
elseif info.type == "file" then
43+
info.abspath = Path:new(basedir, info.name):absolute()
44+
vim.fn.writefile({}, info.abspath)
45+
end
46+
end
47+
end
48+
makefs(fs.content, testdir)
49+
vim.cmd("tcd " .. testdir)
50+
return fs
51+
end
52+
53+
utils.teardown_test_fs = function()
54+
rm_test_dir()
55+
end
56+
57+
utils.clear_test_state = function()
58+
-- TODO: Clear internal state?
59+
vim.cmd("top new | wincmd o")
60+
local keepbufnr = vim.api.nvim_get_current_buf()
61+
for _, bufnr in ipairs(vim.api.nvim_list_bufs()) do
62+
if bufnr ~= keepbufnr then
63+
vim.api.nvim_buf_delete(bufnr, { force = true })
64+
end
65+
end
66+
assert(#vim.api.nvim_tabpage_list_wins(0) == 1, "Failed to properly clear tab")
67+
assert(#vim.api.nvim_list_bufs() == 1, "Failed to properly clear buffers")
68+
end
69+
70+
utils.editfile = function(testfile)
71+
vim.cmd("e " .. testfile)
72+
assert.are.same(vim.fn.fnamemodify(vim.api.nvim_buf_get_name(0), ":p"), vim.fn.fnamemodify(testfile, ":p"))
73+
end
74+
75+
return utils

tests/helpers/verify.lua

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
local verify = {}
2+
3+
verify.eventually = function(timeout, assertfunc, failmsg, ...)
4+
local success, args = false, { ... }
5+
vim.wait(timeout or 1000, function()
6+
success = assertfunc(unpack(args))
7+
return success
8+
end)
9+
assert(success, failmsg)
10+
end
11+
12+
verify.after = function(timeout, assertfunc, failmsg)
13+
vim.wait(timeout, function()
14+
return false
15+
end)
16+
assert(assertfunc(), failmsg)
17+
end
18+
19+
verify.bufnr_is_not = function(start_bufnr, timeout)
20+
verify.eventually(timeout or 500, function()
21+
return start_bufnr ~= vim.api.nvim_get_current_buf()
22+
end, string.format("Current buffer is '%s' when expected to not be", start_bufnr))
23+
end
24+
25+
verify.tree_focused = function(timeout)
26+
verify.eventually(timeout or 1000, function()
27+
return vim.api.nvim_buf_get_option(0, "filetype") == "neo-tree"
28+
end, "Current buffer is not a 'neo-tree' filetype")
29+
end
30+
31+
verify.tree_node_is = function(expected_node_id, timeout)
32+
verify.eventually(timeout or 500, function()
33+
local state = require("neo-tree.sources.manager").get_state("filesystem")
34+
local node = state.tree:get_node()
35+
if not node then
36+
return false
37+
end
38+
local node_id = node:get_id()
39+
if node_id ~= expected_node_id then
40+
return false
41+
end
42+
if state.position.node_id ~= expected_node_id then
43+
return false
44+
end
45+
return true
46+
end, string.format("Tree node '%s' not focused", expected_node_id))
47+
end
48+
49+
return verify

tests/mininit.lua

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
-- Need the absolute path as when doing the testing we will issue things like `tcd` to change directory
2+
-- to where our temporary filesystem lives
3+
vim.opt.rtp = {
4+
vim.fn.fnamemodify(vim.trim(vim.fn.system("git rev-parse --show-toplevel")), ":p"),
5+
vim.env.VIMRUNTIME,
6+
}
7+
8+
vim.cmd([[
9+
packadd plenary.nvim
10+
packadd nui.nvim
11+
]])
12+
13+
require("neo-tree").setup()
14+
15+
vim.opt.swapfile = false
16+
17+
vim.cmd([[
18+
runtime plugin/neo-tree.vim
19+
]])
20+
21+
-- For debugging
22+
P = function(...)
23+
print(unpack(vim.tbl_map(vim.inspect, { ... })))
24+
end

0 commit comments

Comments
 (0)