xref: /minix3/external/bsd/kyua-cli/dist/utils/fs/lua_module_test.cpp (revision 11be35a165022172ed3cea20f2b5df0307540b0e)
1*11be35a1SLionel Sambuc // Copyright 2011 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 "utils/fs/lua_module.hpp"
30*11be35a1SLionel Sambuc 
31*11be35a1SLionel Sambuc #include <atf-c++.hpp>
32*11be35a1SLionel Sambuc #include <lutok/operations.hpp>
33*11be35a1SLionel Sambuc #include <lutok/state.hpp>
34*11be35a1SLionel Sambuc #include <lutok/test_utils.hpp>
35*11be35a1SLionel Sambuc 
36*11be35a1SLionel Sambuc #include "utils/fs/operations.hpp"
37*11be35a1SLionel Sambuc #include "utils/fs/path.hpp"
38*11be35a1SLionel Sambuc 
39*11be35a1SLionel Sambuc namespace fs = utils::fs;
40*11be35a1SLionel Sambuc 
41*11be35a1SLionel Sambuc 
42*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(open_fs);
ATF_TEST_CASE_BODY(open_fs)43*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(open_fs)
44*11be35a1SLionel Sambuc {
45*11be35a1SLionel Sambuc     lutok::state state;
46*11be35a1SLionel Sambuc     stack_balance_checker checker(state);
47*11be35a1SLionel Sambuc     fs::open_fs(state);
48*11be35a1SLionel Sambuc     lutok::do_string(state, "return fs.basename", 1);
49*11be35a1SLionel Sambuc     ATF_REQUIRE(state.is_function());
50*11be35a1SLionel Sambuc     lutok::do_string(state, "return fs.dirname", 1);
51*11be35a1SLionel Sambuc     ATF_REQUIRE(state.is_function());
52*11be35a1SLionel Sambuc     lutok::do_string(state, "return fs.join", 1);
53*11be35a1SLionel Sambuc     ATF_REQUIRE(state.is_function());
54*11be35a1SLionel Sambuc     state.pop(3);
55*11be35a1SLionel Sambuc }
56*11be35a1SLionel Sambuc 
57*11be35a1SLionel Sambuc 
58*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(basename__ok);
ATF_TEST_CASE_BODY(basename__ok)59*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(basename__ok)
60*11be35a1SLionel Sambuc {
61*11be35a1SLionel Sambuc     lutok::state state;
62*11be35a1SLionel Sambuc     fs::open_fs(state);
63*11be35a1SLionel Sambuc 
64*11be35a1SLionel Sambuc     lutok::do_string(state, "return fs.basename('/my/test//file_foobar')", 1);
65*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ("file_foobar", state.to_string());
66*11be35a1SLionel Sambuc     state.pop(1);
67*11be35a1SLionel Sambuc }
68*11be35a1SLionel Sambuc 
69*11be35a1SLionel Sambuc 
70*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(basename__fail);
ATF_TEST_CASE_BODY(basename__fail)71*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(basename__fail)
72*11be35a1SLionel Sambuc {
73*11be35a1SLionel Sambuc     lutok::state state;
74*11be35a1SLionel Sambuc     fs::open_fs(state);
75*11be35a1SLionel Sambuc 
76*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW_RE(lutok::error, "Need a string",
77*11be35a1SLionel Sambuc                          lutok::do_string(state, "return fs.basename({})", 1));
78*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW_RE(lutok::error, "Invalid path",
79*11be35a1SLionel Sambuc                          lutok::do_string(state, "return fs.basename('')", 1));
80*11be35a1SLionel Sambuc }
81*11be35a1SLionel Sambuc 
82*11be35a1SLionel Sambuc 
83*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(dirname__ok);
ATF_TEST_CASE_BODY(dirname__ok)84*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(dirname__ok)
85*11be35a1SLionel Sambuc {
86*11be35a1SLionel Sambuc     lutok::state state;
87*11be35a1SLionel Sambuc     fs::open_fs(state);
88*11be35a1SLionel Sambuc 
89*11be35a1SLionel Sambuc     lutok::do_string(state, "return fs.dirname('/my/test//file_foobar')", 1);
90*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ("/my/test", state.to_string());
91*11be35a1SLionel Sambuc     state.pop(1);
92*11be35a1SLionel Sambuc }
93*11be35a1SLionel Sambuc 
94*11be35a1SLionel Sambuc 
95*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(dirname__fail);
ATF_TEST_CASE_BODY(dirname__fail)96*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(dirname__fail)
97*11be35a1SLionel Sambuc {
98*11be35a1SLionel Sambuc     lutok::state state;
99*11be35a1SLionel Sambuc     fs::open_fs(state);
100*11be35a1SLionel Sambuc 
101*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW_RE(lutok::error, "Need a string",
102*11be35a1SLionel Sambuc                          lutok::do_string(state, "return fs.dirname({})", 1));
103*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW_RE(lutok::error, "Invalid path",
104*11be35a1SLionel Sambuc                          lutok::do_string(state, "return fs.dirname('')", 1));
105*11be35a1SLionel Sambuc }
106*11be35a1SLionel Sambuc 
107*11be35a1SLionel Sambuc 
108*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(exists__ok);
ATF_TEST_CASE_BODY(exists__ok)109*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(exists__ok)
110*11be35a1SLionel Sambuc {
111*11be35a1SLionel Sambuc     lutok::state state;
112*11be35a1SLionel Sambuc     fs::open_fs(state);
113*11be35a1SLionel Sambuc 
114*11be35a1SLionel Sambuc     atf::utils::create_file("foo", "");
115*11be35a1SLionel Sambuc 
116*11be35a1SLionel Sambuc     lutok::do_string(state, "return fs.exists('foo')", 1);
117*11be35a1SLionel Sambuc     ATF_REQUIRE(state.to_boolean());
118*11be35a1SLionel Sambuc     state.pop(1);
119*11be35a1SLionel Sambuc 
120*11be35a1SLionel Sambuc     lutok::do_string(state, "return fs.exists('bar')", 1);
121*11be35a1SLionel Sambuc     ATF_REQUIRE(!state.to_boolean());
122*11be35a1SLionel Sambuc     state.pop(1);
123*11be35a1SLionel Sambuc }
124*11be35a1SLionel Sambuc 
125*11be35a1SLionel Sambuc 
126*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(exists__fail);
ATF_TEST_CASE_BODY(exists__fail)127*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(exists__fail)
128*11be35a1SLionel Sambuc {
129*11be35a1SLionel Sambuc     lutok::state state;
130*11be35a1SLionel Sambuc     fs::open_fs(state);
131*11be35a1SLionel Sambuc 
132*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW_RE(lutok::error, "Need a string",
133*11be35a1SLionel Sambuc                          lutok::do_string(state, "return fs.exists({})", 1));
134*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW_RE(lutok::error, "Invalid path",
135*11be35a1SLionel Sambuc                          lutok::do_string(state, "return fs.exists('')", 1));
136*11be35a1SLionel Sambuc }
137*11be35a1SLionel Sambuc 
138*11be35a1SLionel Sambuc 
139*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(files__none);
ATF_TEST_CASE_BODY(files__none)140*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(files__none)
141*11be35a1SLionel Sambuc {
142*11be35a1SLionel Sambuc     lutok::state state;
143*11be35a1SLionel Sambuc     state.open_table();
144*11be35a1SLionel Sambuc     fs::open_fs(state);
145*11be35a1SLionel Sambuc 
146*11be35a1SLionel Sambuc     fs::mkdir(fs::path("root"), 0755);
147*11be35a1SLionel Sambuc 
148*11be35a1SLionel Sambuc     lutok::do_string(state,
149*11be35a1SLionel Sambuc                      "names = {}\n"
150*11be35a1SLionel Sambuc                      "for file in fs.files('root') do\n"
151*11be35a1SLionel Sambuc                      "    table.insert(names, file)\n"
152*11be35a1SLionel Sambuc                      "end\n"
153*11be35a1SLionel Sambuc                      "table.sort(names)\n"
154*11be35a1SLionel Sambuc                      "return table.concat(names, ' ')", 1);
155*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(". ..", state.to_string());
156*11be35a1SLionel Sambuc     state.pop(1);
157*11be35a1SLionel Sambuc }
158*11be35a1SLionel Sambuc 
159*11be35a1SLionel Sambuc 
160*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(files__some);
ATF_TEST_CASE_BODY(files__some)161*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(files__some)
162*11be35a1SLionel Sambuc {
163*11be35a1SLionel Sambuc     lutok::state state;
164*11be35a1SLionel Sambuc     state.open_table();
165*11be35a1SLionel Sambuc     fs::open_fs(state);
166*11be35a1SLionel Sambuc 
167*11be35a1SLionel Sambuc     fs::mkdir(fs::path("root"), 0755);
168*11be35a1SLionel Sambuc     atf::utils::create_file("root/file1", "");
169*11be35a1SLionel Sambuc     atf::utils::create_file("root/file2", "");
170*11be35a1SLionel Sambuc 
171*11be35a1SLionel Sambuc     lutok::do_string(state,
172*11be35a1SLionel Sambuc                      "names = {}\n"
173*11be35a1SLionel Sambuc                      "for file in fs.files('root') do\n"
174*11be35a1SLionel Sambuc                      "    table.insert(names, file)\n"
175*11be35a1SLionel Sambuc                      "end\n"
176*11be35a1SLionel Sambuc                      "table.sort(names)\n"
177*11be35a1SLionel Sambuc                      "return table.concat(names, ' ')", 1);
178*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(". .. file1 file2", state.to_string());
179*11be35a1SLionel Sambuc     state.pop(1);
180*11be35a1SLionel Sambuc }
181*11be35a1SLionel Sambuc 
182*11be35a1SLionel Sambuc 
183*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(files__fail_arg);
ATF_TEST_CASE_BODY(files__fail_arg)184*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(files__fail_arg)
185*11be35a1SLionel Sambuc {
186*11be35a1SLionel Sambuc     lutok::state state;
187*11be35a1SLionel Sambuc     fs::open_fs(state);
188*11be35a1SLionel Sambuc 
189*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW_RE(lutok::error, "Need a string parameter",
190*11be35a1SLionel Sambuc                          lutok::do_string(state, "fs.files({})"));
191*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW_RE(lutok::error, "Invalid path",
192*11be35a1SLionel Sambuc                          lutok::do_string(state, "fs.files('')"));
193*11be35a1SLionel Sambuc }
194*11be35a1SLionel Sambuc 
195*11be35a1SLionel Sambuc 
196*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(files__fail_opendir);
ATF_TEST_CASE_BODY(files__fail_opendir)197*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(files__fail_opendir)
198*11be35a1SLionel Sambuc {
199*11be35a1SLionel Sambuc     lutok::state state;
200*11be35a1SLionel Sambuc     fs::open_fs(state);
201*11be35a1SLionel Sambuc 
202*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW_RE(lutok::error, "Failed to open directory",
203*11be35a1SLionel Sambuc                          lutok::do_string(state, "fs.files('root')"));
204*11be35a1SLionel Sambuc }
205*11be35a1SLionel Sambuc 
206*11be35a1SLionel Sambuc 
207*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(is_absolute__ok);
ATF_TEST_CASE_BODY(is_absolute__ok)208*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(is_absolute__ok)
209*11be35a1SLionel Sambuc {
210*11be35a1SLionel Sambuc     lutok::state state;
211*11be35a1SLionel Sambuc     fs::open_fs(state);
212*11be35a1SLionel Sambuc 
213*11be35a1SLionel Sambuc     lutok::do_string(state, "return fs.is_absolute('my/test//file_foobar')", 1);
214*11be35a1SLionel Sambuc     ATF_REQUIRE(!state.to_boolean());
215*11be35a1SLionel Sambuc     lutok::do_string(state, "return fs.is_absolute('/my/test//file_foobar')", 1);
216*11be35a1SLionel Sambuc     ATF_REQUIRE(state.to_boolean());
217*11be35a1SLionel Sambuc     state.pop(2);
218*11be35a1SLionel Sambuc }
219*11be35a1SLionel Sambuc 
220*11be35a1SLionel Sambuc 
221*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(is_absolute__fail);
ATF_TEST_CASE_BODY(is_absolute__fail)222*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(is_absolute__fail)
223*11be35a1SLionel Sambuc {
224*11be35a1SLionel Sambuc     lutok::state state;
225*11be35a1SLionel Sambuc     fs::open_fs(state);
226*11be35a1SLionel Sambuc 
227*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW_RE(lutok::error, "Need a string",
228*11be35a1SLionel Sambuc                          lutok::do_string(state, "return fs.is_absolute({})", 1));
229*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW_RE(lutok::error, "Invalid path",
230*11be35a1SLionel Sambuc                          lutok::do_string(state, "return fs.is_absolute('')", 1));
231*11be35a1SLionel Sambuc }
232*11be35a1SLionel Sambuc 
233*11be35a1SLionel Sambuc 
234*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(join__ok);
ATF_TEST_CASE_BODY(join__ok)235*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(join__ok)
236*11be35a1SLionel Sambuc {
237*11be35a1SLionel Sambuc     lutok::state state;
238*11be35a1SLionel Sambuc     fs::open_fs(state);
239*11be35a1SLionel Sambuc 
240*11be35a1SLionel Sambuc     lutok::do_string(state, "return fs.join('/a/b///', 'c/d')", 1);
241*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ("/a/b/c/d", state.to_string());
242*11be35a1SLionel Sambuc     state.pop(1);
243*11be35a1SLionel Sambuc }
244*11be35a1SLionel Sambuc 
245*11be35a1SLionel Sambuc 
246*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(join__fail);
ATF_TEST_CASE_BODY(join__fail)247*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(join__fail)
248*11be35a1SLionel Sambuc {
249*11be35a1SLionel Sambuc     lutok::state state;
250*11be35a1SLionel Sambuc     fs::open_fs(state);
251*11be35a1SLionel Sambuc 
252*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW_RE(lutok::error, "Need a string",
253*11be35a1SLionel Sambuc                          lutok::do_string(state, "return fs.join({}, 'a')", 1));
254*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW_RE(lutok::error, "Need a string",
255*11be35a1SLionel Sambuc                          lutok::do_string(state, "return fs.join('a', {})", 1));
256*11be35a1SLionel Sambuc 
257*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW_RE(lutok::error, "Invalid path",
258*11be35a1SLionel Sambuc                          lutok::do_string(state, "return fs.join('', 'a')", 1));
259*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW_RE(lutok::error, "Invalid path",
260*11be35a1SLionel Sambuc                          lutok::do_string(state, "return fs.join('a', '')", 1));
261*11be35a1SLionel Sambuc 
262*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW_RE(lutok::error, "Cannot join.*'a/b'.*'/c'",
263*11be35a1SLionel Sambuc                          lutok::do_string(state, "fs.join('a/b', '/c')"));
264*11be35a1SLionel Sambuc }
265*11be35a1SLionel Sambuc 
266*11be35a1SLionel Sambuc 
ATF_INIT_TEST_CASES(tcs)267*11be35a1SLionel Sambuc ATF_INIT_TEST_CASES(tcs)
268*11be35a1SLionel Sambuc {
269*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, open_fs);
270*11be35a1SLionel Sambuc 
271*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, basename__ok);
272*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, basename__fail);
273*11be35a1SLionel Sambuc 
274*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, dirname__ok);
275*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, dirname__fail);
276*11be35a1SLionel Sambuc 
277*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, exists__ok);
278*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, exists__fail);
279*11be35a1SLionel Sambuc 
280*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, files__none);
281*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, files__some);
282*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, files__fail_arg);
283*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, files__fail_opendir);
284*11be35a1SLionel Sambuc 
285*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, is_absolute__ok);
286*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, is_absolute__fail);
287*11be35a1SLionel Sambuc 
288*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, join__ok);
289*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, join__fail);
290*11be35a1SLionel Sambuc }
291