Skip to content

Commit 1e1bca3

Browse files
authored
test: add command tests, add gh action to run tests on PRs, part of #77 (#122)
1 parent d219292 commit 1e1bca3

File tree

8 files changed

+332
-203
lines changed

8 files changed

+332
-203
lines changed

.github/workflows/ci.yml

+29-3
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@ on:
33
push:
44
branches:
55
- main
6-
- v1.x
76
pull_request:
87
branches:
98
- main
10-
- v1.x
119

1210
jobs:
13-
linux:
11+
stylua-check:
1412
runs-on: ubuntu-latest
1513
steps:
1614
- uses: actions/checkout@v1
@@ -20,3 +18,31 @@ jobs:
2018
with:
2119
token: ${{ secrets.GITHUB_TOKEN }}
2220
args: --check lua/
21+
22+
plenary-tests:
23+
runs-on: ubuntu-20.04
24+
steps:
25+
- uses: actions/checkout@v2
26+
- run: date +%F > todays-date
27+
- name: Restore cache for today's nightly.
28+
uses: actions/cache@v2
29+
with:
30+
path: build
31+
key: ${{ runner.os }}-appimage-${{ hashFiles('todays-date') }}
32+
33+
- name: Prepare
34+
run: |
35+
test -d build || {
36+
mkdir -p build
37+
wget https://github.com/neovim/neovim/releases/download/nightly/nvim.appimage
38+
chmod +x nvim.appimage
39+
mv nvim.appimage ./build/nvim
40+
}
41+
git clone https://github.com/MunifTanjim/nui.nvim ~/.local/share/nvim/site/pack/vendor/start/nui.nvim
42+
git clone --depth 1 https://github.com/nvim-lua/plenary.nvim ~/.local/share/nvim/site/pack/vendor/start/plenary.nvim
43+
ln -s $(pwd) ~/.local/share/nvim/site/pack/vendor/start
44+
45+
- name: Run tests
46+
run: |
47+
export PATH="${PWD}/build/:${PATH}"
48+
make test

Makefile

+6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
.PHONY: test
22
test:
3+
@echo ""
4+
@echo "TEST WITH BLANK CONFIG "
35
nvim --headless --noplugin -u tests/mininit.lua -c "PlenaryBustedDirectory tests/neo-tree/ { minimal_init = 'tests/mininit.lua' }"
46

7+
@echo ""
8+
@echo "TEST WITH follow_current_file = true"
9+
nvim --headless --noplugin -u tests/init_follow_current_file.lua -c "PlenaryBustedDirectory tests/neo-tree/ { minimal_init = 'tests/init_follow_current_file.lua' }"
10+
511
.PHONY: format
612
format:
713
stylua ./lua ./tests

lua/neo-tree/sources/filesystem/README.md

-142
This file was deleted.

tests/helpers/util.lua

+24-4
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,40 @@ utils.setup_test_fs = function()
2323
name = "foo",
2424
type = "dir",
2525
content = {
26+
{
27+
name = "bar",
28+
type = "dir",
29+
content = {
30+
{ name = "baz1.txt", type = "file" },
31+
{ name = "baz2.txt", type = "file" },
32+
},
33+
},
2634
{ name = "foofile1.txt", type = "file" },
2735
{ name = "foofile2.txt", type = "file" },
2836
},
2937
},
30-
{ name = "bar", type = "dir" },
31-
{ name = "topfile1.txt", type = "file" },
38+
{ name = "bar", type = "dir", id = "empty_dir" },
39+
{ name = "topfile1.txt", type = "file", id = "topfile1" },
40+
{ name = "topfile2.txt", type = "file", id = "topfile2" },
3241
},
42+
lookup = {},
3343
}
34-
local function makefs(content, basedir)
44+
45+
local function makefs(content, basedir, relative_root_path)
46+
relative_root_path = relative_root_path or "."
3547
for _, info in ipairs(content) do
48+
local relative_path = relative_root_path .. "/" .. info.name
49+
-- create lookups
50+
fs.lookup[relative_path] = info
51+
if info.id then
52+
fs.lookup[info.id] = info
53+
end
54+
-- create actual files and directories
3655
if info.type == "dir" then
3756
info.abspath = Path:new(basedir, info.name):absolute()
3857
vim.fn.mkdir(info.abspath, "p")
3958
if info.content then
40-
makefs(info.content, info.abspath)
59+
makefs(info.content, info.abspath, relative_path)
4160
end
4261
elseif info.type == "file" then
4362
info.abspath = Path:new(basedir, info.name):absolute()
@@ -46,6 +65,7 @@ utils.setup_test_fs = function()
4665
end
4766
end
4867
makefs(fs.content, testdir)
68+
4969
vim.cmd("tcd " .. testdir)
5070
return fs
5171
end

