xref: /minix3/external/bsd/kyua-cli/dist/cli/common.cpp (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
111be35a1SLionel Sambuc // Copyright 2011 Google Inc.
211be35a1SLionel Sambuc // All rights reserved.
311be35a1SLionel Sambuc //
411be35a1SLionel Sambuc // Redistribution and use in source and binary forms, with or without
511be35a1SLionel Sambuc // modification, are permitted provided that the following conditions are
611be35a1SLionel Sambuc // met:
711be35a1SLionel Sambuc //
811be35a1SLionel Sambuc // * Redistributions of source code must retain the above copyright
911be35a1SLionel Sambuc //   notice, this list of conditions and the following disclaimer.
1011be35a1SLionel Sambuc // * Redistributions in binary form must reproduce the above copyright
1111be35a1SLionel Sambuc //   notice, this list of conditions and the following disclaimer in the
1211be35a1SLionel Sambuc //   documentation and/or other materials provided with the distribution.
1311be35a1SLionel Sambuc // * Neither the name of Google Inc. nor the names of its contributors
1411be35a1SLionel Sambuc //   may be used to endorse or promote products derived from this software
1511be35a1SLionel Sambuc //   without specific prior written permission.
1611be35a1SLionel Sambuc //
1711be35a1SLionel Sambuc // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1811be35a1SLionel Sambuc // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1911be35a1SLionel Sambuc // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2011be35a1SLionel Sambuc // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2111be35a1SLionel Sambuc // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2211be35a1SLionel Sambuc // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2311be35a1SLionel Sambuc // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2411be35a1SLionel Sambuc // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2511be35a1SLionel Sambuc // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2611be35a1SLionel Sambuc // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2711be35a1SLionel Sambuc // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2811be35a1SLionel Sambuc 
2911be35a1SLionel Sambuc #include "cli/common.hpp"
3011be35a1SLionel Sambuc 
3111be35a1SLionel Sambuc #include <algorithm>
3211be35a1SLionel Sambuc 
3311be35a1SLionel Sambuc #include "engine/filters.hpp"
3411be35a1SLionel Sambuc #include "engine/test_case.hpp"
3511be35a1SLionel Sambuc #include "engine/test_program.hpp"
3611be35a1SLionel Sambuc #include "engine/test_result.hpp"
3711be35a1SLionel Sambuc #include "utils/cmdline/exceptions.hpp"
3811be35a1SLionel Sambuc #include "utils/cmdline/parser.ipp"
3911be35a1SLionel Sambuc #include "utils/datetime.hpp"
4011be35a1SLionel Sambuc #include "utils/env.hpp"
4111be35a1SLionel Sambuc #include "utils/format/macros.hpp"
4211be35a1SLionel Sambuc #include "utils/logging/macros.hpp"
4311be35a1SLionel Sambuc #include "utils/fs/exceptions.hpp"
4411be35a1SLionel Sambuc #include "utils/fs/operations.hpp"
4511be35a1SLionel Sambuc #include "utils/fs/path.hpp"
4611be35a1SLionel Sambuc #include "utils/optional.ipp"
4711be35a1SLionel Sambuc 
4811be35a1SLionel Sambuc namespace cmdline = utils::cmdline;
4911be35a1SLionel Sambuc namespace datetime = utils::datetime;
5011be35a1SLionel Sambuc namespace fs = utils::fs;
5111be35a1SLionel Sambuc 
5211be35a1SLionel Sambuc using utils::none;
5311be35a1SLionel Sambuc using utils::optional;
5411be35a1SLionel Sambuc 
5511be35a1SLionel Sambuc 
5611be35a1SLionel Sambuc /// Standard definition of the option to specify the build root.
5711be35a1SLionel Sambuc const cmdline::path_option cli::build_root_option(
5811be35a1SLionel Sambuc     "build-root",
5911be35a1SLionel Sambuc     "Path to the built test programs, if different from the location of the "
6011be35a1SLionel Sambuc     "Kyuafile scripts",
6111be35a1SLionel Sambuc     "path");
6211be35a1SLionel Sambuc 
6311be35a1SLionel Sambuc 
6411be35a1SLionel Sambuc /// Standard definition of the option to specify a Kyuafile.
6511be35a1SLionel Sambuc const cmdline::path_option cli::kyuafile_option(
6611be35a1SLionel Sambuc     'k', "kyuafile",
6711be35a1SLionel Sambuc     "Path to the test suite definition",
6811be35a1SLionel Sambuc     "file", "Kyuafile");
6911be35a1SLionel Sambuc 
7011be35a1SLionel Sambuc 
71*84d9c625SLionel Sambuc /// Standard definition of the option to specify filters on test results.
72*84d9c625SLionel Sambuc const cmdline::list_option cli::results_filter_option(
73*84d9c625SLionel Sambuc     "results-filter", "Comma-separated list of result types to include in "
74*84d9c625SLionel Sambuc     "the report", "types", "skipped,xfail,broken,failed");
75*84d9c625SLionel Sambuc 
76*84d9c625SLionel Sambuc 
7711be35a1SLionel Sambuc /// Standard definition of the option to specify the store.
7811be35a1SLionel Sambuc const cmdline::path_option cli::store_option(
7911be35a1SLionel Sambuc     's', "store",
8011be35a1SLionel Sambuc     "Path to the store database",
8111be35a1SLionel Sambuc     "file", "~/.kyua/store.db");
8211be35a1SLionel Sambuc 
8311be35a1SLionel Sambuc 
84*84d9c625SLionel Sambuc namespace {
85*84d9c625SLionel Sambuc 
86*84d9c625SLionel Sambuc 
87*84d9c625SLionel Sambuc /// Converts a set of result type names to identifiers.
88*84d9c625SLionel Sambuc ///
89*84d9c625SLionel Sambuc /// \param names The collection of names to process; may be empty.
90*84d9c625SLionel Sambuc ///
91*84d9c625SLionel Sambuc /// \return The result type identifiers corresponding to the input names.
92*84d9c625SLionel Sambuc ///
93*84d9c625SLionel Sambuc /// \throw std::runtime_error If any name in the input names is invalid.
94*84d9c625SLionel Sambuc static cli::result_types
parse_types(const std::vector<std::string> & names)95*84d9c625SLionel Sambuc parse_types(const std::vector< std::string >& names)
96*84d9c625SLionel Sambuc {
97*84d9c625SLionel Sambuc     using engine::test_result;
98*84d9c625SLionel Sambuc     typedef std::map< std::string, test_result::result_type > types_map;
99*84d9c625SLionel Sambuc     types_map valid_types;
100*84d9c625SLionel Sambuc     valid_types["broken"] = test_result::broken;
101*84d9c625SLionel Sambuc     valid_types["failed"] = test_result::failed;
102*84d9c625SLionel Sambuc     valid_types["passed"] = test_result::passed;
103*84d9c625SLionel Sambuc     valid_types["skipped"] = test_result::skipped;
104*84d9c625SLionel Sambuc     valid_types["xfail"] = test_result::expected_failure;
105*84d9c625SLionel Sambuc 
106*84d9c625SLionel Sambuc     cli::result_types types;
107*84d9c625SLionel Sambuc     for (std::vector< std::string >::const_iterator iter = names.begin();
108*84d9c625SLionel Sambuc          iter != names.end(); ++iter) {
109*84d9c625SLionel Sambuc         const types_map::const_iterator match = valid_types.find(*iter);
110*84d9c625SLionel Sambuc         if (match == valid_types.end())
111*84d9c625SLionel Sambuc             throw std::runtime_error(F("Unknown result type '%s'") % *iter);
112*84d9c625SLionel Sambuc         else
113*84d9c625SLionel Sambuc             types.push_back((*match).second);
114*84d9c625SLionel Sambuc     }
115*84d9c625SLionel Sambuc     return types;
116*84d9c625SLionel Sambuc }
117*84d9c625SLionel Sambuc 
118*84d9c625SLionel Sambuc 
119*84d9c625SLionel Sambuc }  // anonymous namespace
120*84d9c625SLionel Sambuc 
121*84d9c625SLionel Sambuc 
12211be35a1SLionel Sambuc /// Gets the path to the build root, if any.
12311be35a1SLionel Sambuc ///
12411be35a1SLionel Sambuc /// This is just syntactic sugar to simplify quierying the 'build_root_option'.
12511be35a1SLionel Sambuc ///
12611be35a1SLionel Sambuc /// \param cmdline The parsed command line.
12711be35a1SLionel Sambuc ///
12811be35a1SLionel Sambuc /// \return The path to the build root, if specified; none otherwise.
12911be35a1SLionel Sambuc optional< fs::path >
build_root_path(const cmdline::parsed_cmdline & cmdline)13011be35a1SLionel Sambuc cli::build_root_path(const cmdline::parsed_cmdline& cmdline)
13111be35a1SLionel Sambuc {
13211be35a1SLionel Sambuc     optional< fs::path > build_root;
13311be35a1SLionel Sambuc     if (cmdline.has_option(build_root_option.long_name()))
13411be35a1SLionel Sambuc         build_root = cmdline.get_option< cmdline::path_option >(
13511be35a1SLionel Sambuc             build_root_option.long_name());
13611be35a1SLionel Sambuc     return build_root;
13711be35a1SLionel Sambuc }
13811be35a1SLionel Sambuc 
13911be35a1SLionel Sambuc 
14011be35a1SLionel Sambuc /// Gets the value of the HOME environment variable with path validation.
14111be35a1SLionel Sambuc ///
14211be35a1SLionel Sambuc /// \return The value of the HOME environment variable if it is a valid path;
14311be35a1SLionel Sambuc ///     none if it is not defined or if it contains an invalid path.
14411be35a1SLionel Sambuc optional< fs::path >
get_home(void)14511be35a1SLionel Sambuc cli::get_home(void)
14611be35a1SLionel Sambuc {
14711be35a1SLionel Sambuc     const optional< std::string > home = utils::getenv("HOME");
14811be35a1SLionel Sambuc     if (home) {
14911be35a1SLionel Sambuc         try {
15011be35a1SLionel Sambuc             return utils::make_optional(fs::path(home.get()));
15111be35a1SLionel Sambuc         } catch (const fs::error& e) {
15211be35a1SLionel Sambuc             LW(F("Invalid value '%s' in HOME environment variable: %s") %
15311be35a1SLionel Sambuc                home.get() % e.what());
15411be35a1SLionel Sambuc             return none;
15511be35a1SLionel Sambuc         }
15611be35a1SLionel Sambuc     } else
15711be35a1SLionel Sambuc         return none;
15811be35a1SLionel Sambuc }
15911be35a1SLionel Sambuc 
16011be35a1SLionel Sambuc 
16111be35a1SLionel Sambuc /// Gets the path to the Kyuafile to be loaded.
16211be35a1SLionel Sambuc ///
16311be35a1SLionel Sambuc /// This is just syntactic sugar to simplify quierying the 'kyuafile_option'.
16411be35a1SLionel Sambuc ///
16511be35a1SLionel Sambuc /// \param cmdline The parsed command line.
16611be35a1SLionel Sambuc ///
16711be35a1SLionel Sambuc /// \return The path to the Kyuafile to be loaded.
16811be35a1SLionel Sambuc fs::path
kyuafile_path(const cmdline::parsed_cmdline & cmdline)16911be35a1SLionel Sambuc cli::kyuafile_path(const cmdline::parsed_cmdline& cmdline)
17011be35a1SLionel Sambuc {
17111be35a1SLionel Sambuc     return cmdline.get_option< cmdline::path_option >(
17211be35a1SLionel Sambuc         kyuafile_option.long_name());
17311be35a1SLionel Sambuc }
17411be35a1SLionel Sambuc 
17511be35a1SLionel Sambuc 
176*84d9c625SLionel Sambuc /// Gets the filters for the result types.
177*84d9c625SLionel Sambuc ///
178*84d9c625SLionel Sambuc /// \param cmdline The parsed command line.
179*84d9c625SLionel Sambuc ///
180*84d9c625SLionel Sambuc /// \return A collection of result types to be used for filtering.
181*84d9c625SLionel Sambuc ///
182*84d9c625SLionel Sambuc /// \throw std::runtime_error If any of the user-provided filters is invalid.
183*84d9c625SLionel Sambuc cli::result_types
get_result_types(const utils::cmdline::parsed_cmdline & cmdline)184*84d9c625SLionel Sambuc cli::get_result_types(const utils::cmdline::parsed_cmdline& cmdline)
185*84d9c625SLionel Sambuc {
186*84d9c625SLionel Sambuc     result_types types = parse_types(
187*84d9c625SLionel Sambuc         cmdline.get_option< cmdline::list_option >("results-filter"));
188*84d9c625SLionel Sambuc     if (types.empty()) {
189*84d9c625SLionel Sambuc         types.push_back(engine::test_result::passed);
190*84d9c625SLionel Sambuc         types.push_back(engine::test_result::skipped);
191*84d9c625SLionel Sambuc         types.push_back(engine::test_result::expected_failure);
192*84d9c625SLionel Sambuc         types.push_back(engine::test_result::broken);
193*84d9c625SLionel Sambuc         types.push_back(engine::test_result::failed);
194*84d9c625SLionel Sambuc     }
195*84d9c625SLionel Sambuc     return types;
196*84d9c625SLionel Sambuc }
197*84d9c625SLionel Sambuc 
198*84d9c625SLionel Sambuc 
19911be35a1SLionel Sambuc /// Gets the path to the store to be used.
20011be35a1SLionel Sambuc ///
20111be35a1SLionel Sambuc /// This has the side-effect of creating the directory in which to store the
20211be35a1SLionel Sambuc /// database if and only if the path to the database matches the default value.
20311be35a1SLionel Sambuc /// When the user does not specify an override for the location of the database,
20411be35a1SLionel Sambuc /// he should not care about the directory existing.  Any of this is not a big
20511be35a1SLionel Sambuc /// deal though, because logs are also stored within ~/.kyua and thus we will
20611be35a1SLionel Sambuc /// most likely end up creating the directory anyway.
20711be35a1SLionel Sambuc ///
20811be35a1SLionel Sambuc /// \param cmdline The parsed command line.
20911be35a1SLionel Sambuc ///
21011be35a1SLionel Sambuc /// \return The path to the store to be used.
21111be35a1SLionel Sambuc ///
21211be35a1SLionel Sambuc /// \throw fs::error If the creation of the directory fails.
21311be35a1SLionel Sambuc fs::path
store_path(const cmdline::parsed_cmdline & cmdline)21411be35a1SLionel Sambuc cli::store_path(const cmdline::parsed_cmdline& cmdline)
21511be35a1SLionel Sambuc {
21611be35a1SLionel Sambuc     fs::path store = cmdline.get_option< cmdline::path_option >(
21711be35a1SLionel Sambuc         store_option.long_name());
21811be35a1SLionel Sambuc     if (store == fs::path(store_option.default_value())) {
21911be35a1SLionel Sambuc         const optional< fs::path > home = cli::get_home();
22011be35a1SLionel Sambuc         if (home) {
22111be35a1SLionel Sambuc             store = home.get() / ".kyua/store.db";
22211be35a1SLionel Sambuc             fs::mkdir_p(store.branch_path(), 0777);
22311be35a1SLionel Sambuc         } else {
22411be35a1SLionel Sambuc             store = fs::path("kyua-store.db");
22511be35a1SLionel Sambuc             LW("HOME not defined; creating store database in current "
22611be35a1SLionel Sambuc                "directory");
22711be35a1SLionel Sambuc         }
22811be35a1SLionel Sambuc     }
22911be35a1SLionel Sambuc     LI(F("Store database set to: %s") % store);
23011be35a1SLionel Sambuc     return store;
23111be35a1SLionel Sambuc }
23211be35a1SLionel Sambuc 
23311be35a1SLionel Sambuc 
23411be35a1SLionel Sambuc /// Parses a set of command-line arguments to construct test filters.
23511be35a1SLionel Sambuc ///
23611be35a1SLionel Sambuc /// \param args The command-line arguments representing test filters.
23711be35a1SLionel Sambuc ///
23811be35a1SLionel Sambuc /// \throw cmdline:error If any of the arguments is invalid, or if they
23911be35a1SLionel Sambuc ///     represent a non-disjoint collection of filters.
24011be35a1SLionel Sambuc std::set< engine::test_filter >
parse_filters(const cmdline::args_vector & args)24111be35a1SLionel Sambuc cli::parse_filters(const cmdline::args_vector& args)
24211be35a1SLionel Sambuc {
24311be35a1SLionel Sambuc     std::set< engine::test_filter > filters;
24411be35a1SLionel Sambuc 
24511be35a1SLionel Sambuc     try {
24611be35a1SLionel Sambuc         for (cmdline::args_vector::const_iterator iter = args.begin();
24711be35a1SLionel Sambuc              iter != args.end(); iter++) {
24811be35a1SLionel Sambuc             const engine::test_filter filter(engine::test_filter::parse(*iter));
24911be35a1SLionel Sambuc             if (filters.find(filter) != filters.end())
25011be35a1SLionel Sambuc                 throw cmdline::error(F("Duplicate filter '%s'") % filter.str());
25111be35a1SLionel Sambuc             filters.insert(filter);
25211be35a1SLionel Sambuc         }
25311be35a1SLionel Sambuc         check_disjoint_filters(filters);
25411be35a1SLionel Sambuc     } catch (const std::runtime_error& e) {
25511be35a1SLionel Sambuc         throw cmdline::error(e.what());
25611be35a1SLionel Sambuc     }
25711be35a1SLionel Sambuc 
25811be35a1SLionel Sambuc     return filters;
25911be35a1SLionel Sambuc }
26011be35a1SLionel Sambuc 
26111be35a1SLionel Sambuc 
26211be35a1SLionel Sambuc /// Reports the filters that have not matched any tests as errors.
26311be35a1SLionel Sambuc ///
26411be35a1SLionel Sambuc /// \param unused The collection of unused filters to report.
26511be35a1SLionel Sambuc /// \param ui The user interface object through which errors are to be reported.
26611be35a1SLionel Sambuc ///
26711be35a1SLionel Sambuc /// \return True if there are any unused filters.  The caller should report this
26811be35a1SLionel Sambuc /// as an error to the user by means of a non-successful exit code.
26911be35a1SLionel Sambuc bool
report_unused_filters(const std::set<engine::test_filter> & unused,cmdline::ui * ui)27011be35a1SLionel Sambuc cli::report_unused_filters(const std::set< engine::test_filter >& unused,
27111be35a1SLionel Sambuc                            cmdline::ui* ui)
27211be35a1SLionel Sambuc {
27311be35a1SLionel Sambuc     for (std::set< engine::test_filter >::const_iterator iter = unused.begin();
27411be35a1SLionel Sambuc          iter != unused.end(); iter++) {
27511be35a1SLionel Sambuc         cmdline::print_warning(ui, F("No test cases matched by the filter '%s'")
27611be35a1SLionel Sambuc                                % (*iter).str());
27711be35a1SLionel Sambuc     }
27811be35a1SLionel Sambuc 
27911be35a1SLionel Sambuc     return !unused.empty();
28011be35a1SLionel Sambuc }
28111be35a1SLionel Sambuc 
28211be35a1SLionel Sambuc 
28311be35a1SLionel Sambuc /// Formats a time delta for user presentation.
28411be35a1SLionel Sambuc ///
28511be35a1SLionel Sambuc /// \param delta The time delta to format.
28611be35a1SLionel Sambuc ///
28711be35a1SLionel Sambuc /// \return A user-friendly representation of the time delta.
28811be35a1SLionel Sambuc std::string
format_delta(const datetime::delta & delta)28911be35a1SLionel Sambuc cli::format_delta(const datetime::delta& delta)
29011be35a1SLionel Sambuc {
29111be35a1SLionel Sambuc     return F("%.3ss") % (delta.seconds + (delta.useconds / 1000000.0));
29211be35a1SLionel Sambuc }
29311be35a1SLionel Sambuc 
29411be35a1SLionel Sambuc 
29511be35a1SLionel Sambuc /// Formats a test case result for user presentation.
29611be35a1SLionel Sambuc ///
29711be35a1SLionel Sambuc /// \param result The result to format.
29811be35a1SLionel Sambuc ///
29911be35a1SLionel Sambuc /// \return A user-friendly representation of the result.
30011be35a1SLionel Sambuc std::string
format_result(const engine::test_result & result)30111be35a1SLionel Sambuc cli::format_result(const engine::test_result& result)
30211be35a1SLionel Sambuc {
30311be35a1SLionel Sambuc     std::string text;
30411be35a1SLionel Sambuc 
30511be35a1SLionel Sambuc     using engine::test_result;
30611be35a1SLionel Sambuc     switch (result.type()) {
30711be35a1SLionel Sambuc     case test_result::broken: text = "broken"; break;
30811be35a1SLionel Sambuc     case test_result::expected_failure: text = "expected_failure"; break;
30911be35a1SLionel Sambuc     case test_result::failed: text = "failed"; break;
31011be35a1SLionel Sambuc     case test_result::passed: text = "passed"; break;
31111be35a1SLionel Sambuc     case test_result::skipped: text = "skipped"; break;
31211be35a1SLionel Sambuc     }
31311be35a1SLionel Sambuc     INV(!text.empty());
31411be35a1SLionel Sambuc 
31511be35a1SLionel Sambuc     if (!result.reason().empty())
31611be35a1SLionel Sambuc         text += ": " + result.reason();
31711be35a1SLionel Sambuc 
31811be35a1SLionel Sambuc     return text;
31911be35a1SLionel Sambuc }
32011be35a1SLionel Sambuc 
32111be35a1SLionel Sambuc 
32211be35a1SLionel Sambuc /// Formats the identifier of a test case for user presentation.
32311be35a1SLionel Sambuc ///
32411be35a1SLionel Sambuc /// \param test_case The test case whose identifier to format.
32511be35a1SLionel Sambuc ///
32611be35a1SLionel Sambuc /// \return A string representing the test case uniquely within a test suite.
32711be35a1SLionel Sambuc std::string
format_test_case_id(const engine::test_case & test_case)32811be35a1SLionel Sambuc cli::format_test_case_id(const engine::test_case& test_case)
32911be35a1SLionel Sambuc {
33011be35a1SLionel Sambuc     return F("%s:%s") % test_case.container_test_program().relative_path() %
33111be35a1SLionel Sambuc         test_case.name();
33211be35a1SLionel Sambuc }
33311be35a1SLionel Sambuc 
33411be35a1SLionel Sambuc 
33511be35a1SLionel Sambuc /// Formats a filter using the same syntax of a test case.
33611be35a1SLionel Sambuc ///
33711be35a1SLionel Sambuc /// \param test_filter The filter to format.
33811be35a1SLionel Sambuc ///
33911be35a1SLionel Sambuc /// \return A string representing the test filter.
34011be35a1SLionel Sambuc std::string
format_test_case_id(const engine::test_filter & test_filter)34111be35a1SLionel Sambuc cli::format_test_case_id(const engine::test_filter& test_filter)
34211be35a1SLionel Sambuc {
34311be35a1SLionel Sambuc     return F("%s:%s") % test_filter.test_program % test_filter.test_case;
34411be35a1SLionel Sambuc }
345