xref: /freebsd/contrib/kyua/cli/cmd_report_junit.cpp (revision b0d29bc47dba79f6f38e67eabadfb4b32ffd9390)
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 "cli/cmd_report_junit.hpp"
30 
31 #include <cstddef>
32 #include <cstdlib>
33 #include <set>
34 
35 #include "cli/common.ipp"
36 #include "drivers/report_junit.hpp"
37 #include "drivers/scan_results.hpp"
38 #include "engine/filters.hpp"
39 #include "store/layout.hpp"
40 #include "utils/cmdline/options.hpp"
41 #include "utils/cmdline/parser.ipp"
42 #include "utils/defs.hpp"
43 #include "utils/optional.ipp"
44 #include "utils/stream.hpp"
45 
46 namespace cmdline = utils::cmdline;
47 namespace config = utils::config;
48 namespace fs = utils::fs;
49 namespace layout = store::layout;
50 
51 using cli::cmd_report_junit;
52 using utils::optional;
53 
54 
55 /// Default constructor for cmd_report.
cmd_report_junit(void)56 cmd_report_junit::cmd_report_junit(void) : cli_command(
57     "report-junit", "", 0, 0,
58     "Generates a JUnit report with the result of a test suite run")
59 {
60     add_option(results_file_open_option);
61     add_option(cmdline::path_option("output", "Path to the output file", "path",
62                                     "/dev/stdout"));
63 }
64 
65 
66 /// Entry point for the "report" subcommand.
67 ///
68 /// \param cmdline Representation of the command line to the subcommand.
69 ///
70 /// \return 0 if everything is OK, 1 if the statement is invalid or if there is
71 /// any other problem.
72 int
run(cmdline::ui *,const cmdline::parsed_cmdline & cmdline,const config::tree &)73 cmd_report_junit::run(cmdline::ui* /* ui */,
74                       const cmdline::parsed_cmdline& cmdline,
75                       const config::tree& /* user_config */)
76 {
77     const fs::path results_file = layout::find_results(
78         results_file_open(cmdline));
79 
80     std::auto_ptr< std::ostream > output = utils::open_ostream(
81         cmdline.get_option< cmdline::path_option >("output"));
82 
83     drivers::report_junit_hooks hooks(*output.get());
84     drivers::scan_results::drive(results_file,
85                                  std::set< engine::test_filter >(),
86                                  hooks);
87 
88     return EXIT_SUCCESS;
89 }
90