1
0
forked from a/repotool
repotool/test.lua

111 lines
2.8 KiB
Lua
Raw Normal View History

2025-07-06 21:40:46 +00:00
#!/usr/bin/env lua
-- Test runner for repotool
-- This script discovers and runs all test files in the test/ directory
-- Add src to package path
package.path = package.path .. ";./src/?.lua"
-- ANSI color codes
local colors = {
green = "\27[32m",
red = "\27[31m",
yellow = "\27[33m",
reset = "\27[0m"
}
-- Test statistics
local stats = {
total = 0,
passed = 0,
failed = 0,
errors = {}
}
-- Simple test framework
_G.test = {}
_G.test.current_suite = nil
_G.test.current_test = nil
function _G.test.suite(name, fn)
_G.test.current_suite = name
print("\n" .. colors.yellow .. "Testing " .. name .. colors.reset)
fn()
_G.test.current_suite = nil
end
function _G.test.case(name, fn)
_G.test.current_test = name
stats.total = stats.total + 1
local status, err = pcall(fn)
if status then
stats.passed = stats.passed + 1
print(" " .. colors.green .. "" .. colors.reset .. " " .. name)
else
stats.failed = stats.failed + 1
print(" " .. colors.red .. "" .. colors.reset .. " " .. name)
table.insert(stats.errors, {
suite = _G.test.current_suite,
test = name,
error = err
})
end
_G.test.current_test = nil
end
function _G.assert_eq(actual, expected, message)
if actual ~= expected then
error((message or "Assertion failed") .. "\n Expected: " .. tostring(expected) .. "\n Actual: " .. tostring(actual))
end
end
function _G.assert_nil(value, message)
if value ~= nil then
error((message or "Expected nil") .. "\n Got: " .. tostring(value))
end
end
function _G.assert_not_nil(value, message)
if value == nil then
error(message or "Expected non-nil value")
end
end
-- Discover and run test files
local function run_tests()
local handle = io.popen("find test -name '*.lua' -type f | sort")
local test_files = handle:read("*a")
handle:close()
for file in test_files:gmatch("[^\n]+") do
-- Skip the test.lua file itself
if not file:match("test%.lua$") then
dofile(file)
end
end
end
-- Main
print(colors.yellow .. "Running repotool tests..." .. colors.reset)
run_tests()
-- Print summary
print("\n" .. string.rep("-", 40))
print("Test Summary:")
print(" Total: " .. stats.total)
print(" " .. colors.green .. "Passed: " .. stats.passed .. colors.reset)
print(" " .. colors.red .. "Failed: " .. stats.failed .. colors.reset)
-- Print errors if any
if #stats.errors > 0 then
print("\n" .. colors.red .. "Failures:" .. colors.reset)
for _, err in ipairs(stats.errors) do
print("\n " .. err.suite .. " > " .. err.test)
print(" " .. err.error:gsub("\n", "\n "))
end
end
-- Exit with appropriate code
os.exit(stats.failed > 0 and 1 or 0)