Skip to content

test: add command tests, add gh action to run tests on PRs, part of #77 #122

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ on:
push:
branches:
- main
- v1.x
pull_request:
branches:
- main
- v1.x

jobs:
linux:
stylua-check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
Expand All @@ -20,3 +18,31 @@ jobs:
with:
token: ${{ secrets.GITHUB_TOKEN }}
args: --check lua/

plenary-tests:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2
- run: date +%F > todays-date
- name: Restore cache for today's nightly.
uses: actions/cache@v2
with:
path: build
key: ${{ runner.os }}-appimage-${{ hashFiles('todays-date') }}

- name: Prepare
run: |
test -d build || {
mkdir -p build
wget https://github.com/neovim/neovim/releases/download/nightly/nvim.appimage
chmod +x nvim.appimage
mv nvim.appimage ./build/nvim
}
git clone https://github.com/MunifTanjim/nui.nvim ~/.local/share/nvim/site/pack/vendor/start/nui.nvim
git clone --depth 1 https://github.com/nvim-lua/plenary.nvim ~/.local/share/nvim/site/pack/vendor/start/plenary.nvim
ln -s $(pwd) ~/.local/share/nvim/site/pack/vendor/start

- name: Run tests
run: |
export PATH="${PWD}/build/:${PATH}"
make test
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
.PHONY: test
test:
@echo ""
@echo "TEST WITH BLANK CONFIG "
nvim --headless --noplugin -u tests/mininit.lua -c "PlenaryBustedDirectory tests/neo-tree/ { minimal_init = 'tests/mininit.lua' }"

@echo ""
@echo "TEST WITH follow_current_file = true"
nvim --headless --noplugin -u tests/init_follow_current_file.lua -c "PlenaryBustedDirectory tests/neo-tree/ { minimal_init = 'tests/init_follow_current_file.lua' }"

.PHONY: format
format:
stylua ./lua ./tests
142 changes: 0 additions & 142 deletions lua/neo-tree/sources/filesystem/README.md

This file was deleted.

28 changes: 24 additions & 4 deletions tests/helpers/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,40 @@ utils.setup_test_fs = function()
name = "foo",
type = "dir",
content = {
{
name = "bar",
type = "dir",
content = {
{ name = "baz1.txt", type = "file" },
{ name = "baz2.txt", type = "file" },
},
},
{ name = "foofile1.txt", type = "file" },
{ name = "foofile2.txt", type = "file" },
},
},
{ name = "bar", type = "dir" },
{ name = "topfile1.txt", type = "file" },
{ name = "bar", type = "dir", id = "empty_dir" },
{ name = "topfile1.txt", type = "file", id = "topfile1" },
{ name = "topfile2.txt", type = "file", id = "topfile2" },
},
lookup = {},
}
local function makefs(content, basedir)

local function makefs(content, basedir, relative_root_path)
relative_root_path = relative_root_path or "."
for _, info in ipairs(content) do
local relative_path = relative_root_path .. "/" .. info.name
-- create lookups
fs.lookup[relative_path] = info
if info.id then
fs.lookup[info.id] = info
end
-- create actual files and directories
if info.type == "dir" then
info.abspath = Path:new(basedir, info.name):absolute()
vim.fn.mkdir(info.abspath, "p")
if info.content then
makefs(info.content, info.abspath)
makefs(info.content, info.abspath, relative_path)
end
elseif info.type == "file" then
info.abspath = Path:new(basedir, info.name):absolute()
Expand All @@ -46,6 +65,7 @@ utils.setup_test_fs = function()
end
end
makefs(fs.content, testdir)

vim.cmd("tcd " .. testdir)
return fs
end
Expand Down
85 changes: 74 additions & 11 deletions tests/helpers/verify.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,46 @@ verify.after = function(timeout, assertfunc, failmsg)
assert(assertfunc(), failmsg)
end

verify.bufnr_is_not = function(start_bufnr, timeout)
verify.bufnr_is = function(bufnr, timeout)
verify.eventually(timeout or 500, function()
return start_bufnr ~= vim.api.nvim_get_current_buf()
end, string.format("Current buffer is '%s' when expected to not be", start_bufnr))
return bufnr == vim.api.nvim_get_current_buf()
end, string.format("Current buffer is expected to be '%s' but is not", bufnr))
end

verify.bufnr_is_not = function(bufnr, timeout)
verify.eventually(timeout or 500, function()
return bufnr ~= vim.api.nvim_get_current_buf()
end, string.format("Current buffer is '%s' when expected to not be", bufnr))
end

verify.buf_name_endswith = function(buf_name, timeout)
verify.eventually(
timeout or 500,
function()
if buf_name == "" then
return true
end
local n = vim.api.nvim_buf_get_name(0)
if n:sub(-#buf_name) == buf_name then
return true
end
end,
string.format("Current buffer name is expected to be end with '%s' but it does not", buf_name)
)
end

verify.buf_name_is = function(buf_name, timeout)
verify.eventually(
timeout or 500,
function()
return buf_name == vim.api.nvim_buf_get_name(0)
end,
string.format(
"Current buffer name is expected to be '%s' but is %s",
buf_name,
vim.api.nvim_buf_get_name(0)
)
)
end

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

verify.tree_node_is = function(expected_node_id, timeout)
verify.tree_node_is = function(source_name, expected_node_id, timeout)
verify.eventually(timeout or 500, function()
local state = require("neo-tree.sources.manager").get_state("filesystem")
local node = state.tree:get_node()
if not node then
local state = require("neo-tree.sources.manager").get_state(source_name)
if not state.tree then
return false
end
local node_id = node:get_id()
if node_id ~= expected_node_id then
local success, node = pcall(state.tree.get_node, state.tree)
if not success then
return false
end
if state.position.node_id ~= expected_node_id then
if not node then
return false
end
return true
local node_id = node:get_id()
if node_id == expected_node_id then
return true
end
return false
end, string.format("Tree node '%s' not focused", expected_node_id))
end

verify.filesystem_tree_node_is = function(expected_node_id, timeout)
verify.tree_node_is("filesystem", expected_node_id, timeout)
end

verify.buffers_tree_node_is = function(expected_node_id, timeout)
verify.tree_node_is("buffers", expected_node_id, timeout)
end

verify.git_status_tree_node_is = function(expected_node_id, timeout)
verify.tree_node_is("git_status", expected_node_id, timeout)
end

verify.window_handle_is = function(winid, timeout)
verify.eventually(timeout or 500, function()
return winid == vim.api.nvim_get_current_win()
end, string.format("Current window handle is expected to be '%s' but is not", winid))
end

verify.window_handle_is_not = function(winid, timeout)
verify.eventually(timeout or 500, function()
return winid ~= vim.api.nvim_get_current_win()
end, string.format("Current window handle is not expected to be '%s' but it is", winid))
end

return verify
28 changes: 28 additions & 0 deletions tests/init_follow_current_file.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-- Need the absolute path as when doing the testing we will issue things like `tcd` to change directory
-- to where our temporary filesystem lives
vim.opt.rtp = {
vim.fn.fnamemodify(vim.trim(vim.fn.system("git rev-parse --show-toplevel")), ":p"),
vim.env.VIMRUNTIME,
}

vim.cmd([[
packadd plenary.nvim
packadd nui.nvim
]])

require("neo-tree").setup({
filesystem = {
follow_current_file = true,
},
})

vim.opt.swapfile = false

vim.cmd([[
runtime plugin/neo-tree.vim
]])

-- For debugging
P = function(...)
print(unpack(vim.tbl_map(vim.inspect, { ... })))
end
Loading