1 // Copyright 2011 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/common.hpp"
36 #include "engine/filters.hpp"
37 #include "model/test_program.hpp"
38 #include "model/test_result.hpp"
39 #include "store/layout.hpp"
40 #include "utils/cmdline/exceptions.hpp"
41 #include "utils/cmdline/options.hpp"
42 #include "utils/cmdline/parser.ipp"
43 #include "utils/cmdline/ui.hpp"
44 #include "utils/datetime.hpp"
45 #include "utils/env.hpp"
46 #include "utils/format/macros.hpp"
47 #include "utils/logging/macros.hpp"
48 #include "utils/fs/exceptions.hpp"
49 #include "utils/fs/operations.hpp"
50 #include "utils/fs/path.hpp"
51 #include "utils/optional.ipp"
52 #include "utils/sanity.hpp"
54 #if defined(HAVE_CONFIG_H)
58 namespace cmdline = utils::cmdline;
59 namespace datetime = utils::datetime;
60 namespace fs = utils::fs;
61 namespace layout = store::layout;
64 using utils::optional;
67 /// Standard definition of the option to specify the build root.
68 const cmdline::path_option cli::build_root_option(
70 "Path to the built test programs, if different from the location of the "
75 /// Standard definition of the option to specify a Kyuafile.
76 const cmdline::path_option cli::kyuafile_option(
78 "Path to the test suite definition",
82 /// Standard definition of the option to specify filters on test results.
83 const cmdline::list_option cli::results_filter_option(
84 "results-filter", "Comma-separated list of result types to include in "
85 "the report", "types", "skipped,xfail,broken,failed");
88 /// Standard definition of the option to specify the results file.
90 /// TODO(jmmv): Should support a git-like syntax to go back in time, like
91 /// --results-file=LATEST^N where N indicates how many runs to go back to.
92 const cmdline::string_option cli::results_file_create_option(
94 "Path to the results file to create; if left to the default value, the "
95 "name of the file is automatically computed for the current test suite",
96 "file", layout::results_auto_create_name);
99 /// Standard definition of the option to specify the results file.
101 /// TODO(jmmv): Should support a git-like syntax to go back in time, like
102 /// --results-file=LATEST^N where N indicates how many runs to go back to.
103 const cmdline::string_option cli::results_file_open_option(
105 "Path to the results file to open or the identifier of the current test "
106 "suite or a previous results file for automatic lookup; if left to the "
107 "default value, uses the current directory as the test suite name",
108 "file", layout::results_auto_open_name);
114 /// Gets the path to the historical database if it exists.
116 /// TODO(jmmv): This function should go away. It only exists as a temporary
117 /// transitional path to force the use of the stale ~/.kyua/store.db if it
120 /// \return A path if the file is found; none otherwise.
121 static optional< fs::path >
122 get_historical_db(void)
124 optional< fs::path > home = utils::get_home();
126 const fs::path old_db = home.get() / ".kyua/store.db";
127 if (fs::exists(old_db)) {
128 if (old_db.is_absolute())
129 return utils::make_optional(old_db);
131 return utils::make_optional(old_db.to_absolute());
141 /// Converts a set of result type names to identifiers.
143 /// \param names The collection of names to process; may be empty.
145 /// \return The result type identifiers corresponding to the input names.
147 /// \throw std::runtime_error If any name in the input names is invalid.
148 static cli::result_types
149 parse_types(const std::vector< std::string >& names)
151 typedef std::map< std::string, model::test_result_type > types_map;
152 types_map valid_types;
153 valid_types["broken"] = model::test_result_broken;
154 valid_types["failed"] = model::test_result_failed;
155 valid_types["passed"] = model::test_result_passed;
156 valid_types["skipped"] = model::test_result_skipped;
157 valid_types["xfail"] = model::test_result_expected_failure;
159 cli::result_types types;
160 for (std::vector< std::string >::const_iterator iter = names.begin();
161 iter != names.end(); ++iter) {
162 const types_map::const_iterator match = valid_types.find(*iter);
163 if (match == valid_types.end())
164 throw std::runtime_error(F("Unknown result type '%s'") % *iter);
166 types.push_back((*match).second);
172 } // anonymous namespace
175 /// Gets the path to the build root, if any.
177 /// This is just syntactic sugar to simplify quierying the 'build_root_option'.
179 /// \param cmdline The parsed command line.
181 /// \return The path to the build root, if specified; none otherwise.
183 cli::build_root_path(const cmdline::parsed_cmdline& cmdline)
185 optional< fs::path > build_root;
186 if (cmdline.has_option(build_root_option.long_name()))
187 build_root = cmdline.get_option< cmdline::path_option >(
188 build_root_option.long_name());
193 /// Gets the path to the Kyuafile to be loaded.
195 /// This is just syntactic sugar to simplify quierying the 'kyuafile_option'.
197 /// \param cmdline The parsed command line.
199 /// \return The path to the Kyuafile to be loaded.
201 cli::kyuafile_path(const cmdline::parsed_cmdline& cmdline)
203 return cmdline.get_option< cmdline::path_option >(
204 kyuafile_option.long_name());
208 /// Gets the value of the results-file flag for the creation of a new file.
210 /// \param cmdline The parsed command line from which to extract any possible
211 /// override for the location of the database via the --results-file flag.
213 /// \return The path to the database to be used.
215 /// \throw cmdline::error If the value passed to the flag is invalid.
217 cli::results_file_create(const cmdline::parsed_cmdline& cmdline)
219 std::string results_file = cmdline.get_option< cmdline::string_option >(
220 results_file_create_option.long_name());
221 if (results_file == results_file_create_option.default_value()) {
222 const optional< fs::path > historical_db = get_historical_db();
224 results_file = historical_db.get().str();
227 (void)fs::path(results_file);
228 } catch (const fs::error& e) {
229 throw cmdline::usage_error(F("Invalid value passed to --%s") %
230 results_file_create_option.long_name());
237 /// Gets the value of the results-file flag for the lookup of the file.
239 /// \param cmdline The parsed command line from which to extract any possible
240 /// override for the location of the database via the --results-file flag.
242 /// \return The path to the database to be used.
244 /// \throw cmdline::error If the value passed to the flag is invalid.
246 cli::results_file_open(const cmdline::parsed_cmdline& cmdline)
248 std::string results_file = cmdline.get_option< cmdline::string_option >(
249 results_file_open_option.long_name());
250 if (results_file == results_file_open_option.default_value()) {
251 const optional< fs::path > historical_db = get_historical_db();
253 results_file = historical_db.get().str();
256 (void)fs::path(results_file);
257 } catch (const fs::error& e) {
258 throw cmdline::usage_error(F("Invalid value passed to --%s") %
259 results_file_open_option.long_name());
266 /// Gets the filters for the result types.
268 /// \param cmdline The parsed command line.
270 /// \return A collection of result types to be used for filtering.
272 /// \throw std::runtime_error If any of the user-provided filters is invalid.
274 cli::get_result_types(const utils::cmdline::parsed_cmdline& cmdline)
276 result_types types = parse_types(
277 cmdline.get_option< cmdline::list_option >("results-filter"));
279 types.push_back(model::test_result_passed);
280 types.push_back(model::test_result_skipped);
281 types.push_back(model::test_result_expected_failure);
282 types.push_back(model::test_result_broken);
283 types.push_back(model::test_result_failed);
289 /// Parses a set of command-line arguments to construct test filters.
291 /// \param args The command-line arguments representing test filters.
293 /// \return A set of test filters.
295 /// \throw cmdline:error If any of the arguments is invalid, or if they
296 /// represent a non-disjoint collection of filters.
297 std::set< engine::test_filter >
298 cli::parse_filters(const cmdline::args_vector& args)
300 std::set< engine::test_filter > filters;
303 for (cmdline::args_vector::const_iterator iter = args.begin();
304 iter != args.end(); iter++) {
305 const engine::test_filter filter(engine::test_filter::parse(*iter));
306 if (filters.find(filter) != filters.end())
307 throw cmdline::error(F("Duplicate filter '%s'") % filter.str());
308 filters.insert(filter);
310 check_disjoint_filters(filters);
311 } catch (const std::runtime_error& e) {
312 throw cmdline::error(e.what());
319 /// Reports the filters that have not matched any tests as errors.
321 /// \param unused The collection of unused filters to report.
322 /// \param ui The user interface object through which errors are to be reported.
324 /// \return True if there are any unused filters. The caller should report this
325 /// as an error to the user by means of a non-successful exit code.
327 cli::report_unused_filters(const std::set< engine::test_filter >& unused,
330 for (std::set< engine::test_filter >::const_iterator iter = unused.begin();
331 iter != unused.end(); iter++) {
332 cmdline::print_warning(ui, F("No test cases matched by the filter "
333 "'%s'.") % (*iter).str());
336 return !unused.empty();
340 /// Formats a time delta for user presentation.
342 /// \param delta The time delta to format.
344 /// \return A user-friendly representation of the time delta.
346 cli::format_delta(const datetime::delta& delta)
348 return F("%.3ss") % (delta.seconds + (delta.useconds / 1000000.0));
352 /// Formats a test case result for user presentation.
354 /// \param result The result to format.
356 /// \return A user-friendly representation of the result.
358 cli::format_result(const model::test_result& result)
362 switch (result.type()) {
363 case model::test_result_broken: text = "broken"; break;
364 case model::test_result_expected_failure: text = "expected_failure"; break;
365 case model::test_result_failed: text = "failed"; break;
366 case model::test_result_passed: text = "passed"; break;
367 case model::test_result_skipped: text = "skipped"; break;
371 if (!result.reason().empty())
372 text += ": " + result.reason();
378 /// Formats the identifier of a test case for user presentation.
380 /// \param test_program The test program containing the test case.
381 /// \param test_case_name The name of the test case.
383 /// \return A string representing the test case uniquely within a test suite.
385 cli::format_test_case_id(const model::test_program& test_program,
386 const std::string& test_case_name)
388 return F("%s:%s") % test_program.relative_path() % test_case_name;
392 /// Formats a filter using the same syntax of a test case.
394 /// \param test_filter The filter to format.
396 /// \return A string representing the test filter.
398 cli::format_test_case_id(const engine::test_filter& test_filter)
400 return F("%s:%s") % test_filter.test_program % test_filter.test_case;
404 /// Prints the version header information to the interface output.
406 /// \param ui Interface to which to write the version details.
408 cli::write_version_header(utils::cmdline::ui* ui)
410 ui->out(PACKAGE " (" PACKAGE_NAME ") " PACKAGE_VERSION);