xref: /freebsd/contrib/kyua/cli/cmd_about_test.cpp (revision 3c4ba5f55438f7afd4f4b0b56f88f2bb505fd6a6)
1 // Copyright 2010 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_about.hpp"
30 
31 #if defined(HAVE_CONFIG_H)
32 #   include "config.h"
33 #endif
34 
35 #include <cstdlib>
36 
37 #include <atf-c++.hpp>
38 
39 #include "cli/common.ipp"
40 #include "engine/config.hpp"
41 #include "utils/cmdline/exceptions.hpp"
42 #include "utils/cmdline/parser.hpp"
43 #include "utils/cmdline/ui_mock.hpp"
44 #include "utils/config/tree.ipp"
45 #include "utils/env.hpp"
46 #include "utils/fs/operations.hpp"
47 #include "utils/fs/path.hpp"
48 
49 namespace cmdline = utils::cmdline;
50 namespace fs = utils::fs;
51 
52 using cli::cmd_about;
53 
54 
55 ATF_TEST_CASE_WITHOUT_HEAD(all_topics__ok);
56 ATF_TEST_CASE_BODY(all_topics__ok)
57 {
58     cmdline::args_vector args;
59     args.push_back("about");
60 
61     fs::mkdir(fs::path("fake-docs"), 0755);
62     atf::utils::create_file("fake-docs/AUTHORS",
63                             "Content of AUTHORS\n"
64                             "* First author\n"
65                             " * garbage\n"
66                             "* Second author\n");
67     atf::utils::create_file("fake-docs/CONTRIBUTORS",
68                             "Content of CONTRIBUTORS\n"
69                             "* First contributor\n"
70                             " * garbage\n"
71                             "* Second contributor\n");
72     atf::utils::create_file("fake-docs/LICENSE", "Content of LICENSE\n");
73 
74     utils::setenv("KYUA_DOCDIR", "fake-docs");
75     cmd_about cmd;
76     cmdline::ui_mock ui;
77     ATF_REQUIRE_EQ(EXIT_SUCCESS, cmd.main(&ui, args, engine::default_config()));
78 
79     ATF_REQUIRE(atf::utils::grep_string(PACKAGE_NAME, ui.out_log()[0]));
80     ATF_REQUIRE(atf::utils::grep_string(PACKAGE_VERSION, ui.out_log()[0]));
81 
82     ATF_REQUIRE(!atf::utils::grep_collection("Content of AUTHORS",
83                                              ui.out_log()));
84     ATF_REQUIRE(atf::utils::grep_collection("\\* First author", ui.out_log()));
85     ATF_REQUIRE(!atf::utils::grep_collection("garbage", ui.out_log()));
86     ATF_REQUIRE(atf::utils::grep_collection("\\* Second author", ui.out_log()));
87 
88     ATF_REQUIRE(!atf::utils::grep_collection("Content of CONTRIBUTORS",
89                                              ui.out_log()));
90     ATF_REQUIRE(atf::utils::grep_collection("\\* First contributor",
91                                             ui.out_log()));
92     ATF_REQUIRE(!atf::utils::grep_collection("garbage", ui.out_log()));
93     ATF_REQUIRE(atf::utils::grep_collection("\\* Second contributor",
94                                             ui.out_log()));
95 
96     ATF_REQUIRE(atf::utils::grep_collection("Content of LICENSE",
97                                             ui.out_log()));
98 
99     ATF_REQUIRE(atf::utils::grep_collection("Homepage", ui.out_log()));
100     ATF_REQUIRE(ui.err_log().empty());
101 }
102 
103 
104 ATF_TEST_CASE_WITHOUT_HEAD(all_topics__missing_docs);
105 ATF_TEST_CASE_BODY(all_topics__missing_docs)
106 {
107     cmdline::args_vector args;
108     args.push_back("about");
109 
110     utils::setenv("KYUA_DOCDIR", "fake-docs");
111     cmd_about cmd;
112     cmdline::ui_mock ui;
113     ATF_REQUIRE_EQ(EXIT_FAILURE, cmd.main(&ui, args, engine::default_config()));
114 
115     ATF_REQUIRE(atf::utils::grep_string(PACKAGE_NAME, ui.out_log()[0]));
116     ATF_REQUIRE(atf::utils::grep_string(PACKAGE_VERSION, ui.out_log()[0]));
117 
118     ATF_REQUIRE(atf::utils::grep_collection("Homepage", ui.out_log()));
119 
120     ATF_REQUIRE(atf::utils::grep_collection("Failed to open.*AUTHORS",
121                                             ui.err_log()));
122     ATF_REQUIRE(atf::utils::grep_collection("Failed to open.*CONTRIBUTORS",
123                                             ui.err_log()));
124     ATF_REQUIRE(atf::utils::grep_collection("Failed to open.*LICENSE",
125                                             ui.err_log()));
126 }
127 
128 
129 ATF_TEST_CASE_WITHOUT_HEAD(topic_authors__ok);
130 ATF_TEST_CASE_BODY(topic_authors__ok)
131 {
132     cmdline::args_vector args;
133     args.push_back("about");
134     args.push_back("authors");
135 
136     fs::mkdir(fs::path("fake-docs"), 0755);
137     atf::utils::create_file("fake-docs/AUTHORS",
138                             "Content of AUTHORS\n"
139                             "* First author\n"
140                             " * garbage\n"
141                             "* Second author\n");
142     atf::utils::create_file("fake-docs/CONTRIBUTORS",
143                             "Content of CONTRIBUTORS\n"
144                             "* First contributor\n"
145                             " * garbage\n"
146                             "* Second contributor\n");
147 
148     utils::setenv("KYUA_DOCDIR", "fake-docs");
149     cmd_about cmd;
150     cmdline::ui_mock ui;
151     ATF_REQUIRE_EQ(EXIT_SUCCESS, cmd.main(&ui, args, engine::default_config()));
152     ATF_REQUIRE(!atf::utils::grep_string(PACKAGE_NAME, ui.out_log()[0]));
153 
154     ATF_REQUIRE(!atf::utils::grep_collection("Content of AUTHORS",
155                                              ui.out_log()));
156     ATF_REQUIRE(atf::utils::grep_collection("\\* First author", ui.out_log()));
157     ATF_REQUIRE(!atf::utils::grep_collection("garbage", ui.out_log()));
158     ATF_REQUIRE(atf::utils::grep_collection("\\* Second author", ui.out_log()));
159 
160     ATF_REQUIRE(!atf::utils::grep_collection("Content of CONTRIBUTORS",
161                                              ui.out_log()));
162     ATF_REQUIRE(atf::utils::grep_collection("\\* First contributor",
163                                             ui.out_log()));
164     ATF_REQUIRE(!atf::utils::grep_collection("garbage", ui.out_log()));
165     ATF_REQUIRE(atf::utils::grep_collection("\\* Second contributor",
166                                             ui.out_log()));
167 
168     ATF_REQUIRE(!atf::utils::grep_collection("LICENSE", ui.out_log()));
169     ATF_REQUIRE(!atf::utils::grep_collection("Homepage", ui.out_log()));
170     ATF_REQUIRE(ui.err_log().empty());
171 }
172 
173 
174 ATF_TEST_CASE_WITHOUT_HEAD(topic_authors__missing_doc);
175 ATF_TEST_CASE_BODY(topic_authors__missing_doc)
176 {
177     cmdline::args_vector args;
178     args.push_back("about");
179     args.push_back("authors");
180 
181     utils::setenv("KYUA_DOCDIR", "fake-docs");
182     cmd_about cmd;
183     cmdline::ui_mock ui;
184     ATF_REQUIRE_EQ(EXIT_FAILURE, cmd.main(&ui, args, engine::default_config()));
185 
186     ATF_REQUIRE_EQ(0, ui.out_log().size());
187 
188     ATF_REQUIRE(atf::utils::grep_collection("Failed to open.*AUTHORS",
189                                             ui.err_log()));
190     ATF_REQUIRE(atf::utils::grep_collection("Failed to open.*CONTRIBUTORS",
191                                             ui.err_log()));
192     ATF_REQUIRE(!atf::utils::grep_collection("Failed to open.*LICENSE",
193                                              ui.err_log()));
194 }
195 
196 
197 ATF_TEST_CASE_WITHOUT_HEAD(topic_license__ok);
198 ATF_TEST_CASE_BODY(topic_license__ok)
199 {
200     cmdline::args_vector args;
201     args.push_back("about");
202     args.push_back("license");
203 
204     fs::mkdir(fs::path("fake-docs"), 0755);
205     atf::utils::create_file("fake-docs/LICENSE", "Content of LICENSE\n");
206 
207     utils::setenv("KYUA_DOCDIR", "fake-docs");
208     cmd_about cmd;
209     cmdline::ui_mock ui;
210     ATF_REQUIRE_EQ(EXIT_SUCCESS, cmd.main(&ui, args, engine::default_config()));
211     ATF_REQUIRE(!atf::utils::grep_string(PACKAGE_NAME, ui.out_log()[0]));
212     ATF_REQUIRE(!atf::utils::grep_collection("AUTHORS", ui.out_log()));
213     ATF_REQUIRE(!atf::utils::grep_collection("CONTRIBUTORS", ui.out_log()));
214     ATF_REQUIRE(atf::utils::grep_collection("Content of LICENSE",
215                                             ui.out_log()));
216     ATF_REQUIRE(!atf::utils::grep_collection("Homepage", ui.out_log()));
217     ATF_REQUIRE(ui.err_log().empty());
218 }
219 
220 
221 ATF_TEST_CASE_WITHOUT_HEAD(topic_license__missing_doc);
222 ATF_TEST_CASE_BODY(topic_license__missing_doc)
223 {
224     cmdline::args_vector args;
225     args.push_back("about");
226     args.push_back("license");
227 
228     utils::setenv("KYUA_DOCDIR", "fake-docs");
229     cmd_about cmd;
230     cmdline::ui_mock ui;
231     ATF_REQUIRE_EQ(EXIT_FAILURE, cmd.main(&ui, args, engine::default_config()));
232 
233     ATF_REQUIRE_EQ(0, ui.out_log().size());
234 
235     ATF_REQUIRE(!atf::utils::grep_collection("Failed to open.*AUTHORS",
236                                              ui.err_log()));
237     ATF_REQUIRE(!atf::utils::grep_collection("Failed to open.*CONTRIBUTORS",
238                                              ui.err_log()));
239     ATF_REQUIRE(atf::utils::grep_collection("Failed to open.*LICENSE",
240                                             ui.err_log()));
241 }
242 
243 
244 ATF_TEST_CASE_WITHOUT_HEAD(topic_version__ok);
245 ATF_TEST_CASE_BODY(topic_version__ok)
246 {
247     cmdline::args_vector args;
248     args.push_back("about");
249     args.push_back("version");
250 
251     utils::setenv("KYUA_DOCDIR", "fake-docs");
252     cmd_about cmd;
253     cmdline::ui_mock ui;
254     ATF_REQUIRE_EQ(EXIT_SUCCESS, cmd.main(&ui, args, engine::default_config()));
255     ATF_REQUIRE_EQ(1, ui.out_log().size());
256     ATF_REQUIRE(atf::utils::grep_string(PACKAGE_NAME, ui.out_log()[0]));
257     ATF_REQUIRE(atf::utils::grep_string(PACKAGE_VERSION, ui.out_log()[0]));
258     ATF_REQUIRE(ui.err_log().empty());
259 }
260 
261 
262 ATF_TEST_CASE_WITHOUT_HEAD(invalid_args);
263 ATF_TEST_CASE_BODY(invalid_args)
264 {
265     cmdline::args_vector args;
266     args.push_back("about");
267     args.push_back("first");
268     args.push_back("second");
269 
270     cmd_about cmd;
271     cmdline::ui_mock ui;
272     ATF_REQUIRE_THROW_RE(cmdline::usage_error, "Too many arguments",
273                          cmd.main(&ui, args, engine::default_config()));
274     ATF_REQUIRE(ui.out_log().empty());
275     ATF_REQUIRE(ui.err_log().empty());
276 }
277 
278 
279 ATF_TEST_CASE_WITHOUT_HEAD(invalid_topic);
280 ATF_TEST_CASE_BODY(invalid_topic)
281 {
282     cmdline::args_vector args;
283     args.push_back("about");
284     args.push_back("foo");
285 
286     cmd_about cmd;
287     cmdline::ui_mock ui;
288     ATF_REQUIRE_THROW_RE(cmdline::usage_error, "Invalid about topic 'foo'",
289                          cmd.main(&ui, args, engine::default_config()));
290     ATF_REQUIRE(ui.out_log().empty());
291     ATF_REQUIRE(ui.err_log().empty());
292 }
293 
294 
295 ATF_INIT_TEST_CASES(tcs)
296 {
297     ATF_ADD_TEST_CASE(tcs, all_topics__ok);
298     ATF_ADD_TEST_CASE(tcs, all_topics__missing_docs);
299     ATF_ADD_TEST_CASE(tcs, topic_authors__ok);
300     ATF_ADD_TEST_CASE(tcs, topic_authors__missing_doc);
301     ATF_ADD_TEST_CASE(tcs, topic_license__ok);
302     ATF_ADD_TEST_CASE(tcs, topic_license__missing_doc);
303     ATF_ADD_TEST_CASE(tcs, topic_version__ok);
304     ATF_ADD_TEST_CASE(tcs, invalid_args);
305     ATF_ADD_TEST_CASE(tcs, invalid_topic);
306 }
307