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/cmd_test.hpp"
33 #include "cli/common.ipp"
34 #include "drivers/run_tests.hpp"
35 #include "model/test_program.hpp"
36 #include "model/test_result.hpp"
37 #include "store/layout.hpp"
38 #include "utils/cmdline/options.hpp"
39 #include "utils/cmdline/parser.ipp"
40 #include "utils/cmdline/ui.hpp"
41 #include "utils/config/tree.ipp"
42 #include "utils/datetime.hpp"
43 #include "utils/format/macros.hpp"
44 #include "utils/fs/path.hpp"
46 namespace cmdline = utils::cmdline;
47 namespace config = utils::config;
48 namespace datetime = utils::datetime;
49 namespace fs = utils::fs;
50 namespace layout = store::layout;
58 /// Hooks to print a progress report of the execution of the tests.
59 class print_hooks : public drivers::run_tests::base_hooks {
60 /// Object to interact with the I/O of the program.
63 /// Whether the tests are executed in parallel or not.
67 /// The amount of positive test results found so far.
68 unsigned long good_count;
70 /// The amount of negative test results found so far.
71 unsigned long bad_count;
73 /// Constructor for the hooks.
75 /// \param ui_ Object to interact with the I/O of the program.
76 /// \param parallel_ True if we are executing more than one test at once.
77 print_hooks(cmdline::ui* ui_, const bool parallel_) :
85 /// Called when the processing of a test case begins.
87 /// \param test_program The test program containing the test case.
88 /// \param test_case_name The name of the test case being executed.
90 got_test_case(const model::test_program& test_program,
91 const std::string& test_case_name)
94 _ui->out(F("%s -> ") %
95 cli::format_test_case_id(test_program, test_case_name),
100 /// Called when a result of a test case becomes available.
102 /// \param test_program The test program containing the test case.
103 /// \param test_case_name The name of the test case being executed.
104 /// \param result The result of the execution of the test case.
105 /// \param duration The time it took to run the test.
107 got_result(const model::test_program& test_program,
108 const std::string& test_case_name,
109 const model::test_result& result,
110 const datetime::delta& duration)
113 _ui->out(F("%s -> ") %
114 cli::format_test_case_id(test_program, test_case_name),
117 _ui->out(F("%s [%s]") % cli::format_result(result) %
118 cli::format_delta(duration));
127 } // anonymous namespace
130 /// Default constructor for cmd_test.
131 cmd_test::cmd_test(void) : cli_command(
132 "test", "[test-program ...]", 0, -1, "Run tests")
134 add_option(build_root_option);
135 add_option(kyuafile_option);
136 add_option(results_file_create_option);
140 /// Entry point for the "test" subcommand.
142 /// \param ui Object to interact with the I/O of the program.
143 /// \param cmdline Representation of the command line to the subcommand.
144 /// \param user_config The runtime configuration of the program.
146 /// \return 0 if all tests passed, 1 otherwise.
148 cmd_test::run(cmdline::ui* ui, const cmdline::parsed_cmdline& cmdline,
149 const config::tree& user_config)
151 const layout::results_id_file_pair results = layout::new_db(
152 results_file_create(cmdline), kyuafile_path(cmdline).branch_path());
154 const bool parallel = (user_config.lookup< config::positive_int_node >(
157 print_hooks hooks(ui, parallel);
158 const drivers::run_tests::result result = drivers::run_tests::drive(
159 kyuafile_path(cmdline), build_root_path(cmdline), results.second,
160 parse_filters(cmdline.arguments()), user_config, hooks);
163 if (hooks.good_count > 0 || hooks.bad_count > 0) {
165 if (!results.first.empty()) {
166 ui->out(F("Results file id is %s") % results.first);
168 ui->out(F("Results saved to %s") % results.second);
171 ui->out(F("%s/%s passed (%s failed)") % hooks.good_count %
172 (hooks.good_count + hooks.bad_count) % hooks.bad_count);
174 exit_code = (hooks.bad_count == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
176 // TODO(jmmv): Delete created empty file; it's useless!
177 if (!results.first.empty()) {
178 ui->out(F("Results file id is %s") % results.first);
180 ui->out(F("Results saved to %s") % results.second);
181 exit_code = EXIT_SUCCESS;
184 return report_unused_filters(result.unused_filters, ui) ?
185 EXIT_FAILURE : exit_code;