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"
25 #include "util/string.h"
27 class TestServerModManager : public TestBase
30 TestServerModManager() { TestManager::registerTestModule(this); }
31 const char *getName() { return "TestServerModManager"; }
33 void runTests(IGameDef *gamedef);
36 void testIsConsistent();
37 void testUnsatisfiedMods();
39 void testGetModsWrongDir();
40 void testGetModspec();
41 void testGetModNamesWrongDir();
42 void testGetModNames();
43 void testGetModMediaPathsWrongDir();
44 void testGetModMediaPaths();
47 static TestServerModManager g_test_instance;
49 void TestServerModManager::runTests(IGameDef *gamedef)
51 const char *saved_env_mt_subgame_path = getenv("MINETEST_SUBGAME_PATH");
52 const char *saved_env_mt_mod_path = getenv("MINETEST_MOD_PATH");
55 std::string subgame_path("MINETEST_SUBGAME_PATH=");
56 subgame_path.append(TEST_SUBGAME_PATH);
57 _putenv(subgame_path.c_str());
59 std::string mod_path("MINETEST_MOD_PATH=");
60 mod_path.append(TEST_MOD_PATH);
61 _putenv(mod_path.c_str());
64 setenv("MINETEST_SUBGAME_PATH", TEST_SUBGAME_PATH, 1);
65 setenv("MINETEST_MOD_PATH", TEST_MOD_PATH, 1);
69 TEST(testIsConsistent);
70 TEST(testGetModsWrongDir);
71 TEST(testUnsatisfiedMods);
74 TEST(testGetModNamesWrongDir);
75 TEST(testGetModNames);
76 TEST(testGetModMediaPathsWrongDir);
77 TEST(testGetModMediaPaths);
81 std::string subgame_path("MINETEST_SUBGAME_PATH=");
82 if (saved_env_mt_subgame_path)
83 subgame_path.append(saved_env_mt_subgame_path);
84 _putenv(subgame_path.c_str());
86 std::string mod_path("MINETEST_MOD_PATH=");
87 if (saved_env_mt_mod_path)
88 mod_path.append(saved_env_mt_mod_path);
89 _putenv(mod_path.c_str());
92 if (saved_env_mt_subgame_path)
93 setenv("MINETEST_SUBGAME_PATH", saved_env_mt_subgame_path, 1);
95 unsetenv("MINETEST_SUBGAME_PATH");
96 if (saved_env_mt_mod_path)
97 setenv("MINETEST_MOD_PATH", saved_env_mt_mod_path, 1);
99 unsetenv("MINETEST_MOD_PATH");
103 void TestServerModManager::testCreation()
105 std::string path = std::string(TEST_WORLDDIR) + DIR_DELIM + "world.mt";
106 Settings world_config;
107 world_config.set("gameid", "devtest");
108 world_config.set("load_mod_test_mod", "true");
109 UASSERTEQ(bool, world_config.updateConfigFile(path.c_str()), true);
110 ServerModManager sm(TEST_WORLDDIR);
113 void TestServerModManager::testGetModsWrongDir()
115 // Test in non worlddir to ensure no mods are found
116 ServerModManager sm(std::string(TEST_WORLDDIR) + DIR_DELIM + "..");
117 UASSERTEQ(bool, sm.getMods().empty(), true);
120 void TestServerModManager::testUnsatisfiedMods()
122 ServerModManager sm(std::string(TEST_WORLDDIR));
123 UASSERTEQ(bool, sm.getUnsatisfiedMods().empty(), true);
126 void TestServerModManager::testIsConsistent()
128 ServerModManager sm(std::string(TEST_WORLDDIR));
129 UASSERTEQ(bool, sm.isConsistent(), true);
132 void TestServerModManager::testGetMods()
134 ServerModManager sm(std::string(TEST_WORLDDIR));
135 const auto &mods = sm.getMods();
136 UASSERTEQ(bool, mods.empty(), false);
138 // Ensure we found basenodes mod (part of devtest)
139 // and test_mod (for testing MINETEST_MOD_PATH).
140 bool default_found = false;
141 bool test_mod_found = false;
142 for (const auto &m : mods) {
143 if (m.name == "basenodes")
144 default_found = true;
145 if (m.name == "test_mod")
146 test_mod_found = true;
148 // Verify if paths are not empty
149 UASSERTEQ(bool, m.path.empty(), false);
152 UASSERTEQ(bool, default_found, true);
153 UASSERTEQ(bool, test_mod_found, true);
156 void TestServerModManager::testGetModspec()
158 ServerModManager sm(std::string(TEST_WORLDDIR));
159 UASSERTEQ(const ModSpec *, sm.getModSpec("wrongmod"), NULL);
160 UASSERT(sm.getModSpec("basenodes") != NULL);
163 void TestServerModManager::testGetModNamesWrongDir()
165 ServerModManager sm(std::string(TEST_WORLDDIR) + DIR_DELIM + "..");
166 std::vector<std::string> result;
167 sm.getModNames(result);
168 UASSERTEQ(bool, result.empty(), true);
171 void TestServerModManager::testGetModNames()
173 ServerModManager sm(std::string(TEST_WORLDDIR));
174 std::vector<std::string> result;
175 sm.getModNames(result);
176 UASSERTEQ(bool, result.empty(), false);
177 UASSERT(std::find(result.begin(), result.end(), "basenodes") != result.end());
180 void TestServerModManager::testGetModMediaPathsWrongDir()
182 ServerModManager sm(std::string(TEST_WORLDDIR) + DIR_DELIM + "..");
183 std::vector<std::string> result;
184 sm.getModsMediaPaths(result);
185 UASSERTEQ(bool, result.empty(), true);
188 void TestServerModManager::testGetModMediaPaths()
190 ServerModManager sm(std::string(TEST_WORLDDIR));
191 std::vector<std::string> result;
192 sm.getModsMediaPaths(result);
193 UASSERTEQ(bool, result.empty(), false);
195 // Test media overriding:
196 // unittests depends on basenodes to override default_dirt.png,
197 // thus the unittests texture path must come first in the returned media paths to take priority
198 auto it = std::find(result.begin(), result.end(), sm.getModSpec("unittests")->path + DIR_DELIM + "textures");
199 UASSERT(it != result.end());
200 UASSERT(std::find(++it, result.end(), sm.getModSpec("basenodes")->path + DIR_DELIM + "textures") != result.end());