1 // Copyright 2010 The Kyua Authors.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "cli/main.hpp"
37 #include <atf-c++.hpp>
39 #include "utils/cmdline/base_command.ipp"
40 #include "utils/cmdline/exceptions.hpp"
41 #include "utils/cmdline/globals.hpp"
42 #include "utils/cmdline/options.hpp"
43 #include "utils/cmdline/parser.hpp"
44 #include "utils/cmdline/ui_mock.hpp"
45 #include "utils/datetime.hpp"
46 #include "utils/defs.hpp"
47 #include "utils/env.hpp"
48 #include "utils/fs/operations.hpp"
49 #include "utils/fs/path.hpp"
50 #include "utils/logging/macros.hpp"
51 #include "utils/logging/operations.hpp"
52 #include "utils/process/child.ipp"
53 #include "utils/process/status.hpp"
54 #include "utils/test_utils.ipp"
56 namespace cmdline = utils::cmdline;
57 namespace config = utils::config;
58 namespace datetime = utils::datetime;
59 namespace fs = utils::fs;
60 namespace logging = utils::logging;
61 namespace process = utils::process;
67 /// Fake command implementation that crashes during its execution.
68 class cmd_mock_crash : public cli::cli_command {
70 /// Constructs a new mock command.
72 /// All command parameters are set to irrelevant values.
73 cmd_mock_crash(void) :
74 cli::cli_command("mock_error", "", 0, 0, "Mock command that crashes")
78 /// Runs the mock command.
80 /// \return Nothing because this function always aborts.
82 run(cmdline::ui* /* ui */,
83 const cmdline::parsed_cmdline& /* cmdline */,
84 const config::tree& /* user_config */)
86 utils::abort_without_coredump();
91 /// Fake command implementation that throws an exception during its execution.
92 class cmd_mock_error : public cli::cli_command {
93 /// Whether the command raises an exception captured by the parent or not.
95 /// If this is true, the command will raise a std::runtime_error exception
96 /// or a subclass of it. The main program is in charge of capturing these
97 /// and reporting them appropriately. If false, this raises another
98 /// exception that does not inherit from std::runtime_error.
102 /// Constructs a new mock command.
104 /// \param unhandled If true, make run raise an exception not catched by the
106 cmd_mock_error(const bool unhandled) :
107 cli::cli_command("mock_error", "", 0, 0,
108 "Mock command that raises an error"),
109 _unhandled(unhandled)
113 /// Runs the mock command.
115 /// \return Nothing because this function always aborts.
117 /// \throw std::logic_error If _unhandled is true.
118 /// \throw std::runtime_error If _unhandled is false.
120 run(cmdline::ui* /* ui */,
121 const cmdline::parsed_cmdline& /* cmdline */,
122 const config::tree& /* user_config */)
125 throw std::logic_error("This is unhandled");
127 throw std::runtime_error("Runtime error");
132 /// Fake command implementation that prints messages during its execution.
133 class cmd_mock_write : public cli::cli_command {
135 /// Constructs a new mock command.
137 /// All command parameters are set to irrelevant values.
138 cmd_mock_write(void) : cli::cli_command(
139 "mock_write", "", 0, 0, "Mock command that prints output")
143 /// Runs the mock command.
145 /// \param ui Object to interact with the I/O of the program.
147 /// \return Nothing because this function always aborts.
150 const cmdline::parsed_cmdline& /* cmdline */,
151 const config::tree& /* user_config */)
153 ui->out("stdout message from subcommand");
154 ui->err("stderr message from subcommand");
160 } // anonymous namespace
163 ATF_TEST_CASE_WITHOUT_HEAD(detail__default_log_name__home);
164 ATF_TEST_CASE_BODY(detail__default_log_name__home)
166 datetime::set_mock_now(2011, 2, 21, 21, 10, 30, 0);
167 cmdline::init("progname1");
169 utils::setenv("HOME", "/home//fake");
170 utils::setenv("TMPDIR", "/do/not/use/this");
172 fs::path("/home/fake/.kyua/logs/progname1.20110221-211030.log"),
173 cli::detail::default_log_name());
177 ATF_TEST_CASE_WITHOUT_HEAD(detail__default_log_name__tmpdir);
178 ATF_TEST_CASE_BODY(detail__default_log_name__tmpdir)
180 datetime::set_mock_now(2011, 2, 21, 21, 10, 50, 987);
181 cmdline::init("progname2");
183 utils::unsetenv("HOME");
184 utils::setenv("TMPDIR", "/a/b//c");
185 ATF_REQUIRE_EQ(fs::path("/a/b/c/progname2.20110221-211050.log"),
186 cli::detail::default_log_name());
190 ATF_TEST_CASE_WITHOUT_HEAD(detail__default_log_name__hardcoded);
191 ATF_TEST_CASE_BODY(detail__default_log_name__hardcoded)
193 datetime::set_mock_now(2011, 2, 21, 21, 15, 00, 123456);
194 cmdline::init("progname3");
196 utils::unsetenv("HOME");
197 utils::unsetenv("TMPDIR");
198 ATF_REQUIRE_EQ(fs::path("/tmp/progname3.20110221-211500.log"),
199 cli::detail::default_log_name());
203 ATF_TEST_CASE_WITHOUT_HEAD(main__no_args);
204 ATF_TEST_CASE_BODY(main__no_args)
206 logging::set_inmemory();
207 cmdline::init("progname");
210 const char* const argv[] = {"progname", NULL};
213 ATF_REQUIRE_EQ(3, cli::main(&ui, argc, argv));
214 ATF_REQUIRE(ui.out_log().empty());
215 ATF_REQUIRE(atf::utils::grep_collection("Usage error: No command provided",
217 ATF_REQUIRE(atf::utils::grep_collection("Type.*progname help",
222 ATF_TEST_CASE_WITHOUT_HEAD(main__unknown_command);
223 ATF_TEST_CASE_BODY(main__unknown_command)
225 logging::set_inmemory();
226 cmdline::init("progname");
229 const char* const argv[] = {"progname", "foo", NULL};
232 ATF_REQUIRE_EQ(3, cli::main(&ui, argc, argv));
233 ATF_REQUIRE(ui.out_log().empty());
234 ATF_REQUIRE(atf::utils::grep_collection("Usage error: Unknown command.*foo",
236 ATF_REQUIRE(atf::utils::grep_collection("Type.*progname help",
241 ATF_TEST_CASE_WITHOUT_HEAD(main__logfile__default);
242 ATF_TEST_CASE_BODY(main__logfile__default)
244 logging::set_inmemory();
245 datetime::set_mock_now(2011, 2, 21, 21, 30, 00, 0);
246 cmdline::init("progname");
249 const char* const argv[] = {"progname", NULL};
252 ATF_REQUIRE(!fs::exists(fs::path(
253 ".kyua/logs/progname.20110221-213000.log")));
254 ATF_REQUIRE_EQ(3, cli::main(&ui, argc, argv));
255 ATF_REQUIRE(fs::exists(fs::path(
256 ".kyua/logs/progname.20110221-213000.log")));
260 ATF_TEST_CASE_WITHOUT_HEAD(main__logfile__override);
261 ATF_TEST_CASE_BODY(main__logfile__override)
263 logging::set_inmemory();
264 datetime::set_mock_now(2011, 2, 21, 21, 30, 00, 321);
265 cmdline::init("progname");
268 const char* const argv[] = {"progname", "--logfile=test.log", NULL};
271 ATF_REQUIRE(!fs::exists(fs::path("test.log")));
272 ATF_REQUIRE_EQ(3, cli::main(&ui, argc, argv));
273 ATF_REQUIRE(!fs::exists(fs::path(
274 ".kyua/logs/progname.20110221-213000.log")));
275 ATF_REQUIRE(fs::exists(fs::path("test.log")));
279 ATF_TEST_CASE_WITHOUT_HEAD(main__loglevel__default);
280 ATF_TEST_CASE_BODY(main__loglevel__default)
282 logging::set_inmemory();
283 cmdline::init("progname");
286 const char* const argv[] = {"progname", "--logfile=test.log", NULL};
288 LD("Mock debug message");
289 LE("Mock error message");
290 LI("Mock info message");
291 LW("Mock warning message");
294 ATF_REQUIRE_EQ(3, cli::main(&ui, argc, argv));
295 ATF_REQUIRE(!atf::utils::grep_file("Mock debug message", "test.log"));
296 ATF_REQUIRE(atf::utils::grep_file("Mock error message", "test.log"));
297 ATF_REQUIRE(atf::utils::grep_file("Mock info message", "test.log"));
298 ATF_REQUIRE(atf::utils::grep_file("Mock warning message", "test.log"));
302 ATF_TEST_CASE_WITHOUT_HEAD(main__loglevel__higher);
303 ATF_TEST_CASE_BODY(main__loglevel__higher)
305 logging::set_inmemory();
306 cmdline::init("progname");
309 const char* const argv[] = {"progname", "--logfile=test.log",
310 "--loglevel=debug", NULL};
312 LD("Mock debug message");
313 LE("Mock error message");
314 LI("Mock info message");
315 LW("Mock warning message");
318 ATF_REQUIRE_EQ(3, cli::main(&ui, argc, argv));
319 ATF_REQUIRE(atf::utils::grep_file("Mock debug message", "test.log"));
320 ATF_REQUIRE(atf::utils::grep_file("Mock error message", "test.log"));
321 ATF_REQUIRE(atf::utils::grep_file("Mock info message", "test.log"));
322 ATF_REQUIRE(atf::utils::grep_file("Mock warning message", "test.log"));
326 ATF_TEST_CASE_WITHOUT_HEAD(main__loglevel__lower);
327 ATF_TEST_CASE_BODY(main__loglevel__lower)
329 logging::set_inmemory();
330 cmdline::init("progname");
333 const char* const argv[] = {"progname", "--logfile=test.log",
334 "--loglevel=warning", NULL};
336 LD("Mock debug message");
337 LE("Mock error message");
338 LI("Mock info message");
339 LW("Mock warning message");
342 ATF_REQUIRE_EQ(3, cli::main(&ui, argc, argv));
343 ATF_REQUIRE(!atf::utils::grep_file("Mock debug message", "test.log"));
344 ATF_REQUIRE(atf::utils::grep_file("Mock error message", "test.log"));
345 ATF_REQUIRE(!atf::utils::grep_file("Mock info message", "test.log"));
346 ATF_REQUIRE(atf::utils::grep_file("Mock warning message", "test.log"));
350 ATF_TEST_CASE_WITHOUT_HEAD(main__loglevel__error);
351 ATF_TEST_CASE_BODY(main__loglevel__error)
353 logging::set_inmemory();
354 cmdline::init("progname");
357 const char* const argv[] = {"progname", "--logfile=test.log",
358 "--loglevel=i-am-invalid", NULL};
361 ATF_REQUIRE_EQ(3, cli::main(&ui, argc, argv));
362 ATF_REQUIRE(atf::utils::grep_collection("Usage error.*i-am-invalid",
364 ATF_REQUIRE(!fs::exists(fs::path("test.log")));
368 ATF_TEST_CASE_WITHOUT_HEAD(main__subcommand__ok);
369 ATF_TEST_CASE_BODY(main__subcommand__ok)
371 logging::set_inmemory();
372 cmdline::init("progname");
375 const char* const argv[] = {"progname", "mock_write", NULL};
378 ATF_REQUIRE_EQ(EXIT_FAILURE,
379 cli::main(&ui, argc, argv,
380 cli::cli_command_ptr(new cmd_mock_write())));
381 ATF_REQUIRE_EQ(1, ui.out_log().size());
382 ATF_REQUIRE_EQ("stdout message from subcommand", ui.out_log()[0]);
383 ATF_REQUIRE_EQ(1, ui.err_log().size());
384 ATF_REQUIRE_EQ("stderr message from subcommand", ui.err_log()[0]);
388 ATF_TEST_CASE_WITHOUT_HEAD(main__subcommand__invalid_args);
389 ATF_TEST_CASE_BODY(main__subcommand__invalid_args)
391 logging::set_inmemory();
392 cmdline::init("progname");
395 const char* const argv[] = {"progname", "mock_write", "bar", NULL};
399 cli::main(&ui, argc, argv,
400 cli::cli_command_ptr(new cmd_mock_write())));
401 ATF_REQUIRE(ui.out_log().empty());
402 ATF_REQUIRE(atf::utils::grep_collection(
403 "Usage error for command mock_write: Too many arguments.",
405 ATF_REQUIRE(atf::utils::grep_collection("Type.*progname help",
410 ATF_TEST_CASE_WITHOUT_HEAD(main__subcommand__runtime_error);
411 ATF_TEST_CASE_BODY(main__subcommand__runtime_error)
413 logging::set_inmemory();
414 cmdline::init("progname");
417 const char* const argv[] = {"progname", "mock_error", NULL};
420 ATF_REQUIRE_EQ(2, cli::main(&ui, argc, argv,
421 cli::cli_command_ptr(new cmd_mock_error(false))));
422 ATF_REQUIRE(ui.out_log().empty());
423 ATF_REQUIRE(atf::utils::grep_collection("progname: E: Runtime error.",
428 ATF_TEST_CASE_WITHOUT_HEAD(main__subcommand__unhandled_exception);
429 ATF_TEST_CASE_BODY(main__subcommand__unhandled_exception)
431 logging::set_inmemory();
432 cmdline::init("progname");
435 const char* const argv[] = {"progname", "mock_error", NULL};
438 ATF_REQUIRE_THROW(std::logic_error, cli::main(&ui, argc, argv,
439 cli::cli_command_ptr(new cmd_mock_error(true))));
444 do_subcommand_crash(void)
446 logging::set_inmemory();
447 cmdline::init("progname");
450 const char* const argv[] = {"progname", "mock_error", NULL};
453 cli::main(&ui, argc, argv,
454 cli::cli_command_ptr(new cmd_mock_crash()));
458 ATF_TEST_CASE_WITHOUT_HEAD(main__subcommand__crash);
459 ATF_TEST_CASE_BODY(main__subcommand__crash)
461 const process::status status = process::child::fork_files(
462 do_subcommand_crash, fs::path("stdout.txt"),
463 fs::path("stderr.txt"))->wait();
464 ATF_REQUIRE(status.signaled());
465 ATF_REQUIRE_EQ(SIGABRT, status.termsig());
466 ATF_REQUIRE(atf::utils::grep_file("Fatal signal", "stderr.txt"));
470 ATF_INIT_TEST_CASES(tcs)
472 ATF_ADD_TEST_CASE(tcs, detail__default_log_name__home);
473 ATF_ADD_TEST_CASE(tcs, detail__default_log_name__tmpdir);
474 ATF_ADD_TEST_CASE(tcs, detail__default_log_name__hardcoded);
476 ATF_ADD_TEST_CASE(tcs, main__no_args);
477 ATF_ADD_TEST_CASE(tcs, main__unknown_command);
478 ATF_ADD_TEST_CASE(tcs, main__logfile__default);
479 ATF_ADD_TEST_CASE(tcs, main__logfile__override);
480 ATF_ADD_TEST_CASE(tcs, main__loglevel__default);
481 ATF_ADD_TEST_CASE(tcs, main__loglevel__higher);
482 ATF_ADD_TEST_CASE(tcs, main__loglevel__lower);
483 ATF_ADD_TEST_CASE(tcs, main__loglevel__error);
484 ATF_ADD_TEST_CASE(tcs, main__subcommand__ok);
485 ATF_ADD_TEST_CASE(tcs, main__subcommand__invalid_args);
486 ATF_ADD_TEST_CASE(tcs, main__subcommand__runtime_error);
487 ATF_ADD_TEST_CASE(tcs, main__subcommand__unhandled_exception);
488 ATF_ADD_TEST_CASE(tcs, main__subcommand__crash);