]> git.lizzy.rs Git - minetest.git/blob - src/unittest/test_servermodmanager.cpp
Disable Prometheus in singleplayer mode
[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 "settings.h"
24 #include "test_config.h"
25 #include "util/string.h"
26
27 class TestServerModManager : public TestBase
28 {
29 public:
30         TestServerModManager() { TestManager::registerTestModule(this); }
31         const char *getName() { return "TestServerModManager"; }
32
33         void runTests(IGameDef *gamedef);
34
35         void testCreation();
36         void testIsConsistent();
37         void testUnsatisfiedMods();
38         void testGetMods();
39         void testGetModsWrongDir();
40         void testGetModspec();
41         void testGetModNamesWrongDir();
42         void testGetModNames();
43         void testGetModMediaPathsWrongDir();
44         void testGetModMediaPaths();
45 };
46
47 static TestServerModManager g_test_instance;
48
49 void TestServerModManager::runTests(IGameDef *gamedef)
50 {
51         const char *saved_env_mt_subgame_path = getenv("MINETEST_SUBGAME_PATH");
52         const char *saved_env_mt_mod_path = getenv("MINETEST_MOD_PATH");
53 #ifdef WIN32
54         {
55                 std::string subgame_path("MINETEST_SUBGAME_PATH=");
56                 subgame_path.append(TEST_SUBGAME_PATH);
57                 _putenv(subgame_path.c_str());
58
59                 std::string mod_path("MINETEST_MOD_PATH=");
60                 mod_path.append(TEST_MOD_PATH);
61                 _putenv(mod_path.c_str());
62         }
63 #else
64         setenv("MINETEST_SUBGAME_PATH", TEST_SUBGAME_PATH, 1);
65         setenv("MINETEST_MOD_PATH", TEST_MOD_PATH, 1);
66 #endif
67
68         TEST(testCreation);
69         TEST(testIsConsistent);
70         TEST(testGetModsWrongDir);
71         TEST(testUnsatisfiedMods);
72         TEST(testGetMods);
73         TEST(testGetModspec);
74         TEST(testGetModNamesWrongDir);
75         TEST(testGetModNames);
76         TEST(testGetModMediaPathsWrongDir);
77         TEST(testGetModMediaPaths);
78
79 #ifdef WIN32
80         {
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());
85
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());
90         }
91 #else
92         if (saved_env_mt_subgame_path)
93                 setenv("MINETEST_SUBGAME_PATH", saved_env_mt_subgame_path, 1);
94         else
95                 unsetenv("MINETEST_SUBGAME_PATH");
96         if (saved_env_mt_mod_path)
97                 setenv("MINETEST_MOD_PATH", saved_env_mt_mod_path, 1);
98         else
99                 unsetenv("MINETEST_MOD_PATH");
100 #endif
101 }
102
103 void TestServerModManager::testCreation()
104 {
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);
111 }
112
113 void TestServerModManager::testGetModsWrongDir()
114 {
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);
118 }
119
120 void TestServerModManager::testUnsatisfiedMods()
121 {
122         ServerModManager sm(std::string(TEST_WORLDDIR));
123         UASSERTEQ(bool, sm.getUnsatisfiedMods().empty(), true);
124 }
125
126 void TestServerModManager::testIsConsistent()
127 {
128         ServerModManager sm(std::string(TEST_WORLDDIR));
129         UASSERTEQ(bool, sm.isConsistent(), true);
130 }
131
132 void TestServerModManager::testGetMods()
133 {
134         ServerModManager sm(std::string(TEST_WORLDDIR));
135         const auto &mods = sm.getMods();
136         UASSERTEQ(bool, mods.empty(), false);
137
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;
147
148                 // Verify if paths are not empty
149                 UASSERTEQ(bool, m.path.empty(), false);
150         }
151
152         UASSERTEQ(bool, default_found, true);
153         UASSERTEQ(bool, test_mod_found, true);
154 }
155
156 void TestServerModManager::testGetModspec()
157 {
158         ServerModManager sm(std::string(TEST_WORLDDIR));
159         UASSERTEQ(const ModSpec *, sm.getModSpec("wrongmod"), NULL);
160         UASSERT(sm.getModSpec("basenodes") != NULL);
161 }
162
163 void TestServerModManager::testGetModNamesWrongDir()
164 {
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);
169 }
170
171 void TestServerModManager::testGetModNames()
172 {
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());
178 }
179
180 void TestServerModManager::testGetModMediaPathsWrongDir()
181 {
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);
186 }
187
188 void TestServerModManager::testGetModMediaPaths()
189 {
190         ServerModManager sm(std::string(TEST_WORLDDIR));
191         std::vector<std::string> result;
192         sm.getModsMediaPaths(result);
193         UASSERTEQ(bool, result.empty(), false);
194
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());
201 }