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 subgame_path.append(saved_env_mt_subgame_path);
75 _putenv(subgame_path.c_str());
78 setenv("MINETEST_SUBGAME_PATH", saved_env_mt_subgame_path, 1);
82 void TestServerModManager::testCreation()
84 ServerModManager sm(TEST_WORLDDIR);
87 void TestServerModManager::testGetModsWrongDir()
89 // Test in non worlddir to ensure no mods are found
90 ServerModManager sm(std::string(TEST_WORLDDIR) + DIR_DELIM + "..");
91 UASSERTEQ(bool, sm.getMods().empty(), true);
94 void TestServerModManager::testUnsatisfiedMods()
96 ServerModManager sm(std::string(TEST_WORLDDIR));
97 UASSERTEQ(bool, sm.getUnsatisfiedMods().empty(), true);
100 void TestServerModManager::testIsConsistent()
102 ServerModManager sm(std::string(TEST_WORLDDIR));
103 UASSERTEQ(bool, sm.isConsistent(), true);
106 void TestServerModManager::testGetMods()
108 ServerModManager sm(std::string(TEST_WORLDDIR));
109 const auto &mods = sm.getMods();
110 UASSERTEQ(bool, mods.empty(), false);
112 // Ensure we found default mod inside the test folder
113 bool default_found = false;
114 for (const auto &m : mods) {
115 if (m.name == "default")
116 default_found = true;
118 // Verify if paths are not empty
119 UASSERTEQ(bool, m.path.empty(), false);
122 UASSERTEQ(bool, default_found, true);
125 void TestServerModManager::testGetModspec()
127 ServerModManager sm(std::string(TEST_WORLDDIR));
128 UASSERTEQ(const ModSpec *, sm.getModSpec("wrongmod"), NULL);
129 UASSERT(sm.getModSpec("default") != NULL);
132 void TestServerModManager::testGetModNamesWrongDir()
134 ServerModManager sm(std::string(TEST_WORLDDIR) + DIR_DELIM + "..");
135 std::vector<std::string> result;
136 sm.getModNames(result);
137 UASSERTEQ(bool, result.empty(), true);
140 void TestServerModManager::testGetModNames()
142 ServerModManager sm(std::string(TEST_WORLDDIR));
143 std::vector<std::string> result;
144 sm.getModNames(result);
145 UASSERTEQ(bool, result.empty(), false);
146 UASSERT(std::find(result.begin(), result.end(), "default") != result.end());
149 void TestServerModManager::testGetModMediaPathsWrongDir()
151 ServerModManager sm(std::string(TEST_WORLDDIR) + DIR_DELIM + "..");
152 std::vector<std::string> result;
153 sm.getModsMediaPaths(result);
154 UASSERTEQ(bool, result.empty(), true);
157 void TestServerModManager::testGetModMediaPaths()
159 ServerModManager sm(std::string(TEST_WORLDDIR));
160 std::vector<std::string> result;
161 sm.getModsMediaPaths(result);
162 UASSERTEQ(bool, result.empty(), false);
163 // We should have 5 folders for each mod (textures, media, locale, model, sounds)
164 UASSERTEQ(unsigned long, result.size() % 5, 0);