2 // Automated Testing Framework (atf)
4 // Copyright (c) 2007 The NetBSD Foundation, Inc.
5 // All rights reserved.
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
10 // 1. Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 // 2. Redistributions in binary form must reproduce the above copyright
13 // notice, this list of conditions and the following disclaimer in the
14 // documentation and/or other materials provided with the distribution.
16 // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17 // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43 #include "atf-c/defs.h"
45 #include "atf-c++/detail/application.hpp"
46 #include "atf-c++/detail/fs.hpp"
47 #include "atf-c++/detail/sanity.hpp"
48 #include "atf-c++/detail/text.hpp"
49 #include "atf-c++/detail/ui.hpp"
53 typedef std::auto_ptr< std::ostream > ostream_ptr;
56 open_outfile(const atf::fs::path& path)
59 if (path.str() == "-")
60 osp = ostream_ptr(new std::ofstream("/dev/stdout"));
62 osp = ostream_ptr(new std::ofstream(path.c_str()));
64 throw std::runtime_error("Could not create file " + path.str());
69 format_tv(struct timeval* tv)
71 std::ostringstream output;
72 output << static_cast< long >(tv->tv_sec) << '.'
73 << std::setfill('0') << std::setw(6)
74 << static_cast< long >(tv->tv_usec);
78 // ------------------------------------------------------------------------
79 // The "writer" interface.
80 // ------------------------------------------------------------------------
83 //! \brief A base class that defines an output format.
85 //! The writer base class defines a generic interface to output formats.
86 //! This is meant to be subclassed, and each subclass can redefine any
87 //! method to format the information as it wishes.
89 //! This class is not tied to a output stream nor a file because, depending
90 //! on the output format, we will want to write to a single file or to
96 virtual ~writer(void) {}
98 virtual void write_info(const std::string&, const std::string&) {}
99 virtual void write_ntps(size_t) {}
100 virtual void write_tp_start(const std::string&, size_t) {}
101 virtual void write_tp_end(struct timeval*, const std::string&) {}
102 virtual void write_tc_start(const std::string&) {}
103 virtual void write_tc_stdout_line(const std::string&) {}
104 virtual void write_tc_stderr_line(const std::string&) {}
105 virtual void write_tc_end(const std::string&, struct timeval*,
106 const std::string&) {}
107 virtual void write_eof(void) {}
110 // ------------------------------------------------------------------------
111 // The "csv_writer" class.
112 // ------------------------------------------------------------------------
115 //! \brief A very simple plain-text output format.
117 //! The csv_writer class implements a very simple plain-text output
118 //! format that summarizes the results of each executed test case. The
119 //! results are meant to be easily parseable by third-party tools, hence
120 //! they are formatted as a CSV file.
122 class csv_writer : public writer {
126 std::string m_tpname;
127 std::string m_tcname;
130 csv_writer(const atf::fs::path& p) :
131 m_os(open_outfile(p))
137 write_tp_start(const std::string& name,
138 size_t ntcs ATF_DEFS_ATTRIBUTE_UNUSED)
146 write_tp_end(struct timeval* tv, const std::string& reason)
148 const std::string timestamp = format_tv(tv);
151 (*m_os) << "tp, " << timestamp << ", " << m_tpname << ", bogus, "
154 (*m_os) << "tp, " << timestamp << ", "<< m_tpname << ", failed\n";
156 (*m_os) << "tp, " << timestamp << ", "<< m_tpname << ", passed\n";
161 write_tc_start(const std::string& name)
168 write_tc_end(const std::string& state, struct timeval* tv,
169 const std::string& reason)
171 std::string str = m_tpname + ", " + m_tcname + ", " + state;
173 str += ", " + reason;
174 (*m_os) << "tc, " << format_tv(tv) << ", " << str << "\n";
176 if (state == "failed")
181 // ------------------------------------------------------------------------
182 // The "ticker_writer" class.
183 // ------------------------------------------------------------------------
186 //! \brief A console-friendly output format.
188 //! The ticker_writer class implements a formatter that is user-friendly
189 //! in the sense that it shows the execution of test cases in an easy to
190 //! read format. It is not meant to be parseable and its format can
191 //! freely change across releases.
193 class ticker_writer : public writer {
196 size_t m_curtp, m_ntps;
197 size_t m_tcs_passed, m_tcs_failed, m_tcs_skipped, m_tcs_expected_failures;
198 std::string m_tcname, m_tpname;
199 std::vector< std::string > m_failed_tcs;
200 std::map< std::string, std::string > m_expected_failures_tcs;
201 std::vector< std::string > m_failed_tps;
204 write_info(const std::string& what, const std::string& val)
206 if (what == "tests.root") {
207 (*m_os) << "Tests root: " << val << "\n\n";
212 write_ntps(size_t ntps)
218 m_tcs_expected_failures = 0;
223 write_tp_start(const std::string& tp, size_t ntcs)
225 using atf::text::to_string;
226 using atf::ui::format_text;
230 (*m_os) << format_text(tp + " (" + to_string(m_curtp) +
231 "/" + to_string(m_ntps) + "): " +
232 to_string(ntcs) + " test cases")
238 write_tp_end(struct timeval* tv, const std::string& reason)
240 using atf::ui::format_text_with_tag;
244 if (!reason.empty()) {
245 (*m_os) << format_text_with_tag("BOGUS TEST PROGRAM: Cannot "
246 "trust its results because "
247 "of `" + reason + "'",
248 m_tpname + ": ", false)
250 m_failed_tps.push_back(m_tpname);
252 (*m_os) << "[" << format_tv(tv) << "s]\n\n";
259 write_tc_start(const std::string& tcname)
263 (*m_os) << " " + tcname + ": ";
268 write_tc_end(const std::string& state, struct timeval* tv,
269 const std::string& reason)
273 (*m_os) << "[" << format_tv(tv) << "s] ";
275 if (state == "expected_death" || state == "expected_exit" ||
276 state == "expected_failure" || state == "expected_signal" ||
277 state == "expected_timeout") {
278 str = "Expected failure: " + reason;
279 m_tcs_expected_failures++;
280 m_expected_failures_tcs[m_tpname + ":" + m_tcname] = reason;
281 } else if (state == "failed") {
282 str = "Failed: " + reason;
284 m_failed_tcs.push_back(m_tpname + ":" + m_tcname);
285 } else if (state == "passed") {
288 } else if (state == "skipped") {
289 str = "Skipped: " + reason;
294 // XXX Wrap text. format_text_with_tag does not currently allow
295 // to specify the current column, which is needed because we have
296 // already printed the tc's name.
297 (*m_os) << str << '\n';
303 write_expected_failures(const std::map< std::string, std::string >& xfails,
306 using atf::ui::format_text;
307 using atf::ui::format_text_with_tag;
309 os << format_text("Test cases for known bugs:") << "\n";
311 for (std::map< std::string, std::string >::const_iterator iter =
312 xfails.begin(); iter != xfails.end(); iter++) {
313 const std::string& name = (*iter).first;
314 const std::string& reason = (*iter).second;
316 os << format_text_with_tag(reason, " " + name + ": ", false)
324 using atf::text::join;
325 using atf::text::to_string;
326 using atf::ui::format_text;
327 using atf::ui::format_text_with_tag;
329 if (!m_failed_tps.empty()) {
330 (*m_os) << format_text("Failed (bogus) test programs:")
332 (*m_os) << format_text_with_tag(join(m_failed_tps, ", "),
333 " ", false) << "\n\n";
336 if (!m_expected_failures_tcs.empty()) {
337 write_expected_failures(m_expected_failures_tcs, *m_os);
341 if (!m_failed_tcs.empty()) {
342 (*m_os) << format_text("Failed test cases:") << "\n";
343 (*m_os) << format_text_with_tag(join(m_failed_tcs, ", "),
344 " ", false) << "\n\n";
347 (*m_os) << format_text("Summary for " + to_string(m_ntps) +
348 " test programs:") << "\n";
349 (*m_os) << format_text_with_tag(to_string(m_tcs_passed) +
350 " passed test cases.",
352 (*m_os) << format_text_with_tag(to_string(m_tcs_failed) +
353 " failed test cases.",
355 (*m_os) << format_text_with_tag(to_string(m_tcs_expected_failures) +
356 " expected failed test cases.",
358 (*m_os) << format_text_with_tag(to_string(m_tcs_skipped) +
359 " skipped test cases.",
364 ticker_writer(const atf::fs::path& p) :
365 m_os(open_outfile(p))
370 // ------------------------------------------------------------------------
372 // ------------------------------------------------------------------------
375 //! \brief A single-file XML output format.
377 //! The xml_writer class implements a formatter that prints the results
378 //! of test cases in an XML format easily parseable later on by other
381 class xml_writer : public writer {
384 std::string m_tcname, m_tpname;
388 attrval(const std::string& str)
395 elemval(const std::string& str)
398 for (std::string::const_iterator iter = str.begin();
399 iter != str.end(); iter++) {
401 case '&': ostr += "&"; break;
402 case '<': ostr += "<"; break;
403 case '>': ostr += ">"; break;
404 default: ostr += *iter;
411 write_info(const std::string& what, const std::string& val)
413 (*m_os) << "<info class=\"" << what << "\">" << val << "</info>\n";
417 write_tp_start(const std::string& tp,
418 size_t ntcs ATF_DEFS_ATTRIBUTE_UNUSED)
420 (*m_os) << "<tp id=\"" << attrval(tp) << "\">\n";
424 write_tp_end(struct timeval* tv, const std::string& reason)
427 (*m_os) << "<failed>" << elemval(reason) << "</failed>\n";
428 (*m_os) << "<tp-time>" << format_tv(tv) << "</tp-time>";
429 (*m_os) << "</tp>\n";
433 write_tc_start(const std::string& tcname)
435 (*m_os) << "<tc id=\"" << attrval(tcname) << "\">\n";
439 write_tc_stdout_line(const std::string& line)
441 (*m_os) << "<so>" << elemval(line) << "</so>\n";
445 write_tc_stderr_line(const std::string& line)
447 (*m_os) << "<se>" << elemval(line) << "</se>\n";
451 write_tc_end(const std::string& state, struct timeval* tv,
452 const std::string& reason)
456 if (state == "expected_death" || state == "expected_exit" ||
457 state == "expected_failure" || state == "expected_signal" ||
458 state == "expected_timeout") {
459 (*m_os) << "<" << state << ">" << elemval(reason)
460 << "</" << state << ">\n";
461 } else if (state == "passed") {
462 (*m_os) << "<passed />\n";
463 } else if (state == "failed") {
464 (*m_os) << "<failed>" << elemval(reason) << "</failed>\n";
465 } else if (state == "skipped") {
466 (*m_os) << "<skipped>" << elemval(reason) << "</skipped>\n";
469 (*m_os) << "<tc-time>" << format_tv(tv) << "</tc-time>";
470 (*m_os) << "</tc>\n";
476 (*m_os) << "</tests-results>\n";
480 xml_writer(const atf::fs::path& p) :
481 m_os(open_outfile(p))
483 (*m_os) << "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"
484 << "<!DOCTYPE tests-results PUBLIC "
485 "\"-//NetBSD//DTD ATF Tests Results 0.1//EN\" "
486 "\"http://www.NetBSD.org/XML/atf/tests-results.dtd\">\n\n"
491 // ------------------------------------------------------------------------
492 // The "converter" class.
493 // ------------------------------------------------------------------------
496 //! \brief A reader that redirects events to multiple writers.
498 //! The converter class implements an atf_tps_reader that, for each event
499 //! raised by the parser, redirects it to multiple writers so that they
500 //! can reformat it according to their output rules.
502 class converter : public atf::atf_report::atf_tps_reader {
503 typedef std::vector< writer* > outs_vector;
507 got_info(const std::string& what, const std::string& val)
509 for (outs_vector::iterator iter = m_outs.begin();
510 iter != m_outs.end(); iter++)
511 (*iter)->write_info(what, val);
515 got_ntps(size_t ntps)
517 for (outs_vector::iterator iter = m_outs.begin();
518 iter != m_outs.end(); iter++)
519 (*iter)->write_ntps(ntps);
523 got_tp_start(const std::string& tp, size_t ntcs)
525 for (outs_vector::iterator iter = m_outs.begin();
526 iter != m_outs.end(); iter++)
527 (*iter)->write_tp_start(tp, ntcs);
531 got_tp_end(struct timeval* tv, const std::string& reason)
533 for (outs_vector::iterator iter = m_outs.begin();
534 iter != m_outs.end(); iter++)
535 (*iter)->write_tp_end(tv, reason);
539 got_tc_start(const std::string& tcname)
541 for (outs_vector::iterator iter = m_outs.begin();
542 iter != m_outs.end(); iter++)
543 (*iter)->write_tc_start(tcname);
547 got_tc_stdout_line(const std::string& line)
549 for (outs_vector::iterator iter = m_outs.begin();
550 iter != m_outs.end(); iter++)
551 (*iter)->write_tc_stdout_line(line);
555 got_tc_stderr_line(const std::string& line)
557 for (outs_vector::iterator iter = m_outs.begin();
558 iter != m_outs.end(); iter++)
559 (*iter)->write_tc_stderr_line(line);
563 got_tc_end(const std::string& state, struct timeval* tv,
564 const std::string& reason)
566 for (outs_vector::iterator iter = m_outs.begin();
567 iter != m_outs.end(); iter++)
568 (*iter)->write_tc_end(state, tv, reason);
574 for (outs_vector::iterator iter = m_outs.begin();
575 iter != m_outs.end(); iter++)
576 (*iter)->write_eof();
580 converter(std::istream& is) :
581 atf::atf_report::atf_tps_reader(is)
587 for (outs_vector::iterator iter = m_outs.begin();
588 iter != m_outs.end(); iter++)
593 add_output(const std::string& fmt, const atf::fs::path& p)
596 m_outs.push_back(new csv_writer(p));
597 } else if (fmt == "ticker") {
598 m_outs.push_back(new ticker_writer(p));
599 } else if (fmt == "xml") {
600 m_outs.push_back(new xml_writer(p));
602 throw std::runtime_error("Unknown format `" + fmt + "'");
606 // ------------------------------------------------------------------------
607 // The "atf_report" class.
608 // ------------------------------------------------------------------------
610 class atf_report : public atf::application::app {
611 static const char* m_description;
613 typedef std::pair< std::string, atf::fs::path > fmt_path_pair;
614 std::vector< fmt_path_pair > m_oflags;
616 void process_option(int, const char*);
617 options_set specific_options(void) const;
625 const char* atf_report::m_description =
626 "atf-report is a tool that parses the output of atf-run and "
627 "generates user-friendly reports in multiple different formats.";
629 atf_report::atf_report(void) :
630 app(m_description, "atf-report(1)", "atf(7)")
635 atf_report::process_option(int ch, const char* arg)
640 std::string str(arg);
641 std::string::size_type pos = str.find(':');
642 if (pos == std::string::npos)
643 throw std::runtime_error("Syntax error in -o option");
645 std::string fmt = str.substr(0, pos);
646 atf::fs::path path = atf::fs::path(str.substr(pos + 1));
647 m_oflags.push_back(fmt_path_pair(fmt, path));
657 atf_report::options_set
658 atf_report::specific_options(void)
661 using atf::application::option;
663 opts.insert(option('o', "fmt:path", "Adds a new output file; multiple "
664 "ones can be specified, and a - "
665 "path means stdout"));
670 atf_report::main(void)
673 throw std::runtime_error("No arguments allowed");
675 if (m_oflags.empty())
676 m_oflags.push_back(fmt_path_pair("ticker", atf::fs::path("-")));
678 // Look for path duplicates.
679 std::set< atf::fs::path > paths;
680 for (std::vector< fmt_path_pair >::const_iterator iter = m_oflags.begin();
681 iter != m_oflags.end(); iter++) {
682 atf::fs::path p = (*iter).second;
683 if (p == atf::fs::path("/dev/stdout"))
684 p = atf::fs::path("-");
685 if (paths.find(p) != paths.end())
686 throw std::runtime_error("The file `" + p.str() + "' was "
687 "specified more than once");
688 paths.insert((*iter).second);
691 // Generate the output files.
692 converter cnv(std::cin);
693 for (std::vector< fmt_path_pair >::const_iterator iter = m_oflags.begin();
694 iter != m_oflags.end(); iter++)
695 cnv.add_output((*iter).first, (*iter).second);
702 main(int argc, char* const* argv)
704 return atf_report().run(argc, argv);