1 // Copyright 2010 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/cmd_test.hpp"
33 #include "cli/common.ipp"
34 #include "engine/drivers/run_tests.hpp"
35 #include "engine/test_case.hpp"
36 #include "engine/test_result.hpp"
37 #include "utils/cmdline/options.hpp"
38 #include "utils/cmdline/parser.ipp"
39 #include "utils/cmdline/ui.hpp"
40 #include "utils/datetime.hpp"
41 #include "utils/format/macros.hpp"
43 namespace cmdline
= utils::cmdline
;
44 namespace config
= utils::config
;
45 namespace datetime
= utils::datetime
;
46 namespace fs
= utils::fs
;
47 namespace run_tests
= engine::drivers::run_tests
;
55 /// Hooks to print a progress report of the execution of the tests.
56 class print_hooks
: public run_tests::base_hooks
{
57 /// Object to interact with the I/O of the program.
61 /// The amount of positive test results found so far.
62 unsigned long good_count
;
64 /// The amount of negative test results found so far.
65 unsigned long bad_count
;
67 /// Constructor for the hooks.
69 /// \param ui_ Object to interact with the I/O of the program.
70 print_hooks(cmdline::ui
* ui_
) :
77 /// Called when the processing of a test case begins.
79 /// \param test_case The test case.
81 got_test_case(const engine::test_case_ptr
& test_case
)
83 _ui
->out(F("%s -> ") % cli::format_test_case_id(*test_case
), false);
86 /// Called when a result of a test case becomes available.
88 /// \param unused_test_case The test case.
89 /// \param result The result of the execution of the test case.
90 /// \param duration The time it took to run the test.
92 got_result(const engine::test_case_ptr
& UTILS_UNUSED_PARAM(test_case
),
93 const engine::test_result
& result
,
94 const datetime::delta
& duration
)
96 _ui
->out(F("%s [%s]") % cli::format_result(result
) %
97 cli::format_delta(duration
));
106 } // anonymous namespace
109 /// Default constructor for cmd_test.
110 cmd_test::cmd_test(void) : cli_command(
111 "test", "[test-program ...]", 0, -1, "Run tests")
113 add_option(build_root_option
);
114 add_option(kyuafile_option
);
115 add_option(store_option
);
119 /// Entry point for the "test" subcommand.
121 /// \param ui Object to interact with the I/O of the program.
122 /// \param cmdline Representation of the command line to the subcommand.
123 /// \param user_config The runtime configuration of the program.
125 /// \return 0 if all tests passed, 1 otherwise.
127 cmd_test::run(cmdline::ui
* ui
, const cmdline::parsed_cmdline
& cmdline
,
128 const config::tree
& user_config
)
130 print_hooks
hooks(ui
);
131 const run_tests::result result
= run_tests::drive(
132 kyuafile_path(cmdline
), build_root_path(cmdline
), store_path(cmdline
),
133 parse_filters(cmdline
.arguments()), user_config
, hooks
);
136 if (hooks
.good_count
> 0 || hooks
.bad_count
> 0) {
138 ui
->out(F("%s/%s passed (%s failed)") % hooks
.good_count
%
139 (hooks
.good_count
+ hooks
.bad_count
) % hooks
.bad_count
);
141 exit_code
= (hooks
.bad_count
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
);
143 exit_code
= EXIT_SUCCESS
;
145 ui
->out(F("Committed action %s") % result
.action_id
);
147 return report_unused_filters(result
.unused_filters
, ui
) ?
148 EXIT_FAILURE
: exit_code
;