3 Copyright (C) 2018 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "server/mods.h"
24 #include "test_config.h"
26 class TestServerModManager : public TestBase
29 TestServerModManager() { TestManager::registerTestModule(this); }
30 const char *getName() { return "TestServerModManager"; }
32 void runTests(IGameDef *gamedef);
35 void testIsConsistent();
36 void testUnsatisfiedMods();
38 void testGetModsWrongDir();
39 void testGetModspec();
40 void testGetModNamesWrongDir();
41 void testGetModNames();
42 void testGetModMediaPathsWrongDir();
43 void testGetModMediaPaths();
46 static TestServerModManager g_test_instance;
48 void TestServerModManager::runTests(IGameDef *gamedef)
50 const char *saved_env_mt_subgame_path = getenv("MINETEST_SUBGAME_PATH");
53 std::string subgame_path("MINETEST_SUBGAME_PATH=");
54 subgame_path.append(TEST_SUBGAME_PATH);
55 _putenv(subgame_path.c_str());
58 setenv("MINETEST_SUBGAME_PATH", TEST_SUBGAME_PATH, 1);
62 TEST(testIsConsistent);
63 TEST(testGetModsWrongDir);
64 TEST(testUnsatisfiedMods);
67 TEST(testGetModNamesWrongDir);
68 TEST(testGetModNames);
69 TEST(testGetModMediaPathsWrongDir);
70 TEST(testGetModMediaPaths);
74 std::string subgame_path("MINETEST_SUBGAME_PATH=");
75 if (saved_env_mt_subgame_path)
76 subgame_path.append(saved_env_mt_subgame_path);
77 _putenv(subgame_path.c_str());
80 if (saved_env_mt_subgame_path)
81 setenv("MINETEST_SUBGAME_PATH", saved_env_mt_subgame_path, 1);
83 unsetenv("MINETEST_SUBGAME_PATH");
87 void TestServerModManager::testCreation()
89 std::string path = std::string(TEST_WORLDDIR) + DIR_DELIM + "world.mt";
90 Settings world_config;
91 world_config.set("gameid", "minimal");
92 UASSERTEQ(bool, world_config.updateConfigFile(path.c_str()), true);
93 ServerModManager sm(TEST_WORLDDIR);
96 void TestServerModManager::testGetModsWrongDir()
98 // Test in non worlddir to ensure no mods are found
99 ServerModManager sm(std::string(TEST_WORLDDIR) + DIR_DELIM + "..");
100 UASSERTEQ(bool, sm.getMods().empty(), true);
103 void TestServerModManager::testUnsatisfiedMods()
105 ServerModManager sm(std::string(TEST_WORLDDIR));
106 UASSERTEQ(bool, sm.getUnsatisfiedMods().empty(), true);
109 void TestServerModManager::testIsConsistent()
111 ServerModManager sm(std::string(TEST_WORLDDIR));
112 UASSERTEQ(bool, sm.isConsistent(), true);
115 void TestServerModManager::testGetMods()
117 ServerModManager sm(std::string(TEST_WORLDDIR));
118 const auto &mods = sm.getMods();
119 UASSERTEQ(bool, mods.empty(), false);
121 // Ensure we found default mod inside the test folder
122 bool default_found = false;
123 for (const auto &m : mods) {
124 if (m.name == "default")
125 default_found = true;
127 // Verify if paths are not empty
128 UASSERTEQ(bool, m.path.empty(), false);
131 UASSERTEQ(bool, default_found, true);
134 void TestServerModManager::testGetModspec()
136 ServerModManager sm(std::string(TEST_WORLDDIR));
137 UASSERTEQ(const ModSpec *, sm.getModSpec("wrongmod"), NULL);
138 UASSERT(sm.getModSpec("default") != NULL);
141 void TestServerModManager::testGetModNamesWrongDir()
143 ServerModManager sm(std::string(TEST_WORLDDIR) + DIR_DELIM + "..");
144 std::vector<std::string> result;
145 sm.getModNames(result);
146 UASSERTEQ(bool, result.empty(), true);
149 void TestServerModManager::testGetModNames()
151 ServerModManager sm(std::string(TEST_WORLDDIR));
152 std::vector<std::string> result;
153 sm.getModNames(result);
154 UASSERTEQ(bool, result.empty(), false);
155 UASSERT(std::find(result.begin(), result.end(), "default") != result.end());
158 void TestServerModManager::testGetModMediaPathsWrongDir()
160 ServerModManager sm(std::string(TEST_WORLDDIR) + DIR_DELIM + "..");
161 std::vector<std::string> result;
162 sm.getModsMediaPaths(result);
163 UASSERTEQ(bool, result.empty(), true);
166 void TestServerModManager::testGetModMediaPaths()
168 ServerModManager sm(std::string(TEST_WORLDDIR));
169 std::vector<std::string> result;
170 sm.getModsMediaPaths(result);
171 UASSERTEQ(bool, result.empty(), false);
172 // We should have 5 folders for each mod (textures, media, locale, model, sounds)
173 UASSERTEQ(unsigned long, result.size() % 5, 0);