mirror of
https://github.com/luanti-org/luanti.git
synced 2025-08-16 18:01:40 +00:00
Make unittests less reliant on files in the source distribution
This commit is contained in:
parent
863c9b55b4
commit
133f706bf3
11 changed files with 70 additions and 79 deletions
|
@ -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
|
||||
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<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);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue