1 // Copyright 2011 Google Inc.
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"
33 #include "engine/filters.hpp"
34 #include "engine/test_case.hpp"
35 #include "engine/test_program.hpp"
36 #include "engine/test_result.hpp"
37 #include "utils/cmdline/exceptions.hpp"
38 #include "utils/cmdline/parser.ipp"
39 #include "utils/datetime.hpp"
40 #include "utils/env.hpp"
41 #include "utils/format/macros.hpp"
42 #include "utils/logging/macros.hpp"
43 #include "utils/fs/exceptions.hpp"
44 #include "utils/fs/operations.hpp"
45 #include "utils/fs/path.hpp"
46 #include "utils/optional.ipp"
48 namespace cmdline
= utils::cmdline
;
49 namespace datetime
= utils::datetime
;
50 namespace fs
= utils::fs
;
53 using utils::optional
;
56 /// Standard definition of the option to specify the build root.
57 const cmdline::path_option
cli::build_root_option(
59 "Path to the built test programs, if different from the location of the "
64 /// Standard definition of the option to specify a Kyuafile.
65 const cmdline::path_option
cli::kyuafile_option(
67 "Path to the test suite definition",
71 /// Standard definition of the option to specify filters on test results.
72 const cmdline::list_option
cli::results_filter_option(
73 "results-filter", "Comma-separated list of result types to include in "
74 "the report", "types", "skipped,xfail,broken,failed");
77 /// Standard definition of the option to specify the store.
78 const cmdline::path_option
cli::store_option(
80 "Path to the store database",
81 "file", "~/.kyua/store.db");
87 /// Converts a set of result type names to identifiers.
89 /// \param names The collection of names to process; may be empty.
91 /// \return The result type identifiers corresponding to the input names.
93 /// \throw std::runtime_error If any name in the input names is invalid.
94 static cli::result_types
95 parse_types(const std::vector
< std::string
>& names
)
97 using engine::test_result
;
98 typedef std::map
< std::string
, test_result::result_type
> types_map
;
99 types_map valid_types
;
100 valid_types
["broken"] = test_result::broken
;
101 valid_types
["failed"] = test_result::failed
;
102 valid_types
["passed"] = test_result::passed
;
103 valid_types
["skipped"] = test_result::skipped
;
104 valid_types
["xfail"] = test_result::expected_failure
;
106 cli::result_types types
;
107 for (std::vector
< std::string
>::const_iterator iter
= names
.begin();
108 iter
!= names
.end(); ++iter
) {
109 const types_map::const_iterator match
= valid_types
.find(*iter
);
110 if (match
== valid_types
.end())
111 throw std::runtime_error(F("Unknown result type '%s'") % *iter
);
113 types
.push_back((*match
).second
);
119 } // anonymous namespace
122 /// Gets the path to the build root, if any.
124 /// This is just syntactic sugar to simplify quierying the 'build_root_option'.
126 /// \param cmdline The parsed command line.
128 /// \return The path to the build root, if specified; none otherwise.
130 cli::build_root_path(const cmdline::parsed_cmdline
& cmdline
)
132 optional
< fs::path
> build_root
;
133 if (cmdline
.has_option(build_root_option
.long_name()))
134 build_root
= cmdline
.get_option
< cmdline::path_option
>(
135 build_root_option
.long_name());
140 /// Gets the value of the HOME environment variable with path validation.
142 /// \return The value of the HOME environment variable if it is a valid path;
143 /// none if it is not defined or if it contains an invalid path.
147 const optional
< std::string
> home
= utils::getenv("HOME");
150 return utils::make_optional(fs::path(home
.get()));
151 } catch (const fs::error
& e
) {
152 LW(F("Invalid value '%s' in HOME environment variable: %s") %
153 home
.get() % e
.what());
161 /// Gets the path to the Kyuafile to be loaded.
163 /// This is just syntactic sugar to simplify quierying the 'kyuafile_option'.
165 /// \param cmdline The parsed command line.
167 /// \return The path to the Kyuafile to be loaded.
169 cli::kyuafile_path(const cmdline::parsed_cmdline
& cmdline
)
171 return cmdline
.get_option
< cmdline::path_option
>(
172 kyuafile_option
.long_name());
176 /// Gets the filters for the result types.
178 /// \param cmdline The parsed command line.
180 /// \return A collection of result types to be used for filtering.
182 /// \throw std::runtime_error If any of the user-provided filters is invalid.
184 cli::get_result_types(const utils::cmdline::parsed_cmdline
& cmdline
)
186 result_types types
= parse_types(
187 cmdline
.get_option
< cmdline::list_option
>("results-filter"));
189 types
.push_back(engine::test_result::passed
);
190 types
.push_back(engine::test_result::skipped
);
191 types
.push_back(engine::test_result::expected_failure
);
192 types
.push_back(engine::test_result::broken
);
193 types
.push_back(engine::test_result::failed
);
199 /// Gets the path to the store to be used.
201 /// This has the side-effect of creating the directory in which to store the
202 /// database if and only if the path to the database matches the default value.
203 /// When the user does not specify an override for the location of the database,
204 /// he should not care about the directory existing. Any of this is not a big
205 /// deal though, because logs are also stored within ~/.kyua and thus we will
206 /// most likely end up creating the directory anyway.
208 /// \param cmdline The parsed command line.
210 /// \return The path to the store to be used.
212 /// \throw fs::error If the creation of the directory fails.
214 cli::store_path(const cmdline::parsed_cmdline
& cmdline
)
216 fs::path store
= cmdline
.get_option
< cmdline::path_option
>(
217 store_option
.long_name());
218 if (store
== fs::path(store_option
.default_value())) {
219 const optional
< fs::path
> home
= cli::get_home();
221 store
= home
.get() / ".kyua/store.db";
222 fs::mkdir_p(store
.branch_path(), 0777);
224 store
= fs::path("kyua-store.db");
225 LW("HOME not defined; creating store database in current "
229 LI(F("Store database set to: %s") % store
);
234 /// Parses a set of command-line arguments to construct test filters.
236 /// \param args The command-line arguments representing test filters.
238 /// \throw cmdline:error If any of the arguments is invalid, or if they
239 /// represent a non-disjoint collection of filters.
240 std::set
< engine::test_filter
>
241 cli::parse_filters(const cmdline::args_vector
& args
)
243 std::set
< engine::test_filter
> filters
;
246 for (cmdline::args_vector::const_iterator iter
= args
.begin();
247 iter
!= args
.end(); iter
++) {
248 const engine::test_filter
filter(engine::test_filter::parse(*iter
));
249 if (filters
.find(filter
) != filters
.end())
250 throw cmdline::error(F("Duplicate filter '%s'") % filter
.str());
251 filters
.insert(filter
);
253 check_disjoint_filters(filters
);
254 } catch (const std::runtime_error
& e
) {
255 throw cmdline::error(e
.what());
262 /// Reports the filters that have not matched any tests as errors.
264 /// \param unused The collection of unused filters to report.
265 /// \param ui The user interface object through which errors are to be reported.
267 /// \return True if there are any unused filters. The caller should report this
268 /// as an error to the user by means of a non-successful exit code.
270 cli::report_unused_filters(const std::set
< engine::test_filter
>& unused
,
273 for (std::set
< engine::test_filter
>::const_iterator iter
= unused
.begin();
274 iter
!= unused
.end(); iter
++) {
275 cmdline::print_warning(ui
, F("No test cases matched by the filter '%s'")
279 return !unused
.empty();
283 /// Formats a time delta for user presentation.
285 /// \param delta The time delta to format.
287 /// \return A user-friendly representation of the time delta.
289 cli::format_delta(const datetime::delta
& delta
)
291 return F("%.3ss") % (delta
.seconds
+ (delta
.useconds
/ 1000000.0));
295 /// Formats a test case result for user presentation.
297 /// \param result The result to format.
299 /// \return A user-friendly representation of the result.
301 cli::format_result(const engine::test_result
& result
)
305 using engine::test_result
;
306 switch (result
.type()) {
307 case test_result::broken
: text
= "broken"; break;
308 case test_result::expected_failure
: text
= "expected_failure"; break;
309 case test_result::failed
: text
= "failed"; break;
310 case test_result::passed
: text
= "passed"; break;
311 case test_result::skipped
: text
= "skipped"; break;
315 if (!result
.reason().empty())
316 text
+= ": " + result
.reason();
322 /// Formats the identifier of a test case for user presentation.
324 /// \param test_case The test case whose identifier to format.
326 /// \return A string representing the test case uniquely within a test suite.
328 cli::format_test_case_id(const engine::test_case
& test_case
)
330 return F("%s:%s") % test_case
.container_test_program().relative_path() %
335 /// Formats a filter using the same syntax of a test case.
337 /// \param test_filter The filter to format.
339 /// \return A string representing the test filter.
341 cli::format_test_case_id(const engine::test_filter
& test_filter
)
343 return F("%s:%s") % test_filter
.test_program
% test_filter
.test_case
;