1 // Copyright 2010 Google Inc.
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/env.hpp"
45 #include "utils/fs/operations.hpp"
46 #include "utils/fs/path.hpp"
47 
48 namespace cmdline = utils::cmdline;
49 namespace fs = utils::fs;
50 
51 using cli::cmd_about;
52 
53 
54 ATF_TEST_CASE_WITHOUT_HEAD(all_topics__ok);
55 ATF_TEST_CASE_BODY(all_topics__ok)
56 {
57     cmdline::args_vector args;
58     args.push_back("about");
59 
60     fs::mkdir(fs::path("fake-docs"), 0755);
61     atf::utils::create_file("fake-docs/AUTHORS", "Content of AUTHORS\n");
62     atf::utils::create_file("fake-docs/COPYING", "Content of COPYING\n");
63 
64     utils::setenv("KYUA_DOCDIR", "fake-docs");
65     cmd_about cmd;
66     cmdline::ui_mock ui;
67     ATF_REQUIRE_EQ(EXIT_SUCCESS, cmd.main(&ui, args, engine::default_config()));
68     ATF_REQUIRE(atf::utils::grep_string(PACKAGE_NAME, ui.out_log()[0]));
69     ATF_REQUIRE(atf::utils::grep_string(PACKAGE_VERSION, ui.out_log()[0]));
70     ATF_REQUIRE(atf::utils::grep_collection("Content of AUTHORS",
71                                             ui.out_log()));
72     ATF_REQUIRE(atf::utils::grep_collection("Content of COPYING",
73                                             ui.out_log()));
74     ATF_REQUIRE(atf::utils::grep_collection("Homepage", ui.out_log()));
75     ATF_REQUIRE(ui.err_log().empty());
76 }
77 
78 
79 ATF_TEST_CASE_WITHOUT_HEAD(all_topics__missing_docs);
80 ATF_TEST_CASE_BODY(all_topics__missing_docs)
81 {
82     cmdline::args_vector args;
83     args.push_back("about");
84 
85     utils::setenv("KYUA_DOCDIR", "fake-docs");
86     cmd_about cmd;
87     cmdline::ui_mock ui;
88     ATF_REQUIRE_EQ(EXIT_FAILURE, cmd.main(&ui, args, engine::default_config()));
89 
90     ATF_REQUIRE(atf::utils::grep_string(PACKAGE_NAME, ui.out_log()[0]));
91     ATF_REQUIRE(atf::utils::grep_string(PACKAGE_VERSION, ui.out_log()[0]));
92 
93     ATF_REQUIRE(atf::utils::grep_collection("Homepage", ui.out_log()));
94 
95     ATF_REQUIRE(atf::utils::grep_collection("Failed to open.*AUTHORS",
96                                             ui.err_log()));
97     ATF_REQUIRE(atf::utils::grep_collection("Failed to open.*COPYING",
98                                             ui.err_log()));
99 }
100 
101 
102 ATF_TEST_CASE_WITHOUT_HEAD(topic_authors__ok);
103 ATF_TEST_CASE_BODY(topic_authors__ok)
104 {
105     cmdline::args_vector args;
106     args.push_back("about");
107     args.push_back("authors");
108 
109     fs::mkdir(fs::path("fake-docs"), 0755);
110     atf::utils::create_file("fake-docs/AUTHORS", "Content of AUTHORS\n");
111 
112     utils::setenv("KYUA_DOCDIR", "fake-docs");
113     cmd_about cmd;
114     cmdline::ui_mock ui;
115     ATF_REQUIRE_EQ(EXIT_SUCCESS, cmd.main(&ui, args, engine::default_config()));
116     ATF_REQUIRE(!atf::utils::grep_string(PACKAGE_NAME, ui.out_log()[0]));
117     ATF_REQUIRE(atf::utils::grep_collection("Content of AUTHORS",
118                                             ui.out_log()));
119     ATF_REQUIRE(!atf::utils::grep_collection("COPYING", ui.out_log()));
120     ATF_REQUIRE(!atf::utils::grep_collection("Homepage", ui.out_log()));
121     ATF_REQUIRE(ui.err_log().empty());
122 }
123 
124 
125 ATF_TEST_CASE_WITHOUT_HEAD(topic_authors__missing_doc);
126 ATF_TEST_CASE_BODY(topic_authors__missing_doc)
127 {
128     cmdline::args_vector args;
129     args.push_back("about");
130     args.push_back("authors");
131 
132     utils::setenv("KYUA_DOCDIR", "fake-docs");
133     cmd_about cmd;
134     cmdline::ui_mock ui;
135     ATF_REQUIRE_EQ(EXIT_FAILURE, cmd.main(&ui, args, engine::default_config()));
136 
137     ATF_REQUIRE_EQ(0, ui.out_log().size());
138 
139     ATF_REQUIRE(atf::utils::grep_collection("Failed to open.*AUTHORS",
140                                             ui.err_log()));
141     ATF_REQUIRE(!atf::utils::grep_collection("Failed to open.*COPYING",
142                                              ui.err_log()));
143 }
144 
145 
146 ATF_TEST_CASE_WITHOUT_HEAD(topic_license__ok);
147 ATF_TEST_CASE_BODY(topic_license__ok)
148 {
149     cmdline::args_vector args;
150     args.push_back("about");
151     args.push_back("license");
152 
153     fs::mkdir(fs::path("fake-docs"), 0755);
154     atf::utils::create_file("fake-docs/COPYING", "Content of COPYING\n");
155 
156     utils::setenv("KYUA_DOCDIR", "fake-docs");
157     cmd_about cmd;
158     cmdline::ui_mock ui;
159     ATF_REQUIRE_EQ(EXIT_SUCCESS, cmd.main(&ui, args, engine::default_config()));
160     ATF_REQUIRE(!atf::utils::grep_string(PACKAGE_NAME, ui.out_log()[0]));
161     ATF_REQUIRE(!atf::utils::grep_collection("AUTHORS", ui.out_log()));
162     ATF_REQUIRE(atf::utils::grep_collection("Content of COPYING",
163                                             ui.out_log()));
164     ATF_REQUIRE(!atf::utils::grep_collection("Homepage", ui.out_log()));
165     ATF_REQUIRE(ui.err_log().empty());
166 }
167 
168 
169 ATF_TEST_CASE_WITHOUT_HEAD(topic_license__missing_doc);
170 ATF_TEST_CASE_BODY(topic_license__missing_doc)
171 {
172     cmdline::args_vector args;
173     args.push_back("about");
174     args.push_back("license");
175 
176     utils::setenv("KYUA_DOCDIR", "fake-docs");
177     cmd_about cmd;
178     cmdline::ui_mock ui;
179     ATF_REQUIRE_EQ(EXIT_FAILURE, cmd.main(&ui, args, engine::default_config()));
180 
181     ATF_REQUIRE_EQ(0, ui.out_log().size());
182 
183     ATF_REQUIRE(!atf::utils::grep_collection("Failed to open.*AUTHORS",
184                                              ui.err_log()));
185     ATF_REQUIRE(atf::utils::grep_collection("Failed to open.*COPYING",
186                                             ui.err_log()));
187 }
188 
189 
190 ATF_TEST_CASE_WITHOUT_HEAD(topic_version__ok);
191 ATF_TEST_CASE_BODY(topic_version__ok)
192 {
193     cmdline::args_vector args;
194     args.push_back("about");
195     args.push_back("version");
196 
197     utils::setenv("KYUA_DOCDIR", "fake-docs");
198     cmd_about cmd;
199     cmdline::ui_mock ui;
200     ATF_REQUIRE_EQ(EXIT_SUCCESS, cmd.main(&ui, args, engine::default_config()));
201     ATF_REQUIRE_EQ(1, ui.out_log().size());
202     ATF_REQUIRE(atf::utils::grep_string(PACKAGE_NAME, ui.out_log()[0]));
203     ATF_REQUIRE(atf::utils::grep_string(PACKAGE_VERSION, ui.out_log()[0]));
204     ATF_REQUIRE(ui.err_log().empty());
205 }
206 
207 
208 ATF_TEST_CASE_WITHOUT_HEAD(invalid_args);
209 ATF_TEST_CASE_BODY(invalid_args)
210 {
211     cmdline::args_vector args;
212     args.push_back("about");
213     args.push_back("first");
214     args.push_back("second");
215 
216     cmd_about cmd;
217     cmdline::ui_mock ui;
218     ATF_REQUIRE_THROW_RE(cmdline::usage_error, "Too many arguments",
219                          cmd.main(&ui, args, engine::default_config()));
220     ATF_REQUIRE(ui.out_log().empty());
221     ATF_REQUIRE(ui.err_log().empty());
222 }
223 
224 
225 ATF_TEST_CASE_WITHOUT_HEAD(invalid_topic);
226 ATF_TEST_CASE_BODY(invalid_topic)
227 {
228     cmdline::args_vector args;
229     args.push_back("about");
230     args.push_back("foo");
231 
232     cmd_about cmd;
233     cmdline::ui_mock ui;
234     ATF_REQUIRE_THROW_RE(cmdline::usage_error, "Invalid about topic 'foo'",
235                          cmd.main(&ui, args, engine::default_config()));
236     ATF_REQUIRE(ui.out_log().empty());
237     ATF_REQUIRE(ui.err_log().empty());
238 }
239 
240 
241 ATF_INIT_TEST_CASES(tcs)
242 {
243     ATF_ADD_TEST_CASE(tcs, all_topics__ok);
244     ATF_ADD_TEST_CASE(tcs, all_topics__missing_docs);
245     ATF_ADD_TEST_CASE(tcs, topic_authors__ok);
246     ATF_ADD_TEST_CASE(tcs, topic_authors__missing_doc);
247     ATF_ADD_TEST_CASE(tcs, topic_license__ok);
248     ATF_ADD_TEST_CASE(tcs, topic_license__missing_doc);
249     ATF_ADD_TEST_CASE(tcs, topic_version__ok);
250     ATF_ADD_TEST_CASE(tcs, invalid_args);
251     ATF_ADD_TEST_CASE(tcs, invalid_topic);
252 }
253