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_test.hpp" 30 31 #include <cstdlib> 32 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" 42 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; 48 49 using cli::cmd_test; 50 51 52 namespace { 53 54 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. 58 cmdline::ui* _ui; 59 60 public: 61 /// The amount of positive test results found so far. 62 unsigned long good_count; 63 64 /// The amount of negative test results found so far. 65 unsigned long bad_count; 66 67 /// Constructor for the hooks. 68 /// 69 /// \param ui_ Object to interact with the I/O of the program. 70 print_hooks(cmdline::ui* ui_) : 71 _ui(ui_), 72 good_count(0), 73 bad_count(0) 74 { 75 } 76 77 /// Called when the processing of a test case begins. 78 /// 79 /// \param test_case The test case. 80 virtual void 81 got_test_case(const engine::test_case_ptr& test_case) 82 { 83 _ui->out(F("%s -> ") % cli::format_test_case_id(*test_case), false); 84 } 85 86 /// Called when a result of a test case becomes available. 87 /// 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. 91 virtual void 92 got_result(const engine::test_case_ptr& UTILS_UNUSED_PARAM(test_case), 93 const engine::test_result& result, 94 const datetime::delta& duration) 95 { 96 _ui->out(F("%s [%s]") % cli::format_result(result) % 97 cli::format_delta(duration)); 98 if (result.good()) 99 good_count++; 100 else 101 bad_count++; 102 } 103 }; 104 105 106 } // anonymous namespace 107 108 109 /// Default constructor for cmd_test. 110 cmd_test::cmd_test(void) : cli_command( 111 "test", "[test-program ...]", 0, -1, "Run tests") 112 { 113 add_option(build_root_option); 114 add_option(kyuafile_option); 115 add_option(store_option); 116 } 117 118 119 /// Entry point for the "test" subcommand. 120 /// 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. 124 /// 125 /// \return 0 if all tests passed, 1 otherwise. 126 int 127 cmd_test::run(cmdline::ui* ui, const cmdline::parsed_cmdline& cmdline, 128 const config::tree& user_config) 129 { 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); 134 135 int exit_code; 136 if (hooks.good_count > 0 || hooks.bad_count > 0) { 137 ui->out(""); 138 ui->out(F("%s/%s passed (%s failed)") % hooks.good_count % 139 (hooks.good_count + hooks.bad_count) % hooks.bad_count); 140 141 exit_code = (hooks.bad_count == 0 ? EXIT_SUCCESS : EXIT_FAILURE); 142 } else 143 exit_code = EXIT_SUCCESS; 144 145 ui->out(F("Committed action %s") % result.action_id); 146 147 return report_unused_filters(result.unused_filters, ui) ? 148 EXIT_FAILURE : exit_code; 149 } 150