Make unittests less reliant on files in the source distribution

This commit is contained in:
sfan5 2024-01-10 19:10:58 +01:00
parent 863c9b55b4
commit 133f706bf3
11 changed files with 70 additions and 79 deletions

@ -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"
)

@ -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)

@ -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:

@ -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@"

@ -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));

@ -1 +0,0 @@
-- deliberately empty

@ -1,2 +0,0 @@
name = test_mod
description = A mod doing nothing, to test if MINETEST_MOD_PATH is recognised

@ -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 <scripting_server.h>
#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;

@ -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"

@ -21,9 +21,10 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <algorithm>
#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
}
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<std::string> 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<std::string> 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<std::string> 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<std::string> result;
sm.getModsMediaPaths(result);
UASSERTEQ(bool, result.empty(), false);