Line data Source code
1 : // Copyright (c) 2006-2021 Made to Order Software Corp. All Rights Reserved
2 : //
3 : // https://snapwebsites.org/project/advgetopt
4 : // contact@m2osw.com
5 : //
6 : // This program is free software; you can redistribute it and/or modify
7 : // it under the terms of the GNU General Public License as published by
8 : // the Free Software Foundation; either version 2 of the License, or
9 : // (at your option) any later version.
10 : //
11 : // This program is distributed in the hope that it will be useful,
12 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : // GNU General Public License for more details.
15 : //
16 : // You should have received a copy of the GNU General Public License along
17 : // with this program; if not, write to the Free Software Foundation, Inc.,
18 : // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 :
20 : // self
21 : //
22 : #include "catch_main.h"
23 :
24 :
25 : // advgetopt lib
26 : //
27 : #include <advgetopt/exception.h>
28 :
29 :
30 : // snapdev lib
31 : //
32 : #include <snapdev/safe_setenv.h>
33 :
34 :
35 : // C++ lib
36 : //
37 : #include <fstream>
38 :
39 :
40 : // C lib
41 : //
42 : #include <unistd.h>
43 :
44 :
45 : // last include
46 : //
47 : #include <snapdev/poison.h>
48 :
49 :
50 :
51 :
52 :
53 :
54 :
55 11 : CATCH_TEST_CASE("configuration_filenames", "[config][getopt][filenames]")
56 : {
57 18 : CATCH_START_SECTION("Configuration Files")
58 1 : SNAP_CATCH2_NAMESPACE::init_tmp_dir("unittest-any", "any");
59 :
60 1 : advgetopt::options_environment environment_options;
61 :
62 1 : char const * confs[] =
63 : {
64 1 : SNAP_CATCH2_NAMESPACE::g_config_filename.c_str(),
65 : ".config/file.mdi",
66 : "/etc/snapwebsites/server.conf",
67 : nullptr
68 1 : };
69 :
70 1 : environment_options.f_project_name = "unittest-any";
71 1 : environment_options.f_options = nullptr;
72 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
73 1 : environment_options.f_help_header = "Testing all possible filenames";
74 1 : environment_options.f_configuration_files = confs;
75 :
76 2 : advgetopt::getopt opt(environment_options);
77 :
78 2 : advgetopt::string_list_t const filenames(opt.get_configuration_filenames(false, false));
79 :
80 1 : CATCH_REQUIRE(filenames.size() == 6);
81 1 : CATCH_REQUIRE(filenames[0] == SNAP_CATCH2_NAMESPACE::g_config_filename);
82 1 : CATCH_REQUIRE(filenames[1] == SNAP_CATCH2_NAMESPACE::g_config_project_filename);
83 1 : CATCH_REQUIRE(filenames[2] == ".config/file.mdi");
84 1 : CATCH_REQUIRE(filenames[3] == ".config/unittest-any.d/50-file.mdi");
85 1 : CATCH_REQUIRE(filenames[4] == "/etc/snapwebsites/server.conf");
86 1 : CATCH_REQUIRE(filenames[5] == "/etc/snapwebsites/unittest-any.d/50-server.conf");
87 : CATCH_END_SECTION()
88 :
89 18 : CATCH_START_SECTION("Configuration Files (writable)")
90 1 : SNAP_CATCH2_NAMESPACE::init_tmp_dir("unittest-writable", "writable");
91 :
92 1 : advgetopt::options_environment environment_options;
93 :
94 1 : char const * confs[] =
95 : {
96 : ".config/file.mdi",
97 1 : SNAP_CATCH2_NAMESPACE::g_config_filename.c_str(),
98 : "/etc/snapwebsites/server.conf",
99 : nullptr
100 1 : };
101 :
102 1 : environment_options.f_project_name = "unittest-writable";
103 1 : environment_options.f_options = nullptr;
104 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
105 1 : environment_options.f_help_header = "Testing all possible filenames";
106 1 : environment_options.f_configuration_files = confs;
107 :
108 2 : advgetopt::getopt opt(environment_options);
109 :
110 2 : advgetopt::string_list_t const filenames(opt.get_configuration_filenames(false, true));
111 :
112 1 : CATCH_REQUIRE(filenames.size() == 3);
113 1 : CATCH_REQUIRE(filenames[0] == ".config/unittest-writable.d/50-file.mdi");
114 1 : CATCH_REQUIRE(filenames[1] == SNAP_CATCH2_NAMESPACE::g_config_project_filename);
115 1 : CATCH_REQUIRE(filenames[2] == "/etc/snapwebsites/unittest-writable.d/50-server.conf");
116 : CATCH_END_SECTION()
117 :
118 18 : CATCH_START_SECTION("Configuration File + Directories")
119 1 : SNAP_CATCH2_NAMESPACE::init_tmp_dir("unittest-with-directories", "with-dirs", true);
120 :
121 1 : advgetopt::options_environment environment_options;
122 :
123 1 : char const * dirs[] =
124 : {
125 1 : SNAP_CATCH2_NAMESPACE::g_config_filename.c_str(),
126 : ".config",
127 : "/etc/snapwebsites",
128 : nullptr
129 1 : };
130 :
131 1 : environment_options.f_project_name = "unittest-with-directories";
132 1 : environment_options.f_options = nullptr;
133 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
134 1 : environment_options.f_help_header = "Testing all possible filenames";
135 1 : environment_options.f_configuration_filename = "snapfirewall.conf";
136 1 : environment_options.f_configuration_directories = dirs;
137 :
138 2 : advgetopt::getopt opt(environment_options);
139 :
140 2 : advgetopt::string_list_t const filenames(opt.get_configuration_filenames(false, false));
141 :
142 1 : CATCH_REQUIRE(filenames.size() == 6);
143 1 : CATCH_REQUIRE(filenames[0] == SNAP_CATCH2_NAMESPACE::g_config_filename + "/snapfirewall.conf");
144 1 : CATCH_REQUIRE(filenames[1] == SNAP_CATCH2_NAMESPACE::g_config_project_filename + "/50-snapfirewall.conf");
145 1 : CATCH_REQUIRE(filenames[2] == ".config/snapfirewall.conf");
146 1 : CATCH_REQUIRE(filenames[3] == ".config/unittest-with-directories.d/50-snapfirewall.conf");
147 1 : CATCH_REQUIRE(filenames[4] == "/etc/snapwebsites/snapfirewall.conf");
148 1 : CATCH_REQUIRE(filenames[5] == "/etc/snapwebsites/unittest-with-directories.d/50-snapfirewall.conf");
149 : CATCH_END_SECTION()
150 :
151 18 : CATCH_START_SECTION("Configuration File + Directories + '--config-dir'")
152 1 : SNAP_CATCH2_NAMESPACE::init_tmp_dir("unittest-with-directories-and-config-dir", "with-many-dirs", true);
153 :
154 1 : advgetopt::options_environment environment_options;
155 :
156 1 : char const * dirs[] =
157 : {
158 1 : SNAP_CATCH2_NAMESPACE::g_config_filename.c_str(),
159 : ".config",
160 : "/etc/advgetopt",
161 : nullptr
162 1 : };
163 :
164 1 : environment_options.f_project_name = "unittest-with-directories-and-config-dir";
165 1 : environment_options.f_options = nullptr;
166 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
167 1 : environment_options.f_help_header = "Testing all possible filenames";
168 1 : environment_options.f_configuration_filename = "snapmerger.conf";
169 1 : environment_options.f_configuration_directories = dirs;
170 :
171 1 : char const * cargv[] =
172 : {
173 : "/usr/bin/config",
174 : "--config-dir",
175 : "/var/lib/advgetopt",
176 : "--config-dir",
177 : "/opt/config",
178 : nullptr
179 : };
180 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
181 1 : char ** argv = const_cast<char **>(cargv);
182 :
183 2 : advgetopt::getopt opt(environment_options, argc, argv);
184 :
185 2 : advgetopt::string_list_t const filenames(opt.get_configuration_filenames(false, false));
186 :
187 1 : CATCH_REQUIRE(filenames.size() == 10);
188 1 : CATCH_REQUIRE(filenames[0] == "/var/lib/advgetopt/snapmerger.conf");
189 1 : CATCH_REQUIRE(filenames[1] == "/var/lib/advgetopt/unittest-with-directories-and-config-dir.d/50-snapmerger.conf");
190 1 : CATCH_REQUIRE(filenames[2] == "/opt/config/snapmerger.conf");
191 1 : CATCH_REQUIRE(filenames[3] == "/opt/config/unittest-with-directories-and-config-dir.d/50-snapmerger.conf");
192 1 : CATCH_REQUIRE(filenames[4] == SNAP_CATCH2_NAMESPACE::g_config_filename + "/snapmerger.conf");
193 1 : CATCH_REQUIRE(filenames[5] == SNAP_CATCH2_NAMESPACE::g_config_project_filename + "/50-snapmerger.conf");
194 1 : CATCH_REQUIRE(filenames[6] == ".config/snapmerger.conf");
195 1 : CATCH_REQUIRE(filenames[7] == ".config/unittest-with-directories-and-config-dir.d/50-snapmerger.conf");
196 1 : CATCH_REQUIRE(filenames[8] == "/etc/advgetopt/snapmerger.conf");
197 1 : CATCH_REQUIRE(filenames[9] == "/etc/advgetopt/unittest-with-directories-and-config-dir.d/50-snapmerger.conf");
198 : CATCH_END_SECTION()
199 :
200 18 : CATCH_START_SECTION("Existing Configuration Files")
201 :
202 10 : CATCH_WHEN("R/W Config must exist--no user defined config")
203 : {
204 1 : SNAP_CATCH2_NAMESPACE::init_tmp_dir("unittest-must-exist", "must-be-here");
205 :
206 : {
207 2 : std::ofstream config_file;
208 1 : config_file.open(SNAP_CATCH2_NAMESPACE::g_config_filename, std::ios_base::out | std::ios_base::binary | std::ios_base::trunc);
209 1 : CATCH_REQUIRE(config_file.good());
210 1 : config_file <<
211 : "# Auto-generated\n"
212 : "ip=192.168.0.1\n"
213 : "wall=iptables\n"
214 : ;
215 : }
216 :
217 1 : unlink(SNAP_CATCH2_NAMESPACE::g_config_project_filename.c_str());
218 :
219 1 : char const * confs[] =
220 : {
221 1 : SNAP_CATCH2_NAMESPACE::g_config_filename.c_str(),
222 : ".config/file-which-was-never-created.mdi",
223 : "/etc/snapwebsites/not-an-existing-file.conf",
224 : nullptr
225 1 : };
226 :
227 1 : advgetopt::options_environment environment_options;
228 1 : environment_options.f_project_name = "unittest";
229 1 : environment_options.f_options = nullptr;
230 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
231 1 : environment_options.f_help_header = "Testing all possible filenames";
232 1 : environment_options.f_configuration_files = confs;
233 :
234 2 : advgetopt::getopt opt(environment_options);
235 :
236 2 : advgetopt::string_list_t const filenames(opt.get_configuration_filenames(true, false));
237 :
238 1 : CATCH_REQUIRE(filenames.size() == 1);
239 1 : CATCH_REQUIRE(filenames[0] == SNAP_CATCH2_NAMESPACE::g_config_filename);
240 : }
241 :
242 10 : CATCH_WHEN("R/W Config must exist--user defined config exists")
243 : {
244 1 : SNAP_CATCH2_NAMESPACE::init_tmp_dir("unittest-user-exist", "existing");
245 :
246 : {
247 2 : std::ofstream config_file;
248 1 : config_file.open(SNAP_CATCH2_NAMESPACE::g_config_filename, std::ios_base::out | std::ios_base::binary | std::ios_base::trunc);
249 1 : CATCH_REQUIRE(config_file.good());
250 1 : config_file <<
251 : "# Auto-generated\n"
252 : "block-ip=192.168.6.11\n"
253 : "firewall=iptables\n"
254 : ;
255 : }
256 :
257 : {
258 2 : std::ofstream config_file;
259 1 : config_file.open(SNAP_CATCH2_NAMESPACE::g_config_project_filename, std::ios_base::out | std::ios_base::binary | std::ios_base::trunc);
260 1 : CATCH_REQUIRE(config_file.good());
261 1 : config_file <<
262 : "# Auto-generated\n"
263 : "ip=10.0.2.5\n"
264 : "duration=6h\n"
265 : ;
266 : }
267 :
268 1 : char const * confs[] =
269 : {
270 : ".config/file-which-was-never-created.mdi",
271 : "/etc/snapwebsites/not-an-existing-file.conf",
272 1 : SNAP_CATCH2_NAMESPACE::g_config_filename.c_str(),
273 : nullptr
274 1 : };
275 :
276 1 : advgetopt::options_environment environment_options;
277 1 : environment_options.f_project_name = "unittest-user-exist";
278 1 : environment_options.f_options = nullptr;
279 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
280 1 : environment_options.f_help_header = "Testing all possible filenames";
281 1 : environment_options.f_configuration_files = confs;
282 :
283 2 : advgetopt::getopt opt(environment_options);
284 :
285 2 : advgetopt::string_list_t const filenames(opt.get_configuration_filenames(true, false));
286 :
287 1 : CATCH_REQUIRE(filenames.size() == 2);
288 1 : CATCH_REQUIRE(filenames[0] == SNAP_CATCH2_NAMESPACE::g_config_filename);
289 1 : CATCH_REQUIRE(filenames[1] == SNAP_CATCH2_NAMESPACE::g_config_project_filename);
290 : }
291 :
292 10 : CATCH_WHEN("Writable Config must exist--user defined config exists")
293 : {
294 1 : SNAP_CATCH2_NAMESPACE::init_tmp_dir("unittest-writable-exist", "present");
295 :
296 : {
297 2 : std::ofstream config_file;
298 1 : config_file.open(SNAP_CATCH2_NAMESPACE::g_config_filename, std::ios_base::out | std::ios_base::binary | std::ios_base::trunc);
299 1 : CATCH_REQUIRE(config_file.good());
300 1 : config_file <<
301 : "# Auto-generated\n"
302 : "block-ip=192.168.6.11\n"
303 : "firewall=iptables\n"
304 : ;
305 : }
306 :
307 : {
308 2 : std::ofstream config_file;
309 1 : config_file.open(SNAP_CATCH2_NAMESPACE::g_config_project_filename, std::ios_base::out | std::ios_base::binary | std::ios_base::trunc);
310 1 : CATCH_REQUIRE(config_file.good());
311 1 : config_file <<
312 : "# Auto-generated\n"
313 : "ip=10.0.2.5\n"
314 : "duration=6h\n"
315 : ;
316 : }
317 :
318 1 : char const * confs[] =
319 : {
320 : ".config/file-which-was-never-created.mdi",
321 1 : SNAP_CATCH2_NAMESPACE::g_config_filename.c_str(),
322 : "/etc/snapwebsites/not/an-existing-file.conf",
323 : nullptr
324 1 : };
325 :
326 1 : advgetopt::options_environment environment_options;
327 1 : environment_options.f_project_name = "unittest-writable-exist";
328 1 : environment_options.f_options = nullptr;
329 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
330 1 : environment_options.f_help_header = "Testing all possible filenames";
331 1 : environment_options.f_configuration_files = confs;
332 :
333 2 : advgetopt::getopt opt(environment_options);
334 :
335 2 : advgetopt::string_list_t const filenames(opt.get_configuration_filenames(true, true));
336 :
337 1 : CATCH_REQUIRE(filenames.size() == 1);
338 1 : CATCH_REQUIRE(filenames[0] == SNAP_CATCH2_NAMESPACE::g_config_project_filename);
339 : }
340 :
341 10 : CATCH_WHEN("Writable Config must exist--user defined config exists and we test with a user folder")
342 : {
343 1 : SNAP_CATCH2_NAMESPACE::init_tmp_dir("unittest-writable-user", "user-write");
344 :
345 : {
346 2 : std::ofstream config_file;
347 1 : config_file.open(SNAP_CATCH2_NAMESPACE::g_config_filename, std::ios_base::out | std::ios_base::binary | std::ios_base::trunc);
348 1 : CATCH_REQUIRE(config_file.good());
349 1 : config_file <<
350 : "# Auto-generated\n"
351 : "block-ip=192.168.6.11\n"
352 : "firewall=iptables\n"
353 : ;
354 : }
355 :
356 : {
357 2 : std::ofstream config_file;
358 1 : config_file.open(SNAP_CATCH2_NAMESPACE::g_config_project_filename, std::ios_base::out | std::ios_base::binary | std::ios_base::trunc);
359 1 : CATCH_REQUIRE(config_file.good());
360 1 : config_file <<
361 : "# Auto-generated\n"
362 : "ip=10.0.2.5\n"
363 : "duration=6h\n"
364 : ;
365 : }
366 :
367 1 : char const * confs[] =
368 : {
369 : "~/.config/file-which-was-never-created.mdi",
370 1 : SNAP_CATCH2_NAMESPACE::g_config_filename.c_str(),
371 : "/etc/snapwebsites/not/an-existing-file.conf",
372 : nullptr
373 1 : };
374 :
375 1 : advgetopt::options_environment environment_options;
376 1 : environment_options.f_project_name = "unittest-writable-user";
377 1 : environment_options.f_options = nullptr;
378 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
379 1 : environment_options.f_help_header = "Testing all possible filenames";
380 1 : environment_options.f_configuration_files = confs;
381 :
382 2 : advgetopt::getopt opt(environment_options);
383 :
384 2 : advgetopt::string_list_t const filenames(opt.get_configuration_filenames(true, true));
385 :
386 1 : CATCH_REQUIRE(filenames.size() == 1);
387 1 : CATCH_REQUIRE(filenames[0] == SNAP_CATCH2_NAMESPACE::g_config_project_filename);
388 : }
389 :
390 10 : CATCH_WHEN("R/W Config test must exist--user defined config exists and we test with a user folder")
391 : {
392 1 : SNAP_CATCH2_NAMESPACE::init_tmp_dir("unittest-user-folder", "tilde");
393 :
394 2 : std::string tmpdir(SNAP_CATCH2_NAMESPACE::g_tmp_dir);
395 1 : tmpdir += "/.config/home-that-never-gets-created";
396 2 : snap::safe_setenv env("HOME", tmpdir);
397 :
398 : {
399 2 : std::ofstream config_file;
400 1 : config_file.open(SNAP_CATCH2_NAMESPACE::g_config_project_filename, std::ios_base::out | std::ios_base::binary | std::ios_base::trunc);
401 1 : CATCH_REQUIRE(config_file.good());
402 1 : config_file <<
403 : "# Auto-generated\n"
404 : "ip=10.0.2.5\n"
405 : "duration=6h\n"
406 : ;
407 : }
408 :
409 1 : char const * dirs[] =
410 : {
411 : "~/.config/folder-which-was-never-created",
412 : "/etc/snapwebsites/not-an-existing-folder",
413 1 : SNAP_CATCH2_NAMESPACE::g_config_filename.c_str(),
414 : nullptr
415 1 : };
416 :
417 1 : advgetopt::options_environment environment_options;
418 1 : environment_options.f_project_name = "unittest-user-folder";
419 1 : environment_options.f_options = nullptr;
420 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
421 1 : environment_options.f_help_header = "Testing all possible filenames";
422 1 : environment_options.f_configuration_filename = "snapfirewall.conf";
423 1 : environment_options.f_configuration_directories = dirs;
424 :
425 2 : advgetopt::getopt opt(environment_options);
426 :
427 2 : advgetopt::string_list_t const filenames(opt.get_configuration_filenames(false, false));
428 :
429 1 : CATCH_REQUIRE(filenames.size() == 5);
430 1 : CATCH_REQUIRE(filenames[0] == tmpdir + "/.config/folder-which-was-never-created/snapfirewall.conf");
431 1 : CATCH_REQUIRE(filenames[1] == "/etc/snapwebsites/not-an-existing-folder/snapfirewall.conf");
432 1 : CATCH_REQUIRE(filenames[2] == "/etc/snapwebsites/not-an-existing-folder/unittest-user-folder.d/50-snapfirewall.conf");
433 1 : CATCH_REQUIRE(filenames[3] == SNAP_CATCH2_NAMESPACE::g_config_filename + "/snapfirewall.conf");
434 1 : CATCH_REQUIRE(filenames[4] == SNAP_CATCH2_NAMESPACE::g_config_filename + "/unittest-user-folder.d/50-snapfirewall.conf");
435 : }
436 : CATCH_END_SECTION()
437 9 : }
438 :
439 :
440 :
441 5 : CATCH_TEST_CASE("load_configuration_file", "[config][getopt][filenames]")
442 : {
443 6 : CATCH_START_SECTION("Load a Configuration File")
444 : {
445 1 : SNAP_CATCH2_NAMESPACE::init_tmp_dir("load", "tool");
446 :
447 : {
448 2 : std::ofstream config_file;
449 1 : config_file.open(SNAP_CATCH2_NAMESPACE::g_config_filename, std::ios_base::out | std::ios_base::binary | std::ios_base::trunc);
450 1 : CATCH_REQUIRE(config_file.good());
451 1 : config_file <<
452 : "# Auto-generated\n"
453 : "sizes=132\n"
454 : "filenames=green,orange,blue brown white\n"
455 : ;
456 : }
457 :
458 1 : char const * confs[] =
459 : {
460 : "~/.config/file-which-was-never-created.mdi",
461 1 : SNAP_CATCH2_NAMESPACE::g_config_filename.c_str(),
462 : "/etc/snapwebsites/not/an-existing-file.conf",
463 : nullptr
464 1 : };
465 :
466 1 : char const * const separators[] {
467 : ",",
468 : " ",
469 : nullptr
470 : };
471 :
472 1 : advgetopt::option const options[] =
473 : {
474 : advgetopt::define_option(
475 : advgetopt::Name("sizes")
476 : , advgetopt::ShortName('s')
477 : , advgetopt::Flags(advgetopt::all_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
478 : , advgetopt::Help("sizes.")
479 : ),
480 : advgetopt::define_option(
481 : advgetopt::Name("filenames")
482 : , advgetopt::Flags(advgetopt::all_flags<advgetopt::GETOPT_FLAG_REQUIRED, advgetopt::GETOPT_FLAG_MULTIPLE>())
483 : , advgetopt::Help("enter a list of filenames.")
484 : , advgetopt::DefaultValue("a.out")
485 : , advgetopt::Separators(separators)
486 6 : ),
487 : advgetopt::end_options()
488 7 : };
489 :
490 1 : advgetopt::options_environment environment_options;
491 1 : environment_options.f_project_name = "load";
492 1 : environment_options.f_options = options;
493 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
494 1 : environment_options.f_help_header = "Testing loading a filenames";
495 1 : environment_options.f_configuration_files = confs;
496 :
497 2 : advgetopt::getopt opt(environment_options);
498 :
499 1 : opt.process_configuration_file(SNAP_CATCH2_NAMESPACE::g_config_filename);
500 :
501 1 : CATCH_REQUIRE(opt.size("sizes") == 1);
502 1 : CATCH_REQUIRE(opt.get_string("sizes") == "132");
503 :
504 1 : CATCH_REQUIRE(opt.size("filenames") == 5);
505 1 : CATCH_REQUIRE(opt.get_string("filenames") == "green");
506 1 : CATCH_REQUIRE(opt.get_string("filenames", 0) == "green");
507 1 : CATCH_REQUIRE(opt.get_string("filenames", 1) == "orange");
508 1 : CATCH_REQUIRE(opt.get_string("filenames", 2) == "blue");
509 1 : CATCH_REQUIRE(opt.get_string("filenames", 3) == "brown");
510 1 : CATCH_REQUIRE(opt.get_string("filenames", 4) == "white");
511 : }
512 : CATCH_END_SECTION()
513 :
514 6 : CATCH_START_SECTION("Load an Extended Configuration File")
515 : {
516 1 : SNAP_CATCH2_NAMESPACE::init_tmp_dir("load-extended", "extended");
517 :
518 : {
519 2 : std::ofstream config_file;
520 1 : config_file.open(SNAP_CATCH2_NAMESPACE::g_config_filename, std::ios_base::out | std::ios_base::binary | std::ios_base::trunc);
521 1 : CATCH_REQUIRE(config_file.good());
522 1 : config_file <<
523 : "# Auto-generated\n"
524 : "sizes=132\n"
525 : "object=property.obj\n"
526 : "filenames=green,orange,blue brown white\n"
527 : "visibility=hidden\n"
528 : ;
529 : }
530 :
531 1 : char const * confs[] =
532 : {
533 : "~/.config/file-which-was-never-created.mdi",
534 1 : SNAP_CATCH2_NAMESPACE::g_config_filename.c_str(),
535 : "/etc/snapwebsites/not/an-existing-file.conf",
536 : nullptr
537 1 : };
538 :
539 1 : char const * const separators[] {
540 : ",",
541 : " ",
542 : nullptr
543 : };
544 :
545 1 : advgetopt::option const options[] =
546 : {
547 : advgetopt::define_option(
548 : advgetopt::Name("sizes")
549 : , advgetopt::ShortName('s')
550 : , advgetopt::Flags(advgetopt::all_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
551 : , advgetopt::Help("sizes.")
552 : ),
553 : advgetopt::define_option(
554 : advgetopt::Name("filenames")
555 : , advgetopt::Flags(advgetopt::all_flags<advgetopt::GETOPT_FLAG_REQUIRED, advgetopt::GETOPT_FLAG_MULTIPLE>())
556 : , advgetopt::Help("enter a list of filenames.")
557 : , advgetopt::DefaultValue("a.out")
558 : , advgetopt::Separators(separators)
559 6 : ),
560 : advgetopt::end_options()
561 7 : };
562 :
563 1 : advgetopt::options_environment environment_options;
564 1 : environment_options.f_project_name = "load-extended";
565 1 : environment_options.f_options = options;
566 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS | advgetopt::GETOPT_ENVIRONMENT_FLAG_DYNAMIC_PARAMETERS;
567 1 : environment_options.f_help_header = "Testing loading filenames";
568 1 : environment_options.f_configuration_files = confs;
569 :
570 2 : advgetopt::getopt opt(environment_options);
571 :
572 1 : opt.process_configuration_file(SNAP_CATCH2_NAMESPACE::g_config_filename);
573 :
574 1 : CATCH_REQUIRE(opt.size("sizes") == 1);
575 1 : CATCH_REQUIRE(opt.get_string("sizes") == "132");
576 :
577 1 : CATCH_REQUIRE(opt.size("filenames") == 5);
578 1 : CATCH_REQUIRE(opt.get_string("filenames") == "green");
579 1 : CATCH_REQUIRE(opt.get_string("filenames", 0) == "green");
580 1 : CATCH_REQUIRE(opt.get_string("filenames", 1) == "orange");
581 1 : CATCH_REQUIRE(opt.get_string("filenames", 2) == "blue");
582 1 : CATCH_REQUIRE(opt.get_string("filenames", 3) == "brown");
583 1 : CATCH_REQUIRE(opt.get_string("filenames", 4) == "white");
584 :
585 1 : CATCH_REQUIRE(opt.size("object") == 1);
586 1 : CATCH_REQUIRE(opt.get_string("object") == "property.obj");
587 :
588 1 : CATCH_REQUIRE(opt.size("visibility") == 1);
589 1 : CATCH_REQUIRE(opt.get_string("visibility") == "hidden");
590 : }
591 : CATCH_END_SECTION()
592 :
593 6 : CATCH_START_SECTION("Load a Configuration File with Sections")
594 : {
595 1 : SNAP_CATCH2_NAMESPACE::init_tmp_dir("load-with-sections", "sections");
596 :
597 : {
598 2 : std::ofstream config_file;
599 1 : config_file.open(SNAP_CATCH2_NAMESPACE::g_config_filename, std::ios_base::out | std::ios_base::binary | std::ios_base::trunc);
600 1 : CATCH_REQUIRE(config_file.good());
601 1 : config_file <<
602 : "# Auto-generated\n"
603 : "\n"
604 : "[integers]\n"
605 : "sizes=132\n"
606 : "\n"
607 : "[objects]\n"
608 : "object=property.obj\n"
609 : "filenames=green orange blue brown white\n"
610 : "\n"
611 : "[flags]\n"
612 : "visibility=hidden\n"
613 : "\n"
614 : "[integers]\n"
615 : "max=1111\n"
616 : "\n"
617 : "# vim: ts=4 sw=4 et\n"
618 : ;
619 : }
620 :
621 1 : char const * confs[] =
622 : {
623 : "~/.config/file-which-was-never-created.mdi",
624 1 : SNAP_CATCH2_NAMESPACE::g_config_filename.c_str(),
625 : "/etc/snapwebsites/not/an-existing-file.conf",
626 : nullptr
627 1 : };
628 :
629 1 : char const * const separators[] {
630 : ",",
631 : " ",
632 : nullptr
633 : };
634 :
635 1 : advgetopt::option const options[] =
636 : {
637 : advgetopt::define_option(
638 : advgetopt::Name("objects::object")
639 : , advgetopt::Flags(advgetopt::all_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
640 : , advgetopt::Help("object.")
641 : ),
642 : advgetopt::define_option(
643 : advgetopt::Name("integers::sizes")
644 : , advgetopt::ShortName('s')
645 : , advgetopt::Flags(advgetopt::all_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
646 : , advgetopt::Help("sizes.")
647 : ),
648 : advgetopt::define_option(
649 : advgetopt::Name("objects::filenames")
650 : , advgetopt::Flags(advgetopt::all_flags<advgetopt::GETOPT_FLAG_REQUIRED, advgetopt::GETOPT_FLAG_MULTIPLE>())
651 : , advgetopt::Help("enter a list of filenames.")
652 : , advgetopt::DefaultValue("a.out")
653 : , advgetopt::Separators(separators)
654 6 : ),
655 : advgetopt::define_option(
656 : advgetopt::Name("integers::max")
657 : , advgetopt::Flags(advgetopt::all_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
658 : , advgetopt::Help("maximum value.")
659 : , advgetopt::DefaultValue("+oo")
660 : ),
661 : advgetopt::define_option(
662 : advgetopt::Name("flags::visibility")
663 : , advgetopt::Flags(advgetopt::all_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
664 : , advgetopt::Help("visibility.")
665 : , advgetopt::DefaultValue("flashy")
666 : ),
667 : advgetopt::end_options()
668 7 : };
669 :
670 1 : advgetopt::options_environment environment_options;
671 1 : environment_options.f_project_name = "load-sections";
672 1 : environment_options.f_options = options;
673 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
674 1 : environment_options.f_help_header = "Testing loading sections";
675 1 : environment_options.f_configuration_files = confs;
676 :
677 2 : advgetopt::getopt opt(environment_options);
678 :
679 1 : opt.process_configuration_file(SNAP_CATCH2_NAMESPACE::g_config_filename);
680 :
681 1 : CATCH_REQUIRE(opt.size("integers::sizes") == 1);
682 1 : CATCH_REQUIRE(opt.get_string("integers::sizes") == "132");
683 :
684 1 : CATCH_REQUIRE(opt.size("objects::filenames") == 5);
685 1 : CATCH_REQUIRE(opt.get_string("objects::filenames") == "green");
686 1 : CATCH_REQUIRE(opt.get_string("objects::filenames", 0) == "green");
687 1 : CATCH_REQUIRE(opt.get_string("objects::filenames", 1) == "orange");
688 1 : CATCH_REQUIRE(opt.get_string("objects::filenames", 2) == "blue");
689 1 : CATCH_REQUIRE(opt.get_string("objects::filenames", 3) == "brown");
690 1 : CATCH_REQUIRE(opt.get_string("objects::filenames", 4) == "white");
691 :
692 1 : CATCH_REQUIRE(opt.size("integers::max") == 1);
693 1 : CATCH_REQUIRE(opt.get_string("integers::max") == "1111");
694 :
695 1 : CATCH_REQUIRE(opt.size("objects::object") == 1);
696 1 : CATCH_REQUIRE(opt.get_string("objects::object") == "property.obj");
697 :
698 1 : CATCH_REQUIRE(opt.size("flags::visibility") == 1);
699 1 : CATCH_REQUIRE(opt.get_string("flags::visibility") == "hidden");
700 :
701 2 : std::string const name(advgetopt::CONFIGURATION_SECTIONS);
702 1 : CATCH_REQUIRE(opt.size(name) == 3);
703 1 : CATCH_REQUIRE(opt.get_string(name) == "flags");
704 1 : CATCH_REQUIRE(opt.get_string(name, 0) == "flags");
705 1 : CATCH_REQUIRE(opt.get_string(name, 1) == "integers");
706 1 : CATCH_REQUIRE(opt.get_string(name, 2) == "objects");
707 : }
708 : CATCH_END_SECTION()
709 3 : }
710 :
711 :
712 :
713 4 : CATCH_TEST_CASE("load_multiple_configurations", "[config][getopt][filenames]")
714 : {
715 4 : CATCH_START_SECTION("Configuration Files")
716 : {
717 1 : SNAP_CATCH2_NAMESPACE::init_tmp_dir("multiple", "multiplicity");
718 :
719 1 : advgetopt::options_environment environment_options;
720 :
721 2 : std::string tmpdir(SNAP_CATCH2_NAMESPACE::g_tmp_dir);
722 1 : tmpdir += "/.config/home";
723 :
724 2 : std::stringstream ss;
725 1 : ss << "mkdir -p " << tmpdir;
726 1 : if(system(ss.str().c_str()) != 0)
727 : {
728 0 : std::cerr << "fatal error: creating sub-temporary directory \"" << tmpdir << "\" failed.\n";
729 0 : exit(1);
730 : }
731 :
732 2 : snap::safe_setenv env("HOME", tmpdir);
733 :
734 : {
735 2 : std::ofstream config_file;
736 1 : config_file.open(SNAP_CATCH2_NAMESPACE::g_config_filename, std::ios_base::out | std::ios_base::binary | std::ios_base::trunc);
737 1 : CATCH_REQUIRE(config_file.good());
738 1 : config_file <<
739 : "# Auto-generated\n"
740 : "ip=10.0.2.5\n"
741 : "duration=6h\n"
742 : "size=604\n"
743 : "gap=6\n"
744 : "filename=utf9.txt\n"
745 : ;
746 : }
747 :
748 : {
749 2 : std::ofstream config_file;
750 1 : config_file.open(SNAP_CATCH2_NAMESPACE::g_config_project_filename, std::ios_base::out | std::ios_base::binary | std::ios_base::trunc);
751 1 : CATCH_REQUIRE(config_file.good());
752 1 : config_file <<
753 : "# Auto-generated\n"
754 : "ip=10.1.7.205\n"
755 : "gap=9\n"
756 : "filename=utf7.txt\n"
757 : ;
758 : }
759 :
760 : {
761 2 : std::ofstream config_file;
762 1 : config_file.open(tmpdir + "/advgetopt.conf", std::ios_base::out | std::ios_base::binary | std::ios_base::trunc);
763 1 : CATCH_REQUIRE(config_file.good());
764 1 : config_file <<
765 : "# Auto-generated\n"
766 : "duration=105min\n"
767 : "filename=utf8.txt\n"
768 : ;
769 : }
770 :
771 1 : char const * confs[] =
772 : {
773 1 : SNAP_CATCH2_NAMESPACE::g_config_filename.c_str(),
774 1 : SNAP_CATCH2_NAMESPACE::g_config_project_filename.c_str(),
775 : "~/advgetopt.conf",
776 : nullptr
777 2 : };
778 :
779 1 : advgetopt::option const options[] =
780 : {
781 : advgetopt::define_option(
782 : advgetopt::Name("size")
783 : , advgetopt::ShortName('s')
784 : , advgetopt::Flags(advgetopt::all_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
785 : , advgetopt::Help("size.")
786 : ),
787 : advgetopt::define_option(
788 : advgetopt::Name("filename")
789 : , advgetopt::Flags(advgetopt::all_flags<advgetopt::GETOPT_FLAG_REQUIRED, advgetopt::GETOPT_FLAG_MULTIPLE>())
790 : , advgetopt::Help("enter a filenames.")
791 : , advgetopt::DefaultValue("a.out")
792 : ),
793 : advgetopt::define_option(
794 : advgetopt::Name("duration")
795 : , advgetopt::Flags(advgetopt::all_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
796 : , advgetopt::Help("how long it lasts.")
797 : ),
798 : advgetopt::define_option(
799 : advgetopt::Name("gap")
800 : , advgetopt::Flags(advgetopt::all_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
801 : , advgetopt::Help("gap size.")
802 : ),
803 : advgetopt::define_option(
804 : advgetopt::Name("ip")
805 : , advgetopt::Flags(advgetopt::all_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
806 : , advgetopt::Help("enter the ip address.")
807 : ),
808 : advgetopt::end_options()
809 : };
810 :
811 1 : environment_options.f_project_name = "unittest";
812 1 : environment_options.f_options = options;
813 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
814 1 : environment_options.f_help_header = "Testing a load with multiple filenames and see that we get the latest";
815 1 : environment_options.f_configuration_files = confs;
816 :
817 2 : advgetopt::getopt opt(environment_options);
818 :
819 1 : opt.parse_configuration_files(0, nullptr);
820 :
821 1 : CATCH_REQUIRE(opt.size("size") == 1);
822 1 : CATCH_REQUIRE(opt.get_string("size") == "604");
823 :
824 : // although it is marked as multiple, the old entries are still
825 : // overwritten with newer versions; if the last entry had multiple
826 : // filenames, then we'd get get multiple names here
827 : //
828 1 : CATCH_REQUIRE(opt.size("filename") == 1);
829 1 : CATCH_REQUIRE(opt.get_string("filename", 0) == "utf8.txt");
830 :
831 1 : CATCH_REQUIRE(opt.size("duration") == 1);
832 1 : CATCH_REQUIRE(opt.get_string("duration") == "105min");
833 :
834 1 : CATCH_REQUIRE(opt.size("gap") == 1);
835 1 : CATCH_REQUIRE(opt.get_string("gap") == "9");
836 :
837 1 : CATCH_REQUIRE(opt.size("ip") == 1);
838 1 : CATCH_REQUIRE(opt.get_string("ip") == "10.1.7.205");
839 : }
840 : CATCH_END_SECTION()
841 :
842 4 : CATCH_START_SECTION("Configuration Files with Sections")
843 : {
844 1 : SNAP_CATCH2_NAMESPACE::init_tmp_dir("multiple-with-sections", "multiplicity-with-sections");
845 :
846 1 : advgetopt::options_environment environment_options;
847 :
848 2 : std::string tmpdir(SNAP_CATCH2_NAMESPACE::g_tmp_dir);
849 1 : tmpdir += "/.config/home2";
850 :
851 2 : std::stringstream ss;
852 1 : ss << "mkdir -p " << tmpdir;
853 1 : if(system(ss.str().c_str()) != 0)
854 : {
855 0 : std::cerr << "fatal error: creating sub-temporary directory \"" << tmpdir << "\" failed.\n";
856 0 : exit(1);
857 : }
858 :
859 2 : snap::safe_setenv env("HOME", tmpdir);
860 :
861 : {
862 2 : std::ofstream config_file;
863 1 : config_file.open(SNAP_CATCH2_NAMESPACE::g_config_filename, std::ios_base::out | std::ios_base::binary | std::ios_base::trunc);
864 1 : CATCH_REQUIRE(config_file.good());
865 1 : config_file <<
866 : "# Auto-generated\n"
867 : "\n"
868 : "[connection]\n"
869 : "ip=10.0.2.5\n"
870 : "duration=6h\n"
871 : "size=604\n"
872 : "\n"
873 : "[data-settings]\n"
874 : "gap=6\n"
875 : "filename=utf9.txt\n"
876 : ;
877 : }
878 :
879 : {
880 2 : std::ofstream config_file;
881 1 : config_file.open(SNAP_CATCH2_NAMESPACE::g_config_project_filename, std::ios_base::out | std::ios_base::binary | std::ios_base::trunc);
882 1 : CATCH_REQUIRE(config_file.good());
883 1 : config_file <<
884 : "# Auto-generated\n"
885 : "\n"
886 : "[connection]\n"
887 : "duration=3min\n"
888 : "\n"
889 : "[data-settings]\n"
890 : "gap=9\n"
891 : "filename=utf7.txt\n"
892 : ;
893 : }
894 :
895 : {
896 2 : std::ofstream config_file;
897 1 : config_file.open(tmpdir + "/advgetopt.conf", std::ios_base::out | std::ios_base::binary | std::ios_base::trunc);
898 1 : CATCH_REQUIRE(config_file.good());
899 1 : config_file <<
900 : "# Auto-generated\n"
901 : "\n"
902 : "[connection]\n"
903 : "ip=192.168.255.3\n"
904 : "\n"
905 : "[data-settings]\n"
906 : "filename=utf8.txt\n"
907 : ;
908 : }
909 :
910 1 : char const * confs[] =
911 : {
912 1 : SNAP_CATCH2_NAMESPACE::g_config_filename.c_str(),
913 1 : SNAP_CATCH2_NAMESPACE::g_config_project_filename.c_str(),
914 : "~/advgetopt.conf",
915 : nullptr
916 2 : };
917 :
918 1 : advgetopt::option const options[] =
919 : {
920 : advgetopt::define_option(
921 : advgetopt::Name("connection::size")
922 : , advgetopt::ShortName('s')
923 : , advgetopt::Flags(advgetopt::all_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
924 : , advgetopt::Help("size.")
925 : ),
926 : advgetopt::define_option(
927 : advgetopt::Name("data-settings::filename")
928 : , advgetopt::Flags(advgetopt::all_flags<advgetopt::GETOPT_FLAG_REQUIRED, advgetopt::GETOPT_FLAG_MULTIPLE>())
929 : , advgetopt::Help("enter a filenames.")
930 : , advgetopt::DefaultValue("a.out")
931 : ),
932 : advgetopt::define_option(
933 : advgetopt::Name("connection::duration")
934 : , advgetopt::Flags(advgetopt::all_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
935 : , advgetopt::Help("how long it lasts.")
936 : ),
937 : advgetopt::define_option(
938 : advgetopt::Name("data-settings::gap")
939 : , advgetopt::Flags(advgetopt::all_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
940 : , advgetopt::Help("gap size.")
941 : ),
942 : advgetopt::define_option(
943 : advgetopt::Name("connection::ip")
944 : , advgetopt::Flags(advgetopt::all_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
945 : , advgetopt::Help("enter the ip address.")
946 : ),
947 : advgetopt::end_options()
948 : };
949 :
950 1 : environment_options.f_project_name = "unittest";
951 1 : environment_options.f_options = options;
952 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
953 1 : environment_options.f_help_header = "Testing a load with multiple filenames and see that we get the latest";
954 1 : environment_options.f_configuration_files = confs;
955 :
956 2 : advgetopt::getopt opt(environment_options);
957 :
958 1 : opt.parse_configuration_files(0, nullptr);
959 :
960 1 : CATCH_REQUIRE(opt.size("connection::size") == 1);
961 1 : CATCH_REQUIRE(opt.get_string("connection::size") == "604");
962 :
963 : // although it is marked as multiple, the old entries are still
964 : // overwritten with newer versions; if the last entry had multiple
965 : // filenames, then we'd get get multiple names here
966 : //
967 1 : CATCH_REQUIRE(opt.size("data-settings::filename") == 1);
968 1 : CATCH_REQUIRE(opt.get_string("data-settings::filename", 0) == "utf8.txt");
969 :
970 1 : CATCH_REQUIRE(opt.size("connection::duration") == 1);
971 1 : CATCH_REQUIRE(opt.get_string("connection::duration") == "3min");
972 :
973 1 : CATCH_REQUIRE(opt.size("data-settings::gap") == 1);
974 1 : CATCH_REQUIRE(opt.get_string("data-settings::gap") == "9");
975 :
976 1 : CATCH_REQUIRE(opt.size("connection::ip") == 1);
977 1 : CATCH_REQUIRE(opt.get_string("connection::ip") == "192.168.255.3");
978 :
979 2 : std::string const name(advgetopt::CONFIGURATION_SECTIONS);
980 1 : CATCH_REQUIRE(opt.size(name) == 2);
981 1 : CATCH_REQUIRE(opt.get_string(name) == "connection");
982 1 : CATCH_REQUIRE(opt.get_string(name, 1) == "data-settings");
983 : }
984 : CATCH_END_SECTION()
985 2 : }
986 :
987 :
988 :
989 8 : CATCH_TEST_CASE("load_invalid_configuration_file", "[config][getopt][filenames][invalid]")
990 : {
991 12 : CATCH_START_SECTION("Load with Unexpected Parameter Name (one letter--dynamic allowed)")
992 : {
993 1 : SNAP_CATCH2_NAMESPACE::init_tmp_dir("loading-invalid", "invalid-one-letter");
994 :
995 : {
996 2 : std::ofstream config_file;
997 1 : config_file.open(SNAP_CATCH2_NAMESPACE::g_config_filename, std::ios_base::out | std::ios_base::binary | std::ios_base::trunc);
998 1 : CATCH_REQUIRE(config_file.good());
999 1 : config_file <<
1000 : "# Auto-generated\n"
1001 : "sizes=-132\n"
1002 : "f=dynamic\n"
1003 : ;
1004 : }
1005 :
1006 1 : char const * confs[] =
1007 : {
1008 : "~/.config/file-which-was-never-created.mdi",
1009 1 : SNAP_CATCH2_NAMESPACE::g_config_filename.c_str(),
1010 : "/etc/snapwebsites/not/an-existing-file.conf",
1011 : nullptr
1012 1 : };
1013 :
1014 1 : char const * const separators[] {
1015 : ",",
1016 : " ",
1017 : nullptr
1018 : };
1019 :
1020 1 : advgetopt::option const options[] =
1021 : {
1022 : advgetopt::define_option(
1023 : advgetopt::Name("sizes")
1024 : , advgetopt::ShortName('s')
1025 : , advgetopt::Flags(advgetopt::all_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
1026 : , advgetopt::Help("sizes.")
1027 : ),
1028 : advgetopt::define_option(
1029 : advgetopt::Name("filenames")
1030 : , advgetopt::Flags(advgetopt::all_flags<advgetopt::GETOPT_FLAG_REQUIRED, advgetopt::GETOPT_FLAG_MULTIPLE>())
1031 : , advgetopt::Help("enter a list of filenames.")
1032 : , advgetopt::DefaultValue("a.out")
1033 : , advgetopt::Separators(separators)
1034 6 : ),
1035 : advgetopt::end_options()
1036 7 : };
1037 :
1038 1 : advgetopt::options_environment environment_options;
1039 1 : environment_options.f_project_name = "loading-invalid";
1040 1 : environment_options.f_options = options;
1041 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS | advgetopt::GETOPT_ENVIRONMENT_FLAG_DYNAMIC_PARAMETERS;
1042 1 : environment_options.f_help_header = "Testing loading a one letter parameter";
1043 1 : environment_options.f_configuration_files = confs;
1044 :
1045 2 : advgetopt::getopt opt(environment_options);
1046 :
1047 2 : SNAP_CATCH2_NAMESPACE::push_expected_log(
1048 : "error: unknown option \"f\" found in configuration file \""
1049 2 : + SNAP_CATCH2_NAMESPACE::g_config_filename
1050 3 : + "\".");
1051 1 : opt.process_configuration_file(SNAP_CATCH2_NAMESPACE::g_config_filename);
1052 1 : SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty();
1053 :
1054 1 : CATCH_REQUIRE(opt.size("sizes") == 1);
1055 1 : CATCH_REQUIRE(opt.get_string("sizes") == "-132");
1056 1 : CATCH_REQUIRE(opt.get_long("sizes") == -132);
1057 :
1058 1 : CATCH_REQUIRE(opt.size("filenames") == 0);
1059 : }
1060 : CATCH_END_SECTION()
1061 :
1062 12 : CATCH_START_SECTION("Load with Unexpected Parameter Name (one letter--no dynamic allowed)")
1063 : {
1064 1 : SNAP_CATCH2_NAMESPACE::init_tmp_dir("loading-undefined", "undefined-one-letter");
1065 :
1066 : {
1067 2 : std::ofstream config_file;
1068 1 : config_file.open(SNAP_CATCH2_NAMESPACE::g_config_filename, std::ios_base::out | std::ios_base::binary | std::ios_base::trunc);
1069 1 : CATCH_REQUIRE(config_file.good());
1070 1 : config_file <<
1071 : "# Auto-generated\n"
1072 : "sizes=-132\n"
1073 : "f=dynamic\n"
1074 : ;
1075 : }
1076 :
1077 1 : char const * confs[] =
1078 : {
1079 : "~/.config/file-which-was-never-created.mdi",
1080 1 : SNAP_CATCH2_NAMESPACE::g_config_filename.c_str(),
1081 : "/etc/snapwebsites/not/an-existing-file.conf",
1082 : nullptr
1083 1 : };
1084 :
1085 1 : char const * const separators[] {
1086 : ",",
1087 : " ",
1088 : nullptr
1089 : };
1090 :
1091 1 : advgetopt::option const options[] =
1092 : {
1093 : advgetopt::define_option(
1094 : advgetopt::Name("sizes")
1095 : , advgetopt::ShortName('s')
1096 : , advgetopt::Flags(advgetopt::all_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
1097 : , advgetopt::Help("sizes.")
1098 : ),
1099 : advgetopt::define_option(
1100 : advgetopt::Name("filenames")
1101 : , advgetopt::Flags(advgetopt::all_flags<advgetopt::GETOPT_FLAG_REQUIRED, advgetopt::GETOPT_FLAG_MULTIPLE>())
1102 : , advgetopt::Help("enter a list of filenames.")
1103 : , advgetopt::DefaultValue("a.out")
1104 : , advgetopt::Separators(separators)
1105 6 : ),
1106 : advgetopt::end_options()
1107 7 : };
1108 :
1109 1 : advgetopt::options_environment environment_options;
1110 1 : environment_options.f_project_name = "loading-invalid";
1111 1 : environment_options.f_options = options;
1112 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
1113 1 : environment_options.f_help_header = "Testing loading a one letter parameter";
1114 1 : environment_options.f_configuration_files = confs;
1115 :
1116 2 : advgetopt::getopt opt(environment_options);
1117 :
1118 2 : SNAP_CATCH2_NAMESPACE::push_expected_log(
1119 : "error: unknown option \"f\" found in configuration file \""
1120 2 : + SNAP_CATCH2_NAMESPACE::g_config_filename
1121 3 : + "\".");
1122 1 : opt.process_configuration_file(SNAP_CATCH2_NAMESPACE::g_config_filename);
1123 1 : SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty();
1124 :
1125 1 : CATCH_REQUIRE(opt.size("sizes") == 1);
1126 1 : CATCH_REQUIRE(opt.get_string("sizes") == "-132");
1127 1 : CATCH_REQUIRE(opt.get_long("sizes") == -132);
1128 :
1129 1 : CATCH_REQUIRE(opt.size("filenames") == 0);
1130 : }
1131 : CATCH_END_SECTION()
1132 :
1133 12 : CATCH_START_SECTION("Load with Unexpected Parameter Name (undefined & no dynamic fields are allowed)")
1134 : {
1135 1 : SNAP_CATCH2_NAMESPACE::init_tmp_dir("loading-invalid-dynamic", "invalid-dynamic");
1136 :
1137 : {
1138 2 : std::ofstream config_file;
1139 1 : config_file.open(SNAP_CATCH2_NAMESPACE::g_config_filename, std::ios_base::out | std::ios_base::binary | std::ios_base::trunc);
1140 1 : CATCH_REQUIRE(config_file.good());
1141 1 : config_file <<
1142 : "# Auto-generated\n"
1143 : "sizes=-1001\n"
1144 : "dynamic=\"undefined argument\"\n"
1145 : ;
1146 : }
1147 :
1148 1 : char const * confs[] =
1149 : {
1150 : "~/.config/file-which-was-never-created.mdi",
1151 1 : SNAP_CATCH2_NAMESPACE::g_config_filename.c_str(),
1152 : "/etc/snapwebsites/not/an-existing-file.conf",
1153 : nullptr
1154 1 : };
1155 :
1156 1 : char const * const separators[] {
1157 : ",",
1158 : " ",
1159 : nullptr
1160 : };
1161 :
1162 1 : advgetopt::option const options[] =
1163 : {
1164 : advgetopt::define_option(
1165 : advgetopt::Name("sizes")
1166 : , advgetopt::ShortName('s')
1167 : , advgetopt::Flags(advgetopt::all_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
1168 : , advgetopt::Help("sizes.")
1169 : ),
1170 : advgetopt::define_option(
1171 : advgetopt::Name("filenames")
1172 : , advgetopt::Flags(advgetopt::all_flags<advgetopt::GETOPT_FLAG_REQUIRED, advgetopt::GETOPT_FLAG_MULTIPLE>())
1173 : , advgetopt::Help("enter a list of filenames.")
1174 : , advgetopt::DefaultValue("a.out")
1175 : , advgetopt::Separators(separators)
1176 6 : ),
1177 : advgetopt::end_options()
1178 7 : };
1179 :
1180 1 : advgetopt::options_environment environment_options;
1181 1 : environment_options.f_project_name = "loading-invalid";
1182 1 : environment_options.f_options = options;
1183 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
1184 1 : environment_options.f_help_header = "Testing loading an unknown parameter and no dynamic allowed";
1185 1 : environment_options.f_configuration_files = confs;
1186 :
1187 2 : advgetopt::getopt opt(environment_options);
1188 :
1189 2 : SNAP_CATCH2_NAMESPACE::push_expected_log(
1190 : "error: unknown option \"dynamic\" found in configuration file \""
1191 2 : + SNAP_CATCH2_NAMESPACE::g_config_filename
1192 3 : + "\".");
1193 1 : opt.process_configuration_file(SNAP_CATCH2_NAMESPACE::g_config_filename);
1194 1 : SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty();
1195 :
1196 1 : CATCH_REQUIRE(opt.size("sizes") == 1);
1197 1 : CATCH_REQUIRE(opt.get_string("sizes") == "-1001");
1198 1 : CATCH_REQUIRE(opt.get_long("sizes") == -1001);
1199 :
1200 1 : CATCH_REQUIRE(opt.size("filenames") == 0);
1201 : }
1202 : CATCH_END_SECTION()
1203 :
1204 12 : CATCH_START_SECTION("Load with Parameter not Supported in Configuration Files")
1205 : {
1206 1 : SNAP_CATCH2_NAMESPACE::init_tmp_dir("loading-invalid-config", "invalid-param-in-config");
1207 :
1208 : {
1209 2 : std::ofstream config_file;
1210 1 : config_file.open(SNAP_CATCH2_NAMESPACE::g_config_filename, std::ios_base::out | std::ios_base::binary | std::ios_base::trunc);
1211 1 : CATCH_REQUIRE(config_file.good());
1212 1 : config_file <<
1213 : "# Auto-generated\n"
1214 : "sizes=-1001\n"
1215 : "filenames=unexpected, argument, in, configuration, file\n"
1216 : ;
1217 : }
1218 :
1219 1 : char const * confs[] =
1220 : {
1221 : "~/.config/file-which-was-never-created.mdi",
1222 1 : SNAP_CATCH2_NAMESPACE::g_config_filename.c_str(),
1223 : "/etc/snapwebsites/not/an-existing-file.conf",
1224 : nullptr
1225 1 : };
1226 :
1227 1 : char const * const separators[] {
1228 : ",",
1229 : " ",
1230 : nullptr
1231 : };
1232 :
1233 1 : advgetopt::option const options[] =
1234 : {
1235 : advgetopt::define_option(
1236 : advgetopt::Name("sizes")
1237 : , advgetopt::ShortName('s')
1238 : , advgetopt::Flags(advgetopt::all_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
1239 : , advgetopt::Help("sizes.")
1240 : ),
1241 : advgetopt::define_option(
1242 : advgetopt::Name("filenames")
1243 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED, advgetopt::GETOPT_FLAG_MULTIPLE>())
1244 : , advgetopt::Help("enter a list of filenames.")
1245 : , advgetopt::DefaultValue("a.out")
1246 : , advgetopt::Separators(separators)
1247 6 : ),
1248 : advgetopt::end_options()
1249 7 : };
1250 :
1251 1 : advgetopt::options_environment environment_options;
1252 1 : environment_options.f_project_name = "loading-invalid";
1253 1 : environment_options.f_options = options;
1254 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
1255 1 : environment_options.f_help_header = "Testing loading an unknown parameter and no dynamic allowed";
1256 1 : environment_options.f_configuration_files = confs;
1257 :
1258 2 : advgetopt::getopt opt(environment_options);
1259 :
1260 2 : SNAP_CATCH2_NAMESPACE::push_expected_log(
1261 : "error: option \"filenames\" is not supported in configuration files (found in \""
1262 2 : + SNAP_CATCH2_NAMESPACE::g_config_filename
1263 3 : + "\").");
1264 1 : opt.process_configuration_file(SNAP_CATCH2_NAMESPACE::g_config_filename);
1265 1 : SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty();
1266 :
1267 1 : CATCH_REQUIRE(opt.size("sizes") == 1);
1268 1 : CATCH_REQUIRE(opt.get_string("sizes") == "-1001");
1269 1 : CATCH_REQUIRE(opt.get_long("sizes") == -1001);
1270 :
1271 1 : CATCH_REQUIRE(opt.size("filenames") == 0);
1272 : }
1273 : CATCH_END_SECTION()
1274 :
1275 12 : CATCH_START_SECTION("Load a Configuration File with a Flag given a Value")
1276 : {
1277 1 : SNAP_CATCH2_NAMESPACE::init_tmp_dir("load-flag-with-value", "unexpected-value-in-config");
1278 :
1279 : {
1280 2 : std::ofstream config_file;
1281 1 : config_file.open(SNAP_CATCH2_NAMESPACE::g_config_filename, std::ios_base::out | std::ios_base::binary | std::ios_base::trunc);
1282 1 : CATCH_REQUIRE(config_file.good());
1283 1 : config_file <<
1284 : "# Auto-generated\n"
1285 : "sizes=4153629\n"
1286 : "color-flag=true\n"
1287 : ;
1288 : }
1289 :
1290 1 : char const * confs[] =
1291 : {
1292 : "~/.config/file-which-was-never-created.mdi",
1293 1 : SNAP_CATCH2_NAMESPACE::g_config_filename.c_str(),
1294 : "/etc/snapwebsites/not/an-existing-file.conf",
1295 : nullptr
1296 1 : };
1297 :
1298 1 : advgetopt::option const options[] =
1299 : {
1300 : advgetopt::define_option(
1301 : advgetopt::Name("sizes")
1302 : , advgetopt::ShortName('s')
1303 : , advgetopt::Flags(advgetopt::all_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
1304 : , advgetopt::Help("sizes.")
1305 : ),
1306 : advgetopt::define_option(
1307 : advgetopt::Name("color-flag")
1308 : , advgetopt::Flags(advgetopt::all_flags<advgetopt::GETOPT_FLAG_FLAG>())
1309 : , advgetopt::Help("flag that you want color.")
1310 : ),
1311 : advgetopt::end_options()
1312 : };
1313 :
1314 1 : advgetopt::options_environment environment_options;
1315 1 : environment_options.f_project_name = "load";
1316 1 : environment_options.f_options = options;
1317 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
1318 1 : environment_options.f_help_header = "Testing loading an invalid flag";
1319 1 : environment_options.f_configuration_files = confs;
1320 :
1321 2 : advgetopt::getopt opt(environment_options);
1322 :
1323 2 : SNAP_CATCH2_NAMESPACE::push_expected_log(
1324 : "error: option \"color_flag\" cannot be given a value in configuration file \""
1325 2 : + SNAP_CATCH2_NAMESPACE::g_config_filename
1326 3 : + "\".");
1327 1 : opt.process_configuration_file(SNAP_CATCH2_NAMESPACE::g_config_filename);
1328 1 : SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty();
1329 :
1330 1 : CATCH_REQUIRE(opt.size("sizes") == 1);
1331 1 : CATCH_REQUIRE(opt.get_string("sizes") == "4153629");
1332 :
1333 1 : CATCH_REQUIRE(opt.size("color-flag") == 0);
1334 1 : CATCH_REQUIRE_FALSE(opt.is_defined("color-flag"));
1335 : }
1336 : CATCH_END_SECTION()
1337 :
1338 12 : CATCH_START_SECTION("Load a Configuration File with an Invalid Sections Definition")
1339 : {
1340 1 : SNAP_CATCH2_NAMESPACE::init_tmp_dir("load-with-invalid-sections", "invalid-sections");
1341 :
1342 : {
1343 2 : std::ofstream config_file;
1344 1 : config_file.open(SNAP_CATCH2_NAMESPACE::g_config_filename, std::ios_base::out | std::ios_base::binary | std::ios_base::trunc);
1345 1 : CATCH_REQUIRE(config_file.good());
1346 1 : config_file <<
1347 : "# Auto-generated\n"
1348 : "[integers]\n"
1349 : "sizes=639\n"
1350 : ;
1351 : }
1352 :
1353 1 : char const * confs[] =
1354 : {
1355 : "~/.config/file-which-was-never-created.mdi",
1356 1 : SNAP_CATCH2_NAMESPACE::g_config_filename.c_str(),
1357 : "/etc/snapwebsites/not/an-existing-file.conf",
1358 : nullptr
1359 1 : };
1360 :
1361 1 : advgetopt::option const options[] =
1362 : {
1363 : advgetopt::define_option(
1364 : advgetopt::Name("integers::sizes")
1365 : , advgetopt::ShortName('s')
1366 : , advgetopt::Flags(advgetopt::all_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
1367 : , advgetopt::Help("sizes.")
1368 : ),
1369 : advgetopt::define_option(
1370 : advgetopt::Name(advgetopt::CONFIGURATION_SECTIONS)
1371 : , advgetopt::Flags(advgetopt::all_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
1372 : , advgetopt::Help("MULTIPLE missing.")
1373 : ),
1374 : advgetopt::end_options()
1375 : };
1376 :
1377 1 : advgetopt::options_environment environment_options;
1378 1 : environment_options.f_project_name = "load-invalid-sections";
1379 1 : environment_options.f_options = options;
1380 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
1381 1 : environment_options.f_help_header = "Testing loading invalid sections declaration";
1382 1 : environment_options.f_configuration_files = confs;
1383 :
1384 2 : advgetopt::getopt opt(environment_options);
1385 :
1386 1 : SNAP_CATCH2_NAMESPACE::push_expected_log("error: option \"configuration_sections\" must have GETOPT_FLAG_MULTIPLE set.");
1387 1 : opt.process_configuration_file(SNAP_CATCH2_NAMESPACE::g_config_filename);
1388 1 : SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty();
1389 :
1390 : // it failed early so it's not considered to be 100% initialized
1391 : //
1392 1 : CATCH_REQUIRE_THROWS_MATCHES(
1393 : opt.size("integers::sizes-parameter")
1394 : , advgetopt::getopt_initialization
1395 : , Catch::Matchers::ExceptionMessage(
1396 : "getopt_exception: function called too soon, parser is not done yet (i.e. is_defined(), get_string(), get_integer() cannot be called until the parser is done)"));
1397 : }
1398 : CATCH_END_SECTION()
1399 12 : }
1400 :
1401 :
1402 :
1403 : // vim: ts=4 sw=4 et nowrap
|