From 133f706bf3e4c001b8f42179af3f070cc1940d1e Mon Sep 17 00:00:00 2001 From: sfan5 Date: Wed, 10 Jan 2024 19:10:58 +0100 Subject: [PATCH] Make unittests less reliant on files in the source distribution --- src/unittest/CMakeLists.txt | 9 --- src/unittest/helpers/helper_moveaction.lua | 15 ----- src/unittest/mock_server.h | 7 +- src/unittest/test_config.h.in | 8 --- src/unittest/test_filesys.cpp | 2 +- src/unittest/test_mod/test_mod/init.lua | 1 - src/unittest/test_mod/test_mod/mod.conf | 2 - src/unittest/test_moveaction.cpp | 31 +++++++-- src/unittest/test_server_shutdown_state.cpp | 1 - src/unittest/test_servermodmanager.cpp | 73 +++++++++++---------- src/unittest/test_world/do_not_remove.txt | 0 11 files changed, 70 insertions(+), 79 deletions(-) delete mode 100644 src/unittest/helpers/helper_moveaction.lua delete mode 100644 src/unittest/test_config.h.in delete mode 100644 src/unittest/test_mod/test_mod/init.lua delete mode 100644 src/unittest/test_mod/test_mod/mod.conf delete mode 100644 src/unittest/test_world/do_not_remove.txt diff --git a/src/unittest/CMakeLists.txt b/src/unittest/CMakeLists.txt index fd47358a2..84c545459 100644 --- a/src/unittest/CMakeLists.txt +++ b/src/unittest/CMakeLists.txt @@ -47,12 +47,3 @@ set (UNITTEST_CLIENT_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/test_keycode.cpp PARENT_SCOPE) -set (TEST_WORLDDIR ${CMAKE_CURRENT_SOURCE_DIR}/test_world) -set (TEST_SUBGAME_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../../games/devtest) -set (TEST_MOD_PATH ${CMAKE_CURRENT_SOURCE_DIR}/test_mod) -set (HELPERS_PATH ${CMAKE_CURRENT_SOURCE_DIR}/helpers) - -configure_file( - "${CMAKE_CURRENT_SOURCE_DIR}/test_config.h.in" - "${PROJECT_BINARY_DIR}/test_config.h" -) diff --git a/src/unittest/helpers/helper_moveaction.lua b/src/unittest/helpers/helper_moveaction.lua deleted file mode 100644 index edb950e81..000000000 --- a/src/unittest/helpers/helper_moveaction.lua +++ /dev/null @@ -1,15 +0,0 @@ -minetest.register_allow_player_inventory_action(function(player, action, inventory, info) - if action == "move" then - return info.count - end - - if info.stack:get_name() == "default:water" then - return 0 - end - - if info.stack:get_name() == "default:lava" then - return 5 - end - - return info.stack:get_count() -end) diff --git a/src/unittest/mock_server.h b/src/unittest/mock_server.h index d39ffd5b5..4c0e5fe16 100644 --- a/src/unittest/mock_server.h +++ b/src/unittest/mock_server.h @@ -22,8 +22,11 @@ with this program; if not, write to the Free Software Foundation, Inc., class MockServer : public Server { public: - MockServer() : Server(TEST_WORLDDIR, SubgameSpec("fakespec", "fakespec"), true, - Address(), true, nullptr) + /* Set path_world to a real existing folder if you plan to initialize scripting! */ + MockServer(const std::string &path_world = "fakepath") : + Server(path_world, SubgameSpec("fakespec", "fakespec"), true, + Address(), true, nullptr + ) {} private: diff --git a/src/unittest/test_config.h.in b/src/unittest/test_config.h.in deleted file mode 100644 index cb04b1fe3..000000000 --- a/src/unittest/test_config.h.in +++ /dev/null @@ -1,8 +0,0 @@ -// Filled in by the build system - -#pragma once - -#define TEST_WORLDDIR "@TEST_WORLDDIR@" -#define TEST_SUBGAME_PATH "@TEST_SUBGAME_PATH@" -#define TEST_MOD_PATH "@TEST_MOD_PATH@" -#define HELPERS_PATH "@HELPERS_PATH@" diff --git a/src/unittest/test_filesys.cpp b/src/unittest/test_filesys.cpp index 8b63d083f..dde541a7c 100644 --- a/src/unittest/test_filesys.cpp +++ b/src/unittest/test_filesys.cpp @@ -269,7 +269,7 @@ void TestFileSys::testRemoveRelativePathComponent() void TestFileSys::testSafeWriteToFile() { - const std::string dest_path = fs::TempPath() + DIR_DELIM + "testSafeWriteToFile.txt"; + const std::string dest_path = getTestTempFile(); const std::string test_data("hello\0world", 11); fs::safeWriteToFile(dest_path, test_data); UASSERT(fs::PathExists(dest_path)); diff --git a/src/unittest/test_mod/test_mod/init.lua b/src/unittest/test_mod/test_mod/init.lua deleted file mode 100644 index 724a863f5..000000000 --- a/src/unittest/test_mod/test_mod/init.lua +++ /dev/null @@ -1 +0,0 @@ --- deliberately empty diff --git a/src/unittest/test_mod/test_mod/mod.conf b/src/unittest/test_mod/test_mod/mod.conf deleted file mode 100644 index 56c64b2d8..000000000 --- a/src/unittest/test_mod/test_mod/mod.conf +++ /dev/null @@ -1,2 +0,0 @@ -name = test_mod -description = A mod doing nothing, to test if MINETEST_MOD_PATH is recognised diff --git a/src/unittest/test_moveaction.cpp b/src/unittest/test_moveaction.cpp index fb11faff8..62a5237a3 100644 --- a/src/unittest/test_moveaction.cpp +++ b/src/unittest/test_moveaction.cpp @@ -18,13 +18,12 @@ with this program; if not, write to the Free Software Foundation, Inc., */ #include "test.h" -#include "test_config.h" #include "mock_inventorymanager.h" #include "mock_server.h" #include "mock_serveractiveobject.h" -#include +#include "scripting_server.h" class TestMoveAction : public TestBase @@ -47,16 +46,36 @@ public: static TestMoveAction g_test_instance; +const static char *helper_lua_src = R"( +core.register_allow_player_inventory_action(function(player, action, inventory, info) + if action == "move" then + return info.count + end + + if info.stack:get_name() == "default:water" then + return 0 + end + if info.stack:get_name() == "default:lava" then + return 5 + end + + return info.stack:get_count() +end) +)"; + void TestMoveAction::runTests(IGameDef *gamedef) { - MockServer server; + MockServer server(getTestTempDirectory()); + + const auto helper_lua = getTestTempFile(); + std::ofstream ofs(helper_lua, std::ios::out | std::ios::binary); + ofs << helper_lua_src; + ofs.close(); ServerScripting server_scripting(&server); try { server_scripting.loadMod(Server::getBuiltinLuaPath() + DIR_DELIM "init.lua", BUILTIN_MOD_NAME); - server_scripting.loadMod( - std::string(HELPERS_PATH) + DIR_DELIM "helper_moveaction.lua", BUILTIN_MOD_NAME - ); + server_scripting.loadMod(helper_lua, BUILTIN_MOD_NAME); } catch (ModError &e) { // Print backtrace in case of syntax errors rawstream << e.what() << std::endl; diff --git a/src/unittest/test_server_shutdown_state.cpp b/src/unittest/test_server_shutdown_state.cpp index 679030696..b52fbcb82 100644 --- a/src/unittest/test_server_shutdown_state.cpp +++ b/src/unittest/test_server_shutdown_state.cpp @@ -18,7 +18,6 @@ with this program; if not, write to the Free Software Foundation, Inc., */ #include "test.h" -#include "test_config.h" #include "mock_server.h" diff --git a/src/unittest/test_servermodmanager.cpp b/src/unittest/test_servermodmanager.cpp index e33ca1019..e9f10b2c1 100644 --- a/src/unittest/test_servermodmanager.cpp +++ b/src/unittest/test_servermodmanager.cpp @@ -21,9 +21,10 @@ with this program; if not, write to the Free Software Foundation, Inc., #include #include "server/mods.h" #include "settings.h" -#include "test_config.h" #include "util/string.h" +#define SUBGAME_ID "devtest" + class TestServerModManager : public TestBase { public: @@ -32,6 +33,8 @@ public: void runTests(IGameDef *gamedef); + std::string m_worlddir; + void testCreation(); void testIsConsistent(); void testUnsatisfiedMods(); @@ -48,17 +51,35 @@ static TestServerModManager g_test_instance; void TestServerModManager::runTests(IGameDef *gamedef) { - const char *saved_env_mt_mod_path = getenv("MINETEST_MOD_PATH"); + if (!findSubgame(SUBGAME_ID).isValid()) { + warningstream << "Can't find game " SUBGAME_ID ", skipping this module." << std::endl; + return; + } + + auto test_mods = getTestTempDirectory().append(DIR_DELIM "test_mods"); + { + auto p = test_mods + (DIR_DELIM "test_mod" DIR_DELIM); + fs::CreateAllDirs(p); + std::ofstream ofs1(p + "mod.conf", std::ios::out | std::ios::binary); + ofs1 << "name = test_mod\n" + << "description = Does nothing\n"; + std::ofstream ofs2(p + "init.lua", std::ios::out | std::ios::binary); + ofs2 << "-- intentionally empty\n"; + } + #ifdef WIN32 { std::string mod_path("MINETEST_MOD_PATH="); - mod_path.append(TEST_MOD_PATH); + mod_path.append(test_mods); _putenv(mod_path.c_str()); } #else - setenv("MINETEST_MOD_PATH", TEST_MOD_PATH, 1); + setenv("MINETEST_MOD_PATH", test_mods.c_str(), 1); #endif + m_worlddir = getTestTempDirectory().append(DIR_DELIM "world"); + fs::CreateDir(m_worlddir); + TEST(testCreation); TEST(testIsConsistent); TEST(testGetModsWrongDir); @@ -71,58 +92,42 @@ void TestServerModManager::runTests(IGameDef *gamedef) TEST(testGetModMediaPaths); // TODO: test MINETEST_SUBGAME_PATH -#ifdef WIN32 - { - std::string subgame_path("MINETEST_SUBGAME_PATH="); - if (saved_env_mt_subgame_path) - subgame_path.append(saved_env_mt_subgame_path); - _putenv(subgame_path.c_str()); - - std::string mod_path("MINETEST_MOD_PATH="); - if (saved_env_mt_mod_path) - mod_path.append(saved_env_mt_mod_path); - _putenv(mod_path.c_str()); - } -#else - if (saved_env_mt_mod_path) - setenv("MINETEST_MOD_PATH", saved_env_mt_mod_path, 1); - else - unsetenv("MINETEST_MOD_PATH"); -#endif + unsetenv("MINETEST_MOD_PATH"); } void TestServerModManager::testCreation() { - std::string path = std::string(TEST_WORLDDIR) + DIR_DELIM + "world.mt"; + std::string path = m_worlddir + DIR_DELIM + "world.mt"; Settings world_config; - world_config.set("gameid", "devtest"); + world_config.set("gameid", SUBGAME_ID); world_config.set("load_mod_test_mod", "true"); UASSERTEQ(bool, world_config.updateConfigFile(path.c_str()), true); - ServerModManager sm(TEST_WORLDDIR); + + ServerModManager sm(m_worlddir); } void TestServerModManager::testGetModsWrongDir() { // Test in non worlddir to ensure no mods are found - ServerModManager sm(std::string(TEST_WORLDDIR) + DIR_DELIM + ".."); + ServerModManager sm(m_worlddir + DIR_DELIM + ".."); UASSERTEQ(bool, sm.getMods().empty(), true); } void TestServerModManager::testUnsatisfiedMods() { - ServerModManager sm(std::string(TEST_WORLDDIR)); + ServerModManager sm(m_worlddir); UASSERTEQ(bool, sm.getUnsatisfiedMods().empty(), true); } void TestServerModManager::testIsConsistent() { - ServerModManager sm(std::string(TEST_WORLDDIR)); + ServerModManager sm(m_worlddir); UASSERTEQ(bool, sm.isConsistent(), true); } void TestServerModManager::testGetMods() { - ServerModManager sm(std::string(TEST_WORLDDIR)); + ServerModManager sm(m_worlddir); const auto &mods = sm.getMods(); UASSERTEQ(bool, mods.empty(), false); @@ -146,14 +151,14 @@ void TestServerModManager::testGetMods() void TestServerModManager::testGetModspec() { - ServerModManager sm(std::string(TEST_WORLDDIR)); + ServerModManager sm(m_worlddir); UASSERTEQ(const ModSpec *, sm.getModSpec("wrongmod"), NULL); UASSERT(sm.getModSpec("basenodes") != NULL); } void TestServerModManager::testGetModNamesWrongDir() { - ServerModManager sm(std::string(TEST_WORLDDIR) + DIR_DELIM + ".."); + ServerModManager sm(m_worlddir + DIR_DELIM + ".."); std::vector result; sm.getModNames(result); UASSERTEQ(bool, result.empty(), true); @@ -161,7 +166,7 @@ void TestServerModManager::testGetModNamesWrongDir() void TestServerModManager::testGetModNames() { - ServerModManager sm(std::string(TEST_WORLDDIR)); + ServerModManager sm(m_worlddir); std::vector result; sm.getModNames(result); UASSERTEQ(bool, result.empty(), false); @@ -170,7 +175,7 @@ void TestServerModManager::testGetModNames() void TestServerModManager::testGetModMediaPathsWrongDir() { - ServerModManager sm(std::string(TEST_WORLDDIR) + DIR_DELIM + ".."); + ServerModManager sm(m_worlddir + DIR_DELIM + ".."); std::vector result; sm.getModsMediaPaths(result); UASSERTEQ(bool, result.empty(), true); @@ -178,7 +183,7 @@ void TestServerModManager::testGetModMediaPathsWrongDir() void TestServerModManager::testGetModMediaPaths() { - ServerModManager sm(std::string(TEST_WORLDDIR)); + ServerModManager sm(m_worlddir); std::vector result; sm.getModsMediaPaths(result); UASSERTEQ(bool, result.empty(), false); diff --git a/src/unittest/test_world/do_not_remove.txt b/src/unittest/test_world/do_not_remove.txt deleted file mode 100644 index e69de29bb..000000000