1// Copyright 2014 The Kyua Authors. 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 "drivers/report_junit.hpp" 30 31#include <algorithm> 32 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" 44 45namespace config = utils::config; 46namespace datetime = utils::datetime; 47namespace text = utils::text; 48 49 50/// Converts a test program name into a class-like name. 51/// 52/// \param test_program Test program from which to extract the name. 53/// 54/// \return A class-like representation of the test program's identifier. 55std::string 56drivers::junit_classname(const model::test_program& test_program) 57{ 58 std::string classname = test_program.relative_path().str(); 59 std::replace(classname.begin(), classname.end(), '/', '.'); 60 return classname; 61} 62 63 64/// Converts a test case's duration to a second-based representation. 65/// 66/// \param delta The duration to convert. 67/// 68/// \return A second-based with millisecond-precision representation of the 69/// input duration. 70std::string 71drivers::junit_duration(const datetime::delta& delta) 72{ 73 return F("%.3s") % (delta.seconds + (delta.useconds / 1000000.0)); 74} 75 76 77/// String to prepend to the formatted test case metadata. 78const char* const drivers::junit_metadata_header = 79 "Test case metadata\n" 80 "------------------\n" 81 "\n"; 82 83 84/// String to prepend to the formatted test case timing details. 85const char* const drivers::junit_timing_header = 86 "\n" 87 "Timing information\n" 88 "------------------\n" 89 "\n"; 90 91 92/// String to append to the formatted test case metadata. 93const char* const drivers::junit_stderr_header = 94 "\n" 95 "Original stderr\n" 96 "---------------\n" 97 "\n"; 98 99 100/// Formats a test's metadata for recording in stderr. 101/// 102/// \param metadata The metadata to format. 103/// 104/// \return A string with the metadata contents that can be prepended to the 105/// original test's stderr. 106std::string 107drivers::junit_metadata(const model::metadata& metadata) 108{ 109 const model::properties_map props = metadata.to_properties(); 110 if (props.empty()) 111 return ""; 112 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; 119 } else { 120 output << F("%s = %s\n") % (*iter).first % (*iter).second; 121 } 122 } 123 return output.str(); 124} 125 126 127/// Formats a test's timing information for recording in stderr. 128/// 129/// \param start_time The start time of the test. 130/// \param end_time The end time of the test. 131/// 132/// \return A string with the timing information that can be prepended to the 133/// original test's stderr. 134std::string 135drivers::junit_timing(const datetime::timestamp& start_time, 136 const datetime::timestamp& end_time) 137{ 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); 143 return output.str(); 144} 145 146 147/// Constructor for the hooks. 148/// 149/// \param [out] output_ Stream to which to write the report. 150drivers::report_junit_hooks::report_junit_hooks(std::ostream& output_) : 151 _output(output_) 152{ 153} 154 155 156/// Callback executed when the context is loaded. 157/// 158/// \param context The context loaded from the database. 159void 160drivers::report_junit_hooks::got_context(const model::context& context) 161{ 162 _output << "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n"; 163 _output << "<testsuite>\n"; 164 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); 173 } 174 _output << "</properties>\n"; 175} 176 177 178/// Callback executed when a test results is found. 179/// 180/// \param iter Container for the test result's data. 181void 182drivers::report_junit_hooks::got_result(store::results_iterator& iter) 183{ 184 const model::test_result result = iter.result(); 185 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()); 190 191 std::string stderr_contents; 192 193 switch (result.type()) { 194 case model::test_result_failed: 195 _output << F("<failure message=\"%s\"/>\n") 196 % text::escape_xml(result.reason()); 197 break; 198 199 case model::test_result_expected_failure: 200 stderr_contents += ("Expected failure result details\n" 201 "-------------------------------\n" 202 "\n" 203 + result.reason() + "\n" 204 "\n"); 205 break; 206 207 case model::test_result_passed: 208 // Passed results have no status nodes. 209 break; 210 211 case model::test_result_skipped: 212 _output << "<skipped/>\n"; 213 stderr_contents += ("Skipped result details\n" 214 "----------------------\n" 215 "\n" 216 + result.reason() + "\n" 217 "\n"); 218 break; 219 220 default: 221 _output << F("<error message=\"%s\"/>\n") 222 % text::escape_xml(result.reason()); 223 } 224 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); 229 } 230 231 { 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()); 235 } 236 stderr_contents += junit_timing(iter.start_time(), iter.end_time()); 237 { 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"; 242 } else { 243 stderr_contents += real_stderr_contents; 244 } 245 } 246 _output << "<system-err>" << text::escape_xml(stderr_contents) 247 << "</system-err>\n"; 248 249 _output << "</testcase>\n"; 250} 251 252 253/// Finalizes the report. 254void 255drivers::report_junit_hooks::end(const drivers::scan_results::result& /* r */) 256{ 257 _output << "</testsuite>\n"; 258} 259