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"
23 #include "test_config.h"
25 class TestServerModManager : public TestBase
28 TestServerModManager() { TestManager::registerTestModule(this); }
29 const char *getName() { return "TestServerModManager"; }
31 void runTests(IGameDef *gamedef);
34 void testIsConsistent();
35 void testUnsatisfiedMods();
37 void testGetModsWrongDir();
38 void testGetModspec();
39 void testGetModNamesWrongDir();
40 void testGetModNames();
41 void testGetModMediaPathsWrongDir();
42 void testGetModMediaPaths();
45 static TestServerModManager g_test_instance;
47 void TestServerModManager::runTests(IGameDef *gamedef)
49 const char *saved_env_mt_subgame_path = getenv("MINETEST_SUBGAME_PATH");
52 std::string subgame_path("MINETEST_SUBGAME_PATH=");
53 subgame_path.append(TEST_SUBGAME_PATH);
54 _putenv(subgame_path.c_str());
57 setenv("MINETEST_SUBGAME_PATH", TEST_SUBGAME_PATH, 1);
61 TEST(testIsConsistent);
62 TEST(testGetModsWrongDir);
63 TEST(testUnsatisfiedMods);
66 TEST(testGetModNamesWrongDir);
67 TEST(testGetModNames);
68 TEST(testGetModMediaPathsWrongDir);
69 TEST(testGetModMediaPaths);
73 std::string subgame_path("MINETEST_SUBGAME_PATH=");
74 if (saved_env_mt_subgame_path)
75 subgame_path.append(saved_env_mt_subgame_path);
76 _putenv(subgame_path.c_str());
79 if (saved_env_mt_subgame_path)
80 setenv("MINETEST_SUBGAME_PATH", saved_env_mt_subgame_path, 1);
82 unsetenv("MINETEST_SUBGAME_PATH");
86 void TestServerModManager::testCreation()
88 ServerModManager sm(TEST_WORLDDIR);
91 void TestServerModManager::testGetModsWrongDir()
93 // Test in non worlddir to ensure no mods are found
94 ServerModManager sm(std::string(TEST_WORLDDIR) + DIR_DELIM + "..");
95 UASSERTEQ(bool, sm.getMods().empty(), true);
98 void TestServerModManager::testUnsatisfiedMods()
100 ServerModManager sm(std::string(TEST_WORLDDIR));
101 UASSERTEQ(bool, sm.getUnsatisfiedMods().empty(), true);
104 void TestServerModManager::testIsConsistent()
106 ServerModManager sm(std::string(TEST_WORLDDIR));
107 UASSERTEQ(bool, sm.isConsistent(), true);
110 void TestServerModManager::testGetMods()
112 ServerModManager sm(std::string(TEST_WORLDDIR));
113 const auto &mods = sm.getMods();
114 UASSERTEQ(bool, mods.empty(), false);
116 // Ensure we found default mod inside the test folder
117 bool default_found = false;
118 for (const auto &m : mods) {
119 if (m.name == "default")
120 default_found = true;
122 // Verify if paths are not empty
123 UASSERTEQ(bool, m.path.empty(), false);
126 UASSERTEQ(bool, default_found, true);
129 void TestServerModManager::testGetModspec()
131 ServerModManager sm(std::string(TEST_WORLDDIR));
132 UASSERTEQ(const ModSpec *, sm.getModSpec("wrongmod"), NULL);
133 UASSERT(sm.getModSpec("default") != NULL);
136 void TestServerModManager::testGetModNamesWrongDir()
138 ServerModManager sm(std::string(TEST_WORLDDIR) + DIR_DELIM + "..");
139 std::vector<std::string> result;
140 sm.getModNames(result);
141 UASSERTEQ(bool, result.empty(), true);
144 void TestServerModManager::testGetModNames()
146 ServerModManager sm(std::string(TEST_WORLDDIR));
147 std::vector<std::string> result;
148 sm.getModNames(result);
149 UASSERTEQ(bool, result.empty(), false);
150 UASSERT(std::find(result.begin(), result.end(), "default") != result.end());
153 void TestServerModManager::testGetModMediaPathsWrongDir()
155 ServerModManager sm(std::string(TEST_WORLDDIR) + DIR_DELIM + "..");
156 std::vector<std::string> result;
157 sm.getModsMediaPaths(result);
158 UASSERTEQ(bool, result.empty(), true);
161 void TestServerModManager::testGetModMediaPaths()
163 ServerModManager sm(std::string(TEST_WORLDDIR));
164 std::vector<std::string> result;
165 sm.getModsMediaPaths(result);
166 UASSERTEQ(bool, result.empty(), false);
167 // We should have 5 folders for each mod (textures, media, locale, model, sounds)
168 UASSERTEQ(unsigned long, result.size() % 5, 0);