xref: /freebsd/contrib/kyua/cli/common.cpp (revision b0d29bc4)
1*b0d29bc4SBrooks Davis // Copyright 2011 The Kyua Authors.
2*b0d29bc4SBrooks Davis // All rights reserved.
3*b0d29bc4SBrooks Davis //
4*b0d29bc4SBrooks Davis // Redistribution and use in source and binary forms, with or without
5*b0d29bc4SBrooks Davis // modification, are permitted provided that the following conditions are
6*b0d29bc4SBrooks Davis // met:
7*b0d29bc4SBrooks Davis //
8*b0d29bc4SBrooks Davis // * Redistributions of source code must retain the above copyright
9*b0d29bc4SBrooks Davis //   notice, this list of conditions and the following disclaimer.
10*b0d29bc4SBrooks Davis // * Redistributions in binary form must reproduce the above copyright
11*b0d29bc4SBrooks Davis //   notice, this list of conditions and the following disclaimer in the
12*b0d29bc4SBrooks Davis //   documentation and/or other materials provided with the distribution.
13*b0d29bc4SBrooks Davis // * Neither the name of Google Inc. nor the names of its contributors
14*b0d29bc4SBrooks Davis //   may be used to endorse or promote products derived from this software
15*b0d29bc4SBrooks Davis //   without specific prior written permission.
16*b0d29bc4SBrooks Davis //
17*b0d29bc4SBrooks Davis // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*b0d29bc4SBrooks Davis // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*b0d29bc4SBrooks Davis // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*b0d29bc4SBrooks Davis // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*b0d29bc4SBrooks Davis // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*b0d29bc4SBrooks Davis // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*b0d29bc4SBrooks Davis // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*b0d29bc4SBrooks Davis // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*b0d29bc4SBrooks Davis // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*b0d29bc4SBrooks Davis // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*b0d29bc4SBrooks Davis // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*b0d29bc4SBrooks Davis 
29*b0d29bc4SBrooks Davis #include "cli/common.hpp"
30*b0d29bc4SBrooks Davis 
31*b0d29bc4SBrooks Davis #include <algorithm>
32*b0d29bc4SBrooks Davis #include <fstream>
33*b0d29bc4SBrooks Davis #include <iostream>
34*b0d29bc4SBrooks Davis #include <stdexcept>
35*b0d29bc4SBrooks Davis 
36*b0d29bc4SBrooks Davis #include "engine/filters.hpp"
37*b0d29bc4SBrooks Davis #include "model/test_program.hpp"
38*b0d29bc4SBrooks Davis #include "model/test_result.hpp"
39*b0d29bc4SBrooks Davis #include "store/layout.hpp"
40*b0d29bc4SBrooks Davis #include "utils/cmdline/exceptions.hpp"
41*b0d29bc4SBrooks Davis #include "utils/cmdline/options.hpp"
42*b0d29bc4SBrooks Davis #include "utils/cmdline/parser.ipp"
43*b0d29bc4SBrooks Davis #include "utils/cmdline/ui.hpp"
44*b0d29bc4SBrooks Davis #include "utils/datetime.hpp"
45*b0d29bc4SBrooks Davis #include "utils/env.hpp"
46*b0d29bc4SBrooks Davis #include "utils/format/macros.hpp"
47*b0d29bc4SBrooks Davis #include "utils/logging/macros.hpp"
48*b0d29bc4SBrooks Davis #include "utils/fs/exceptions.hpp"
49*b0d29bc4SBrooks Davis #include "utils/fs/operations.hpp"
50*b0d29bc4SBrooks Davis #include "utils/fs/path.hpp"
51*b0d29bc4SBrooks Davis #include "utils/optional.ipp"
52*b0d29bc4SBrooks Davis #include "utils/sanity.hpp"
53*b0d29bc4SBrooks Davis 
54*b0d29bc4SBrooks Davis #if defined(HAVE_CONFIG_H)
55*b0d29bc4SBrooks Davis #   include "config.h"
56*b0d29bc4SBrooks Davis #endif
57*b0d29bc4SBrooks Davis 
58*b0d29bc4SBrooks Davis namespace cmdline = utils::cmdline;
59*b0d29bc4SBrooks Davis namespace datetime = utils::datetime;
60*b0d29bc4SBrooks Davis namespace fs = utils::fs;
61*b0d29bc4SBrooks Davis namespace layout = store::layout;
62*b0d29bc4SBrooks Davis 
63*b0d29bc4SBrooks Davis using utils::none;
64*b0d29bc4SBrooks Davis using utils::optional;
65*b0d29bc4SBrooks Davis 
66*b0d29bc4SBrooks Davis 
67*b0d29bc4SBrooks Davis /// Standard definition of the option to specify the build root.
68*b0d29bc4SBrooks Davis const cmdline::path_option cli::build_root_option(
69*b0d29bc4SBrooks Davis     "build-root",
70*b0d29bc4SBrooks Davis     "Path to the built test programs, if different from the location of the "
71*b0d29bc4SBrooks Davis     "Kyuafile scripts",
72*b0d29bc4SBrooks Davis     "path");
73*b0d29bc4SBrooks Davis 
74*b0d29bc4SBrooks Davis 
75*b0d29bc4SBrooks Davis /// Standard definition of the option to specify a Kyuafile.
76*b0d29bc4SBrooks Davis const cmdline::path_option cli::kyuafile_option(
77*b0d29bc4SBrooks Davis     'k', "kyuafile",
78*b0d29bc4SBrooks Davis     "Path to the test suite definition",
79*b0d29bc4SBrooks Davis     "file", "Kyuafile");
80*b0d29bc4SBrooks Davis 
81*b0d29bc4SBrooks Davis 
82*b0d29bc4SBrooks Davis /// Standard definition of the option to specify filters on test results.
83*b0d29bc4SBrooks Davis const cmdline::list_option cli::results_filter_option(
84*b0d29bc4SBrooks Davis     "results-filter", "Comma-separated list of result types to include in "
85*b0d29bc4SBrooks Davis     "the report", "types", "skipped,xfail,broken,failed");
86*b0d29bc4SBrooks Davis 
87*b0d29bc4SBrooks Davis 
88*b0d29bc4SBrooks Davis /// Standard definition of the option to specify the results file.
89*b0d29bc4SBrooks Davis ///
90*b0d29bc4SBrooks Davis /// TODO(jmmv): Should support a git-like syntax to go back in time, like
91*b0d29bc4SBrooks Davis /// --results-file=LATEST^N where N indicates how many runs to go back to.
92*b0d29bc4SBrooks Davis const cmdline::string_option cli::results_file_create_option(
93*b0d29bc4SBrooks Davis     'r', "results-file",
94*b0d29bc4SBrooks Davis     "Path to the results file to create; if left to the default value, the "
95*b0d29bc4SBrooks Davis     "name of the file is automatically computed for the current test suite",
96*b0d29bc4SBrooks Davis     "file", layout::results_auto_create_name);
97*b0d29bc4SBrooks Davis 
98*b0d29bc4SBrooks Davis 
99*b0d29bc4SBrooks Davis /// Standard definition of the option to specify the results file.
100*b0d29bc4SBrooks Davis ///
101*b0d29bc4SBrooks Davis /// TODO(jmmv): Should support a git-like syntax to go back in time, like
102*b0d29bc4SBrooks Davis /// --results-file=LATEST^N where N indicates how many runs to go back to.
103*b0d29bc4SBrooks Davis const cmdline::string_option cli::results_file_open_option(
104*b0d29bc4SBrooks Davis     'r', "results-file",
105*b0d29bc4SBrooks Davis     "Path to the results file to open or the identifier of the current test "
106*b0d29bc4SBrooks Davis     "suite or a previous results file for automatic lookup; if left to the "
107*b0d29bc4SBrooks Davis     "default value, uses the current directory as the test suite name",
108*b0d29bc4SBrooks Davis     "file", layout::results_auto_open_name);
109*b0d29bc4SBrooks Davis 
110*b0d29bc4SBrooks Davis 
111*b0d29bc4SBrooks Davis namespace {
112*b0d29bc4SBrooks Davis 
113*b0d29bc4SBrooks Davis 
114*b0d29bc4SBrooks Davis /// Gets the path to the historical database if it exists.
115*b0d29bc4SBrooks Davis ///
116*b0d29bc4SBrooks Davis /// TODO(jmmv): This function should go away.  It only exists as a temporary
117*b0d29bc4SBrooks Davis /// transitional path to force the use of the stale ~/.kyua/store.db if it
118*b0d29bc4SBrooks Davis /// exists.
119*b0d29bc4SBrooks Davis ///
120*b0d29bc4SBrooks Davis /// \return A path if the file is found; none otherwise.
121*b0d29bc4SBrooks Davis static optional< fs::path >
get_historical_db(void)122*b0d29bc4SBrooks Davis get_historical_db(void)
123*b0d29bc4SBrooks Davis {
124*b0d29bc4SBrooks Davis     optional< fs::path > home = utils::get_home();
125*b0d29bc4SBrooks Davis     if (home) {
126*b0d29bc4SBrooks Davis         const fs::path old_db = home.get() / ".kyua/store.db";
127*b0d29bc4SBrooks Davis         if (fs::exists(old_db)) {
128*b0d29bc4SBrooks Davis             if (old_db.is_absolute())
129*b0d29bc4SBrooks Davis                 return utils::make_optional(old_db);
130*b0d29bc4SBrooks Davis             else
131*b0d29bc4SBrooks Davis                 return utils::make_optional(old_db.to_absolute());
132*b0d29bc4SBrooks Davis         } else {
133*b0d29bc4SBrooks Davis             return none;
134*b0d29bc4SBrooks Davis         }
135*b0d29bc4SBrooks Davis     } else {
136*b0d29bc4SBrooks Davis         return none;
137*b0d29bc4SBrooks Davis     }
138*b0d29bc4SBrooks Davis }
139*b0d29bc4SBrooks Davis 
140*b0d29bc4SBrooks Davis 
141*b0d29bc4SBrooks Davis /// Converts a set of result type names to identifiers.
142*b0d29bc4SBrooks Davis ///
143*b0d29bc4SBrooks Davis /// \param names The collection of names to process; may be empty.
144*b0d29bc4SBrooks Davis ///
145*b0d29bc4SBrooks Davis /// \return The result type identifiers corresponding to the input names.
146*b0d29bc4SBrooks Davis ///
147*b0d29bc4SBrooks Davis /// \throw std::runtime_error If any name in the input names is invalid.
148*b0d29bc4SBrooks Davis static cli::result_types
parse_types(const std::vector<std::string> & names)149*b0d29bc4SBrooks Davis parse_types(const std::vector< std::string >& names)
150*b0d29bc4SBrooks Davis {
151*b0d29bc4SBrooks Davis     typedef std::map< std::string, model::test_result_type > types_map;
152*b0d29bc4SBrooks Davis     types_map valid_types;
153*b0d29bc4SBrooks Davis     valid_types["broken"] = model::test_result_broken;
154*b0d29bc4SBrooks Davis     valid_types["failed"] = model::test_result_failed;
155*b0d29bc4SBrooks Davis     valid_types["passed"] = model::test_result_passed;
156*b0d29bc4SBrooks Davis     valid_types["skipped"] = model::test_result_skipped;
157*b0d29bc4SBrooks Davis     valid_types["xfail"] = model::test_result_expected_failure;
158*b0d29bc4SBrooks Davis 
159*b0d29bc4SBrooks Davis     cli::result_types types;
160*b0d29bc4SBrooks Davis     for (std::vector< std::string >::const_iterator iter = names.begin();
161*b0d29bc4SBrooks Davis          iter != names.end(); ++iter) {
162*b0d29bc4SBrooks Davis         const types_map::const_iterator match = valid_types.find(*iter);
163*b0d29bc4SBrooks Davis         if (match == valid_types.end())
164*b0d29bc4SBrooks Davis             throw std::runtime_error(F("Unknown result type '%s'") % *iter);
165*b0d29bc4SBrooks Davis         else
166*b0d29bc4SBrooks Davis             types.push_back((*match).second);
167*b0d29bc4SBrooks Davis     }
168*b0d29bc4SBrooks Davis     return types;
169*b0d29bc4SBrooks Davis }
170*b0d29bc4SBrooks Davis 
171*b0d29bc4SBrooks Davis 
172*b0d29bc4SBrooks Davis }  // anonymous namespace
173*b0d29bc4SBrooks Davis 
174*b0d29bc4SBrooks Davis 
175*b0d29bc4SBrooks Davis /// Gets the path to the build root, if any.
176*b0d29bc4SBrooks Davis ///
177*b0d29bc4SBrooks Davis /// This is just syntactic sugar to simplify quierying the 'build_root_option'.
178*b0d29bc4SBrooks Davis ///
179*b0d29bc4SBrooks Davis /// \param cmdline The parsed command line.
180*b0d29bc4SBrooks Davis ///
181*b0d29bc4SBrooks Davis /// \return The path to the build root, if specified; none otherwise.
182*b0d29bc4SBrooks Davis optional< fs::path >
build_root_path(const cmdline::parsed_cmdline & cmdline)183*b0d29bc4SBrooks Davis cli::build_root_path(const cmdline::parsed_cmdline& cmdline)
184*b0d29bc4SBrooks Davis {
185*b0d29bc4SBrooks Davis     optional< fs::path > build_root;
186*b0d29bc4SBrooks Davis     if (cmdline.has_option(build_root_option.long_name()))
187*b0d29bc4SBrooks Davis         build_root = cmdline.get_option< cmdline::path_option >(
188*b0d29bc4SBrooks Davis             build_root_option.long_name());
189*b0d29bc4SBrooks Davis     return build_root;
190*b0d29bc4SBrooks Davis }
191*b0d29bc4SBrooks Davis 
192*b0d29bc4SBrooks Davis 
193*b0d29bc4SBrooks Davis /// Gets the path to the Kyuafile to be loaded.
194*b0d29bc4SBrooks Davis ///
195*b0d29bc4SBrooks Davis /// This is just syntactic sugar to simplify quierying the 'kyuafile_option'.
196*b0d29bc4SBrooks Davis ///
197*b0d29bc4SBrooks Davis /// \param cmdline The parsed command line.
198*b0d29bc4SBrooks Davis ///
199*b0d29bc4SBrooks Davis /// \return The path to the Kyuafile to be loaded.
200*b0d29bc4SBrooks Davis fs::path
kyuafile_path(const cmdline::parsed_cmdline & cmdline)201*b0d29bc4SBrooks Davis cli::kyuafile_path(const cmdline::parsed_cmdline& cmdline)
202*b0d29bc4SBrooks Davis {
203*b0d29bc4SBrooks Davis     return cmdline.get_option< cmdline::path_option >(
204*b0d29bc4SBrooks Davis         kyuafile_option.long_name());
205*b0d29bc4SBrooks Davis }
206*b0d29bc4SBrooks Davis 
207*b0d29bc4SBrooks Davis 
208*b0d29bc4SBrooks Davis /// Gets the value of the results-file flag for the creation of a new file.
209*b0d29bc4SBrooks Davis ///
210*b0d29bc4SBrooks Davis /// \param cmdline The parsed command line from which to extract any possible
211*b0d29bc4SBrooks Davis ///     override for the location of the database via the --results-file flag.
212*b0d29bc4SBrooks Davis ///
213*b0d29bc4SBrooks Davis /// \return The path to the database to be used.
214*b0d29bc4SBrooks Davis ///
215*b0d29bc4SBrooks Davis /// \throw cmdline::error If the value passed to the flag is invalid.
216*b0d29bc4SBrooks Davis std::string
results_file_create(const cmdline::parsed_cmdline & cmdline)217*b0d29bc4SBrooks Davis cli::results_file_create(const cmdline::parsed_cmdline& cmdline)
218*b0d29bc4SBrooks Davis {
219*b0d29bc4SBrooks Davis     std::string results_file = cmdline.get_option< cmdline::string_option >(
220*b0d29bc4SBrooks Davis         results_file_create_option.long_name());
221*b0d29bc4SBrooks Davis     if (results_file == results_file_create_option.default_value()) {
222*b0d29bc4SBrooks Davis         const optional< fs::path > historical_db = get_historical_db();
223*b0d29bc4SBrooks Davis         if (historical_db)
224*b0d29bc4SBrooks Davis             results_file = historical_db.get().str();
225*b0d29bc4SBrooks Davis     } else {
226*b0d29bc4SBrooks Davis         try {
227*b0d29bc4SBrooks Davis             (void)fs::path(results_file);
228*b0d29bc4SBrooks Davis         } catch (const fs::error& e) {
229*b0d29bc4SBrooks Davis             throw cmdline::usage_error(F("Invalid value passed to --%s") %
230*b0d29bc4SBrooks Davis                                        results_file_create_option.long_name());
231*b0d29bc4SBrooks Davis         }
232*b0d29bc4SBrooks Davis     }
233*b0d29bc4SBrooks Davis     return results_file;
234*b0d29bc4SBrooks Davis }
235*b0d29bc4SBrooks Davis 
236*b0d29bc4SBrooks Davis 
237*b0d29bc4SBrooks Davis /// Gets the value of the results-file flag for the lookup of the file.
238*b0d29bc4SBrooks Davis ///
239*b0d29bc4SBrooks Davis /// \param cmdline The parsed command line from which to extract any possible
240*b0d29bc4SBrooks Davis ///     override for the location of the database via the --results-file flag.
241*b0d29bc4SBrooks Davis ///
242*b0d29bc4SBrooks Davis /// \return The path to the database to be used.
243*b0d29bc4SBrooks Davis ///
244*b0d29bc4SBrooks Davis /// \throw cmdline::error If the value passed to the flag is invalid.
245*b0d29bc4SBrooks Davis std::string
results_file_open(const cmdline::parsed_cmdline & cmdline)246*b0d29bc4SBrooks Davis cli::results_file_open(const cmdline::parsed_cmdline& cmdline)
247*b0d29bc4SBrooks Davis {
248*b0d29bc4SBrooks Davis     std::string results_file = cmdline.get_option< cmdline::string_option >(
249*b0d29bc4SBrooks Davis         results_file_open_option.long_name());
250*b0d29bc4SBrooks Davis     if (results_file == results_file_open_option.default_value()) {
251*b0d29bc4SBrooks Davis         const optional< fs::path > historical_db = get_historical_db();
252*b0d29bc4SBrooks Davis         if (historical_db)
253*b0d29bc4SBrooks Davis             results_file = historical_db.get().str();
254*b0d29bc4SBrooks Davis     } else {
255*b0d29bc4SBrooks Davis         try {
256*b0d29bc4SBrooks Davis             (void)fs::path(results_file);
257*b0d29bc4SBrooks Davis         } catch (const fs::error& e) {
258*b0d29bc4SBrooks Davis             throw cmdline::usage_error(F("Invalid value passed to --%s") %
259*b0d29bc4SBrooks Davis                                        results_file_open_option.long_name());
260*b0d29bc4SBrooks Davis         }
261*b0d29bc4SBrooks Davis     }
262*b0d29bc4SBrooks Davis     return results_file;
263*b0d29bc4SBrooks Davis }
264*b0d29bc4SBrooks Davis 
265*b0d29bc4SBrooks Davis 
266*b0d29bc4SBrooks Davis /// Gets the filters for the result types.
267*b0d29bc4SBrooks Davis ///
268*b0d29bc4SBrooks Davis /// \param cmdline The parsed command line.
269*b0d29bc4SBrooks Davis ///
270*b0d29bc4SBrooks Davis /// \return A collection of result types to be used for filtering.
271*b0d29bc4SBrooks Davis ///
272*b0d29bc4SBrooks Davis /// \throw std::runtime_error If any of the user-provided filters is invalid.
273*b0d29bc4SBrooks Davis cli::result_types
get_result_types(const utils::cmdline::parsed_cmdline & cmdline)274*b0d29bc4SBrooks Davis cli::get_result_types(const utils::cmdline::parsed_cmdline& cmdline)
275*b0d29bc4SBrooks Davis {
276*b0d29bc4SBrooks Davis     result_types types = parse_types(
277*b0d29bc4SBrooks Davis         cmdline.get_option< cmdline::list_option >("results-filter"));
278*b0d29bc4SBrooks Davis     if (types.empty()) {
279*b0d29bc4SBrooks Davis         types.push_back(model::test_result_passed);
280*b0d29bc4SBrooks Davis         types.push_back(model::test_result_skipped);
281*b0d29bc4SBrooks Davis         types.push_back(model::test_result_expected_failure);
282*b0d29bc4SBrooks Davis         types.push_back(model::test_result_broken);
283*b0d29bc4SBrooks Davis         types.push_back(model::test_result_failed);
284*b0d29bc4SBrooks Davis     }
285*b0d29bc4SBrooks Davis     return types;
286*b0d29bc4SBrooks Davis }
287*b0d29bc4SBrooks Davis 
288*b0d29bc4SBrooks Davis 
289*b0d29bc4SBrooks Davis /// Parses a set of command-line arguments to construct test filters.
290*b0d29bc4SBrooks Davis ///
291*b0d29bc4SBrooks Davis /// \param args The command-line arguments representing test filters.
292*b0d29bc4SBrooks Davis ///
293*b0d29bc4SBrooks Davis /// \return A set of test filters.
294*b0d29bc4SBrooks Davis ///
295*b0d29bc4SBrooks Davis /// \throw cmdline:error If any of the arguments is invalid, or if they
296*b0d29bc4SBrooks Davis ///     represent a non-disjoint collection of filters.
297*b0d29bc4SBrooks Davis std::set< engine::test_filter >
parse_filters(const cmdline::args_vector & args)298*b0d29bc4SBrooks Davis cli::parse_filters(const cmdline::args_vector& args)
299*b0d29bc4SBrooks Davis {
300*b0d29bc4SBrooks Davis     std::set< engine::test_filter > filters;
301*b0d29bc4SBrooks Davis 
302*b0d29bc4SBrooks Davis     try {
303*b0d29bc4SBrooks Davis         for (cmdline::args_vector::const_iterator iter = args.begin();
304*b0d29bc4SBrooks Davis              iter != args.end(); iter++) {
305*b0d29bc4SBrooks Davis             const engine::test_filter filter(engine::test_filter::parse(*iter));
306*b0d29bc4SBrooks Davis             if (filters.find(filter) != filters.end())
307*b0d29bc4SBrooks Davis                 throw cmdline::error(F("Duplicate filter '%s'") % filter.str());
308*b0d29bc4SBrooks Davis             filters.insert(filter);
309*b0d29bc4SBrooks Davis         }
310*b0d29bc4SBrooks Davis         check_disjoint_filters(filters);
311*b0d29bc4SBrooks Davis     } catch (const std::runtime_error& e) {
312*b0d29bc4SBrooks Davis         throw cmdline::error(e.what());
313*b0d29bc4SBrooks Davis     }
314*b0d29bc4SBrooks Davis 
315*b0d29bc4SBrooks Davis     return filters;
316*b0d29bc4SBrooks Davis }
317*b0d29bc4SBrooks Davis 
318*b0d29bc4SBrooks Davis 
319*b0d29bc4SBrooks Davis /// Reports the filters that have not matched any tests as errors.
320*b0d29bc4SBrooks Davis ///
321*b0d29bc4SBrooks Davis /// \param unused The collection of unused filters to report.
322*b0d29bc4SBrooks Davis /// \param ui The user interface object through which errors are to be reported.
323*b0d29bc4SBrooks Davis ///
324*b0d29bc4SBrooks Davis /// \return True if there are any unused filters.  The caller should report this
325*b0d29bc4SBrooks Davis /// as an error to the user by means of a non-successful exit code.
326*b0d29bc4SBrooks Davis bool
report_unused_filters(const std::set<engine::test_filter> & unused,cmdline::ui * ui)327*b0d29bc4SBrooks Davis cli::report_unused_filters(const std::set< engine::test_filter >& unused,
328*b0d29bc4SBrooks Davis                            cmdline::ui* ui)
329*b0d29bc4SBrooks Davis {
330*b0d29bc4SBrooks Davis     for (std::set< engine::test_filter >::const_iterator iter = unused.begin();
331*b0d29bc4SBrooks Davis          iter != unused.end(); iter++) {
332*b0d29bc4SBrooks Davis         cmdline::print_warning(ui, F("No test cases matched by the filter "
333*b0d29bc4SBrooks Davis                                      "'%s'.") % (*iter).str());
334*b0d29bc4SBrooks Davis     }
335*b0d29bc4SBrooks Davis 
336*b0d29bc4SBrooks Davis     return !unused.empty();
337*b0d29bc4SBrooks Davis }
338*b0d29bc4SBrooks Davis 
339*b0d29bc4SBrooks Davis 
340*b0d29bc4SBrooks Davis /// Formats a time delta for user presentation.
341*b0d29bc4SBrooks Davis ///
342*b0d29bc4SBrooks Davis /// \param delta The time delta to format.
343*b0d29bc4SBrooks Davis ///
344*b0d29bc4SBrooks Davis /// \return A user-friendly representation of the time delta.
345*b0d29bc4SBrooks Davis std::string
format_delta(const datetime::delta & delta)346*b0d29bc4SBrooks Davis cli::format_delta(const datetime::delta& delta)
347*b0d29bc4SBrooks Davis {
348*b0d29bc4SBrooks Davis     return F("%.3ss") % (delta.seconds + (delta.useconds / 1000000.0));
349*b0d29bc4SBrooks Davis }
350*b0d29bc4SBrooks Davis 
351*b0d29bc4SBrooks Davis 
352*b0d29bc4SBrooks Davis /// Formats a test case result for user presentation.
353*b0d29bc4SBrooks Davis ///
354*b0d29bc4SBrooks Davis /// \param result The result to format.
355*b0d29bc4SBrooks Davis ///
356*b0d29bc4SBrooks Davis /// \return A user-friendly representation of the result.
357*b0d29bc4SBrooks Davis std::string
format_result(const model::test_result & result)358*b0d29bc4SBrooks Davis cli::format_result(const model::test_result& result)
359*b0d29bc4SBrooks Davis {
360*b0d29bc4SBrooks Davis     std::string text;
361*b0d29bc4SBrooks Davis 
362*b0d29bc4SBrooks Davis     switch (result.type()) {
363*b0d29bc4SBrooks Davis     case model::test_result_broken: text = "broken"; break;
364*b0d29bc4SBrooks Davis     case model::test_result_expected_failure: text = "expected_failure"; break;
365*b0d29bc4SBrooks Davis     case model::test_result_failed: text = "failed"; break;
366*b0d29bc4SBrooks Davis     case model::test_result_passed: text = "passed"; break;
367*b0d29bc4SBrooks Davis     case model::test_result_skipped: text = "skipped"; break;
368*b0d29bc4SBrooks Davis     }
369*b0d29bc4SBrooks Davis     INV(!text.empty());
370*b0d29bc4SBrooks Davis 
371*b0d29bc4SBrooks Davis     if (!result.reason().empty())
372*b0d29bc4SBrooks Davis         text += ": " + result.reason();
373*b0d29bc4SBrooks Davis 
374*b0d29bc4SBrooks Davis     return text;
375*b0d29bc4SBrooks Davis }
376*b0d29bc4SBrooks Davis 
377*b0d29bc4SBrooks Davis 
378*b0d29bc4SBrooks Davis /// Formats the identifier of a test case for user presentation.
379*b0d29bc4SBrooks Davis ///
380*b0d29bc4SBrooks Davis /// \param test_program The test program containing the test case.
381*b0d29bc4SBrooks Davis /// \param test_case_name The name of the test case.
382*b0d29bc4SBrooks Davis ///
383*b0d29bc4SBrooks Davis /// \return A string representing the test case uniquely within a test suite.
384*b0d29bc4SBrooks Davis std::string
format_test_case_id(const model::test_program & test_program,const std::string & test_case_name)385*b0d29bc4SBrooks Davis cli::format_test_case_id(const model::test_program& test_program,
386*b0d29bc4SBrooks Davis                          const std::string& test_case_name)
387*b0d29bc4SBrooks Davis {
388*b0d29bc4SBrooks Davis     return F("%s:%s") % test_program.relative_path() % test_case_name;
389*b0d29bc4SBrooks Davis }
390*b0d29bc4SBrooks Davis 
391*b0d29bc4SBrooks Davis 
392*b0d29bc4SBrooks Davis /// Formats a filter using the same syntax of a test case.
393*b0d29bc4SBrooks Davis ///
394*b0d29bc4SBrooks Davis /// \param test_filter The filter to format.
395*b0d29bc4SBrooks Davis ///
396*b0d29bc4SBrooks Davis /// \return A string representing the test filter.
397*b0d29bc4SBrooks Davis std::string
format_test_case_id(const engine::test_filter & test_filter)398*b0d29bc4SBrooks Davis cli::format_test_case_id(const engine::test_filter& test_filter)
399*b0d29bc4SBrooks Davis {
400*b0d29bc4SBrooks Davis     return F("%s:%s") % test_filter.test_program % test_filter.test_case;
401*b0d29bc4SBrooks Davis }
402*b0d29bc4SBrooks Davis 
403*b0d29bc4SBrooks Davis 
404*b0d29bc4SBrooks Davis /// Prints the version header information to the interface output.
405*b0d29bc4SBrooks Davis ///
406*b0d29bc4SBrooks Davis /// \param ui Interface to which to write the version details.
407*b0d29bc4SBrooks Davis void
write_version_header(utils::cmdline::ui * ui)408*b0d29bc4SBrooks Davis cli::write_version_header(utils::cmdline::ui* ui)
409*b0d29bc4SBrooks Davis {
410*b0d29bc4SBrooks Davis     ui->out(PACKAGE " (" PACKAGE_NAME ") " PACKAGE_VERSION);
411*b0d29bc4SBrooks Davis }
412