30 TEST_CASE(
"'find_config' returns the path to the highest priority " 31 "configuration file.",
"[Options]")
33 std::vector<Filesystem::path> paths;
34 fakeit::Mock<Filesystem> fs_mock;
35 SECTION(
"First found is given by MAVTABLES_CONFIG_PATH environment " 38 fakeit::When(Method(fs_mock, exists)).AlwaysDo([&](
auto p)
43 setenv(
"MAVTABLES_CONFIG_PATH",
"mtbls.conf",
true);
45 REQUIRE(config_file.has_value());
46 REQUIRE(config_file.value() ==
"mtbls.conf");
47 unsetenv(
"MAVTABLES_CONFIG_PATH");
48 fakeit::Verify(Method(fs_mock, exists)).Exactly(1);
49 REQUIRE(paths.size() == 1);
52 SECTION(
"First found is .mavtablesrc in current directory.")
54 fakeit::When(Method(fs_mock, exists)).AlwaysDo([&](
auto p)
59 setenv(
"MAVTABLES_CONFIG_PATH",
"mtbls.conf",
true);
61 REQUIRE(config_file.has_value());
62 REQUIRE(config_file.value() ==
".mavtablesrc");
63 unsetenv(
"MAVTABLES_CONFIG_PATH");
64 fakeit::Verify(Method(fs_mock, exists)).Exactly(2);
65 REQUIRE(paths.size() == 2);
69 SECTION(
"First found is .mavtablesrc in HOME directory.")
73 fakeit::When(Method(fs_mock, exists)).AlwaysDo([&](
auto p)
76 return p == home_path;
78 setenv(
"MAVTABLES_CONFIG_PATH",
"mtbls.conf",
true);
80 REQUIRE(config_file.has_value());
81 REQUIRE(config_file.value() == home_path);
82 unsetenv(
"MAVTABLES_CONFIG_PATH");
83 fakeit::Verify(Method(fs_mock, exists)).Exactly(3);
84 REQUIRE(paths.size() == 3);
89 SECTION(
"First found is PREFIX/etc/mavtables.conf.")
93 fakeit::When(Method(fs_mock, exists)).AlwaysDo([&](
auto p)
96 return p == (PREFIX
"/etc/mavtables.conf");
98 setenv(
"MAVTABLES_CONFIG_PATH",
"mtbls.conf",
true);
100 REQUIRE(config_file.has_value());
101 REQUIRE(config_file.value() == (PREFIX
"/etc/mavtables.conf"));
102 unsetenv(
"MAVTABLES_CONFIG_PATH");
103 fakeit::Verify(Method(fs_mock, exists)).Exactly(4);
104 REQUIRE(paths.size() == 4);
110 SECTION(
"Failed to find any configuration file.")
114 fakeit::When(Method(fs_mock, exists)).AlwaysDo([&](
auto p)
119 setenv(
"MAVTABLES_CONFIG_PATH",
"mtbls.conf",
true);
121 REQUIRE_FALSE(config_file.has_value());
122 unsetenv(
"MAVTABLES_CONFIG_PATH");
123 fakeit::Verify(Method(fs_mock, exists)).Exactly(4);
124 REQUIRE(paths.size() == 4);
133 TEST_CASE(
"Options's class prints the help message.",
"[Options]")
136 std::string help_message =
137 "usage: <program name here>:\n" 138 " -h [ --help ] print this message\n" 139 " --config arg specify configuration file\n" 140 " --ast print AST of configuration file (do not run)\n" 141 " --version print version and license information\n" 142 " --loglevel arg level of logging, between 0 and 3\n\n";
143 SECTION(
"When given the '-h' flag.")
146 const char *argv[2] = {
"<program name here>",
"-h"};
148 REQUIRE(mock_cout.
buffer() == help_message);
149 REQUIRE_FALSE(options);
151 SECTION(
"When given the '--help' flag.")
154 const char *argv[2] = {
"<program name here>",
"--help"};
156 REQUIRE(mock_cout.
buffer() == help_message);
157 REQUIRE_FALSE(options);
162 TEST_CASE(
"Options's class prints version information when given the " 163 "'--version' flag.",
"[Options]")
167 const char *argv[2] = {
"<program name here>",
"--version"};
171 "mavtables (SHAMU Project) v" + std::to_string(VERSION_MAJOR) +
172 "." + std::to_string(VERSION_MINOR) +
173 "." + std::to_string(VERSION_PATCH) +
174 "\nCopyright (C) 2018 Michael R. Shannon\n" 176 "License: GPL v2.0 or any later version.\n" 177 "This is free software; see the source for copying conditions. " 178 "There is NO\nwarranty; not even for MERCHANTABILITY or FITNESS FOR A " 179 "PARTICULAR PURPOSE.\n");
180 REQUIRE_FALSE(options);
184 TEST_CASE(
"Options's class will use the given configuration file " 185 "(--config flag).",
"[Options]")
188 std::vector<Filesystem::path> paths;
189 fakeit::Mock<Filesystem> fs_mock;
190 SECTION(
"File found.")
193 fakeit::When(Method(fs_mock, exists)).AlwaysDo([&](
auto p)
200 const char *argv[3] = {
"mavtables",
"--config",
"examples/test.conf"};
201 Options options(argc, argv, fs_mock.get());
203 REQUIRE(options.config_file() ==
"examples/test.conf");
205 REQUIRE_FALSE(options.ast());
206 REQUIRE(options.run());
208 REQUIRE(mock_cout.
buffer().empty());
210 fakeit::Verify(Method(fs_mock, exists)).Exactly(1);
211 REQUIRE(paths.size() == 1);
214 SECTION(
"File not found (throws error).")
217 fakeit::When(Method(fs_mock, exists)).AlwaysDo([&](
auto p)
224 const char *argv[3] =
226 "mavtables",
"--config",
"non_existant_file.conf" 230 Options(argc, argv, fs_mock.get()), std::runtime_error);
232 Options(argc, argv, fs_mock.get()),
233 "mavtables could not locate a configuration file");
235 REQUIRE(mock_cout.
buffer().empty());
237 fakeit::Verify(Method(fs_mock, exists)).Exactly(2);
238 REQUIRE(paths.size() == 2);
245 TEST_CASE(
"Options's class finds the configuration file.",
"[Options]")
248 std::vector<Filesystem::path> paths;
249 fakeit::Mock<Filesystem> fs_mock;
250 SECTION(
"First found is given by MAVTABLES_CONFIG_PATH environment " 254 fakeit::When(Method(fs_mock, exists)).AlwaysDo([&](
auto p)
259 setenv(
"MAVTABLES_CONFIG_PATH",
"mtbls.conf",
true);
262 const char *argv[2] = {
"mavtables"};
263 Options options(argc, argv, fs_mock.get());
264 unsetenv(
"MAVTABLES_CONFIG_PATH");
266 REQUIRE(options.config_file() ==
"mtbls.conf");
268 REQUIRE_FALSE(options.ast());
269 REQUIRE(options.run());
271 REQUIRE(mock_cout.
buffer().empty());
273 fakeit::Verify(Method(fs_mock, exists)).Exactly(1);
274 REQUIRE(paths.size() == 1);
277 SECTION(
"First found is .mavtablesrc in current directory.")
280 fakeit::When(Method(fs_mock, exists)).AlwaysDo([&](
auto p)
285 setenv(
"MAVTABLES_CONFIG_PATH",
"mtbls.conf",
true);
288 const char *argv[2] = {
"mavtables"};
289 Options options(argc, argv, fs_mock.get());
290 unsetenv(
"MAVTABLES_CONFIG_PATH");
292 REQUIRE(options.config_file() ==
".mavtablesrc");
294 REQUIRE_FALSE(options.ast());
295 REQUIRE(options.run());
297 REQUIRE(mock_cout.
buffer().empty());
299 fakeit::Verify(Method(fs_mock, exists)).Exactly(2);
300 REQUIRE(paths.size() == 2);
304 SECTION(
"First found is .mavtablesrc in HOME directory.")
309 fakeit::When(Method(fs_mock, exists)).AlwaysDo([&](
auto p)
312 return p == home_path;
314 setenv(
"MAVTABLES_CONFIG_PATH",
"mtbls.conf",
true);
317 const char *argv[2] = {
"mavtables"};
318 Options options(argc, argv, fs_mock.get());
319 unsetenv(
"MAVTABLES_CONFIG_PATH");
321 REQUIRE(options.config_file() == home_path);
323 REQUIRE_FALSE(options.ast());
324 REQUIRE(options.run());
326 REQUIRE(mock_cout.
buffer().empty());
328 fakeit::Verify(Method(fs_mock, exists)).Exactly(3);
329 REQUIRE(paths.size() == 3);
334 SECTION(
"First found is PREFIX/etc/mavtables.conf.")
339 fakeit::When(Method(fs_mock, exists)).AlwaysDo([&](
auto p)
342 return p == (PREFIX
"/etc/mavtables.conf");
344 setenv(
"MAVTABLES_CONFIG_PATH",
"mtbls.conf",
true);
347 const char *argv[2] = {
"mavtables"};
348 Options options(argc, argv, fs_mock.get());
349 unsetenv(
"MAVTABLES_CONFIG_PATH");
351 REQUIRE(options.config_file() == (PREFIX
"/etc/mavtables.conf"));
353 REQUIRE_FALSE(options.ast());
354 REQUIRE(options.run());
356 REQUIRE(mock_cout.
buffer().empty());
358 fakeit::Verify(Method(fs_mock, exists)).Exactly(4);
359 REQUIRE(paths.size() == 4);
365 SECTION(
"Failed to find any configuration file.")
370 fakeit::When(Method(fs_mock, exists)).AlwaysDo([&](
auto p)
375 setenv(
"MAVTABLES_CONFIG_PATH",
"mtbls.conf",
true);
378 const char *argv[2] = {
"mavtables"};
380 Options(argc, argv, fs_mock.get()),
383 Options(argc, argv, fs_mock.get()),
384 "mavtables could not locate a configuration file");
385 unsetenv(
"MAVTABLES_CONFIG_PATH");
387 REQUIRE(mock_cout.
buffer().empty());
389 fakeit::Verify(Method(fs_mock, exists)).Exactly(8);
390 REQUIRE(paths.size() == 8);
403 TEST_CASE(
"Options's class sets run to false and ast to true when the --ast " 404 "flag is given.",
"[Options]")
408 fakeit::Mock<Filesystem> fs_mock;
409 fakeit::When(Method(fs_mock, exists)).AlwaysReturn(
true);
412 const char *argv[4] =
414 "mavtables",
"--ast",
"--config",
"test/mavtables.conf" 418 REQUIRE(options.
config_file() ==
"test/mavtables.conf");
420 REQUIRE(options.
ast());
421 REQUIRE_FALSE(options.
run());
423 REQUIRE(mock_cout.
buffer().empty());
427 TEST_CASE(
"Option's class has a loglevel option",
"[Options]")
431 fakeit::Mock<Filesystem> fs_mock;
432 fakeit::When(Method(fs_mock, exists)).AlwaysReturn(
true);
433 SECTION(
"defaults to loglevel 0")
437 const char *argv[3] = {
"mavtables",
"--config",
"test/mavtables.conf"};
442 REQUIRE(mock_cout.
buffer().empty());
444 SECTION(
"sets the loglevel when the --loglevel is given")
448 const char *argv[5] =
450 "mavtables",
"--loglevel",
"3",
"--config",
"test/mavtables.conf" 456 REQUIRE(mock_cout.
buffer().empty());
TEST_CASE("'find_config' returns the path to the highest priority " "configuration file.", "[Options]")
std::optional< std::string > find_config(const Filesystem &filesystem)
boost::filesystem::path path
std::string config_file()