1 // Copyright 2011 Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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.
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.
29 #include "utils/fs/lua_module.hpp"
31 #include <atf-c++.hpp>
32 #include <lutok/operations.hpp>
33 #include <lutok/state.hpp>
34 #include <lutok/test_utils.hpp>
36 #include "utils/fs/operations.hpp"
37 #include "utils/fs/path.hpp"
39 namespace fs
= utils::fs
;
42 ATF_TEST_CASE_WITHOUT_HEAD(open_fs
);
43 ATF_TEST_CASE_BODY(open_fs
)
46 stack_balance_checker
checker(state
);
48 lutok::do_string(state
, "return fs.basename", 1);
49 ATF_REQUIRE(state
.is_function());
50 lutok::do_string(state
, "return fs.dirname", 1);
51 ATF_REQUIRE(state
.is_function());
52 lutok::do_string(state
, "return fs.join", 1);
53 ATF_REQUIRE(state
.is_function());
58 ATF_TEST_CASE_WITHOUT_HEAD(basename__ok
);
59 ATF_TEST_CASE_BODY(basename__ok
)
64 lutok::do_string(state
, "return fs.basename('/my/test//file_foobar')", 1);
65 ATF_REQUIRE_EQ("file_foobar", state
.to_string());
70 ATF_TEST_CASE_WITHOUT_HEAD(basename__fail
);
71 ATF_TEST_CASE_BODY(basename__fail
)
76 ATF_REQUIRE_THROW_RE(lutok::error
, "Need a string",
77 lutok::do_string(state
, "return fs.basename({})", 1));
78 ATF_REQUIRE_THROW_RE(lutok::error
, "Invalid path",
79 lutok::do_string(state
, "return fs.basename('')", 1));
83 ATF_TEST_CASE_WITHOUT_HEAD(dirname__ok
);
84 ATF_TEST_CASE_BODY(dirname__ok
)
89 lutok::do_string(state
, "return fs.dirname('/my/test//file_foobar')", 1);
90 ATF_REQUIRE_EQ("/my/test", state
.to_string());
95 ATF_TEST_CASE_WITHOUT_HEAD(dirname__fail
);
96 ATF_TEST_CASE_BODY(dirname__fail
)
101 ATF_REQUIRE_THROW_RE(lutok::error
, "Need a string",
102 lutok::do_string(state
, "return fs.dirname({})", 1));
103 ATF_REQUIRE_THROW_RE(lutok::error
, "Invalid path",
104 lutok::do_string(state
, "return fs.dirname('')", 1));
108 ATF_TEST_CASE_WITHOUT_HEAD(exists__ok
);
109 ATF_TEST_CASE_BODY(exists__ok
)
114 atf::utils::create_file("foo", "");
116 lutok::do_string(state
, "return fs.exists('foo')", 1);
117 ATF_REQUIRE(state
.to_boolean());
120 lutok::do_string(state
, "return fs.exists('bar')", 1);
121 ATF_REQUIRE(!state
.to_boolean());
126 ATF_TEST_CASE_WITHOUT_HEAD(exists__fail
);
127 ATF_TEST_CASE_BODY(exists__fail
)
132 ATF_REQUIRE_THROW_RE(lutok::error
, "Need a string",
133 lutok::do_string(state
, "return fs.exists({})", 1));
134 ATF_REQUIRE_THROW_RE(lutok::error
, "Invalid path",
135 lutok::do_string(state
, "return fs.exists('')", 1));
139 ATF_TEST_CASE_WITHOUT_HEAD(files__none
);
140 ATF_TEST_CASE_BODY(files__none
)
146 fs::mkdir(fs::path("root"), 0755);
148 lutok::do_string(state
,
150 "for file in fs.files('root') do\n"
151 " table.insert(names, file)\n"
153 "table.sort(names)\n"
154 "return table.concat(names, ' ')", 1);
155 ATF_REQUIRE_EQ(". ..", state
.to_string());
160 ATF_TEST_CASE_WITHOUT_HEAD(files__some
);
161 ATF_TEST_CASE_BODY(files__some
)
167 fs::mkdir(fs::path("root"), 0755);
168 atf::utils::create_file("root/file1", "");
169 atf::utils::create_file("root/file2", "");
171 lutok::do_string(state
,
173 "for file in fs.files('root') do\n"
174 " table.insert(names, file)\n"
176 "table.sort(names)\n"
177 "return table.concat(names, ' ')", 1);
178 ATF_REQUIRE_EQ(". .. file1 file2", state
.to_string());
183 ATF_TEST_CASE_WITHOUT_HEAD(files__fail_arg
);
184 ATF_TEST_CASE_BODY(files__fail_arg
)
189 ATF_REQUIRE_THROW_RE(lutok::error
, "Need a string parameter",
190 lutok::do_string(state
, "fs.files({})"));
191 ATF_REQUIRE_THROW_RE(lutok::error
, "Invalid path",
192 lutok::do_string(state
, "fs.files('')"));
196 ATF_TEST_CASE_WITHOUT_HEAD(files__fail_opendir
);
197 ATF_TEST_CASE_BODY(files__fail_opendir
)
202 ATF_REQUIRE_THROW_RE(lutok::error
, "Failed to open directory",
203 lutok::do_string(state
, "fs.files('root')"));
207 ATF_TEST_CASE_WITHOUT_HEAD(is_absolute__ok
);
208 ATF_TEST_CASE_BODY(is_absolute__ok
)
213 lutok::do_string(state
, "return fs.is_absolute('my/test//file_foobar')", 1);
214 ATF_REQUIRE(!state
.to_boolean());
215 lutok::do_string(state
, "return fs.is_absolute('/my/test//file_foobar')", 1);
216 ATF_REQUIRE(state
.to_boolean());
221 ATF_TEST_CASE_WITHOUT_HEAD(is_absolute__fail
);
222 ATF_TEST_CASE_BODY(is_absolute__fail
)
227 ATF_REQUIRE_THROW_RE(lutok::error
, "Need a string",
228 lutok::do_string(state
, "return fs.is_absolute({})", 1));
229 ATF_REQUIRE_THROW_RE(lutok::error
, "Invalid path",
230 lutok::do_string(state
, "return fs.is_absolute('')", 1));
234 ATF_TEST_CASE_WITHOUT_HEAD(join__ok
);
235 ATF_TEST_CASE_BODY(join__ok
)
240 lutok::do_string(state
, "return fs.join('/a/b///', 'c/d')", 1);
241 ATF_REQUIRE_EQ("/a/b/c/d", state
.to_string());
246 ATF_TEST_CASE_WITHOUT_HEAD(join__fail
);
247 ATF_TEST_CASE_BODY(join__fail
)
252 ATF_REQUIRE_THROW_RE(lutok::error
, "Need a string",
253 lutok::do_string(state
, "return fs.join({}, 'a')", 1));
254 ATF_REQUIRE_THROW_RE(lutok::error
, "Need a string",
255 lutok::do_string(state
, "return fs.join('a', {})", 1));
257 ATF_REQUIRE_THROW_RE(lutok::error
, "Invalid path",
258 lutok::do_string(state
, "return fs.join('', 'a')", 1));
259 ATF_REQUIRE_THROW_RE(lutok::error
, "Invalid path",
260 lutok::do_string(state
, "return fs.join('a', '')", 1));
262 ATF_REQUIRE_THROW_RE(lutok::error
, "Cannot join.*'a/b'.*'/c'",
263 lutok::do_string(state
, "fs.join('a/b', '/c')"));
267 ATF_INIT_TEST_CASES(tcs
)
269 ATF_ADD_TEST_CASE(tcs
, open_fs
);
271 ATF_ADD_TEST_CASE(tcs
, basename__ok
);
272 ATF_ADD_TEST_CASE(tcs
, basename__fail
);
274 ATF_ADD_TEST_CASE(tcs
, dirname__ok
);
275 ATF_ADD_TEST_CASE(tcs
, dirname__fail
);
277 ATF_ADD_TEST_CASE(tcs
, exists__ok
);
278 ATF_ADD_TEST_CASE(tcs
, exists__fail
);
280 ATF_ADD_TEST_CASE(tcs
, files__none
);
281 ATF_ADD_TEST_CASE(tcs
, files__some
);
282 ATF_ADD_TEST_CASE(tcs
, files__fail_arg
);
283 ATF_ADD_TEST_CASE(tcs
, files__fail_opendir
);
285 ATF_ADD_TEST_CASE(tcs
, is_absolute__ok
);
286 ATF_ADD_TEST_CASE(tcs
, is_absolute__fail
);
288 ATF_ADD_TEST_CASE(tcs
, join__ok
);
289 ATF_ADD_TEST_CASE(tcs
, join__fail
);