From ad261f3e416b9cec6452925818f6c49d209167e4 Mon Sep 17 00:00:00 2001 From: Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com> Date: Fri, 4 Oct 2024 14:21:06 +0200 Subject: [PATCH] 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. --- base.lua | 53 ++++++++++++++++++++++++++++------------------------- 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/base.lua b/base.lua index 723e162..d9fb553 100644 --- a/base.lua +++ b/base.lua @@ -425,6 +425,20 @@ local all_in_table = function(names, names_list) return true 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() for mod, tests_list in pairs(tests_by_mod) do 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.blue(string.rep("-", 80 - #s).."\n") elseif test.result ~= nil then - 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 + print_result_line(test) else local s = ":"..test.mod..":" local rest = s .. test.name @@ -504,12 +508,6 @@ test_harness.restore_players = function(players_data) end - - -local set_tests_done = function() - -end - local get_connected_player_names = function() local connected_player_names = {} for _, p in ipairs(minetest.get_connected_players()) do @@ -520,8 +518,8 @@ end 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 + local connected_player_names = get_connected_player_names() if not test.result and test.func and test.players and @@ -532,10 +530,9 @@ test_harness.run_player_tests = function(list_player_tests, area) area.clear() local ok, err = pcall(test.func) 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) if not ok then - print(" " .. err) failed = failed + 1 if minetest.settings:get_bool("test_harness_failfast", false) then tests_state = TESTS_STATE_ENUM.DONE @@ -633,9 +630,8 @@ local run_tests = function() area.clear() local ok, err = pcall(test.func) 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 - print(" " .. err) failed = failed + 1 if minetest.settings:get_bool("test_harness_failfast", false) then tests_state = TESTS_STATE_ENUM.DONE @@ -647,9 +643,16 @@ local run_tests = function() print("Server tests done, " .. failed .. " tests failed.") - if next(players_tests) == nil or tests_state == TESTS_STATE_ENUM.DONE then - set_tests_done() - return + if next(players_tests) == nil then + tests_state = TESTS_STATE_ENUM.DONE + 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 -- list of needed players