xref: /minix3/external/bsd/kyua-cli/dist/cli/cmd_list.cpp (revision 11be35a165022172ed3cea20f2b5df0307540b0e)
1*11be35a1SLionel Sambuc // Copyright 2010 Google Inc.
2*11be35a1SLionel Sambuc // All rights reserved.
3*11be35a1SLionel Sambuc //
4*11be35a1SLionel Sambuc // Redistribution and use in source and binary forms, with or without
5*11be35a1SLionel Sambuc // modification, are permitted provided that the following conditions are
6*11be35a1SLionel Sambuc // met:
7*11be35a1SLionel Sambuc //
8*11be35a1SLionel Sambuc // * Redistributions of source code must retain the above copyright
9*11be35a1SLionel Sambuc //   notice, this list of conditions and the following disclaimer.
10*11be35a1SLionel Sambuc // * Redistributions in binary form must reproduce the above copyright
11*11be35a1SLionel Sambuc //   notice, this list of conditions and the following disclaimer in the
12*11be35a1SLionel Sambuc //   documentation and/or other materials provided with the distribution.
13*11be35a1SLionel Sambuc // * Neither the name of Google Inc. nor the names of its contributors
14*11be35a1SLionel Sambuc //   may be used to endorse or promote products derived from this software
15*11be35a1SLionel Sambuc //   without specific prior written permission.
16*11be35a1SLionel Sambuc //
17*11be35a1SLionel Sambuc // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*11be35a1SLionel Sambuc // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*11be35a1SLionel Sambuc // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*11be35a1SLionel Sambuc // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*11be35a1SLionel Sambuc // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*11be35a1SLionel Sambuc // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*11be35a1SLionel Sambuc // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*11be35a1SLionel Sambuc // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*11be35a1SLionel Sambuc // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*11be35a1SLionel Sambuc // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*11be35a1SLionel Sambuc // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*11be35a1SLionel Sambuc 
29*11be35a1SLionel Sambuc #include "cli/cmd_list.hpp"
30*11be35a1SLionel Sambuc 
31*11be35a1SLionel Sambuc #include <cstdlib>
32*11be35a1SLionel Sambuc #include <utility>
33*11be35a1SLionel Sambuc #include <vector>
34*11be35a1SLionel Sambuc 
35*11be35a1SLionel Sambuc #include "cli/common.ipp"
36*11be35a1SLionel Sambuc #include "engine/drivers/list_tests.hpp"
37*11be35a1SLionel Sambuc #include "engine/filters.hpp"
38*11be35a1SLionel Sambuc #include "engine/test_case.hpp"
39*11be35a1SLionel Sambuc #include "engine/test_program.hpp"
40*11be35a1SLionel Sambuc #include "utils/cmdline/options.hpp"
41*11be35a1SLionel Sambuc #include "utils/cmdline/parser.ipp"
42*11be35a1SLionel Sambuc #include "utils/cmdline/ui.hpp"
43*11be35a1SLionel Sambuc #include "utils/defs.hpp"
44*11be35a1SLionel Sambuc #include "utils/format/macros.hpp"
45*11be35a1SLionel Sambuc #include "utils/fs/path.hpp"
46*11be35a1SLionel Sambuc 
47*11be35a1SLionel Sambuc namespace cmdline = utils::cmdline;
48*11be35a1SLionel Sambuc namespace config = utils::config;
49*11be35a1SLionel Sambuc namespace fs = utils::fs;
50*11be35a1SLionel Sambuc namespace list_tests = engine::drivers::list_tests;
51*11be35a1SLionel Sambuc 
52*11be35a1SLionel Sambuc 
53*11be35a1SLionel Sambuc namespace {
54*11be35a1SLionel Sambuc 
55*11be35a1SLionel Sambuc 
56*11be35a1SLionel Sambuc /// Hooks for list_tests to print test cases as they come.
57*11be35a1SLionel Sambuc class progress_hooks : public list_tests::base_hooks {
58*11be35a1SLionel Sambuc     /// The ui object to which to print the test cases.
59*11be35a1SLionel Sambuc     cmdline::ui* _ui;
60*11be35a1SLionel Sambuc 
61*11be35a1SLionel Sambuc     /// Whether to print test case details or just their names.
62*11be35a1SLionel Sambuc     bool _verbose;
63*11be35a1SLionel Sambuc 
64*11be35a1SLionel Sambuc public:
65*11be35a1SLionel Sambuc     /// Initializes the hooks.
66*11be35a1SLionel Sambuc     ///
67*11be35a1SLionel Sambuc     /// \param ui_ The ui object to which to print the test cases.
68*11be35a1SLionel Sambuc     /// \param verbose_ Whether to print test case details or just their names.
progress_hooks(cmdline::ui * ui_,const bool verbose_)69*11be35a1SLionel Sambuc     progress_hooks(cmdline::ui* ui_, const bool verbose_) :
70*11be35a1SLionel Sambuc         _ui(ui_),
71*11be35a1SLionel Sambuc         _verbose(verbose_)
72*11be35a1SLionel Sambuc     {
73*11be35a1SLionel Sambuc     }
74*11be35a1SLionel Sambuc 
75*11be35a1SLionel Sambuc     /// Reports a test case as soon as it is found.
76*11be35a1SLionel Sambuc     ///
77*11be35a1SLionel Sambuc     /// \param test_case The test case to report.
78*11be35a1SLionel Sambuc     void
got_test_case(const engine::test_case & test_case)79*11be35a1SLionel Sambuc     got_test_case(const engine::test_case& test_case)
80*11be35a1SLionel Sambuc     {
81*11be35a1SLionel Sambuc         cli::detail::list_test_case(_ui, _verbose, test_case);
82*11be35a1SLionel Sambuc     }
83*11be35a1SLionel Sambuc };
84*11be35a1SLionel Sambuc 
85*11be35a1SLionel Sambuc 
86*11be35a1SLionel Sambuc }  // anonymous namespace
87*11be35a1SLionel Sambuc 
88*11be35a1SLionel Sambuc 
89*11be35a1SLionel Sambuc /// Lists a single test case.
90*11be35a1SLionel Sambuc ///
91*11be35a1SLionel Sambuc /// \param [out] ui Object to interact with the I/O of the program.
92*11be35a1SLionel Sambuc /// \param verbose Whether to be verbose or not.
93*11be35a1SLionel Sambuc /// \param test_case The test case to print.
94*11be35a1SLionel Sambuc void
list_test_case(cmdline::ui * ui,const bool verbose,const engine::test_case & test_case)95*11be35a1SLionel Sambuc cli::detail::list_test_case(cmdline::ui* ui, const bool verbose,
96*11be35a1SLionel Sambuc                             const engine::test_case& test_case)
97*11be35a1SLionel Sambuc {
98*11be35a1SLionel Sambuc     const std::string id = format_test_case_id(test_case);
99*11be35a1SLionel Sambuc     if (!verbose) {
100*11be35a1SLionel Sambuc         ui->out(id);
101*11be35a1SLionel Sambuc     } else {
102*11be35a1SLionel Sambuc         ui->out(F("%s (%s)") % id %
103*11be35a1SLionel Sambuc                 test_case.container_test_program().test_suite_name());
104*11be35a1SLionel Sambuc 
105*11be35a1SLionel Sambuc         // TODO(jmmv): Running these for every test case is probably not the
106*11be35a1SLionel Sambuc         // fastest thing to do.
107*11be35a1SLionel Sambuc         const engine::metadata default_md = engine::metadata_builder().build();
108*11be35a1SLionel Sambuc         const engine::properties_map default_props = default_md.to_properties();
109*11be35a1SLionel Sambuc 
110*11be35a1SLionel Sambuc         const engine::metadata& test_md = test_case.get_metadata();
111*11be35a1SLionel Sambuc         const engine::properties_map test_props = test_md.to_properties();
112*11be35a1SLionel Sambuc 
113*11be35a1SLionel Sambuc         for (engine::properties_map::const_iterator iter = test_props.begin();
114*11be35a1SLionel Sambuc              iter != test_props.end(); iter++) {
115*11be35a1SLionel Sambuc             const engine::properties_map::const_iterator default_iter =
116*11be35a1SLionel Sambuc                 default_props.find((*iter).first);
117*11be35a1SLionel Sambuc             if (default_iter == default_props.end() ||
118*11be35a1SLionel Sambuc                 (*iter).second != (*default_iter).second)
119*11be35a1SLionel Sambuc                 ui->out(F("    %s = %s") % (*iter).first % (*iter).second);
120*11be35a1SLionel Sambuc         }
121*11be35a1SLionel Sambuc     }
122*11be35a1SLionel Sambuc }
123*11be35a1SLionel Sambuc 
124*11be35a1SLionel Sambuc 
125*11be35a1SLionel Sambuc /// Default constructor for cmd_list.
cmd_list(void)126*11be35a1SLionel Sambuc cli::cmd_list::cmd_list(void) :
127*11be35a1SLionel Sambuc     cli_command("list", "[test-program ...]", 0, -1,
128*11be35a1SLionel Sambuc                 "Lists test cases and their meta-data")
129*11be35a1SLionel Sambuc {
130*11be35a1SLionel Sambuc     add_option(build_root_option);
131*11be35a1SLionel Sambuc     add_option(kyuafile_option);
132*11be35a1SLionel Sambuc     add_option(cmdline::bool_option('v', "verbose", "Show properties"));
133*11be35a1SLionel Sambuc }
134*11be35a1SLionel Sambuc 
135*11be35a1SLionel Sambuc 
136*11be35a1SLionel Sambuc /// Entry point for the "list" subcommand.
137*11be35a1SLionel Sambuc ///
138*11be35a1SLionel Sambuc /// \param ui Object to interact with the I/O of the program.
139*11be35a1SLionel Sambuc /// \param cmdline Representation of the command line to the subcommand.
140*11be35a1SLionel Sambuc /// \param unused_user_config The runtime configuration of the program.
141*11be35a1SLionel Sambuc ///
142*11be35a1SLionel Sambuc /// \return 0 to indicate success.
143*11be35a1SLionel Sambuc int
run(cmdline::ui * ui,const cmdline::parsed_cmdline & cmdline,const config::tree & UTILS_UNUSED_PARAM (user_config))144*11be35a1SLionel Sambuc cli::cmd_list::run(cmdline::ui* ui, const cmdline::parsed_cmdline& cmdline,
145*11be35a1SLionel Sambuc                    const config::tree& UTILS_UNUSED_PARAM(user_config))
146*11be35a1SLionel Sambuc {
147*11be35a1SLionel Sambuc     progress_hooks hooks(ui, cmdline.has_option("verbose"));
148*11be35a1SLionel Sambuc     const list_tests::result result = list_tests::drive(
149*11be35a1SLionel Sambuc         kyuafile_path(cmdline), build_root_path(cmdline),
150*11be35a1SLionel Sambuc         parse_filters(cmdline.arguments()), hooks);
151*11be35a1SLionel Sambuc 
152*11be35a1SLionel Sambuc     return report_unused_filters(result.unused_filters, ui) ?
153*11be35a1SLionel Sambuc         EXIT_FAILURE : EXIT_SUCCESS;
154*11be35a1SLionel Sambuc }
155