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