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_test.hpp"
30*11be35a1SLionel Sambuc
31*11be35a1SLionel Sambuc #include <cstdlib>
32*11be35a1SLionel Sambuc
33*11be35a1SLionel Sambuc #include "cli/common.ipp"
34*11be35a1SLionel Sambuc #include "engine/drivers/run_tests.hpp"
35*11be35a1SLionel Sambuc #include "engine/test_case.hpp"
36*11be35a1SLionel Sambuc #include "engine/test_result.hpp"
37*11be35a1SLionel Sambuc #include "utils/cmdline/options.hpp"
38*11be35a1SLionel Sambuc #include "utils/cmdline/parser.ipp"
39*11be35a1SLionel Sambuc #include "utils/cmdline/ui.hpp"
40*11be35a1SLionel Sambuc #include "utils/datetime.hpp"
41*11be35a1SLionel Sambuc #include "utils/format/macros.hpp"
42*11be35a1SLionel Sambuc
43*11be35a1SLionel Sambuc namespace cmdline = utils::cmdline;
44*11be35a1SLionel Sambuc namespace config = utils::config;
45*11be35a1SLionel Sambuc namespace datetime = utils::datetime;
46*11be35a1SLionel Sambuc namespace fs = utils::fs;
47*11be35a1SLionel Sambuc namespace run_tests = engine::drivers::run_tests;
48*11be35a1SLionel Sambuc
49*11be35a1SLionel Sambuc using cli::cmd_test;
50*11be35a1SLionel Sambuc
51*11be35a1SLionel Sambuc
52*11be35a1SLionel Sambuc namespace {
53*11be35a1SLionel Sambuc
54*11be35a1SLionel Sambuc
55*11be35a1SLionel Sambuc /// Hooks to print a progress report of the execution of the tests.
56*11be35a1SLionel Sambuc class print_hooks : public run_tests::base_hooks {
57*11be35a1SLionel Sambuc /// Object to interact with the I/O of the program.
58*11be35a1SLionel Sambuc cmdline::ui* _ui;
59*11be35a1SLionel Sambuc
60*11be35a1SLionel Sambuc public:
61*11be35a1SLionel Sambuc /// The amount of positive test results found so far.
62*11be35a1SLionel Sambuc unsigned long good_count;
63*11be35a1SLionel Sambuc
64*11be35a1SLionel Sambuc /// The amount of negative test results found so far.
65*11be35a1SLionel Sambuc unsigned long bad_count;
66*11be35a1SLionel Sambuc
67*11be35a1SLionel Sambuc /// Constructor for the hooks.
68*11be35a1SLionel Sambuc ///
69*11be35a1SLionel Sambuc /// \param ui_ Object to interact with the I/O of the program.
print_hooks(cmdline::ui * ui_)70*11be35a1SLionel Sambuc print_hooks(cmdline::ui* ui_) :
71*11be35a1SLionel Sambuc _ui(ui_),
72*11be35a1SLionel Sambuc good_count(0),
73*11be35a1SLionel Sambuc bad_count(0)
74*11be35a1SLionel Sambuc {
75*11be35a1SLionel Sambuc }
76*11be35a1SLionel Sambuc
77*11be35a1SLionel Sambuc /// Called when the processing of a test case begins.
78*11be35a1SLionel Sambuc ///
79*11be35a1SLionel Sambuc /// \param test_case The test case.
80*11be35a1SLionel Sambuc virtual void
got_test_case(const engine::test_case_ptr & test_case)81*11be35a1SLionel Sambuc got_test_case(const engine::test_case_ptr& test_case)
82*11be35a1SLionel Sambuc {
83*11be35a1SLionel Sambuc _ui->out(F("%s -> ") % cli::format_test_case_id(*test_case), false);
84*11be35a1SLionel Sambuc }
85*11be35a1SLionel Sambuc
86*11be35a1SLionel Sambuc /// Called when a result of a test case becomes available.
87*11be35a1SLionel Sambuc ///
88*11be35a1SLionel Sambuc /// \param unused_test_case The test case.
89*11be35a1SLionel Sambuc /// \param result The result of the execution of the test case.
90*11be35a1SLionel Sambuc /// \param duration The time it took to run the test.
91*11be35a1SLionel Sambuc virtual void
got_result(const engine::test_case_ptr & UTILS_UNUSED_PARAM (test_case),const engine::test_result & result,const datetime::delta & duration)92*11be35a1SLionel Sambuc got_result(const engine::test_case_ptr& UTILS_UNUSED_PARAM(test_case),
93*11be35a1SLionel Sambuc const engine::test_result& result,
94*11be35a1SLionel Sambuc const datetime::delta& duration)
95*11be35a1SLionel Sambuc {
96*11be35a1SLionel Sambuc _ui->out(F("%s [%s]") % cli::format_result(result) %
97*11be35a1SLionel Sambuc cli::format_delta(duration));
98*11be35a1SLionel Sambuc if (result.good())
99*11be35a1SLionel Sambuc good_count++;
100*11be35a1SLionel Sambuc else
101*11be35a1SLionel Sambuc bad_count++;
102*11be35a1SLionel Sambuc }
103*11be35a1SLionel Sambuc };
104*11be35a1SLionel Sambuc
105*11be35a1SLionel Sambuc
106*11be35a1SLionel Sambuc } // anonymous namespace
107*11be35a1SLionel Sambuc
108*11be35a1SLionel Sambuc
109*11be35a1SLionel Sambuc /// Default constructor for cmd_test.
cmd_test(void)110*11be35a1SLionel Sambuc cmd_test::cmd_test(void) : cli_command(
111*11be35a1SLionel Sambuc "test", "[test-program ...]", 0, -1, "Run tests")
112*11be35a1SLionel Sambuc {
113*11be35a1SLionel Sambuc add_option(build_root_option);
114*11be35a1SLionel Sambuc add_option(kyuafile_option);
115*11be35a1SLionel Sambuc add_option(store_option);
116*11be35a1SLionel Sambuc }
117*11be35a1SLionel Sambuc
118*11be35a1SLionel Sambuc
119*11be35a1SLionel Sambuc /// Entry point for the "test" subcommand.
120*11be35a1SLionel Sambuc ///
121*11be35a1SLionel Sambuc /// \param ui Object to interact with the I/O of the program.
122*11be35a1SLionel Sambuc /// \param cmdline Representation of the command line to the subcommand.
123*11be35a1SLionel Sambuc /// \param user_config The runtime configuration of the program.
124*11be35a1SLionel Sambuc ///
125*11be35a1SLionel Sambuc /// \return 0 if all tests passed, 1 otherwise.
126*11be35a1SLionel Sambuc int
run(cmdline::ui * ui,const cmdline::parsed_cmdline & cmdline,const config::tree & user_config)127*11be35a1SLionel Sambuc cmd_test::run(cmdline::ui* ui, const cmdline::parsed_cmdline& cmdline,
128*11be35a1SLionel Sambuc const config::tree& user_config)
129*11be35a1SLionel Sambuc {
130*11be35a1SLionel Sambuc print_hooks hooks(ui);
131*11be35a1SLionel Sambuc const run_tests::result result = run_tests::drive(
132*11be35a1SLionel Sambuc kyuafile_path(cmdline), build_root_path(cmdline), store_path(cmdline),
133*11be35a1SLionel Sambuc parse_filters(cmdline.arguments()), user_config, hooks);
134*11be35a1SLionel Sambuc
135*11be35a1SLionel Sambuc int exit_code;
136*11be35a1SLionel Sambuc if (hooks.good_count > 0 || hooks.bad_count > 0) {
137*11be35a1SLionel Sambuc ui->out("");
138*11be35a1SLionel Sambuc ui->out(F("%s/%s passed (%s failed)") % hooks.good_count %
139*11be35a1SLionel Sambuc (hooks.good_count + hooks.bad_count) % hooks.bad_count);
140*11be35a1SLionel Sambuc
141*11be35a1SLionel Sambuc exit_code = (hooks.bad_count == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
142*11be35a1SLionel Sambuc } else
143*11be35a1SLionel Sambuc exit_code = EXIT_SUCCESS;
144*11be35a1SLionel Sambuc
145*11be35a1SLionel Sambuc ui->out(F("Committed action %s") % result.action_id);
146*11be35a1SLionel Sambuc
147*11be35a1SLionel Sambuc return report_unused_filters(result.unused_filters, ui) ?
148*11be35a1SLionel Sambuc EXIT_FAILURE : exit_code;
149*11be35a1SLionel Sambuc }
150