1 // Copyright 2014 The Kyua Authors.
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 "drivers/report_junit.hpp"
33 #include "model/context.hpp"
34 #include "model/metadata.hpp"
35 #include "model/test_case.hpp"
36 #include "model/test_program.hpp"
37 #include "model/test_result.hpp"
38 #include "model/types.hpp"
39 #include "store/read_transaction.hpp"
40 #include "utils/datetime.hpp"
41 #include "utils/defs.hpp"
42 #include "utils/format/macros.hpp"
43 #include "utils/text/operations.hpp"
45 namespace config = utils::config;
46 namespace datetime = utils::datetime;
47 namespace text = utils::text;
50 /// Converts a test program name into a class-like name.
52 /// \param test_program Test program from which to extract the name.
54 /// \return A class-like representation of the test program's identifier.
56 drivers::junit_classname(const model::test_program& test_program)
58 std::string classname = test_program.relative_path().str();
59 std::replace(classname.begin(), classname.end(), '/', '.');
64 /// Converts a test case's duration to a second-based representation.
66 /// \param delta The duration to convert.
68 /// \return A second-based with millisecond-precision representation of the
71 drivers::junit_duration(const datetime::delta& delta)
73 return F("%.3s") % (delta.seconds + (delta.useconds / 1000000.0));
77 /// String to prepend to the formatted test case metadata.
78 const char* const drivers::junit_metadata_header =
79 "Test case metadata\n"
80 "------------------\n"
84 /// String to prepend to the formatted test case timing details.
85 const char* const drivers::junit_timing_header =
87 "Timing information\n"
88 "------------------\n"
92 /// String to append to the formatted test case metadata.
93 const char* const drivers::junit_stderr_header =
100 /// Formats a test's metadata for recording in stderr.
102 /// \param metadata The metadata to format.
104 /// \return A string with the metadata contents that can be prepended to the
105 /// original test's stderr.
107 drivers::junit_metadata(const model::metadata& metadata)
109 const model::properties_map props = metadata.to_properties();
113 std::ostringstream output;
114 output << junit_metadata_header;
115 for (model::properties_map::const_iterator iter = props.begin();
116 iter != props.end(); ++iter) {
117 if ((*iter).second.empty()) {
118 output << F("%s is empty\n") % (*iter).first;
120 output << F("%s = %s\n") % (*iter).first % (*iter).second;
127 /// Formats a test's timing information for recording in stderr.
129 /// \param start_time The start time of the test.
130 /// \param end_time The end time of the test.
132 /// \return A string with the timing information that can be prepended to the
133 /// original test's stderr.
135 drivers::junit_timing(const datetime::timestamp& start_time,
136 const datetime::timestamp& end_time)
138 std::ostringstream output;
139 output << junit_timing_header;
140 output << F("Start time: %s\n") % start_time.to_iso8601_in_utc();
141 output << F("End time: %s\n") % end_time.to_iso8601_in_utc();
142 output << F("Duration: %ss\n") % junit_duration(end_time - start_time);
147 /// Constructor for the hooks.
149 /// \param [out] output_ Stream to which to write the report.
150 drivers::report_junit_hooks::report_junit_hooks(std::ostream& output_) :
156 /// Callback executed when the context is loaded.
158 /// \param context The context loaded from the database.
160 drivers::report_junit_hooks::got_context(const model::context& context)
162 _output << "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n";
163 _output << "<testsuite>\n";
165 _output << "<properties>\n";
166 _output << F("<property name=\"cwd\" value=\"%s\"/>\n")
167 % text::escape_xml(context.cwd().str());
168 for (model::properties_map::const_iterator iter =
169 context.env().begin(); iter != context.env().end(); ++iter) {
170 _output << F("<property name=\"env.%s\" value=\"%s\"/>\n")
171 % text::escape_xml((*iter).first)
172 % text::escape_xml((*iter).second);
174 _output << "</properties>\n";
178 /// Callback executed when a test results is found.
180 /// \param iter Container for the test result's data.
182 drivers::report_junit_hooks::got_result(store::results_iterator& iter)
184 const model::test_result result = iter.result();
186 _output << F("<testcase classname=\"%s\" name=\"%s\" time=\"%s\">\n")
187 % text::escape_xml(junit_classname(*iter.test_program()))
188 % text::escape_xml(iter.test_case_name())
189 % junit_duration(iter.end_time() - iter.start_time());
191 std::string stderr_contents;
193 switch (result.type()) {
194 case model::test_result_failed:
195 _output << F("<failure message=\"%s\"/>\n")
196 % text::escape_xml(result.reason());
199 case model::test_result_expected_failure:
200 stderr_contents += ("Expected failure result details\n"
201 "-------------------------------\n"
203 + result.reason() + "\n"
207 case model::test_result_passed:
208 // Passed results have no status nodes.
211 case model::test_result_skipped:
212 _output << "<skipped/>\n";
213 stderr_contents += ("Skipped result details\n"
214 "----------------------\n"
216 + result.reason() + "\n"
221 _output << F("<error message=\"%s\"/>\n")
222 % text::escape_xml(result.reason());
225 const std::string stdout_contents = iter.stdout_contents();
226 if (!stdout_contents.empty()) {
227 _output << F("<system-out>%s</system-out>\n")
228 % text::escape_xml(stdout_contents);
232 const model::test_case& test_case = iter.test_program()->find(
233 iter.test_case_name());
234 stderr_contents += junit_metadata(test_case.get_metadata());
236 stderr_contents += junit_timing(iter.start_time(), iter.end_time());
238 stderr_contents += junit_stderr_header;
239 const std::string real_stderr_contents = iter.stderr_contents();
240 if (real_stderr_contents.empty()) {
241 stderr_contents += "<EMPTY>\n";
243 stderr_contents += real_stderr_contents;
246 _output << "<system-err>" << text::escape_xml(stderr_contents)
247 << "</system-err>\n";
249 _output << "</testcase>\n";
253 /// Finalizes the report.
255 drivers::report_junit_hooks::end(const drivers::scan_results::result& /* r */)
257 _output << "</testsuite>\n";