tests/helpers/verify.lua

+74-11
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,46 @@ verify.after = function(timeout, assertfunc, failmsg)
1616
assert(assertfunc(), failmsg)
1717
end
1818

19-
verify.bufnr_is_not = function(start_bufnr, timeout)
19+
verify.bufnr_is = function(bufnr, timeout)
2020
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))
21+
return bufnr == vim.api.nvim_get_current_buf()
22+
end, string.format("Current buffer is expected to be '%s' but is not", bufnr))
23+
end
24+
25+
verify.bufnr_is_not = function(bufnr, timeout)
26+
verify.eventually(timeout or 500, function()
27+
return bufnr ~= vim.api.nvim_get_current_buf()
28+
end, string.format("Current buffer is '%s' when expected to not be", bufnr))
29+
end
30+
31+
verify.buf_name_endswith = function(buf_name, timeout)
32+
verify.eventually(
33+
timeout or 500,
34+
function()
35+
if buf_name == "" then
36+
return true
37+
end
38+
local n = vim.api.nvim_buf_get_name(0)
39+
if n:sub(-#buf_name) == buf_name then
40+
return true
41+
end
42+
end,
43+
string.format("Current buffer name is expected to be end with '%s' but it does not", buf_name)
44+
)
45+
end
46+
47+
verify.buf_name_is = function(buf_name, timeout)
48+
verify.eventually(
49+
timeout or 500,
50+
function()
51+
return buf_name == vim.api.nvim_buf_get_name(0)
52+
end,
53+
string.format(
54+
"Current buffer name is expected to be '%s' but is %s",
55+
buf_name,
56+
vim.api.nvim_buf_get_name(0)
57+
)
58+
)
2359
end
2460

2561
verify.tree_focused = function(timeout)
@@ -28,22 +64,49 @@ verify.tree_focused = function(timeout)
2864
end, "Current buffer is not a 'neo-tree' filetype")
2965
end
3066

31-
verify.tree_node_is = function(expected_node_id, timeout)
67+
verify.tree_node_is = function(source_name, expected_node_id, timeout)
3268
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
69+
local state = require("neo-tree.sources.manager").get_state(source_name)
70+
if not state.tree then
3671
return false
3772
end
38-
local node_id = node:get_id()
39-
if node_id ~= expected_node_id then
73+
local success, node = pcall(state.tree.get_node, state.tree)
74+
if not success then
4075
return false
4176
end
42-
if state.position.node_id ~= expected_node_id then
77+
if not node then
4378
return false
4479
end
45-
return true
80+
local node_id = node:get_id()
81+
if node_id == expected_node_id then
82+
return true
83+
end
84+
return false
4685
end, string.format("Tree node '%s' not focused", expected_node_id))
4786
end
4887

88+
verify.filesystem_tree_node_is = function(expected_node_id, timeout)
89+
verify.tree_node_is("filesystem", expected_node_id, timeout)
90+
end
91+
92+
verify.buffers_tree_node_is = function(expected_node_id, timeout)
93+
verify.tree_node_is("buffers", expected_node_id, timeout)
94+
end
95+
96+
verify.git_status_tree_node_is = function(expected_node_id, timeout)
97+
verify.tree_node_is("git_status", expected_node_id, timeout)
98+
end
99+
100+
verify.window_handle_is = function(winid, timeout)
101+
verify.eventually(timeout or 500, function()
102+
return winid == vim.api.nvim_get_current_win()
103+
end, string.format("Current window handle is expected to be '%s' but is not", winid))
104+
end
105+
106+
verify.window_handle_is_not = function(winid, timeout)
107+
verify.eventually(timeout or 500, function()
108+
return winid ~= vim.api.nvim_get_current_win()
109+
end, string.format("Current window handle is not expected to be '%s' but it is", winid))
110+
end
111+
49112
return verify

tests/init_follow_current_file.lua

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
filesystem = {
15+
follow_current_file = true,
16+
},
17+
})
18+
19+
vim.opt.swapfile = false
20+
21+
vim.cmd([[
22+
runtime plugin/neo-tree.vim
23+
]])
24+
25+
-- For debugging
26+
P = function(...)
27+
print(unpack(vim.tbl_map(vim.inspect, { ... })))
28+
end

0 commit comments

Comments
 (0)