Files

93 lines
2.3 KiB
Lua

local cfg = vim.fn.stdpath("config")
if type(cfg) == "table" then
cfg = assert(cfg[1])
end
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
for _, f in ipairs(vim.fn.glob(cfg .. "/plugin/*.lua", false, true)) do
dofile(f)
end
local t = require("test")
---@param target string
---@return string[]?
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 targets = (arg and #arg > 0) and arg or { cfg .. "/test" }
---@type string[]
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
local label = f
if f:sub(1, #cfg + 1) == cfg .. "/" then
label = f:sub(#cfg + 2)
end
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