xref: /netbsd-src/external/bsd/kyua-cli/dist/cli/cmd_list.cpp (revision 6b3a42af15b5e090c339512c790dd68f3d11a9d8)
1 // Copyright 2010 Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
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.
16 //
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.
28 
29 #include "cli/cmd_list.hpp"
30 
31 #include <cstdlib>
32 #include <utility>
33 #include <vector>
34 
35 #include "cli/common.ipp"
36 #include "engine/drivers/list_tests.hpp"
37 #include "engine/filters.hpp"
38 #include "engine/test_case.hpp"
39 #include "engine/test_program.hpp"
40 #include "utils/cmdline/options.hpp"
41 #include "utils/cmdline/parser.ipp"
42 #include "utils/cmdline/ui.hpp"
43 #include "utils/defs.hpp"
44 #include "utils/format/macros.hpp"
45 #include "utils/fs/path.hpp"
46 
47 namespace cmdline = utils::cmdline;
48 namespace config = utils::config;
49 namespace fs = utils::fs;
50 namespace list_tests = engine::drivers::list_tests;
51 
52 
53 namespace {
54 
55 
56 /// Hooks for list_tests to print test cases as they come.
57 class progress_hooks : public list_tests::base_hooks {
58     /// The ui object to which to print the test cases.
59     cmdline::ui* _ui;
60 
61     /// Whether to print test case details or just their names.
62     bool _verbose;
63 
64 public:
65     /// Initializes the hooks.
66     ///
67     /// \param ui_ The ui object to which to print the test cases.
68     /// \param verbose_ Whether to print test case details or just their names.
progress_hooks(cmdline::ui * ui_,const bool verbose_)69     progress_hooks(cmdline::ui* ui_, const bool verbose_) :
70         _ui(ui_),
71         _verbose(verbose_)
72     {
73     }
74 
75     /// Reports a test case as soon as it is found.
76     ///
77     /// \param test_case The test case to report.
78     void
got_test_case(const engine::test_case & test_case)79     got_test_case(const engine::test_case& test_case)
80     {
81         cli::detail::list_test_case(_ui, _verbose, test_case);
82     }
83 };
84 
85 
86 }  // anonymous namespace
87 
88 
89 /// Lists a single test case.
90 ///
91 /// \param [out] ui Object to interact with the I/O of the program.
92 /// \param verbose Whether to be verbose or not.
93 /// \param test_case The test case to print.
94 void
list_test_case(cmdline::ui * ui,const bool verbose,const engine::test_case & test_case)95 cli::detail::list_test_case(cmdline::ui* ui, const bool verbose,
96                             const engine::test_case& test_case)
97 {
98     const std::string id = format_test_case_id(test_case);
99     if (!verbose) {
100         ui->out(id);
101     } else {
102         ui->out(F("%s (%s)") % id %
103                 test_case.container_test_program().test_suite_name());
104 
105         // TODO(jmmv): Running these for every test case is probably not the
106         // fastest thing to do.
107         const engine::metadata default_md = engine::metadata_builder().build();
108         const engine::properties_map default_props = default_md.to_properties();
109 
110         const engine::metadata& test_md = test_case.get_metadata();
111         const engine::properties_map test_props = test_md.to_properties();
112 
113         for (engine::properties_map::const_iterator iter = test_props.begin();
114              iter != test_props.end(); iter++) {
115             const engine::properties_map::const_iterator default_iter =
116                 default_props.find((*iter).first);
117             if (default_iter == default_props.end() ||
118                 (*iter).second != (*default_iter).second)
119                 ui->out(F("    %s = %s") % (*iter).first % (*iter).second);
120         }
121     }
122 }
123 
124 
125 /// Default constructor for cmd_list.
cmd_list(void)126 cli::cmd_list::cmd_list(void) :
127     cli_command("list", "[test-program ...]", 0, -1,
128                 "Lists test cases and their meta-data")
129 {
130     add_option(build_root_option);
131     add_option(kyuafile_option);
132     add_option(cmdline::bool_option('v', "verbose", "Show properties"));
133 }
134 
135 
136 /// Entry point for the "list" subcommand.
137 ///
138 /// \param ui Object to interact with the I/O of the program.
139 /// \param cmdline Representation of the command line to the subcommand.
140 /// \param unused_user_config The runtime configuration of the program.
141 ///
142 /// \return 0 to indicate success.
143 int
run(cmdline::ui * ui,const cmdline::parsed_cmdline & cmdline,const config::tree & UTILS_UNUSED_PARAM (user_config))144 cli::cmd_list::run(cmdline::ui* ui, const cmdline::parsed_cmdline& cmdline,
145                    const config::tree& UTILS_UNUSED_PARAM(user_config))
146 {
147     progress_hooks hooks(ui, cmdline.has_option("verbose"));
148     const list_tests::result result = list_tests::drive(
149         kyuafile_path(cmdline), build_root_path(cmdline),
150         parse_filters(cmdline.arguments()), hooks);
151 
152     return report_unused_filters(result.unused_filters, ui) ?
153         EXIT_FAILURE : EXIT_SUCCESS;
154 }
155