]> git.lizzy.rs Git - minetest.git/blob - src/unittest/test_servermodmanager.cpp
Settings: Fix game minetest.conf flags overriding defaults (#9404)
[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
26 class TestServerModManager : public TestBase
27 {
28 public:
29         TestServerModManager() { TestManager::registerTestModule(this); }
30         const char *getName() { return "TestServerModManager"; }
31
32         void runTests(IGameDef *gamedef);
33
34         void testCreation();
35         void testIsConsistent();
36         void testUnsatisfiedMods();
37         void testGetMods();
38         void testGetModsWrongDir();
39         void testGetModspec();
40         void testGetModNamesWrongDir();
41         void testGetModNames();
42         void testGetModMediaPathsWrongDir();
43         void testGetModMediaPaths();
44 };
45
46 static TestServerModManager g_test_instance;
47
48 void TestServerModManager::runTests(IGameDef *gamedef)
49 {
50         const char *saved_env_mt_subgame_path = getenv("MINETEST_SUBGAME_PATH");
51 #ifdef WIN32
52         {
53                 std::string subgame_path("MINETEST_SUBGAME_PATH=");
54                 subgame_path.append(TEST_SUBGAME_PATH);
55                 _putenv(subgame_path.c_str());
56         }
57 #else
58         setenv("MINETEST_SUBGAME_PATH", TEST_SUBGAME_PATH, 1);
59 #endif
60
61         TEST(testCreation);
62         TEST(testIsConsistent);
63         TEST(testGetModsWrongDir);
64         TEST(testUnsatisfiedMods);
65         TEST(testGetMods);
66         TEST(testGetModspec);
67         TEST(testGetModNamesWrongDir);
68         TEST(testGetModNames);
69         TEST(testGetModMediaPathsWrongDir);
70         TEST(testGetModMediaPaths);
71
72 #ifdef WIN32
73         {
74                 std::string subgame_path("MINETEST_SUBGAME_PATH=");
75                 if (saved_env_mt_subgame_path)
76                         subgame_path.append(saved_env_mt_subgame_path);
77                 _putenv(subgame_path.c_str());
78         }
79 #else
80         if (saved_env_mt_subgame_path)
81                 setenv("MINETEST_SUBGAME_PATH", saved_env_mt_subgame_path, 1);
82         else
83                 unsetenv("MINETEST_SUBGAME_PATH");
84 #endif
85 }
86
87 void TestServerModManager::testCreation()
88 {
89         std::string path = std::string(TEST_WORLDDIR) + DIR_DELIM + "world.mt";
90         Settings world_config;
91         world_config.set("gameid", "minimal");
92         UASSERTEQ(bool, world_config.updateConfigFile(path.c_str()), true);
93         ServerModManager sm(TEST_WORLDDIR);
94 }
95
96 void TestServerModManager::testGetModsWrongDir()
97 {
98         // Test in non worlddir to ensure no mods are found
99         ServerModManager sm(std::string(TEST_WORLDDIR) + DIR_DELIM + "..");
100         UASSERTEQ(bool, sm.getMods().empty(), true);
101 }
102
103 void TestServerModManager::testUnsatisfiedMods()
104 {
105         ServerModManager sm(std::string(TEST_WORLDDIR));
106         UASSERTEQ(bool, sm.getUnsatisfiedMods().empty(), true);
107 }
108
109 void TestServerModManager::testIsConsistent()
110 {
111         ServerModManager sm(std::string(TEST_WORLDDIR));
112         UASSERTEQ(bool, sm.isConsistent(), true);
113 }
114
115 void TestServerModManager::testGetMods()
116 {
117         ServerModManager sm(std::string(TEST_WORLDDIR));
118         const auto &mods = sm.getMods();
119         UASSERTEQ(bool, mods.empty(), false);
120
121         // Ensure we found default mod inside the test folder
122         bool default_found = false;
123         for (const auto &m : mods) {
124                 if (m.name == "default")
125                         default_found = true;
126
127                 // Verify if paths are not empty
128                 UASSERTEQ(bool, m.path.empty(), false);
129         }
130
131         UASSERTEQ(bool, default_found, true);
132 }
133
134 void TestServerModManager::testGetModspec()
135 {
136         ServerModManager sm(std::string(TEST_WORLDDIR));
137         UASSERTEQ(const ModSpec *, sm.getModSpec("wrongmod"), NULL);
138         UASSERT(sm.getModSpec("default") != NULL);
139 }
140
141 void TestServerModManager::testGetModNamesWrongDir()
142 {
143         ServerModManager sm(std::string(TEST_WORLDDIR) + DIR_DELIM + "..");
144         std::vector<std::string> result;
145         sm.getModNames(result);
146         UASSERTEQ(bool, result.empty(), true);
147 }
148
149 void TestServerModManager::testGetModNames()
150 {
151         ServerModManager sm(std::string(TEST_WORLDDIR));
152         std::vector<std::string> result;
153         sm.getModNames(result);
154         UASSERTEQ(bool, result.empty(), false);
155         UASSERT(std::find(result.begin(), result.end(), "default") != result.end());
156 }
157
158 void TestServerModManager::testGetModMediaPathsWrongDir()
159 {
160         ServerModManager sm(std::string(TEST_WORLDDIR) + DIR_DELIM + "..");
161         std::vector<std::string> result;
162         sm.getModsMediaPaths(result);
163         UASSERTEQ(bool, result.empty(), true);
164 }
165
166 void TestServerModManager::testGetModMediaPaths()
167 {
168         ServerModManager sm(std::string(TEST_WORLDDIR));
169         std::vector<std::string> result;
170         sm.getModsMediaPaths(result);
171         UASSERTEQ(bool, result.empty(), false);
172         // We should have 5 folders for each mod (textures, media, locale, model, sounds)
173         UASSERTEQ(unsigned long, result.size() % 5, 0);
174 }