mirror of
https://github.com/minetest/minetest.git
synced 2024-11-23 16:13:46 +01:00
Make unittests less reliant on files in the source distribution
This commit is contained in:
parent
863c9b55b4
commit
133f706bf3
@ -47,12 +47,3 @@ set (UNITTEST_CLIENT_SRCS
|
|||||||
${CMAKE_CURRENT_SOURCE_DIR}/test_keycode.cpp
|
${CMAKE_CURRENT_SOURCE_DIR}/test_keycode.cpp
|
||||||
PARENT_SCOPE)
|
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
|
class MockServer : public Server
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
MockServer() : Server(TEST_WORLDDIR, SubgameSpec("fakespec", "fakespec"), true,
|
/* Set path_world to a real existing folder if you plan to initialize scripting! */
|
||||||
Address(), true, nullptr)
|
MockServer(const std::string &path_world = "fakepath") :
|
||||||
|
Server(path_world, SubgameSpec("fakespec", "fakespec"), true,
|
||||||
|
Address(), true, nullptr
|
||||||
|
)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
private:
|
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()
|
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);
|
const std::string test_data("hello\0world", 11);
|
||||||
fs::safeWriteToFile(dest_path, test_data);
|
fs::safeWriteToFile(dest_path, test_data);
|
||||||
UASSERT(fs::PathExists(dest_path));
|
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.h"
|
||||||
#include "test_config.h"
|
|
||||||
|
|
||||||
#include "mock_inventorymanager.h"
|
#include "mock_inventorymanager.h"
|
||||||
#include "mock_server.h"
|
#include "mock_server.h"
|
||||||
#include "mock_serveractiveobject.h"
|
#include "mock_serveractiveobject.h"
|
||||||
|
|
||||||
#include <scripting_server.h>
|
#include "scripting_server.h"
|
||||||
|
|
||||||
|
|
||||||
class TestMoveAction : public TestBase
|
class TestMoveAction : public TestBase
|
||||||
@ -47,16 +46,36 @@ public:
|
|||||||
|
|
||||||
static TestMoveAction g_test_instance;
|
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)
|
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);
|
ServerScripting server_scripting(&server);
|
||||||
try {
|
try {
|
||||||
server_scripting.loadMod(Server::getBuiltinLuaPath() + DIR_DELIM "init.lua", BUILTIN_MOD_NAME);
|
server_scripting.loadMod(Server::getBuiltinLuaPath() + DIR_DELIM "init.lua", BUILTIN_MOD_NAME);
|
||||||
server_scripting.loadMod(
|
server_scripting.loadMod(helper_lua, BUILTIN_MOD_NAME);
|
||||||
std::string(HELPERS_PATH) + DIR_DELIM "helper_moveaction.lua", BUILTIN_MOD_NAME
|
|
||||||
);
|
|
||||||
} catch (ModError &e) {
|
} catch (ModError &e) {
|
||||||
// Print backtrace in case of syntax errors
|
// Print backtrace in case of syntax errors
|
||||||
rawstream << e.what() << std::endl;
|
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.h"
|
||||||
#include "test_config.h"
|
|
||||||
|
|
||||||
#include "mock_server.h"
|
#include "mock_server.h"
|
||||||
|
|
||||||
|
@ -21,9 +21,10 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include "server/mods.h"
|
#include "server/mods.h"
|
||||||
#include "settings.h"
|
#include "settings.h"
|
||||||
#include "test_config.h"
|
|
||||||
#include "util/string.h"
|
#include "util/string.h"
|
||||||
|
|
||||||
|
#define SUBGAME_ID "devtest"
|
||||||
|
|
||||||
class TestServerModManager : public TestBase
|
class TestServerModManager : public TestBase
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@ -32,6 +33,8 @@ public:
|
|||||||
|
|
||||||
void runTests(IGameDef *gamedef);
|
void runTests(IGameDef *gamedef);
|
||||||
|
|
||||||
|
std::string m_worlddir;
|
||||||
|
|
||||||
void testCreation();
|
void testCreation();
|
||||||
void testIsConsistent();
|
void testIsConsistent();
|
||||||
void testUnsatisfiedMods();
|
void testUnsatisfiedMods();
|
||||||
@ -48,17 +51,35 @@ static TestServerModManager g_test_instance;
|
|||||||
|
|
||||||
void TestServerModManager::runTests(IGameDef *gamedef)
|
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
|
#ifdef WIN32
|
||||||
{
|
{
|
||||||
std::string mod_path("MINETEST_MOD_PATH=");
|
std::string mod_path("MINETEST_MOD_PATH=");
|
||||||
mod_path.append(TEST_MOD_PATH);
|
mod_path.append(test_mods);
|
||||||
_putenv(mod_path.c_str());
|
_putenv(mod_path.c_str());
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
setenv("MINETEST_MOD_PATH", TEST_MOD_PATH, 1);
|
setenv("MINETEST_MOD_PATH", test_mods.c_str(), 1);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
m_worlddir = getTestTempDirectory().append(DIR_DELIM "world");
|
||||||
|
fs::CreateDir(m_worlddir);
|
||||||
|
|
||||||
TEST(testCreation);
|
TEST(testCreation);
|
||||||
TEST(testIsConsistent);
|
TEST(testIsConsistent);
|
||||||
TEST(testGetModsWrongDir);
|
TEST(testGetModsWrongDir);
|
||||||
@ -71,58 +92,42 @@ void TestServerModManager::runTests(IGameDef *gamedef)
|
|||||||
TEST(testGetModMediaPaths);
|
TEST(testGetModMediaPaths);
|
||||||
// TODO: test MINETEST_SUBGAME_PATH
|
// 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");
|
unsetenv("MINETEST_MOD_PATH");
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void TestServerModManager::testCreation()
|
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;
|
Settings world_config;
|
||||||
world_config.set("gameid", "devtest");
|
world_config.set("gameid", SUBGAME_ID);
|
||||||
world_config.set("load_mod_test_mod", "true");
|
world_config.set("load_mod_test_mod", "true");
|
||||||
UASSERTEQ(bool, world_config.updateConfigFile(path.c_str()), true);
|
UASSERTEQ(bool, world_config.updateConfigFile(path.c_str()), true);
|
||||||
ServerModManager sm(TEST_WORLDDIR);
|
|
||||||
|
ServerModManager sm(m_worlddir);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TestServerModManager::testGetModsWrongDir()
|
void TestServerModManager::testGetModsWrongDir()
|
||||||
{
|
{
|
||||||
// Test in non worlddir to ensure no mods are found
|
// 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);
|
UASSERTEQ(bool, sm.getMods().empty(), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TestServerModManager::testUnsatisfiedMods()
|
void TestServerModManager::testUnsatisfiedMods()
|
||||||
{
|
{
|
||||||
ServerModManager sm(std::string(TEST_WORLDDIR));
|
ServerModManager sm(m_worlddir);
|
||||||
UASSERTEQ(bool, sm.getUnsatisfiedMods().empty(), true);
|
UASSERTEQ(bool, sm.getUnsatisfiedMods().empty(), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TestServerModManager::testIsConsistent()
|
void TestServerModManager::testIsConsistent()
|
||||||
{
|
{
|
||||||
ServerModManager sm(std::string(TEST_WORLDDIR));
|
ServerModManager sm(m_worlddir);
|
||||||
UASSERTEQ(bool, sm.isConsistent(), true);
|
UASSERTEQ(bool, sm.isConsistent(), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TestServerModManager::testGetMods()
|
void TestServerModManager::testGetMods()
|
||||||
{
|
{
|
||||||
ServerModManager sm(std::string(TEST_WORLDDIR));
|
ServerModManager sm(m_worlddir);
|
||||||
const auto &mods = sm.getMods();
|
const auto &mods = sm.getMods();
|
||||||
UASSERTEQ(bool, mods.empty(), false);
|
UASSERTEQ(bool, mods.empty(), false);
|
||||||
|
|
||||||
@ -146,14 +151,14 @@ void TestServerModManager::testGetMods()
|
|||||||
|
|
||||||
void TestServerModManager::testGetModspec()
|
void TestServerModManager::testGetModspec()
|
||||||
{
|
{
|
||||||
ServerModManager sm(std::string(TEST_WORLDDIR));
|
ServerModManager sm(m_worlddir);
|
||||||
UASSERTEQ(const ModSpec *, sm.getModSpec("wrongmod"), NULL);
|
UASSERTEQ(const ModSpec *, sm.getModSpec("wrongmod"), NULL);
|
||||||
UASSERT(sm.getModSpec("basenodes") != NULL);
|
UASSERT(sm.getModSpec("basenodes") != NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TestServerModManager::testGetModNamesWrongDir()
|
void TestServerModManager::testGetModNamesWrongDir()
|
||||||
{
|
{
|
||||||
ServerModManager sm(std::string(TEST_WORLDDIR) + DIR_DELIM + "..");
|
ServerModManager sm(m_worlddir + DIR_DELIM + "..");
|
||||||
std::vector<std::string> result;
|
std::vector<std::string> result;
|
||||||
sm.getModNames(result);
|
sm.getModNames(result);
|
||||||
UASSERTEQ(bool, result.empty(), true);
|
UASSERTEQ(bool, result.empty(), true);
|
||||||
@ -161,7 +166,7 @@ void TestServerModManager::testGetModNamesWrongDir()
|
|||||||
|
|
||||||
void TestServerModManager::testGetModNames()
|
void TestServerModManager::testGetModNames()
|
||||||
{
|
{
|
||||||
ServerModManager sm(std::string(TEST_WORLDDIR));
|
ServerModManager sm(m_worlddir);
|
||||||
std::vector<std::string> result;
|
std::vector<std::string> result;
|
||||||
sm.getModNames(result);
|
sm.getModNames(result);
|
||||||
UASSERTEQ(bool, result.empty(), false);
|
UASSERTEQ(bool, result.empty(), false);
|
||||||
@ -170,7 +175,7 @@ void TestServerModManager::testGetModNames()
|
|||||||
|
|
||||||
void TestServerModManager::testGetModMediaPathsWrongDir()
|
void TestServerModManager::testGetModMediaPathsWrongDir()
|
||||||
{
|
{
|
||||||
ServerModManager sm(std::string(TEST_WORLDDIR) + DIR_DELIM + "..");
|
ServerModManager sm(m_worlddir + DIR_DELIM + "..");
|
||||||
std::vector<std::string> result;
|
std::vector<std::string> result;
|
||||||
sm.getModsMediaPaths(result);
|
sm.getModsMediaPaths(result);
|
||||||
UASSERTEQ(bool, result.empty(), true);
|
UASSERTEQ(bool, result.empty(), true);
|
||||||
@ -178,7 +183,7 @@ void TestServerModManager::testGetModMediaPathsWrongDir()
|
|||||||
|
|
||||||
void TestServerModManager::testGetModMediaPaths()
|
void TestServerModManager::testGetModMediaPaths()
|
||||||
{
|
{
|
||||||
ServerModManager sm(std::string(TEST_WORLDDIR));
|
ServerModManager sm(m_worlddir);
|
||||||
std::vector<std::string> result;
|
std::vector<std::string> result;
|
||||||
sm.getModsMediaPaths(result);
|
sm.getModsMediaPaths(result);
|
||||||
UASSERTEQ(bool, result.empty(), false);
|
UASSERTEQ(bool, result.empty(), false);
|
||||||
|
Loading…
Reference in New Issue
Block a user