1*b0d29bc4SBrooks Davis // Copyright 2014 The Kyua Authors.
2*b0d29bc4SBrooks Davis // All rights reserved.
3*b0d29bc4SBrooks Davis //
4*b0d29bc4SBrooks Davis // Redistribution and use in source and binary forms, with or without
5*b0d29bc4SBrooks Davis // modification, are permitted provided that the following conditions are
6*b0d29bc4SBrooks Davis // met:
7*b0d29bc4SBrooks Davis //
8*b0d29bc4SBrooks Davis // * Redistributions of source code must retain the above copyright
9*b0d29bc4SBrooks Davis // notice, this list of conditions and the following disclaimer.
10*b0d29bc4SBrooks Davis // * Redistributions in binary form must reproduce the above copyright
11*b0d29bc4SBrooks Davis // notice, this list of conditions and the following disclaimer in the
12*b0d29bc4SBrooks Davis // documentation and/or other materials provided with the distribution.
13*b0d29bc4SBrooks Davis // * Neither the name of Google Inc. nor the names of its contributors
14*b0d29bc4SBrooks Davis // may be used to endorse or promote products derived from this software
15*b0d29bc4SBrooks Davis // without specific prior written permission.
16*b0d29bc4SBrooks Davis //
17*b0d29bc4SBrooks Davis // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*b0d29bc4SBrooks Davis // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*b0d29bc4SBrooks Davis // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*b0d29bc4SBrooks Davis // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*b0d29bc4SBrooks Davis // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*b0d29bc4SBrooks Davis // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*b0d29bc4SBrooks Davis // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*b0d29bc4SBrooks Davis // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*b0d29bc4SBrooks Davis // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*b0d29bc4SBrooks Davis // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*b0d29bc4SBrooks Davis // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*b0d29bc4SBrooks Davis
29*b0d29bc4SBrooks Davis #include "drivers/report_junit.hpp"
30*b0d29bc4SBrooks Davis
31*b0d29bc4SBrooks Davis #include <algorithm>
32*b0d29bc4SBrooks Davis
33*b0d29bc4SBrooks Davis #include "model/context.hpp"
34*b0d29bc4SBrooks Davis #include "model/metadata.hpp"
35*b0d29bc4SBrooks Davis #include "model/test_case.hpp"
36*b0d29bc4SBrooks Davis #include "model/test_program.hpp"
37*b0d29bc4SBrooks Davis #include "model/test_result.hpp"
38*b0d29bc4SBrooks Davis #include "model/types.hpp"
39*b0d29bc4SBrooks Davis #include "store/read_transaction.hpp"
40*b0d29bc4SBrooks Davis #include "utils/datetime.hpp"
41*b0d29bc4SBrooks Davis #include "utils/defs.hpp"
42*b0d29bc4SBrooks Davis #include "utils/format/macros.hpp"
43*b0d29bc4SBrooks Davis #include "utils/text/operations.hpp"
44*b0d29bc4SBrooks Davis
45*b0d29bc4SBrooks Davis namespace config = utils::config;
46*b0d29bc4SBrooks Davis namespace datetime = utils::datetime;
47*b0d29bc4SBrooks Davis namespace text = utils::text;
48*b0d29bc4SBrooks Davis
49*b0d29bc4SBrooks Davis
50*b0d29bc4SBrooks Davis /// Converts a test program name into a class-like name.
51*b0d29bc4SBrooks Davis ///
52*b0d29bc4SBrooks Davis /// \param test_program Test program from which to extract the name.
53*b0d29bc4SBrooks Davis ///
54*b0d29bc4SBrooks Davis /// \return A class-like representation of the test program's identifier.
55*b0d29bc4SBrooks Davis std::string
junit_classname(const model::test_program & test_program)56*b0d29bc4SBrooks Davis drivers::junit_classname(const model::test_program& test_program)
57*b0d29bc4SBrooks Davis {
58*b0d29bc4SBrooks Davis std::string classname = test_program.relative_path().str();
59*b0d29bc4SBrooks Davis std::replace(classname.begin(), classname.end(), '/', '.');
60*b0d29bc4SBrooks Davis return classname;
61*b0d29bc4SBrooks Davis }
62*b0d29bc4SBrooks Davis
63*b0d29bc4SBrooks Davis
64*b0d29bc4SBrooks Davis /// Converts a test case's duration to a second-based representation.
65*b0d29bc4SBrooks Davis ///
66*b0d29bc4SBrooks Davis /// \param delta The duration to convert.
67*b0d29bc4SBrooks Davis ///
68*b0d29bc4SBrooks Davis /// \return A second-based with millisecond-precision representation of the
69*b0d29bc4SBrooks Davis /// input duration.
70*b0d29bc4SBrooks Davis std::string
junit_duration(const datetime::delta & delta)71*b0d29bc4SBrooks Davis drivers::junit_duration(const datetime::delta& delta)
72*b0d29bc4SBrooks Davis {
73*b0d29bc4SBrooks Davis return F("%.3s") % (delta.seconds + (delta.useconds / 1000000.0));
74*b0d29bc4SBrooks Davis }
75*b0d29bc4SBrooks Davis
76*b0d29bc4SBrooks Davis
77*b0d29bc4SBrooks Davis /// String to prepend to the formatted test case metadata.
78*b0d29bc4SBrooks Davis const char* const drivers::junit_metadata_header =
79*b0d29bc4SBrooks Davis "Test case metadata\n"
80*b0d29bc4SBrooks Davis "------------------\n"
81*b0d29bc4SBrooks Davis "\n";
82*b0d29bc4SBrooks Davis
83*b0d29bc4SBrooks Davis
84*b0d29bc4SBrooks Davis /// String to prepend to the formatted test case timing details.
85*b0d29bc4SBrooks Davis const char* const drivers::junit_timing_header =
86*b0d29bc4SBrooks Davis "\n"
87*b0d29bc4SBrooks Davis "Timing information\n"
88*b0d29bc4SBrooks Davis "------------------\n"
89*b0d29bc4SBrooks Davis "\n";
90*b0d29bc4SBrooks Davis
91*b0d29bc4SBrooks Davis
92*b0d29bc4SBrooks Davis /// String to append to the formatted test case metadata.
93*b0d29bc4SBrooks Davis const char* const drivers::junit_stderr_header =
94*b0d29bc4SBrooks Davis "\n"
95*b0d29bc4SBrooks Davis "Original stderr\n"
96*b0d29bc4SBrooks Davis "---------------\n"
97*b0d29bc4SBrooks Davis "\n";
98*b0d29bc4SBrooks Davis
99*b0d29bc4SBrooks Davis
100*b0d29bc4SBrooks Davis /// Formats a test's metadata for recording in stderr.
101*b0d29bc4SBrooks Davis ///
102*b0d29bc4SBrooks Davis /// \param metadata The metadata to format.
103*b0d29bc4SBrooks Davis ///
104*b0d29bc4SBrooks Davis /// \return A string with the metadata contents that can be prepended to the
105*b0d29bc4SBrooks Davis /// original test's stderr.
106*b0d29bc4SBrooks Davis std::string
junit_metadata(const model::metadata & metadata)107*b0d29bc4SBrooks Davis drivers::junit_metadata(const model::metadata& metadata)
108*b0d29bc4SBrooks Davis {
109*b0d29bc4SBrooks Davis const model::properties_map props = metadata.to_properties();
110*b0d29bc4SBrooks Davis if (props.empty())
111*b0d29bc4SBrooks Davis return "";
112*b0d29bc4SBrooks Davis
113*b0d29bc4SBrooks Davis std::ostringstream output;
114*b0d29bc4SBrooks Davis output << junit_metadata_header;
115*b0d29bc4SBrooks Davis for (model::properties_map::const_iterator iter = props.begin();
116*b0d29bc4SBrooks Davis iter != props.end(); ++iter) {
117*b0d29bc4SBrooks Davis if ((*iter).second.empty()) {
118*b0d29bc4SBrooks Davis output << F("%s is empty\n") % (*iter).first;
119*b0d29bc4SBrooks Davis } else {
120*b0d29bc4SBrooks Davis output << F("%s = %s\n") % (*iter).first % (*iter).second;
121*b0d29bc4SBrooks Davis }
122*b0d29bc4SBrooks Davis }
123*b0d29bc4SBrooks Davis return output.str();
124*b0d29bc4SBrooks Davis }
125*b0d29bc4SBrooks Davis
126*b0d29bc4SBrooks Davis
127*b0d29bc4SBrooks Davis /// Formats a test's timing information for recording in stderr.
128*b0d29bc4SBrooks Davis ///
129*b0d29bc4SBrooks Davis /// \param start_time The start time of the test.
130*b0d29bc4SBrooks Davis /// \param end_time The end time of the test.
131*b0d29bc4SBrooks Davis ///
132*b0d29bc4SBrooks Davis /// \return A string with the timing information that can be prepended to the
133*b0d29bc4SBrooks Davis /// original test's stderr.
134*b0d29bc4SBrooks Davis std::string
junit_timing(const datetime::timestamp & start_time,const datetime::timestamp & end_time)135*b0d29bc4SBrooks Davis drivers::junit_timing(const datetime::timestamp& start_time,
136*b0d29bc4SBrooks Davis const datetime::timestamp& end_time)
137*b0d29bc4SBrooks Davis {
138*b0d29bc4SBrooks Davis std::ostringstream output;
139*b0d29bc4SBrooks Davis output << junit_timing_header;
140*b0d29bc4SBrooks Davis output << F("Start time: %s\n") % start_time.to_iso8601_in_utc();
141*b0d29bc4SBrooks Davis output << F("End time: %s\n") % end_time.to_iso8601_in_utc();
142*b0d29bc4SBrooks Davis output << F("Duration: %ss\n") % junit_duration(end_time - start_time);
143*b0d29bc4SBrooks Davis return output.str();
144*b0d29bc4SBrooks Davis }
145*b0d29bc4SBrooks Davis
146*b0d29bc4SBrooks Davis
147*b0d29bc4SBrooks Davis /// Constructor for the hooks.
148*b0d29bc4SBrooks Davis ///
149*b0d29bc4SBrooks Davis /// \param [out] output_ Stream to which to write the report.
report_junit_hooks(std::ostream & output_)150*b0d29bc4SBrooks Davis drivers::report_junit_hooks::report_junit_hooks(std::ostream& output_) :
151*b0d29bc4SBrooks Davis _output(output_)
152*b0d29bc4SBrooks Davis {
153*b0d29bc4SBrooks Davis }
154*b0d29bc4SBrooks Davis
155*b0d29bc4SBrooks Davis
156*b0d29bc4SBrooks Davis /// Callback executed when the context is loaded.
157*b0d29bc4SBrooks Davis ///
158*b0d29bc4SBrooks Davis /// \param context The context loaded from the database.
159*b0d29bc4SBrooks Davis void
got_context(const model::context & context)160*b0d29bc4SBrooks Davis drivers::report_junit_hooks::got_context(const model::context& context)
161*b0d29bc4SBrooks Davis {
162*b0d29bc4SBrooks Davis _output << "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n";
163*b0d29bc4SBrooks Davis _output << "<testsuite>\n";
164*b0d29bc4SBrooks Davis
165*b0d29bc4SBrooks Davis _output << "<properties>\n";
166*b0d29bc4SBrooks Davis _output << F("<property name=\"cwd\" value=\"%s\"/>\n")
167*b0d29bc4SBrooks Davis % text::escape_xml(context.cwd().str());
168*b0d29bc4SBrooks Davis for (model::properties_map::const_iterator iter =
169*b0d29bc4SBrooks Davis context.env().begin(); iter != context.env().end(); ++iter) {
170*b0d29bc4SBrooks Davis _output << F("<property name=\"env.%s\" value=\"%s\"/>\n")
171*b0d29bc4SBrooks Davis % text::escape_xml((*iter).first)
172*b0d29bc4SBrooks Davis % text::escape_xml((*iter).second);
173*b0d29bc4SBrooks Davis }
174*b0d29bc4SBrooks Davis _output << "</properties>\n";
175*b0d29bc4SBrooks Davis }
176*b0d29bc4SBrooks Davis
177*b0d29bc4SBrooks Davis
178*b0d29bc4SBrooks Davis /// Callback executed when a test results is found.
179*b0d29bc4SBrooks Davis ///
180*b0d29bc4SBrooks Davis /// \param iter Container for the test result's data.
181*b0d29bc4SBrooks Davis void
got_result(store::results_iterator & iter)182*b0d29bc4SBrooks Davis drivers::report_junit_hooks::got_result(store::results_iterator& iter)
183*b0d29bc4SBrooks Davis {
184*b0d29bc4SBrooks Davis const model::test_result result = iter.result();
185*b0d29bc4SBrooks Davis
186*b0d29bc4SBrooks Davis _output << F("<testcase classname=\"%s\" name=\"%s\" time=\"%s\">\n")
187*b0d29bc4SBrooks Davis % text::escape_xml(junit_classname(*iter.test_program()))
188*b0d29bc4SBrooks Davis % text::escape_xml(iter.test_case_name())
189*b0d29bc4SBrooks Davis % junit_duration(iter.end_time() - iter.start_time());
190*b0d29bc4SBrooks Davis
191*b0d29bc4SBrooks Davis std::string stderr_contents;
192*b0d29bc4SBrooks Davis
193*b0d29bc4SBrooks Davis switch (result.type()) {
194*b0d29bc4SBrooks Davis case model::test_result_failed:
195*b0d29bc4SBrooks Davis _output << F("<failure message=\"%s\"/>\n")
196*b0d29bc4SBrooks Davis % text::escape_xml(result.reason());
197*b0d29bc4SBrooks Davis break;
198*b0d29bc4SBrooks Davis
199*b0d29bc4SBrooks Davis case model::test_result_expected_failure:
200*b0d29bc4SBrooks Davis stderr_contents += ("Expected failure result details\n"
201*b0d29bc4SBrooks Davis "-------------------------------\n"
202*b0d29bc4SBrooks Davis "\n"
203*b0d29bc4SBrooks Davis + result.reason() + "\n"
204*b0d29bc4SBrooks Davis "\n");
205*b0d29bc4SBrooks Davis break;
206*b0d29bc4SBrooks Davis
207*b0d29bc4SBrooks Davis case model::test_result_passed:
208*b0d29bc4SBrooks Davis // Passed results have no status nodes.
209*b0d29bc4SBrooks Davis break;
210*b0d29bc4SBrooks Davis
211*b0d29bc4SBrooks Davis case model::test_result_skipped:
212*b0d29bc4SBrooks Davis _output << "<skipped/>\n";
213*b0d29bc4SBrooks Davis stderr_contents += ("Skipped result details\n"
214*b0d29bc4SBrooks Davis "----------------------\n"
215*b0d29bc4SBrooks Davis "\n"
216*b0d29bc4SBrooks Davis + result.reason() + "\n"
217*b0d29bc4SBrooks Davis "\n");
218*b0d29bc4SBrooks Davis break;
219*b0d29bc4SBrooks Davis
220*b0d29bc4SBrooks Davis default:
221*b0d29bc4SBrooks Davis _output << F("<error message=\"%s\"/>\n")
222*b0d29bc4SBrooks Davis % text::escape_xml(result.reason());
223*b0d29bc4SBrooks Davis }
224*b0d29bc4SBrooks Davis
225*b0d29bc4SBrooks Davis const std::string stdout_contents = iter.stdout_contents();
226*b0d29bc4SBrooks Davis if (!stdout_contents.empty()) {
227*b0d29bc4SBrooks Davis _output << F("<system-out>%s</system-out>\n")
228*b0d29bc4SBrooks Davis % text::escape_xml(stdout_contents);
229*b0d29bc4SBrooks Davis }
230*b0d29bc4SBrooks Davis
231*b0d29bc4SBrooks Davis {
232*b0d29bc4SBrooks Davis const model::test_case& test_case = iter.test_program()->find(
233*b0d29bc4SBrooks Davis iter.test_case_name());
234*b0d29bc4SBrooks Davis stderr_contents += junit_metadata(test_case.get_metadata());
235*b0d29bc4SBrooks Davis }
236*b0d29bc4SBrooks Davis stderr_contents += junit_timing(iter.start_time(), iter.end_time());
237*b0d29bc4SBrooks Davis {
238*b0d29bc4SBrooks Davis stderr_contents += junit_stderr_header;
239*b0d29bc4SBrooks Davis const std::string real_stderr_contents = iter.stderr_contents();
240*b0d29bc4SBrooks Davis if (real_stderr_contents.empty()) {
241*b0d29bc4SBrooks Davis stderr_contents += "<EMPTY>\n";
242*b0d29bc4SBrooks Davis } else {
243*b0d29bc4SBrooks Davis stderr_contents += real_stderr_contents;
244*b0d29bc4SBrooks Davis }
245*b0d29bc4SBrooks Davis }
246*b0d29bc4SBrooks Davis _output << "<system-err>" << text::escape_xml(stderr_contents)
247*b0d29bc4SBrooks Davis << "</system-err>\n";
248*b0d29bc4SBrooks Davis
249*b0d29bc4SBrooks Davis _output << "</testcase>\n";
250*b0d29bc4SBrooks Davis }
251*b0d29bc4SBrooks Davis
252*b0d29bc4SBrooks Davis
253*b0d29bc4SBrooks Davis /// Finalizes the report.
254*b0d29bc4SBrooks Davis void
end(const drivers::scan_results::result &)255*b0d29bc4SBrooks Davis drivers::report_junit_hooks::end(const drivers::scan_results::result& /* r */)
256*b0d29bc4SBrooks Davis {
257*b0d29bc4SBrooks Davis _output << "</testsuite>\n";
258*b0d29bc4SBrooks Davis }
259