chore(base.lua): refactor test result printing and cleanup

Refactor the code to extract the test result printing logic into a separate function, `print_result_line`, to improve readability and reduce code duplication. This change aims to enhance maintainability by consolidating similar print statements and ensuring consistent display of test results across the script.
This commit is contained in:
Yves-Marie Haussonne 2024-10-04 14:21:06 +02:00
parent 0fafeb9f52
commit ad261f3e41

View File

@ -425,6 +425,20 @@ local all_in_table = function(names, names_list)
return true return true
end end
local print_result_line = function(test)
local s = ":"..test.mod..":"
local rest = s .. test.name
pprint.light_gray(s)
pprint(" ")
pprint(test.name)
pprint(string.rep(" ", 80 - #rest))
if test.result.ok then pprint.green("pass") else pprint.red("FAIL") end
pprint("\n")
if not test.result.ok and test.result.err then
pprint.yellow(" " .. test.result.err .. "\n")
end
end
local display_tests_summary = function() local display_tests_summary = function()
for mod, tests_list in pairs(tests_by_mod) do for mod, tests_list in pairs(tests_by_mod) do
pprint.baby_blue(string.format("%#80s\n", mod)) pprint.baby_blue(string.format("%#80s\n", mod))
@ -434,17 +448,7 @@ local display_tests_summary = function()
pprint.light_gray(":".. test.mod ..":").blue("---- " .. test.name) pprint.light_gray(":".. test.mod ..":").blue("---- " .. test.name)
pprint.blue(string.rep("-", 80 - #s).."\n") pprint.blue(string.rep("-", 80 - #s).."\n")
elseif test.result ~= nil then elseif test.result ~= nil then
local s = ":"..test.mod..":" print_result_line(test)
local rest = s .. test.name
pprint.light_gray(s)
pprint(" ")
pprint(test.name)
pprint(string.rep(" ", 80 - #rest))
if test.result.ok then pprint.green("pass") else pprint.red("FAIL") end
pprint("\n")
if not test.result.ok and test.result.err then
pprint.yellow(" " .. test.result.err .. "\n")
end
else else
local s = ":"..test.mod..":" local s = ":"..test.mod..":"
local rest = s .. test.name local rest = s .. test.name
@ -504,12 +508,6 @@ test_harness.restore_players = function(players_data)
end end
local set_tests_done = function()
end
local get_connected_player_names = function() local get_connected_player_names = function()
local connected_player_names = {} local connected_player_names = {}
for _, p in ipairs(minetest.get_connected_players()) do for _, p in ipairs(minetest.get_connected_players()) do
@ -520,8 +518,8 @@ end
test_harness.run_player_tests = function(list_player_tests, area) test_harness.run_player_tests = function(list_player_tests, area)
local connected_player_names = get_connected_player_names()
for _, test in ipairs(list_player_tests) do for _, test in ipairs(list_player_tests) do
local connected_player_names = get_connected_player_names()
if not test.result and if not test.result and
test.func and test.func and
test.players and test.players and
@ -532,10 +530,9 @@ test_harness.run_player_tests = function(list_player_tests, area)
area.clear() area.clear()
local ok, err = pcall(test.func) local ok, err = pcall(test.func)
test.result = { ok = ok, err = err } test.result = { ok = ok, err = err }
print(string.format(":%s:%-60s %s", test.mod, test.name, ok and "pass" or "FAIL")) print_result_line(test)
test_harness.restore_players(player_data) test_harness.restore_players(player_data)
if not ok then if not ok then
print(" " .. err)
failed = failed + 1 failed = failed + 1
if minetest.settings:get_bool("test_harness_failfast", false) then if minetest.settings:get_bool("test_harness_failfast", false) then
tests_state = TESTS_STATE_ENUM.DONE tests_state = TESTS_STATE_ENUM.DONE
@ -633,9 +630,8 @@ local run_tests = function()
area.clear() area.clear()
local ok, err = pcall(test.func) local ok, err = pcall(test.func)
test.result = { ok = ok, err = err } test.result = { ok = ok, err = err }
print(string.format(":%s:%-60s %s", test.mod, test.name, ok and "pass" or "FAIL")) print_result_line(test)
if not ok then if not ok then
print(" " .. err)
failed = failed + 1 failed = failed + 1
if minetest.settings:get_bool("test_harness_failfast", false) then if minetest.settings:get_bool("test_harness_failfast", false) then
tests_state = TESTS_STATE_ENUM.DONE tests_state = TESTS_STATE_ENUM.DONE
@ -647,9 +643,16 @@ local run_tests = function()
print("Server tests done, " .. failed .. " tests failed.") print("Server tests done, " .. failed .. " tests failed.")
if next(players_tests) == nil or tests_state == TESTS_STATE_ENUM.DONE then if next(players_tests) == nil then
set_tests_done() tests_state = TESTS_STATE_ENUM.DONE
return end
if tests_state == TESTS_STATE_ENUM.DONE then
print("All tests done, " .. failed .. " tests failed.")
display_tests_summary()
if minetest.settings:get_bool("test_harness_stop_server", true) then
request_shutdown()
end
end end
-- list of needed players -- list of needed players