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 size_t m_curtp, m_ntps;
385 std::string m_tcname, m_tpname;
389 attrval(const std::string& str)
396 elemval(const std::string& str)
399 for (std::string::const_iterator iter = str.begin();
400 iter != str.end(); iter++) {
402 case '&': ostr += "&"; break;
403 case '<': ostr += "<"; break;
404 case '>': ostr += ">"; break;
405 default: ostr += *iter;
412 write_info(const std::string& what, const std::string& val)
414 (*m_os) << "<info class=\"" << what << "\">" << val << "</info>\n";
418 write_tp_start(const std::string& tp,
419 size_t ntcs ATF_DEFS_ATTRIBUTE_UNUSED)
421 (*m_os) << "<tp id=\"" << attrval(tp) << "\">\n";
425 write_tp_end(struct timeval* tv, const std::string& reason)
428 (*m_os) << "<failed>" << elemval(reason) << "</failed>\n";
429 (*m_os) << "<tp-time>" << format_tv(tv) << "</tp-time>";
430 (*m_os) << "</tp>\n";
434 write_tc_start(const std::string& tcname)
436 (*m_os) << "<tc id=\"" << attrval(tcname) << "\">\n";
440 write_tc_stdout_line(const std::string& line)
442 (*m_os) << "<so>" << elemval(line) << "</so>\n";
446 write_tc_stderr_line(const std::string& line)
448 (*m_os) << "<se>" << elemval(line) << "</se>\n";
452 write_tc_end(const std::string& state, struct timeval* tv,
453 const std::string& reason)
457 if (state == "expected_death" || state == "expected_exit" ||
458 state == "expected_failure" || state == "expected_signal" ||
459 state == "expected_timeout") {
460 (*m_os) << "<" << state << ">" << elemval(reason)
461 << "</" << state << ">\n";
462 } else if (state == "passed") {
463 (*m_os) << "<passed />\n";
464 } else if (state == "failed") {
465 (*m_os) << "<failed>" << elemval(reason) << "</failed>\n";
466 } else if (state == "skipped") {
467 (*m_os) << "<skipped>" << elemval(reason) << "</skipped>\n";
470 (*m_os) << "<tc-time>" << format_tv(tv) << "</tc-time>";
471 (*m_os) << "</tc>\n";
477 (*m_os) << "</tests-results>\n";
481 xml_writer(const atf::fs::path& p) :
482 m_os(open_outfile(p))
484 (*m_os) << "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"
485 << "<!DOCTYPE tests-results PUBLIC "
486 "\"-//NetBSD//DTD ATF Tests Results 0.1//EN\" "
487 "\"http://www.NetBSD.org/XML/atf/tests-results.dtd\">\n\n"
492 // ------------------------------------------------------------------------
493 // The "converter" class.
494 // ------------------------------------------------------------------------
497 //! \brief A reader that redirects events to multiple writers.
499 //! The converter class implements an atf_tps_reader that, for each event
500 //! raised by the parser, redirects it to multiple writers so that they
501 //! can reformat it according to their output rules.
503 class converter : public atf::atf_report::atf_tps_reader {
504 typedef std::vector< writer* > outs_vector;
508 got_info(const std::string& what, const std::string& val)
510 for (outs_vector::iterator iter = m_outs.begin();
511 iter != m_outs.end(); iter++)
512 (*iter)->write_info(what, val);
516 got_ntps(size_t ntps)
518 for (outs_vector::iterator iter = m_outs.begin();
519 iter != m_outs.end(); iter++)
520 (*iter)->write_ntps(ntps);
524 got_tp_start(const std::string& tp, size_t ntcs)
526 for (outs_vector::iterator iter = m_outs.begin();
527 iter != m_outs.end(); iter++)
528 (*iter)->write_tp_start(tp, ntcs);
532 got_tp_end(struct timeval* tv, const std::string& reason)
534 for (outs_vector::iterator iter = m_outs.begin();
535 iter != m_outs.end(); iter++)
536 (*iter)->write_tp_end(tv, reason);
540 got_tc_start(const std::string& tcname)
542 for (outs_vector::iterator iter = m_outs.begin();
543 iter != m_outs.end(); iter++)
544 (*iter)->write_tc_start(tcname);
548 got_tc_stdout_line(const std::string& line)
550 for (outs_vector::iterator iter = m_outs.begin();
551 iter != m_outs.end(); iter++)
552 (*iter)->write_tc_stdout_line(line);
556 got_tc_stderr_line(const std::string& line)
558 for (outs_vector::iterator iter = m_outs.begin();
559 iter != m_outs.end(); iter++)
560 (*iter)->write_tc_stderr_line(line);
564 got_tc_end(const std::string& state, struct timeval* tv,
565 const std::string& reason)
567 for (outs_vector::iterator iter = m_outs.begin();
568 iter != m_outs.end(); iter++)
569 (*iter)->write_tc_end(state, tv, reason);
575 for (outs_vector::iterator iter = m_outs.begin();
576 iter != m_outs.end(); iter++)
577 (*iter)->write_eof();
581 converter(std::istream& is) :
582 atf::atf_report::atf_tps_reader(is)
588 for (outs_vector::iterator iter = m_outs.begin();
589 iter != m_outs.end(); iter++)
594 add_output(const std::string& fmt, const atf::fs::path& p)
597 m_outs.push_back(new csv_writer(p));
598 } else if (fmt == "ticker") {
599 m_outs.push_back(new ticker_writer(p));
600 } else if (fmt == "xml") {
601 m_outs.push_back(new xml_writer(p));
603 throw std::runtime_error("Unknown format `" + fmt + "'");
607 // ------------------------------------------------------------------------
608 // The "atf_report" class.
609 // ------------------------------------------------------------------------
611 class atf_report : public atf::application::app {
612 static const char* m_description;
614 typedef std::pair< std::string, atf::fs::path > fmt_path_pair;
615 std::vector< fmt_path_pair > m_oflags;
617 void process_option(int, const char*);
618 options_set specific_options(void) const;
626 const char* atf_report::m_description =
627 "atf-report is a tool that parses the output of atf-run and "
628 "generates user-friendly reports in multiple different formats.";
630 atf_report::atf_report(void) :
631 app(m_description, "atf-report(1)", "atf(7)")
636 atf_report::process_option(int ch, const char* arg)
641 std::string str(arg);
642 std::string::size_type pos = str.find(':');
643 if (pos == std::string::npos)
644 throw std::runtime_error("Syntax error in -o option");
646 std::string fmt = str.substr(0, pos);
647 atf::fs::path path = atf::fs::path(str.substr(pos + 1));
648 m_oflags.push_back(fmt_path_pair(fmt, path));
658 atf_report::options_set
659 atf_report::specific_options(void)
662 using atf::application::option;
664 opts.insert(option('o', "fmt:path", "Adds a new output file; multiple "
665 "ones can be specified, and a - "
666 "path means stdout"));
671 atf_report::main(void)
674 throw std::runtime_error("No arguments allowed");
676 if (m_oflags.empty())
677 m_oflags.push_back(fmt_path_pair("ticker", atf::fs::path("-")));
679 // Look for path duplicates.
680 std::set< atf::fs::path > paths;
681 for (std::vector< fmt_path_pair >::const_iterator iter = m_oflags.begin();
682 iter != m_oflags.end(); iter++) {
683 atf::fs::path p = (*iter).second;
684 if (p == atf::fs::path("/dev/stdout"))
685 p = atf::fs::path("-");
686 if (paths.find(p) != paths.end())
687 throw std::runtime_error("The file `" + p.str() + "' was "
688 "specified more than once");
689 paths.insert((*iter).second);
692 // Generate the output files.
693 converter cnv(std::cin);
694 for (std::vector< fmt_path_pair >::const_iterator iter = m_oflags.begin();
695 iter != m_oflags.end(); iter++)
696 cnv.add_output((*iter).first, (*iter).second);
703 main(int argc, char* const* argv)
705 return atf_report().run(argc, argv);