1 // Copyright 2011 The Kyua Authors.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
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.
16 //
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.
28
29 #include "cli/cmd_debug.hpp"
30
31 #include <cstdlib>
32 #include <iostream>
33
34 #include "cli/common.ipp"
35 #include "drivers/debug_test.hpp"
36 #include "engine/filters.hpp"
37 #include "utils/cmdline/exceptions.hpp"
38 #include "utils/cmdline/options.hpp"
39 #include "utils/cmdline/parser.ipp"
40 #include "utils/cmdline/ui.hpp"
41 #include "utils/format/macros.hpp"
42 #include "utils/process/executor.hpp"
43
44 namespace cmdline = utils::cmdline;
45 namespace config = utils::config;
46 namespace executor = utils::process::executor;
47
48 using cli::cmd_debug;
49
50
51 namespace {
52
53
54 const cmdline::bool_option pause_before_cleanup_upon_fail_option(
55 'p',
56 "pause-before-cleanup-upon-fail",
57 "Pauses right before the test cleanup upon fail");
58
59
60 const cmdline::bool_option pause_before_cleanup_option(
61 "pause-before-cleanup",
62 "Pauses right before the test cleanup");
63
64
65 /// The debugger interface implementation.
66 class dbg : public engine::debugger {
67 /// Object to interact with the I/O of the program.
68 cmdline::ui* _ui;
69
70 /// Representation of the command line to the subcommand.
71 const cmdline::parsed_cmdline& _cmdline;
72
73 public:
74 /// Constructor.
75 ///
76 /// \param ui_ Object to interact with the I/O of the program.
77 /// \param cmdline Representation of the command line to the subcommand.
dbg(cmdline::ui * ui,const cmdline::parsed_cmdline & cmdline)78 dbg(cmdline::ui* ui, const cmdline::parsed_cmdline& cmdline) :
79 _ui(ui), _cmdline(cmdline)
80 {}
81
before_cleanup(const model::test_program_ptr &,const model::test_case &,optional<model::test_result> & result,executor::exit_handle & eh) const82 void before_cleanup(
83 const model::test_program_ptr&,
84 const model::test_case&,
85 optional< model::test_result >& result,
86 executor::exit_handle& eh) const
87 {
88 if (_cmdline.has_option(pause_before_cleanup_upon_fail_option
89 .long_name())) {
90 if (result && !result.get().good()) {
91 _ui->out("The test failed and paused right before its cleanup "
92 "routine.");
93 _ui->out(F("Test work dir: %s") % eh.work_directory().str());
94 _ui->out("Press any key to continue...");
95 (void) std::cin.get();
96 }
97 } else if (_cmdline.has_option(pause_before_cleanup_option
98 .long_name())) {
99 _ui->out("The test paused right before its cleanup routine.");
100 _ui->out(F("Test work dir: %s") % eh.work_directory().str());
101 _ui->out("Press any key to continue...");
102 (void) std::cin.get();
103 }
104 };
105
106 };
107
108
109 } // anonymous namespace
110
111
112 /// Default constructor for cmd_debug.
cmd_debug(void)113 cmd_debug::cmd_debug(void) : cli_command(
114 "debug", "test_case", 1, 1,
115 "Executes a single test case providing facilities for debugging")
116 {
117 add_option(build_root_option);
118 add_option(kyuafile_option);
119
120 add_option(pause_before_cleanup_upon_fail_option);
121 add_option(pause_before_cleanup_option);
122
123 add_option(cmdline::path_option(
124 "stdout", "Where to direct the standard output of the test case",
125 "path", "/dev/stdout"));
126
127 add_option(cmdline::path_option(
128 "stderr", "Where to direct the standard error of the test case",
129 "path", "/dev/stderr"));
130 }
131
132
133 /// Entry point for the "debug" subcommand.
134 ///
135 /// \param ui Object to interact with the I/O of the program.
136 /// \param cmdline Representation of the command line to the subcommand.
137 /// \param user_config The runtime debuguration of the program.
138 ///
139 /// \return 0 if everything is OK, 1 if any of the necessary documents cannot be
140 /// opened.
141 int
run(cmdline::ui * ui,const cmdline::parsed_cmdline & cmdline,const config::tree & user_config)142 cmd_debug::run(cmdline::ui* ui, const cmdline::parsed_cmdline& cmdline,
143 const config::tree& user_config)
144 {
145 const std::string& test_case_name = cmdline.arguments()[0];
146 if (test_case_name.find(':') == std::string::npos)
147 throw cmdline::usage_error(F("'%s' is not a test case identifier "
148 "(missing ':'?)") % test_case_name);
149 const engine::test_filter filter = engine::test_filter::parse(
150 test_case_name);
151
152 auto debugger = std::shared_ptr< engine::debugger >(new dbg(ui, cmdline));
153
154 const drivers::debug_test::result result = drivers::debug_test::drive(
155 debugger,
156 kyuafile_path(cmdline), build_root_path(cmdline), filter, user_config,
157 cmdline.get_option< cmdline::path_option >("stdout"),
158 cmdline.get_option< cmdline::path_option >("stderr"));
159
160 ui->out(F("%s -> %s") % cli::format_test_case_id(result.test_case) %
161 cli::format_result(result.test_result));
162
163 return result.test_result.good() ? EXIT_SUCCESS : EXIT_FAILURE;
164 }
165