xref: /minix3/external/bsd/kyua-cli/dist/engine/drivers/debug_test.cpp (revision 11be35a165022172ed3cea20f2b5df0307540b0e)
1*11be35a1SLionel Sambuc // Copyright 2011 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 "engine/drivers/debug_test.hpp"
30*11be35a1SLionel Sambuc 
31*11be35a1SLionel Sambuc #include <stdexcept>
32*11be35a1SLionel Sambuc 
33*11be35a1SLionel Sambuc #include "engine/filters.hpp"
34*11be35a1SLionel Sambuc #include "engine/kyuafile.hpp"
35*11be35a1SLionel Sambuc #include "engine/test_case.hpp"
36*11be35a1SLionel Sambuc #include "engine/test_program.hpp"
37*11be35a1SLionel Sambuc #include "engine/test_result.hpp"
38*11be35a1SLionel Sambuc #include "utils/defs.hpp"
39*11be35a1SLionel Sambuc #include "utils/format/macros.hpp"
40*11be35a1SLionel Sambuc #include "utils/fs/auto_cleaners.hpp"
41*11be35a1SLionel Sambuc #include "utils/optional.ipp"
42*11be35a1SLionel Sambuc #include "utils/signals/interrupts.hpp"
43*11be35a1SLionel Sambuc 
44*11be35a1SLionel Sambuc namespace config = utils::config;
45*11be35a1SLionel Sambuc namespace fs = utils::fs;
46*11be35a1SLionel Sambuc namespace debug_test = engine::drivers::debug_test;
47*11be35a1SLionel Sambuc namespace signals = utils::signals;
48*11be35a1SLionel Sambuc 
49*11be35a1SLionel Sambuc using utils::none;
50*11be35a1SLionel Sambuc using utils::optional;
51*11be35a1SLionel Sambuc 
52*11be35a1SLionel Sambuc 
53*11be35a1SLionel Sambuc namespace {
54*11be35a1SLionel Sambuc 
55*11be35a1SLionel Sambuc 
56*11be35a1SLionel Sambuc /// Looks for a single test case in the Kyuafile.
57*11be35a1SLionel Sambuc ///
58*11be35a1SLionel Sambuc /// \param filter A filter to match the desired test case.
59*11be35a1SLionel Sambuc /// \param kyuafile The test suite in which to look for the test case.
60*11be35a1SLionel Sambuc ///
61*11be35a1SLionel Sambuc /// \return A pointer to the test case if found.
62*11be35a1SLionel Sambuc ///
63*11be35a1SLionel Sambuc /// \throw std::runtime_error If the provided filter matches more than one test
64*11be35a1SLionel Sambuc ///     case or if the test case cannot be found.
65*11be35a1SLionel Sambuc static const engine::test_case_ptr
find_test_case(const engine::test_filter & filter,const engine::kyuafile & kyuafile)66*11be35a1SLionel Sambuc find_test_case(const engine::test_filter& filter,
67*11be35a1SLionel Sambuc                const engine::kyuafile& kyuafile)
68*11be35a1SLionel Sambuc {
69*11be35a1SLionel Sambuc     engine::test_case_ptr found;;
70*11be35a1SLionel Sambuc 
71*11be35a1SLionel Sambuc     for (engine::test_programs_vector::const_iterator p =
72*11be35a1SLionel Sambuc          kyuafile.test_programs().begin(); p != kyuafile.test_programs().end();
73*11be35a1SLionel Sambuc          p++) {
74*11be35a1SLionel Sambuc         const engine::test_program_ptr& test_program = *p;
75*11be35a1SLionel Sambuc 
76*11be35a1SLionel Sambuc         if (!filter.matches_test_program(test_program->relative_path()))
77*11be35a1SLionel Sambuc             continue;
78*11be35a1SLionel Sambuc 
79*11be35a1SLionel Sambuc         const engine::test_cases_vector test_cases = test_program->test_cases();
80*11be35a1SLionel Sambuc 
81*11be35a1SLionel Sambuc         for (engine::test_cases_vector::const_iterator
82*11be35a1SLionel Sambuc              iter = test_cases.begin(); iter != test_cases.end(); iter++) {
83*11be35a1SLionel Sambuc             const engine::test_case_ptr tc = *iter;
84*11be35a1SLionel Sambuc 
85*11be35a1SLionel Sambuc             if (filter.matches_test_case(test_program->relative_path(),
86*11be35a1SLionel Sambuc                                          tc->name())) {
87*11be35a1SLionel Sambuc                 if (found.get() != NULL)
88*11be35a1SLionel Sambuc                     throw std::runtime_error(F("The filter '%s' matches more "
89*11be35a1SLionel Sambuc                                                "than one test case") %
90*11be35a1SLionel Sambuc                                              filter.str());
91*11be35a1SLionel Sambuc                 found = tc;
92*11be35a1SLionel Sambuc             }
93*11be35a1SLionel Sambuc         }
94*11be35a1SLionel Sambuc     }
95*11be35a1SLionel Sambuc 
96*11be35a1SLionel Sambuc     if (found.get() == NULL)
97*11be35a1SLionel Sambuc         throw std::runtime_error(F("Unknown test case '%s'") % filter.str());
98*11be35a1SLionel Sambuc 
99*11be35a1SLionel Sambuc     return found;
100*11be35a1SLionel Sambuc }
101*11be35a1SLionel Sambuc 
102*11be35a1SLionel Sambuc 
103*11be35a1SLionel Sambuc }  // anonymous namespace
104*11be35a1SLionel Sambuc 
105*11be35a1SLionel Sambuc 
106*11be35a1SLionel Sambuc /// Executes the operation.
107*11be35a1SLionel Sambuc ///
108*11be35a1SLionel Sambuc /// \param kyuafile_path The path to the Kyuafile to be loaded.
109*11be35a1SLionel Sambuc /// \param build_root If not none, path to the built test programs.
110*11be35a1SLionel Sambuc /// \param filter The test case filter to locate the test to debug.
111*11be35a1SLionel Sambuc /// \param user_config The end-user configuration properties.
112*11be35a1SLionel Sambuc /// \param stdout_path The name of the file into which to store the test case
113*11be35a1SLionel Sambuc ///     stdout.
114*11be35a1SLionel Sambuc /// \param stderr_path The name of the file into which to store the test case
115*11be35a1SLionel Sambuc ///     stderr.
116*11be35a1SLionel Sambuc ///
117*11be35a1SLionel Sambuc /// \returns A structure with all results computed by this driver.
118*11be35a1SLionel Sambuc debug_test::result
drive(const fs::path & kyuafile_path,const optional<fs::path> build_root,const test_filter & filter,const config::tree & user_config,const fs::path & stdout_path,const fs::path & stderr_path)119*11be35a1SLionel Sambuc debug_test::drive(const fs::path& kyuafile_path,
120*11be35a1SLionel Sambuc                   const optional< fs::path > build_root,
121*11be35a1SLionel Sambuc                   const test_filter& filter,
122*11be35a1SLionel Sambuc                   const config::tree& user_config,
123*11be35a1SLionel Sambuc                   const fs::path& stdout_path,
124*11be35a1SLionel Sambuc                   const fs::path& stderr_path)
125*11be35a1SLionel Sambuc {
126*11be35a1SLionel Sambuc     const engine::kyuafile kyuafile = engine::kyuafile::load(
127*11be35a1SLionel Sambuc         kyuafile_path, build_root);
128*11be35a1SLionel Sambuc     const engine::test_case_ptr test_case = find_test_case(filter, kyuafile);
129*11be35a1SLionel Sambuc     engine::test_case_hooks dummy_hooks;
130*11be35a1SLionel Sambuc 
131*11be35a1SLionel Sambuc     signals::interrupts_handler interrupts;
132*11be35a1SLionel Sambuc 
133*11be35a1SLionel Sambuc     const fs::auto_directory work_directory = fs::auto_directory::mkdtemp(
134*11be35a1SLionel Sambuc         "kyua.XXXXXX");
135*11be35a1SLionel Sambuc 
136*11be35a1SLionel Sambuc     const engine::test_result test_result = debug_test_case(
137*11be35a1SLionel Sambuc         test_case.get(), user_config, dummy_hooks, work_directory.directory(),
138*11be35a1SLionel Sambuc         stdout_path, stderr_path);
139*11be35a1SLionel Sambuc 
140*11be35a1SLionel Sambuc     signals::check_interrupt();
141*11be35a1SLionel Sambuc     return result(test_filter(
142*11be35a1SLionel Sambuc                       test_case->container_test_program().relative_path(),
143*11be35a1SLionel Sambuc                       test_case->name()), test_result);
144*11be35a1SLionel Sambuc }
145