Meh I'll figure out submodules later

This commit is contained in:
mustard 2025-09-16 01:01:02 +02:00
parent 4ca9d44a90
commit 8cb281f436
352 changed files with 66107 additions and 0 deletions

View file

@ -0,0 +1,94 @@
local Path = require("plenary.path")
local fs = {}
function fs.create_temp_dir()
-- Resolve for two reasons.
-- 1. Follow any symlinks which make comparing paths fail. (on macOS, TMPDIR can be under /var which is symlinked to
-- /private/var)
-- 2. Remove any double separators (on macOS TMPDIR can end in a trailing / which absolute doesn't remove, this should
-- be coverted by https://github.com/nvim-lua/plenary.nvim/issues/330).
local temp_dir = vim.fn.resolve(
Path:new(
vim.fn.fnamemodify(vim.fn.tempname(), ":h"),
string.format("neo-tree-test-%s", vim.fn.rand())
):absolute()
)
vim.fn.mkdir(temp_dir, "p")
return temp_dir
end
function fs.create_dir(path)
local abspath = Path:new(path):absolute()
vim.fn.mkdir(abspath, "p")
end
function fs.remove_dir(dir, recursive)
if vim.fn.isdirectory(dir) == 1 then
return vim.fn.delete(dir, recursive and "rf" or "d") == 0
end
return false
end
function fs.write_file(path, content)
local abspath = Path:new(path):absolute()
fs.create_dir(vim.fn.fnamemodify(abspath, ":h"))
vim.fn.writefile(content or {}, abspath)
end
function fs.create_fs_tree(fs_tree)
local function create_items(items, basedir, relative_root_path)
relative_root_path = relative_root_path or "."
for _, item in ipairs(items) do
local relative_path = relative_root_path .. "/" .. item.name
-- create lookups
fs_tree.lookup[relative_path] = item
if item.id then
fs_tree.lookup[item.id] = item
end
-- create actual files and directories
if item.type == "dir" then
item.abspath = Path:new(basedir, item.name):absolute()
fs.create_dir(item.abspath)
if item.items then
create_items(item.items, item.abspath, relative_path)
end
elseif item.type == "file" then
item.abspath = Path:new(basedir, item.name):absolute()
fs.write_file(item.abspath)
end
end
end
create_items(fs_tree.items, fs_tree.abspath)
return fs_tree
end
function fs.init_test(fs_tree)
fs_tree.lookup = {}
if not fs_tree.abspath then
fs_tree.abspath = fs.create_temp_dir()
end
local function setup()
fs.remove_dir(fs_tree.abspath, true)
fs.create_fs_tree(fs_tree)
vim.cmd("tcd " .. fs_tree.abspath)
end
local function teardown()
fs.remove_dir(fs_tree.abspath, true)
end
return {
fs_tree = fs_tree,
setup = setup,
teardown = teardown,
}
end
return fs

View file

@ -0,0 +1,191 @@
local mod = {
fs = require("tests.utils.fs"),
}
function mod.clear_environment()
-- Create fresh window
vim.cmd("top new | wincmd o")
local keepbufnr = vim.api.nvim_get_current_buf()
-- Clear ALL neo-tree state
require("neo-tree.sources.manager")._clear_state()
-- Cleanup any remaining buffers
for _, bufnr in ipairs(vim.api.nvim_list_bufs()) do
if bufnr ~= keepbufnr then
vim.api.nvim_buf_delete(bufnr, { force = true })
end
end
assert(#vim.api.nvim_tabpage_list_wins(0) == 1, "Failed to properly clear tab")
assert(#vim.api.nvim_list_bufs() == 1, "Failed to properly clear buffers")
end
mod.editfile = function(testfile)
vim.cmd("e " .. testfile)
assert.are.same(
vim.fn.fnamemodify(vim.api.nvim_buf_get_name(0), ":p"),
vim.fn.fnamemodify(testfile, ":p")
)
end
function mod.eq(...)
return assert.are.same(...)
end
function mod.neq(...)
return assert["not"].are.same(...)
end
---@param keys string
---@param mode? string
function mod.feedkeys(keys, mode)
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(keys, true, false, true), mode or "x", true)
end
---@param tbl table
---@param keys string[]
function mod.tbl_pick(tbl, keys)
if not keys or #keys == 0 then
return tbl
end
local new_tbl = {}
for _, key in ipairs(keys) do
new_tbl[key] = tbl[key]
end
return new_tbl
end
local orig_require = _G.require
-- can be used to enable/disable package
-- for specific tests
function mod.get_require_switch()
local disabled_packages = {}
local function fake_require(name)
if vim.tbl_contains(disabled_packages, name) then
return error("test: package disabled")
end
return orig_require(name)
end
return {
disable_package = function(name)
_G.require = fake_require
package.loaded[name] = nil
table.insert(disabled_packages, name)
end,
enable_package = function(name)
_G.require = fake_require
disabled_packages = vim.tbl_filter(function(package_name)
return package_name ~= name
end, disabled_packages)
end,
restore = function()
disabled_packages = {}
_G.require = orig_require
end,
}
end
---@param bufnr number
---@param lines string[]
---@param linenr_start? integer (1-indexed)
---@param linenr_end? integer (1-indexed, inclusive)
function mod.assert_buf_lines(bufnr, lines, linenr_start, linenr_end)
mod.eq(
lines,
vim.api.nvim_buf_get_lines(
bufnr,
linenr_start and linenr_start - 1 or 0,
linenr_end or -1,
false
)
)
end
---@param bufnr number
---@param ns_id integer
---@param linenr integer (1-indexed)
---@param byte_start? integer (0-indexed)
---@param byte_end? integer (0-indexed, inclusive)
function mod.get_line_extmarks(bufnr, ns_id, linenr, byte_start, byte_end)
return vim.api.nvim_buf_get_extmarks(
bufnr,
ns_id,
{ linenr - 1, byte_start or 0 },
{ linenr - 1, byte_end and byte_end + 1 or -1 },
{ details = true }
)
end
---@param bufnr number
---@param ns_id integer
---@param linenr integer (1-indexed)
---@param text string
---@return table[]
---@return { byte_start: integer, byte_end: integer } info (byte range: 0-indexed, inclusive)
function mod.get_text_extmarks(bufnr, ns_id, linenr, text)
local line = vim.api.nvim_buf_get_lines(bufnr, linenr - 1, linenr, false)[1]
local byte_start = string.find(line, text) -- 1-indexed
byte_start = byte_start - 1 -- 0-indexed
local byte_end = byte_start + #text - 1 -- inclusive
local extmarks = vim.api.nvim_buf_get_extmarks(
bufnr,
ns_id,
{ linenr - 1, byte_start },
{ linenr - 1, byte_end },
{ details = true }
)
return extmarks, { byte_start = byte_start, byte_end = byte_end }
end
---@param extmark table
---@param linenr number (1-indexed)
---@param text string
---@param hl_group string
function mod.assert_extmark(extmark, linenr, text, hl_group)
mod.eq(extmark[2], linenr - 1)
if text then
local start_col = extmark[3]
mod.eq(extmark[4].end_col - start_col, #text)
end
mod.eq(mod.tbl_pick(extmark[4], { "end_row", "hl_group" }), {
end_row = linenr - 1,
hl_group = hl_group,
})
end
---@param bufnr number
---@param ns_id integer
---@param linenr integer (1-indexed)
---@param text string
---@param hl_group string
function mod.assert_highlight(bufnr, ns_id, linenr, text, hl_group)
local extmarks, info = mod.get_text_extmarks(bufnr, ns_id, linenr, text)
mod.eq(#extmarks, 1)
mod.eq(extmarks[1][3], info.byte_start)
mod.assert_extmark(extmarks[1], linenr, text, hl_group)
end
---@param callback fun(): boolean
---@param options? { interval?: integer, timeout?: integer }
function mod.wait_for(callback, options)
options = options or {}
vim.wait(options.timeout or 1000, callback, options.interval or 100)
end
---@param options? { interval?: integer, timeout?: integer }
function mod.wait_for_neo_tree(options)
local verify = require("tests.utils.verify")
mod.wait_for(function()
return verify.get_state() ~= nil
end, options)
end
return mod

View file

@ -0,0 +1,145 @@
local verify = {}
verify.eventually = function(timeout, assertfunc, failmsg, ...)
local success, args = false, { ... }
vim.wait(timeout or 1000, function()
success = assertfunc(unpack(args))
return success
end)
assert(success, failmsg)
end
local id = 0
---Waits until the next vim.schedule before running assertfunc
verify.schedule = function(assertfunc, timeout, failmsg)
id = id + 1
local scheduled_func_ran = false
local success = false
local args
vim.schedule(function()
args = { assertfunc() }
success = args[1]
scheduled_func_ran = true
end)
local notimeout, errcode = vim.wait(timeout or 1000, function()
return scheduled_func_ran
end)
assert(success, failmsg)
end
verify.after = function(timeout, assertfunc, failmsg)
vim.wait(timeout, function()
return false
end)
assert(assertfunc(), failmsg)
end
verify.bufnr_is = function(bufnr, timeout)
verify.eventually(timeout or 500, function()
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
else
return false
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 not", buf_name))
end
verify.tree_focused = function(timeout)
verify.eventually(timeout or 1000, function()
if not verify.get_state() then
return false
end
return vim.bo[0].filetype == "neo-tree"
end, "Current buffer is not a 'neo-tree' filetype")
end
verify.get_state = function(source_name, winid)
if source_name == nil then
local success
success, source_name = pcall(vim.api.nvim_buf_get_var, 0, "neo_tree_source")
if not success then
return nil
end
end
local state = require("neo-tree.sources.manager").get_state(source_name, nil, winid)
if not state.tree then
return nil
end
if not state._ready then
return nil
end
return state
end
verify.tree_node_is = function(source_name, expected_node_id, winid, timeout)
verify.eventually(timeout or 500, function()
local state = verify.get_state(source_name, winid)
if not state then
return false
end
local success, node = pcall(state.tree.get_node, state.tree)
if not success then
return false
end
if not node then
return false
end
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, winid, timeout)
verify.tree_node_is("filesystem", expected_node_id, winid, timeout)
end
verify.buffers_tree_node_is = function(expected_node_id, winid, timeout)
verify.tree_node_is("buffers", expected_node_id, winid, timeout)
end
verify.git_status_tree_node_is = function(expected_node_id, winid, timeout)
verify.tree_node_is("git_status", expected_node_id, winid, 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