test: add headless test framework

This commit is contained in:
2026-05-07 02:07:35 +02:00
parent 55effc0a67
commit e9d13627c8
10 changed files with 398 additions and 257 deletions
+82
View File
@@ -0,0 +1,82 @@
local cfg = vim.fn.stdpath("config")
package.path = package.path
.. (";" .. cfg .. "/lua/?.lua")
.. (";" .. cfg .. "/lua/?/init.lua")
.. (";" .. cfg .. "/?.lua")
.. (";" .. cfg .. "/?/init.lua")
local opt = vim.fn.stdpath("data") .. "/site/pack/core/opt"
for _, lua_dir in ipairs(vim.fn.glob(opt .. "/*/lua", false, true)) do
package.path = package.path
.. (";" .. lua_dir .. "/?.lua")
.. (";" .. lua_dir .. "/?/init.lua")
end
local t = require("test")
local function gather(target)
local abs = vim.fn.fnamemodify(target, ":p")
if vim.fn.isdirectory(abs) == 1 then
return vim.fn.globpath(abs, "**/*_test.lua", false, true)
end
if vim.fn.filereadable(abs) == 1 then
return { abs }
end
end
local args = arg or {}
local targets = #args > 0 and args or { cfg .. "/test" }
local files = {}
local resolve_failed = false
for _, target in ipairs(targets) do
local matched = gather(target)
if matched then
for _, f in ipairs(matched) do
table.insert(files, f)
end
else
io.stderr:write("no such test target: " .. target .. "\n")
resolve_failed = true
end
end
table.sort(files)
local total_passed, total_failed = 0, 0
for _, f in ipairs(files) do
f = f --[[@as string]]
local label = f:sub(1, #cfg + 1) == cfg .. "/" and f:sub(#cfg + 2) or f
t.start_file(label)
local ok, err = pcall(dofile, f)
if not ok then
t.test("(load)", function()
error(err, 0)
end)
end
local p, fl = t.report()
total_passed = total_passed + p
total_failed = total_failed + fl
end
local function color(code, str)
if vim.env.TEST_COLOR == "1" then
return string.format("\27[%sm%s\27[0m", code, str)
end
return str
end
io.stdout:write("\n")
if total_failed > 0 then
io.stdout:write(
string.format(
"%s, %s\n",
color("32", total_passed .. " passed"),
color("31", total_failed .. " failed")
)
)
os.exit(1)
end
io.stdout:write(color("32", total_passed .. " passed") .. "\n")
if resolve_failed then
os.exit(2)
end