xref: /freebsd/contrib/kyua/cli/cmd_test.cpp (revision d65cd7a57bf0600b722afc770838a5d0c1c3a8e1)
1 // Copyright 2010 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_test.hpp"
30 
31 #include <cstdlib>
32 
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"
45 
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;
51 
52 using cli::cmd_test;
53 
54 
55 namespace {
56 
57 
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.
61     cmdline::ui* _ui;
62 
63     /// Whether the tests are executed in parallel or not.
64     bool _parallel;
65 
66 public:
67     /// The amount of positive test results found so far.
68     unsigned long good_count;
69 
70     /// The amount of negative test results found so far.
71     unsigned long bad_count;
72 
73     /// Constructor for the hooks.
74     ///
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_) :
78         _ui(ui_),
79         _parallel(parallel_),
80         good_count(0),
81         bad_count(0)
82     {
83     }
84 
85     /// Called when the processing of a test case begins.
86     ///
87     /// \param test_program The test program containing the test case.
88     /// \param test_case_name The name of the test case being executed.
89     virtual void
90     got_test_case(const model::test_program& test_program,
91                   const std::string& test_case_name)
92     {
93         if (!_parallel) {
94             _ui->out(F("%s  ->  ") %
95                      cli::format_test_case_id(test_program, test_case_name),
96                      false);
97         }
98     }
99 
100     /// Called when a result of a test case becomes available.
101     ///
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.
106     virtual void
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)
111     {
112         if (_parallel) {
113             _ui->out(F("%s  ->  ") %
114                      cli::format_test_case_id(test_program, test_case_name),
115                      false);
116         }
117         _ui->out(F("%s  [%s]") % cli::format_result(result) %
118             cli::format_delta(duration));
119         if (result.good())
120             good_count++;
121         else
122             bad_count++;
123     }
124 };
125 
126 
127 }  // anonymous namespace
128 
129 
130 /// Default constructor for cmd_test.
131 cmd_test::cmd_test(void) : cli_command(
132     "test", "[test-program ...]", 0, -1, "Run tests")
133 {
134     add_option(build_root_option);
135     add_option(kyuafile_option);
136     add_option(results_file_create_option);
137 }
138 
139 
140 /// Entry point for the "test" subcommand.
141 ///
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.
145 ///
146 /// \return 0 if all tests passed, 1 otherwise.
147 int
148 cmd_test::run(cmdline::ui* ui, const cmdline::parsed_cmdline& cmdline,
149               const config::tree& user_config)
150 {
151     const layout::results_id_file_pair results = layout::new_db(
152         results_file_create(cmdline), kyuafile_path(cmdline).branch_path());
153 
154     const bool parallel = (user_config.lookup< config::positive_int_node >(
155                                "parallelism") > 1);
156 
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);
161 
162     int exit_code;
163     if (hooks.good_count > 0 || hooks.bad_count > 0) {
164         ui->out("");
165         if (!results.first.empty()) {
166             ui->out(F("Results file id is %s") % results.first);
167         }
168         ui->out(F("Results saved to %s") % results.second);
169         ui->out("");
170 
171         ui->out(F("%s/%s passed (%s failed)") % hooks.good_count %
172                 (hooks.good_count + hooks.bad_count) % hooks.bad_count);
173 
174         exit_code = (hooks.bad_count == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
175     } else {
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);
179         }
180         ui->out(F("Results saved to %s") % results.second);
181         exit_code = EXIT_SUCCESS;
182     }
183 
184     return report_unused_filters(result.unused_filters, ui) ?
185         EXIT_FAILURE : exit_code;
186 }
187