-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_config.cpp
More file actions
270 lines (218 loc) · 9.81 KB
/
Copy pathtest_config.cpp
File metadata and controls
270 lines (218 loc) · 9.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// -----------------------------------------------------------------------------
// Config file tests
// Covers saved key value pairs and fallback behavior for invalid saved values.
// -----------------------------------------------------------------------------
#include <catch2/catch_test_macros.hpp>
#include "config.hpp"
#include "ui/package_table/package_table_columns.hpp"
#include <filesystem>
#include <fstream>
#include <map>
#include <set>
#include <string>
#include <glib.h>
#include <glib/gstdio.h>
namespace {
// -----------------------------------------------------------------------------
// Keep config tests away from the user's real config directory.
// -----------------------------------------------------------------------------
std::filesystem::path
test_config_dir()
{
static std::filesystem::path dir = [] {
gchar *tmp_dir = g_dir_make_tmp("dnfui-config-test-XXXXXX", nullptr);
REQUIRE(tmp_dir != nullptr);
std::filesystem::path path(tmp_dir);
g_free(tmp_dir);
REQUIRE(g_setenv("XDG_CONFIG_HOME", path.c_str(), TRUE));
return path;
}();
return dir;
}
// -----------------------------------------------------------------------------
// Return the config file used by the tests.
// -----------------------------------------------------------------------------
std::filesystem::path
test_config_file()
{
return test_config_dir() / "dnfui.conf";
}
// -----------------------------------------------------------------------------
// Remove the test config file before each test.
// -----------------------------------------------------------------------------
void
reset_test_config_file()
{
std::filesystem::remove(test_config_file());
}
// -----------------------------------------------------------------------------
// Return the default visible package table column ids from the shared metadata.
// -----------------------------------------------------------------------------
std::set<std::string>
default_visible_package_table_columns()
{
std::set<std::string> visible;
for (const auto &column : package_table_column_definitions()) {
if (column.default_visible) {
visible.insert(column.id);
}
}
return visible;
}
} // namespace
// -----------------------------------------------------------------------------
// Verify that saving a config map writes the same values that loading reads back.
// -----------------------------------------------------------------------------
TEST_CASE("Config map save and load roundtrip")
{
reset_test_config_file();
const std::map<std::string, std::string> saved = {
{ "paned_position", "420" },
{ "window_width", "1280" },
{ "window_height", "900" },
};
config_save_map(saved);
std::map<std::string, std::string> loaded = config_load_map();
REQUIRE(loaded == saved);
}
// -----------------------------------------------------------------------------
// Verify that comments and malformed lines do not become config keys.
// -----------------------------------------------------------------------------
TEST_CASE("Config loader ignores comments and malformed lines")
{
reset_test_config_file();
std::filesystem::create_directories(test_config_dir());
std::ofstream file(test_config_file());
REQUIRE(file.good());
file << "# comment\n";
file << "missing-separator\n";
file << "valid=value\n";
file << "another=two=parts\n";
file.close();
std::map<std::string, std::string> loaded = config_load_map();
REQUIRE(loaded.size() == 2);
REQUIRE(loaded["valid"] == "value");
REQUIRE(loaded["another"] == "two=parts");
}
// -----------------------------------------------------------------------------
// Verify that the divider position falls back when the config file is missing.
// -----------------------------------------------------------------------------
TEST_CASE("Config paned position uses default when missing")
{
reset_test_config_file();
REQUIRE(config_load_paned_position() == 300);
}
// -----------------------------------------------------------------------------
// Verify that invalid divider positions are ignored.
// -----------------------------------------------------------------------------
TEST_CASE("Config paned position ignores invalid saved values")
{
reset_test_config_file();
config_save_map({ { "paned_position", "123abc" } });
REQUIRE(config_load_paned_position() == 300);
}
// -----------------------------------------------------------------------------
// Verify that a valid divider position is restored.
// -----------------------------------------------------------------------------
TEST_CASE("Config paned position restores valid saved value")
{
reset_test_config_file();
config_save_map({ { "paned_position", "640" } });
REQUIRE(config_load_paned_position() == 640);
}
// -----------------------------------------------------------------------------
// Verify that package table columns use the default visibility when no setting exists.
// -----------------------------------------------------------------------------
TEST_CASE("Package table columns use default visibility when missing")
{
reset_test_config_file();
std::set<std::string> visible = package_table_load_visible_column_ids();
REQUIRE(visible == default_visible_package_table_columns());
REQUIRE(visible.count("package") == 1);
REQUIRE(visible.count("release") == 0);
REQUIRE(visible.count("unknown-column") == 0);
}
// -----------------------------------------------------------------------------
// Verify that saved hidden package table columns are restored.
// -----------------------------------------------------------------------------
TEST_CASE("Package table columns restore saved hidden ids")
{
reset_test_config_file();
config_save_map({ { "package_table_hidden_columns", "summary" } });
std::set<std::string> visible = package_table_load_visible_column_ids();
REQUIRE(visible.count("summary") == 0);
REQUIRE(visible.count("package") == 1);
REQUIRE(visible.count("release") == 1);
}
// -----------------------------------------------------------------------------
// Verify that unknown package table column ids are ignored.
// -----------------------------------------------------------------------------
TEST_CASE("Package table columns ignore unknown hidden ids")
{
reset_test_config_file();
config_save_map({ { "package_table_hidden_columns", "summary,unknown-column" } });
std::set<std::string> visible = package_table_load_visible_column_ids();
REQUIRE(visible.count("summary") == 0);
REQUIRE(visible.count("package") == 1);
REQUIRE(visible.count("unknown-column") == 0);
}
// -----------------------------------------------------------------------------
// Verify that saving visible package table columns stores hidden ids.
// -----------------------------------------------------------------------------
TEST_CASE("Package table columns save visible ids as hidden settings")
{
reset_test_config_file();
package_table_save_visible_column_ids({ "package", "version" });
std::set<std::string> visible = package_table_load_visible_column_ids();
std::map<std::string, std::string> config = config_load_map();
REQUIRE(visible == std::set<std::string>({ "package", "version" }));
REQUIRE(config.count("package_table_hidden_columns") == 1);
REQUIRE(config.count("package_table_columns") == 0);
}
// -----------------------------------------------------------------------------
// Verify that the final visible package table column cannot be hidden.
// -----------------------------------------------------------------------------
TEST_CASE("Package table columns keep one column visible")
{
std::set<std::string> visible = { "package" };
REQUIRE_FALSE(package_table_update_visible_column_ids(visible, "package", false));
REQUIRE(visible == std::set<std::string>({ "package" }));
REQUIRE(package_table_update_visible_column_ids(visible, "version", true));
REQUIRE(package_table_update_visible_column_ids(visible, "package", false));
REQUIRE(visible == std::set<std::string>({ "version" }));
}
// -----------------------------------------------------------------------------
// Verify that unknown package table column ids cannot change visibility.
// -----------------------------------------------------------------------------
TEST_CASE("Package table columns reject unknown visibility changes")
{
std::set<std::string> visible = { "package" };
REQUIRE_FALSE(package_table_update_visible_column_ids(visible, "unknown-column", true));
REQUIRE(visible == std::set<std::string>({ "package" }));
}
// -----------------------------------------------------------------------------
// Verify that the old visible-column key is migrated to the hidden-column key.
// -----------------------------------------------------------------------------
TEST_CASE("Package table columns migrate old visible settings")
{
reset_test_config_file();
config_save_map({ { "package_table_columns", "package,version,unknown-column" } });
std::set<std::string> visible = package_table_load_visible_column_ids();
std::map<std::string, std::string> config = config_load_map();
REQUIRE(visible == std::set<std::string>({ "package", "version" }));
REQUIRE(config.count("package_table_hidden_columns") == 1);
REQUIRE(config.count("package_table_columns") == 0);
}
// -----------------------------------------------------------------------------
// Verify that resetting package table columns restores the default set.
// -----------------------------------------------------------------------------
TEST_CASE("Package table columns reset to defaults")
{
reset_test_config_file();
package_table_save_visible_column_ids({ "package" });
package_table_reset_visible_column_ids();
REQUIRE(package_table_load_visible_column_ids() == default_visible_package_table_columns());
}
// -----------------------------------------------------------------------------
// EOF
// -----------------------------------------------------------------------------