Server: delegate mod management & config to ServerModConfiguration (#7131)
[oweals/minetest.git] / src / unittest / test_servermodmanager.cpp
1 /*
2 Minetest
3 Copyright (C) 2018 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
4
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.
9
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.
14
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.
18 */
19
20 #include "test.h"
21 #include <algorithm>
22 #include "server/mods.h"
23 #include "test_config.h"
24
25 class TestServerModManager : public TestBase
26 {
27 public:
28         TestServerModManager() { TestManager::registerTestModule(this); }
29         const char *getName() { return "TestServerModManager"; }
30
31         void runTests(IGameDef *gamedef);
32
33         void testCreation();
34         void testIsConsistent();
35         void testUnsatisfiedMods();
36         void testGetMods();
37         void testGetModsWrongDir();
38         void testGetModspec();
39         void testGetModNamesWrongDir();
40         void testGetModNames();
41         void testGetModMediaPathsWrongDir();
42         void testGetModMediaPaths();
43 };
44
45 static TestServerModManager g_test_instance;
46
47 void TestServerModManager::runTests(IGameDef *gamedef)
48 {
49         const char *saved_env_mt_subgame_path = getenv("MINETEST_SUBGAME_PATH");
50 #ifdef WIN32
51         {
52                 std::string subgame_path("MINETEST_SUBGAME_PATH=");
53                 subgame_path.append(TEST_SUBGAME_PATH);
54                 _putenv(subgame_path.c_str());
55         }
56 #else
57         setenv("MINETEST_SUBGAME_PATH", TEST_SUBGAME_PATH, 1);
58 #endif
59
60         TEST(testCreation);
61         TEST(testIsConsistent);
62         TEST(testGetModsWrongDir);
63         TEST(testUnsatisfiedMods);
64         TEST(testGetMods);
65         TEST(testGetModspec);
66         TEST(testGetModNamesWrongDir);
67         TEST(testGetModNames);
68         TEST(testGetModMediaPathsWrongDir);
69         TEST(testGetModMediaPaths);
70
71 #ifdef WIN32
72         {
73                 std::string subgame_path("MINETEST_SUBGAME_PATH=");
74                 subgame_path.append(saved_env_mt_subgame_path);
75                 _putenv(subgame_path.c_str());
76         }
77 #else
78         setenv("MINETEST_SUBGAME_PATH", saved_env_mt_subgame_path, 1);
79 #endif
80 }
81
82 void TestServerModManager::testCreation()
83 {
84         ServerModManager sm(TEST_WORLDDIR);
85 }
86
87 void TestServerModManager::testGetModsWrongDir()
88 {
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);
92 }
93
94 void TestServerModManager::testUnsatisfiedMods()
95 {
96         ServerModManager sm(std::string(TEST_WORLDDIR));
97         UASSERTEQ(bool, sm.getUnsatisfiedMods().empty(), true);
98 }
99
100 void TestServerModManager::testIsConsistent()
101 {
102         ServerModManager sm(std::string(TEST_WORLDDIR));
103         UASSERTEQ(bool, sm.isConsistent(), true);
104 }
105
106 void TestServerModManager::testGetMods()
107 {
108         ServerModManager sm(std::string(TEST_WORLDDIR));
109         const auto &mods = sm.getMods();
110         UASSERTEQ(bool, mods.empty(), false);
111
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;
117
118                 // Verify if paths are not empty
119                 UASSERTEQ(bool, m.path.empty(), false);
120         }
121
122         UASSERTEQ(bool, default_found, true);
123 }
124
125 void TestServerModManager::testGetModspec()
126 {
127         ServerModManager sm(std::string(TEST_WORLDDIR));
128         UASSERTEQ(const ModSpec *, sm.getModSpec("wrongmod"), NULL);
129         UASSERT(sm.getModSpec("default") != NULL);
130 }
131
132 void TestServerModManager::testGetModNamesWrongDir()
133 {
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);
138 }
139
140 void TestServerModManager::testGetModNames()
141 {
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());
147 }
148
149 void TestServerModManager::testGetModMediaPathsWrongDir()
150 {
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);
155 }
156
157 void TestServerModManager::testGetModMediaPaths()
158 {
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);
165 }