xref: /minix3/external/bsd/kyua-cli/dist/cli/cmd_about_test.cpp (revision 11be35a165022172ed3cea20f2b5df0307540b0e)
1*11be35a1SLionel Sambuc // Copyright 2010 Google Inc.
2*11be35a1SLionel Sambuc // All rights reserved.
3*11be35a1SLionel Sambuc //
4*11be35a1SLionel Sambuc // Redistribution and use in source and binary forms, with or without
5*11be35a1SLionel Sambuc // modification, are permitted provided that the following conditions are
6*11be35a1SLionel Sambuc // met:
7*11be35a1SLionel Sambuc //
8*11be35a1SLionel Sambuc // * Redistributions of source code must retain the above copyright
9*11be35a1SLionel Sambuc //   notice, this list of conditions and the following disclaimer.
10*11be35a1SLionel Sambuc // * Redistributions in binary form must reproduce the above copyright
11*11be35a1SLionel Sambuc //   notice, this list of conditions and the following disclaimer in the
12*11be35a1SLionel Sambuc //   documentation and/or other materials provided with the distribution.
13*11be35a1SLionel Sambuc // * Neither the name of Google Inc. nor the names of its contributors
14*11be35a1SLionel Sambuc //   may be used to endorse or promote products derived from this software
15*11be35a1SLionel Sambuc //   without specific prior written permission.
16*11be35a1SLionel Sambuc //
17*11be35a1SLionel Sambuc // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*11be35a1SLionel Sambuc // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*11be35a1SLionel Sambuc // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*11be35a1SLionel Sambuc // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*11be35a1SLionel Sambuc // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*11be35a1SLionel Sambuc // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*11be35a1SLionel Sambuc // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*11be35a1SLionel Sambuc // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*11be35a1SLionel Sambuc // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*11be35a1SLionel Sambuc // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*11be35a1SLionel Sambuc // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*11be35a1SLionel Sambuc 
29*11be35a1SLionel Sambuc #include "cli/cmd_about.hpp"
30*11be35a1SLionel Sambuc 
31*11be35a1SLionel Sambuc #if defined(HAVE_CONFIG_H)
32*11be35a1SLionel Sambuc #   include "config.h"
33*11be35a1SLionel Sambuc #endif
34*11be35a1SLionel Sambuc 
35*11be35a1SLionel Sambuc #include <cstdlib>
36*11be35a1SLionel Sambuc 
37*11be35a1SLionel Sambuc #include <atf-c++.hpp>
38*11be35a1SLionel Sambuc 
39*11be35a1SLionel Sambuc #include "cli/common.ipp"
40*11be35a1SLionel Sambuc #include "engine/config.hpp"
41*11be35a1SLionel Sambuc #include "utils/cmdline/exceptions.hpp"
42*11be35a1SLionel Sambuc #include "utils/cmdline/parser.hpp"
43*11be35a1SLionel Sambuc #include "utils/cmdline/ui_mock.hpp"
44*11be35a1SLionel Sambuc #include "utils/env.hpp"
45*11be35a1SLionel Sambuc #include "utils/fs/operations.hpp"
46*11be35a1SLionel Sambuc #include "utils/fs/path.hpp"
47*11be35a1SLionel Sambuc 
48*11be35a1SLionel Sambuc namespace cmdline = utils::cmdline;
49*11be35a1SLionel Sambuc namespace fs = utils::fs;
50*11be35a1SLionel Sambuc 
51*11be35a1SLionel Sambuc using cli::cmd_about;
52*11be35a1SLionel Sambuc 
53*11be35a1SLionel Sambuc 
54*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(all_topics__ok);
ATF_TEST_CASE_BODY(all_topics__ok)55*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(all_topics__ok)
56*11be35a1SLionel Sambuc {
57*11be35a1SLionel Sambuc     cmdline::args_vector args;
58*11be35a1SLionel Sambuc     args.push_back("about");
59*11be35a1SLionel Sambuc 
60*11be35a1SLionel Sambuc     fs::mkdir(fs::path("fake-docs"), 0755);
61*11be35a1SLionel Sambuc     atf::utils::create_file("fake-docs/AUTHORS", "Content of AUTHORS\n");
62*11be35a1SLionel Sambuc     atf::utils::create_file("fake-docs/COPYING", "Content of COPYING\n");
63*11be35a1SLionel Sambuc 
64*11be35a1SLionel Sambuc     utils::setenv("KYUA_DOCDIR", "fake-docs");
65*11be35a1SLionel Sambuc     cmd_about cmd;
66*11be35a1SLionel Sambuc     cmdline::ui_mock ui;
67*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(EXIT_SUCCESS, cmd.main(&ui, args, engine::default_config()));
68*11be35a1SLionel Sambuc     ATF_REQUIRE(atf::utils::grep_string(PACKAGE_NAME, ui.out_log()[0]));
69*11be35a1SLionel Sambuc     ATF_REQUIRE(atf::utils::grep_string(PACKAGE_VERSION, ui.out_log()[0]));
70*11be35a1SLionel Sambuc     ATF_REQUIRE(atf::utils::grep_collection("Content of AUTHORS",
71*11be35a1SLionel Sambuc                                             ui.out_log()));
72*11be35a1SLionel Sambuc     ATF_REQUIRE(atf::utils::grep_collection("Content of COPYING",
73*11be35a1SLionel Sambuc                                             ui.out_log()));
74*11be35a1SLionel Sambuc     ATF_REQUIRE(atf::utils::grep_collection("Homepage", ui.out_log()));
75*11be35a1SLionel Sambuc     ATF_REQUIRE(ui.err_log().empty());
76*11be35a1SLionel Sambuc }
77*11be35a1SLionel Sambuc 
78*11be35a1SLionel Sambuc 
79*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(all_topics__missing_docs);
ATF_TEST_CASE_BODY(all_topics__missing_docs)80*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(all_topics__missing_docs)
81*11be35a1SLionel Sambuc {
82*11be35a1SLionel Sambuc     cmdline::args_vector args;
83*11be35a1SLionel Sambuc     args.push_back("about");
84*11be35a1SLionel Sambuc 
85*11be35a1SLionel Sambuc     utils::setenv("KYUA_DOCDIR", "fake-docs");
86*11be35a1SLionel Sambuc     cmd_about cmd;
87*11be35a1SLionel Sambuc     cmdline::ui_mock ui;
88*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(EXIT_FAILURE, cmd.main(&ui, args, engine::default_config()));
89*11be35a1SLionel Sambuc 
90*11be35a1SLionel Sambuc     ATF_REQUIRE(atf::utils::grep_string(PACKAGE_NAME, ui.out_log()[0]));
91*11be35a1SLionel Sambuc     ATF_REQUIRE(atf::utils::grep_string(PACKAGE_VERSION, ui.out_log()[0]));
92*11be35a1SLionel Sambuc 
93*11be35a1SLionel Sambuc     ATF_REQUIRE(atf::utils::grep_collection("Homepage", ui.out_log()));
94*11be35a1SLionel Sambuc 
95*11be35a1SLionel Sambuc     ATF_REQUIRE(atf::utils::grep_collection("Failed to open.*AUTHORS",
96*11be35a1SLionel Sambuc                                             ui.err_log()));
97*11be35a1SLionel Sambuc     ATF_REQUIRE(atf::utils::grep_collection("Failed to open.*COPYING",
98*11be35a1SLionel Sambuc                                             ui.err_log()));
99*11be35a1SLionel Sambuc }
100*11be35a1SLionel Sambuc 
101*11be35a1SLionel Sambuc 
102*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(topic_authors__ok);
ATF_TEST_CASE_BODY(topic_authors__ok)103*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(topic_authors__ok)
104*11be35a1SLionel Sambuc {
105*11be35a1SLionel Sambuc     cmdline::args_vector args;
106*11be35a1SLionel Sambuc     args.push_back("about");
107*11be35a1SLionel Sambuc     args.push_back("authors");
108*11be35a1SLionel Sambuc 
109*11be35a1SLionel Sambuc     fs::mkdir(fs::path("fake-docs"), 0755);
110*11be35a1SLionel Sambuc     atf::utils::create_file("fake-docs/AUTHORS", "Content of AUTHORS\n");
111*11be35a1SLionel Sambuc 
112*11be35a1SLionel Sambuc     utils::setenv("KYUA_DOCDIR", "fake-docs");
113*11be35a1SLionel Sambuc     cmd_about cmd;
114*11be35a1SLionel Sambuc     cmdline::ui_mock ui;
115*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(EXIT_SUCCESS, cmd.main(&ui, args, engine::default_config()));
116*11be35a1SLionel Sambuc     ATF_REQUIRE(!atf::utils::grep_string(PACKAGE_NAME, ui.out_log()[0]));
117*11be35a1SLionel Sambuc     ATF_REQUIRE(atf::utils::grep_collection("Content of AUTHORS",
118*11be35a1SLionel Sambuc                                             ui.out_log()));
119*11be35a1SLionel Sambuc     ATF_REQUIRE(!atf::utils::grep_collection("COPYING", ui.out_log()));
120*11be35a1SLionel Sambuc     ATF_REQUIRE(!atf::utils::grep_collection("Homepage", ui.out_log()));
121*11be35a1SLionel Sambuc     ATF_REQUIRE(ui.err_log().empty());
122*11be35a1SLionel Sambuc }
123*11be35a1SLionel Sambuc 
124*11be35a1SLionel Sambuc 
125*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(topic_authors__missing_doc);
ATF_TEST_CASE_BODY(topic_authors__missing_doc)126*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(topic_authors__missing_doc)
127*11be35a1SLionel Sambuc {
128*11be35a1SLionel Sambuc     cmdline::args_vector args;
129*11be35a1SLionel Sambuc     args.push_back("about");
130*11be35a1SLionel Sambuc     args.push_back("authors");
131*11be35a1SLionel Sambuc 
132*11be35a1SLionel Sambuc     utils::setenv("KYUA_DOCDIR", "fake-docs");
133*11be35a1SLionel Sambuc     cmd_about cmd;
134*11be35a1SLionel Sambuc     cmdline::ui_mock ui;
135*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(EXIT_FAILURE, cmd.main(&ui, args, engine::default_config()));
136*11be35a1SLionel Sambuc 
137*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(0, ui.out_log().size());
138*11be35a1SLionel Sambuc 
139*11be35a1SLionel Sambuc     ATF_REQUIRE(atf::utils::grep_collection("Failed to open.*AUTHORS",
140*11be35a1SLionel Sambuc                                             ui.err_log()));
141*11be35a1SLionel Sambuc     ATF_REQUIRE(!atf::utils::grep_collection("Failed to open.*COPYING",
142*11be35a1SLionel Sambuc                                              ui.err_log()));
143*11be35a1SLionel Sambuc }
144*11be35a1SLionel Sambuc 
145*11be35a1SLionel Sambuc 
146*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(topic_license__ok);
ATF_TEST_CASE_BODY(topic_license__ok)147*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(topic_license__ok)
148*11be35a1SLionel Sambuc {
149*11be35a1SLionel Sambuc     cmdline::args_vector args;
150*11be35a1SLionel Sambuc     args.push_back("about");
151*11be35a1SLionel Sambuc     args.push_back("license");
152*11be35a1SLionel Sambuc 
153*11be35a1SLionel Sambuc     fs::mkdir(fs::path("fake-docs"), 0755);
154*11be35a1SLionel Sambuc     atf::utils::create_file("fake-docs/COPYING", "Content of COPYING\n");
155*11be35a1SLionel Sambuc 
156*11be35a1SLionel Sambuc     utils::setenv("KYUA_DOCDIR", "fake-docs");
157*11be35a1SLionel Sambuc     cmd_about cmd;
158*11be35a1SLionel Sambuc     cmdline::ui_mock ui;
159*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(EXIT_SUCCESS, cmd.main(&ui, args, engine::default_config()));
160*11be35a1SLionel Sambuc     ATF_REQUIRE(!atf::utils::grep_string(PACKAGE_NAME, ui.out_log()[0]));
161*11be35a1SLionel Sambuc     ATF_REQUIRE(!atf::utils::grep_collection("AUTHORS", ui.out_log()));
162*11be35a1SLionel Sambuc     ATF_REQUIRE(atf::utils::grep_collection("Content of COPYING",
163*11be35a1SLionel Sambuc                                             ui.out_log()));
164*11be35a1SLionel Sambuc     ATF_REQUIRE(!atf::utils::grep_collection("Homepage", ui.out_log()));
165*11be35a1SLionel Sambuc     ATF_REQUIRE(ui.err_log().empty());
166*11be35a1SLionel Sambuc }
167*11be35a1SLionel Sambuc 
168*11be35a1SLionel Sambuc 
169*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(topic_license__missing_doc);
ATF_TEST_CASE_BODY(topic_license__missing_doc)170*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(topic_license__missing_doc)
171*11be35a1SLionel Sambuc {
172*11be35a1SLionel Sambuc     cmdline::args_vector args;
173*11be35a1SLionel Sambuc     args.push_back("about");
174*11be35a1SLionel Sambuc     args.push_back("license");
175*11be35a1SLionel Sambuc 
176*11be35a1SLionel Sambuc     utils::setenv("KYUA_DOCDIR", "fake-docs");
177*11be35a1SLionel Sambuc     cmd_about cmd;
178*11be35a1SLionel Sambuc     cmdline::ui_mock ui;
179*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(EXIT_FAILURE, cmd.main(&ui, args, engine::default_config()));
180*11be35a1SLionel Sambuc 
181*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(0, ui.out_log().size());
182*11be35a1SLionel Sambuc 
183*11be35a1SLionel Sambuc     ATF_REQUIRE(!atf::utils::grep_collection("Failed to open.*AUTHORS",
184*11be35a1SLionel Sambuc                                              ui.err_log()));
185*11be35a1SLionel Sambuc     ATF_REQUIRE(atf::utils::grep_collection("Failed to open.*COPYING",
186*11be35a1SLionel Sambuc                                             ui.err_log()));
187*11be35a1SLionel Sambuc }
188*11be35a1SLionel Sambuc 
189*11be35a1SLionel Sambuc 
190*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(topic_version__ok);
ATF_TEST_CASE_BODY(topic_version__ok)191*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(topic_version__ok)
192*11be35a1SLionel Sambuc {
193*11be35a1SLionel Sambuc     cmdline::args_vector args;
194*11be35a1SLionel Sambuc     args.push_back("about");
195*11be35a1SLionel Sambuc     args.push_back("version");
196*11be35a1SLionel Sambuc 
197*11be35a1SLionel Sambuc     utils::setenv("KYUA_DOCDIR", "fake-docs");
198*11be35a1SLionel Sambuc     cmd_about cmd;
199*11be35a1SLionel Sambuc     cmdline::ui_mock ui;
200*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(EXIT_SUCCESS, cmd.main(&ui, args, engine::default_config()));
201*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(1, ui.out_log().size());
202*11be35a1SLionel Sambuc     ATF_REQUIRE(atf::utils::grep_string(PACKAGE_NAME, ui.out_log()[0]));
203*11be35a1SLionel Sambuc     ATF_REQUIRE(atf::utils::grep_string(PACKAGE_VERSION, ui.out_log()[0]));
204*11be35a1SLionel Sambuc     ATF_REQUIRE(ui.err_log().empty());
205*11be35a1SLionel Sambuc }
206*11be35a1SLionel Sambuc 
207*11be35a1SLionel Sambuc 
208*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(invalid_args);
ATF_TEST_CASE_BODY(invalid_args)209*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(invalid_args)
210*11be35a1SLionel Sambuc {
211*11be35a1SLionel Sambuc     cmdline::args_vector args;
212*11be35a1SLionel Sambuc     args.push_back("about");
213*11be35a1SLionel Sambuc     args.push_back("first");
214*11be35a1SLionel Sambuc     args.push_back("second");
215*11be35a1SLionel Sambuc 
216*11be35a1SLionel Sambuc     cmd_about cmd;
217*11be35a1SLionel Sambuc     cmdline::ui_mock ui;
218*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW_RE(cmdline::usage_error, "Too many arguments",
219*11be35a1SLionel Sambuc                          cmd.main(&ui, args, engine::default_config()));
220*11be35a1SLionel Sambuc     ATF_REQUIRE(ui.out_log().empty());
221*11be35a1SLionel Sambuc     ATF_REQUIRE(ui.err_log().empty());
222*11be35a1SLionel Sambuc }
223*11be35a1SLionel Sambuc 
224*11be35a1SLionel Sambuc 
225*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(invalid_topic);
ATF_TEST_CASE_BODY(invalid_topic)226*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(invalid_topic)
227*11be35a1SLionel Sambuc {
228*11be35a1SLionel Sambuc     cmdline::args_vector args;
229*11be35a1SLionel Sambuc     args.push_back("about");
230*11be35a1SLionel Sambuc     args.push_back("foo");
231*11be35a1SLionel Sambuc 
232*11be35a1SLionel Sambuc     cmd_about cmd;
233*11be35a1SLionel Sambuc     cmdline::ui_mock ui;
234*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW_RE(cmdline::usage_error, "Invalid about topic 'foo'",
235*11be35a1SLionel Sambuc                          cmd.main(&ui, args, engine::default_config()));
236*11be35a1SLionel Sambuc     ATF_REQUIRE(ui.out_log().empty());
237*11be35a1SLionel Sambuc     ATF_REQUIRE(ui.err_log().empty());
238*11be35a1SLionel Sambuc }
239*11be35a1SLionel Sambuc 
240*11be35a1SLionel Sambuc 
ATF_INIT_TEST_CASES(tcs)241*11be35a1SLionel Sambuc ATF_INIT_TEST_CASES(tcs)
242*11be35a1SLionel Sambuc {
243*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, all_topics__ok);
244*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, all_topics__missing_docs);
245*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, topic_authors__ok);
246*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, topic_authors__missing_doc);
247*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, topic_license__ok);
248*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, topic_license__missing_doc);
249*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, topic_version__ok);
250*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, invalid_args);
251*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, invalid_topic);
252*11be35a1SLionel Sambuc }
253