Path: blob/main/contrib/kyua/drivers/report_junit.cpp
39478 views
// Copyright 2014 The Kyua Authors.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions are5// met:6//7// * Redistributions of source code must retain the above copyright8// notice, this list of conditions and the following disclaimer.9// * Redistributions in binary form must reproduce the above copyright10// notice, this list of conditions and the following disclaimer in the11// documentation and/or other materials provided with the distribution.12// * Neither the name of Google Inc. nor the names of its contributors13// may be used to endorse or promote products derived from this software14// without specific prior written permission.15//16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2728#include "drivers/report_junit.hpp"2930#include <algorithm>3132#include "model/context.hpp"33#include "model/metadata.hpp"34#include "model/test_case.hpp"35#include "model/test_program.hpp"36#include "model/test_result.hpp"37#include "model/types.hpp"38#include "store/read_transaction.hpp"39#include "utils/datetime.hpp"40#include "utils/defs.hpp"41#include "utils/format/macros.hpp"42#include "utils/text/operations.hpp"4344namespace config = utils::config;45namespace datetime = utils::datetime;46namespace text = utils::text;474849/// Converts a test program name into a class-like name.50///51/// \param test_program Test program from which to extract the name.52///53/// \return A class-like representation of the test program's identifier.54std::string55drivers::junit_classname(const model::test_program& test_program)56{57std::string classname = test_program.relative_path().str();58std::replace(classname.begin(), classname.end(), '/', '.');59return classname;60}616263/// Converts a test case's duration to a second-based representation.64///65/// \param delta The duration to convert.66///67/// \return A second-based with millisecond-precision representation of the68/// input duration.69std::string70drivers::junit_duration(const datetime::delta& delta)71{72return F("%.3s") % (delta.seconds + (delta.useconds / 1000000.0));73}747576/// String to prepend to the formatted test case metadata.77const char* const drivers::junit_metadata_header =78"Test case metadata\n"79"------------------\n"80"\n";818283/// String to prepend to the formatted test case timing details.84const char* const drivers::junit_timing_header =85"\n"86"Timing information\n"87"------------------\n"88"\n";899091/// String to append to the formatted test case metadata.92const char* const drivers::junit_stderr_header =93"\n"94"Original stderr\n"95"---------------\n"96"\n";979899/// Formats a test's metadata for recording in stderr.100///101/// \param metadata The metadata to format.102///103/// \return A string with the metadata contents that can be prepended to the104/// original test's stderr.105std::string106drivers::junit_metadata(const model::metadata& metadata)107{108const model::properties_map props = metadata.to_properties();109if (props.empty())110return "";111112std::ostringstream output;113output << junit_metadata_header;114for (model::properties_map::const_iterator iter = props.begin();115iter != props.end(); ++iter) {116if ((*iter).second.empty()) {117output << F("%s is empty\n") % (*iter).first;118} else {119output << F("%s = %s\n") % (*iter).first % (*iter).second;120}121}122return output.str();123}124125126/// Formats a test's timing information for recording in stderr.127///128/// \param start_time The start time of the test.129/// \param end_time The end time of the test.130///131/// \return A string with the timing information that can be prepended to the132/// original test's stderr.133std::string134drivers::junit_timing(const datetime::timestamp& start_time,135const datetime::timestamp& end_time)136{137std::ostringstream output;138output << junit_timing_header;139output << F("Start time: %s\n") % start_time.to_iso8601_in_utc();140output << F("End time: %s\n") % end_time.to_iso8601_in_utc();141output << F("Duration: %ss\n") % junit_duration(end_time - start_time);142return output.str();143}144145146/// Constructor for the hooks.147///148/// \param [out] output_ Stream to which to write the report.149drivers::report_junit_hooks::report_junit_hooks(std::ostream& output_) :150_output(output_)151{152}153154155/// Callback executed when the context is loaded.156///157/// \param context The context loaded from the database.158void159drivers::report_junit_hooks::got_context(const model::context& context)160{161_output << "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n";162_output << "<testsuite>\n";163164_output << "<properties>\n";165_output << F("<property name=\"cwd\" value=\"%s\"/>\n")166% text::escape_xml(context.cwd().str());167for (model::properties_map::const_iterator iter =168context.env().begin(); iter != context.env().end(); ++iter) {169_output << F("<property name=\"env.%s\" value=\"%s\"/>\n")170% text::escape_xml((*iter).first)171% text::escape_xml((*iter).second);172}173_output << "</properties>\n";174}175176177/// Callback executed when a test results is found.178///179/// \param iter Container for the test result's data.180void181drivers::report_junit_hooks::got_result(store::results_iterator& iter)182{183const model::test_result result = iter.result();184185_output << F("<testcase classname=\"%s\" name=\"%s\" time=\"%s\">\n")186% text::escape_xml(junit_classname(*iter.test_program()))187% text::escape_xml(iter.test_case_name())188% junit_duration(iter.end_time() - iter.start_time());189190std::string stderr_contents;191192switch (result.type()) {193case model::test_result_failed:194_output << F("<failure message=\"%s\"/>\n")195% text::escape_xml(result.reason());196break;197198case model::test_result_expected_failure:199stderr_contents += ("Expected failure result details\n"200"-------------------------------\n"201"\n"202+ result.reason() + "\n"203"\n");204break;205206case model::test_result_passed:207// Passed results have no status nodes.208break;209210case model::test_result_skipped:211_output << "<skipped/>\n";212stderr_contents += ("Skipped result details\n"213"----------------------\n"214"\n"215+ result.reason() + "\n"216"\n");217break;218219default:220_output << F("<error message=\"%s\"/>\n")221% text::escape_xml(result.reason());222}223224const std::string stdout_contents = iter.stdout_contents();225if (!stdout_contents.empty()) {226_output << F("<system-out>%s</system-out>\n")227% text::escape_xml(stdout_contents);228}229230{231const model::test_case& test_case = iter.test_program()->find(232iter.test_case_name());233stderr_contents += junit_metadata(test_case.get_metadata());234}235stderr_contents += junit_timing(iter.start_time(), iter.end_time());236{237stderr_contents += junit_stderr_header;238const std::string real_stderr_contents = iter.stderr_contents();239if (real_stderr_contents.empty()) {240stderr_contents += "<EMPTY>\n";241} else {242stderr_contents += real_stderr_contents;243}244}245_output << "<system-err>" << text::escape_xml(stderr_contents)246<< "</system-err>\n";247248_output << "</testcase>\n";249}250251252/// Finalizes the report.253void254drivers::report_junit_hooks::end(const drivers::scan_results::result& /* r */)255{256_output << "</testsuite>\n";257}258259260