Skip to content

Commit d1d24b1

Browse files
committed
test: add clear_all_state() method for use in test cases, part of #77
1 parent 4c136ff commit d1d24b1

File tree

4 files changed

+69
-6
lines changed

4 files changed

+69
-6
lines changed

lua/neo-tree/sources/manager.lua

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ local renderer = require("neo-tree.ui.renderer")
88
local inputs = require("neo-tree.ui.inputs")
99
local events = require("neo-tree.events")
1010
local log = require("neo-tree.log")
11+
local fs_watch = require("neo-tree.sources.filesystem.lib.fs_watch")
1112

1213
local M = {}
1314
local source_data = {}
15+
local default_configs = {}
1416

1517
local get_source_data = function(source_name)
1618
if source_name == nil then
@@ -22,7 +24,6 @@ local get_source_data = function(source_name)
2224
end
2325
sd = {
2426
name = source_name,
25-
default_config = {},
2627
state_by_tab = {},
2728
subscriptions = {},
2829
}
@@ -31,7 +32,8 @@ local get_source_data = function(source_name)
3132
end
3233

3334
local function create_state(tabnr, sd)
34-
local state = utils.table_copy(sd.default_config)
35+
local default_config = default_configs[sd.name]
36+
local state = utils.table_copy(default_config)
3537
state.tabnr = tabnr
3638
state.dirty = true
3739
state.position = {
@@ -68,12 +70,25 @@ local function create_state(tabnr, sd)
6870
return state
6971
end
7072

73+
---For use in tests only, completely resets the state of all sources.
74+
---This closes all windows as well since they would be broken by this action.
75+
M._clear_state = function()
76+
fs_watch.unwatch_all()
77+
renderer.close_all_floating_windows()
78+
for _, data in pairs(source_data) do
79+
for _, state in pairs(data.state_by_tab) do
80+
renderer.close(state)
81+
end
82+
end
83+
source_data = {}
84+
end
85+
7186
M.set_default_config = function(source_name, config)
7287
if source_name == nil then
7388
error("set_default_config: source_name cannot be nil")
7489
end
90+
default_configs[source_name] = config
7591
local sd = get_source_data(source_name)
76-
sd.default_config = config
7792
for tabnr, tab_config in pairs(sd.state_by_tab) do
7893
sd.state_by_tab[tabnr] = utils.table_merge(tab_config, config)
7994
end

lua/neo-tree/ui/renderer.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,6 @@ create_window = function(state)
376376
local sourceTitle = state.name:gsub("^%l", string.upper)
377377
win_options = popups.popup_options("Neo-tree " .. sourceTitle, 40, win_options)
378378
win_options.win_options = nil
379-
print(vim.inspect(win_options))
380379
win_options.zindex = 40
381380
local size = { width = 60, height = "80%" }
382381

tests/helpers/util.lua

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,12 @@ utils.teardown_test_fs = function()
5555
end
5656

5757
utils.clear_test_state = function()
58-
-- TODO: Clear internal state?
58+
-- Create fresh window
5959
vim.cmd("top new | wincmd o")
6060
local keepbufnr = vim.api.nvim_get_current_buf()
61+
-- Clear ALL neo-tree state
62+
require("neo-tree.sources.manager")._clear_state()
63+
-- Cleanup any remaining buffers
6164
for _, bufnr in ipairs(vim.api.nvim_list_bufs()) do
6265
if bufnr ~= keepbufnr then
6366
vim.api.nvim_buf_delete(bufnr, { force = true })
@@ -69,7 +72,10 @@ end
6972

7073
utils.editfile = function(testfile)
7174
vim.cmd("e " .. testfile)
72-
assert.are.same(vim.fn.fnamemodify(vim.api.nvim_buf_get_name(0), ":p"), vim.fn.fnamemodify(testfile, ":p"))
75+
assert.are.same(
76+
vim.fn.fnamemodify(vim.api.nvim_buf_get_name(0), ":p"),
77+
vim.fn.fnamemodify(testfile, ":p")
78+
)
7379
end
7480

7581
return utils

tests/neo-tree/neo-tree_spec.lua

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
local util = require("tests.helpers.util")
2+
local verify = require("tests.helpers.verify")
3+
4+
describe("Filesystem source command", function()
5+
local fs = util.setup_test_fs()
6+
7+
after_each(function()
8+
util.clear_test_state()
9+
end)
10+
11+
it("should reveal the current file in the tree", function()
12+
local testfile = fs.content[3].abspath
13+
util.editfile(testfile)
14+
15+
local start_bufnr = vim.api.nvim_get_current_buf()
16+
17+
vim.cmd("NeoTreeReveal")
18+
verify.bufnr_is_not(start_bufnr)
19+
verify.tree_focused()
20+
verify.tree_node_is(testfile)
21+
end)
22+
23+
it("should toggle the reveal-state of the tree", function()
24+
local testfile = fs.content[3].abspath
25+
util.editfile(testfile)
26+
27+
local start_bufnr = vim.api.nvim_get_current_buf()
28+
29+
vim.cmd("NeoTreeRevealToggle")
30+
verify.bufnr_is_not(start_bufnr)
31+
verify.tree_focused()
32+
verify.tree_node_is(testfile)
33+
34+
-- Wait long enough such that the tree _should have_ closed, then assert it is not focused anymore
35+
vim.cmd("NeoTreeRevealToggle")
36+
verify.after(250, function()
37+
return #vim.api.nvim_tabpage_list_wins(0) == 1
38+
and start_bufnr == vim.api.nvim_get_current_buf()
39+
end, "Failed to toggle the tree to a closed state with 'action=reveal'")
40+
end)
41+
42+
util.teardown_test_fs()
43+
end)

0 commit comments

Comments
 (0)