local requested_test = arg and arg[2]
if requested_test then
requested_test = requested_test:gsub("%.lua$", "")
end
print("=" .. string.rep("=", 60))
if requested_test then
print("Running POSIX Lua Function Tests matching: " .. requested_test)
else
print("Running POSIX Lua Function Tests")
end
print("=" .. string.rep("=", 60))
local script_dir
if arg and arg[1] then
script_dir = arg[1]:match("(.*/)")
end
if not script_dir then
script_dir = "./"
end
local lua_tests_dir = script_dir .. "lua/"
local tests = {}
local entries = posix.dir(lua_tests_dir)
if entries then
for i, entry in ipairs(entries) do
if entry:match("^test_.*%.lua$") and entry ~= "test_all.lua" then
local test_name = entry:match("^test_(.*)%.lua$")
if test_name then
table.insert(tests, "test_" .. test_name)
end
end
end
table.sort(tests)
end
if #tests == 0 then
print("ERROR: No test files found in " .. lua_tests_dir)
error("No tests found")
end
local tests_to_run = {}
if requested_test then
local found = false
for _, test_name in ipairs(tests) do
if test_name:find(requested_test, 1, true) then
table.insert(tests_to_run, test_name)
found = true
end
end
if not found then
print("ERROR: No tests matching pattern '" .. requested_test .. "' found. Available tests:")
for _, test_name in ipairs(tests) do
print(" " .. test_name:sub(6))
end
error("No tests found matching pattern: " .. requested_test)
end
else
tests_to_run = tests
end
print("Found " .. #tests .. " test files")
if requested_test then
print("Running " .. #tests_to_run .. " test(s) matching: " .. requested_test)
end
print("")
local test_dir = "/tmp/epkg_posix_test"
os.execute("mkdir -p " .. test_dir)
local failed = 0
local passed = 0
for _, test_name in ipairs(tests_to_run) do
local test_file = lua_tests_dir .. test_name .. ".lua"
local success, err = pcall(function()
dofile(test_file)
end)
if success then
print("PASS: " .. test_name)
passed = passed + 1
else
print("FAIL: " .. test_name .. " - " .. tostring(err))
failed = failed + 1
end
end
print("\n" .. string.rep("=", 60))
print(string.format("Results: %d passed, %d failed", passed, failed))
print(string.rep("=", 60))
if posix.stat(test_dir) ~= nil then
local entries = posix.dir(test_dir)
for i, entry in ipairs(entries) do
if entry ~= "." and entry ~= ".." then
local path = test_dir .. "/" .. entry
local stat = posix.stat(path)
if stat ~= nil then
if stat.type == "directory" then
posix.rmdir(path)
else
posix.unlink(path)
end
end
end
end
posix.rmdir(test_dir)
end
if failed > 0 then
error(string.format("Tests failed: %d passed, %d failed", passed, failed))
end