Path: blob/main/contrib/kyua/utils/fs/lua_module_test.cpp
48081 views
// Copyright 2011 The Kyua Authors.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions are5// met:6//7// * Redistributions of source code must retain the above copyright8// notice, this list of conditions and the following disclaimer.9// * Redistributions in binary form must reproduce the above copyright10// notice, this list of conditions and the following disclaimer in the11// documentation and/or other materials provided with the distribution.12// * Neither the name of Google Inc. nor the names of its contributors13// may be used to endorse or promote products derived from this software14// without specific prior written permission.15//16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2728#include "utils/fs/lua_module.hpp"2930#include <atf-c++.hpp>31#include <lutok/operations.hpp>32#include <lutok/state.hpp>33#include <lutok/test_utils.hpp>3435#include "utils/format/macros.hpp"36#include "utils/fs/operations.hpp"37#include "utils/fs/path.hpp"3839namespace fs = utils::fs;404142ATF_TEST_CASE_WITHOUT_HEAD(open_fs);43ATF_TEST_CASE_BODY(open_fs)44{45lutok::state state;46stack_balance_checker checker(state);47fs::open_fs(state);48lutok::do_string(state, "return fs.basename", 0, 1, 0);49ATF_REQUIRE(state.is_function(-1));50lutok::do_string(state, "return fs.dirname", 0, 1, 0);51ATF_REQUIRE(state.is_function(-1));52lutok::do_string(state, "return fs.join", 0, 1, 0);53ATF_REQUIRE(state.is_function(-1));54state.pop(3);55}565758ATF_TEST_CASE_WITHOUT_HEAD(basename__ok);59ATF_TEST_CASE_BODY(basename__ok)60{61lutok::state state;62fs::open_fs(state);6364lutok::do_string(state, "return fs.basename('/my/test//file_foobar')",650, 1, 0);66ATF_REQUIRE_EQ("file_foobar", state.to_string(-1));67state.pop(1);68}697071ATF_TEST_CASE_WITHOUT_HEAD(basename__fail);72ATF_TEST_CASE_BODY(basename__fail)73{74lutok::state state;75fs::open_fs(state);7677ATF_REQUIRE_THROW_RE(lutok::error, "Need a string",78lutok::do_string(state, "return fs.basename({})",790, 1, 0));80ATF_REQUIRE_THROW_RE(lutok::error, "Invalid path",81lutok::do_string(state, "return fs.basename('')",820, 1, 0));83}848586ATF_TEST_CASE_WITHOUT_HEAD(dirname__ok);87ATF_TEST_CASE_BODY(dirname__ok)88{89lutok::state state;90fs::open_fs(state);9192lutok::do_string(state, "return fs.dirname('/my/test//file_foobar')",930, 1, 0);94ATF_REQUIRE_EQ("/my/test", state.to_string(-1));95state.pop(1);96}979899ATF_TEST_CASE_WITHOUT_HEAD(dirname__fail);100ATF_TEST_CASE_BODY(dirname__fail)101{102lutok::state state;103fs::open_fs(state);104105ATF_REQUIRE_THROW_RE(lutok::error, "Need a string",106lutok::do_string(state, "return fs.dirname({})",1070, 1, 0));108ATF_REQUIRE_THROW_RE(lutok::error, "Invalid path",109lutok::do_string(state, "return fs.dirname('')",1100, 1, 0));111}112113114ATF_TEST_CASE_WITHOUT_HEAD(exists__ok);115ATF_TEST_CASE_BODY(exists__ok)116{117lutok::state state;118fs::open_fs(state);119120atf::utils::create_file("foo", "");121122lutok::do_string(state, "return fs.exists('foo')", 0, 1, 0);123ATF_REQUIRE(state.to_boolean(-1));124state.pop(1);125126lutok::do_string(state, "return fs.exists('bar')", 0, 1, 0);127ATF_REQUIRE(!state.to_boolean(-1));128state.pop(1);129130lutok::do_string(state,131F("return fs.exists('%s')") % fs::current_path(), 0, 1, 0);132ATF_REQUIRE(state.to_boolean(-1));133state.pop(1);134}135136137ATF_TEST_CASE_WITHOUT_HEAD(exists__fail);138ATF_TEST_CASE_BODY(exists__fail)139{140lutok::state state;141fs::open_fs(state);142143ATF_REQUIRE_THROW_RE(lutok::error, "Need a string",144lutok::do_string(state, "return fs.exists({})",1450, 1, 0));146ATF_REQUIRE_THROW_RE(lutok::error, "Invalid path",147lutok::do_string(state, "return fs.exists('')",1480, 1, 0));149}150151152ATF_TEST_CASE_WITHOUT_HEAD(exists__custom_start_dir);153ATF_TEST_CASE_BODY(exists__custom_start_dir)154{155lutok::state state;156fs::open_fs(state, fs::path("subdir"));157158fs::mkdir(fs::path("subdir"), 0755);159atf::utils::create_file("subdir/foo", "");160atf::utils::create_file("bar", "");161162lutok::do_string(state, "return fs.exists('foo')", 0, 1, 0);163ATF_REQUIRE(state.to_boolean(-1));164state.pop(1);165166lutok::do_string(state, "return fs.exists('subdir/foo')", 0, 1, 0);167ATF_REQUIRE(!state.to_boolean(-1));168state.pop(1);169170lutok::do_string(state, "return fs.exists('bar')", 0, 1, 0);171ATF_REQUIRE(!state.to_boolean(-1));172state.pop(1);173174lutok::do_string(state, "return fs.exists('../bar')", 0, 1, 0);175ATF_REQUIRE(state.to_boolean(-1));176state.pop(1);177178lutok::do_string(state,179F("return fs.exists('%s')") % (fs::current_path() / "bar"),1800, 1, 0);181ATF_REQUIRE(state.to_boolean(-1));182state.pop(1);183}184185186ATF_TEST_CASE_WITHOUT_HEAD(files__none);187ATF_TEST_CASE_BODY(files__none)188{189lutok::state state;190state.open_table();191fs::open_fs(state);192193fs::mkdir(fs::path("root"), 0755);194195lutok::do_string(state,196"names = {}\n"197"for file in fs.files('root') do\n"198" table.insert(names, file)\n"199"end\n"200"table.sort(names)\n"201"return table.concat(names, ' ')",2020, 1, 0);203ATF_REQUIRE_EQ(". ..", state.to_string(-1));204state.pop(1);205}206207208ATF_TEST_CASE_WITHOUT_HEAD(files__some);209ATF_TEST_CASE_BODY(files__some)210{211lutok::state state;212state.open_table();213fs::open_fs(state);214215fs::mkdir(fs::path("root"), 0755);216atf::utils::create_file("root/file1", "");217atf::utils::create_file("root/file2", "");218219lutok::do_string(state,220"names = {}\n"221"for file in fs.files('root') do\n"222" table.insert(names, file)\n"223"end\n"224"table.sort(names)\n"225"return table.concat(names, ' ')",2260, 1, 0);227ATF_REQUIRE_EQ(". .. file1 file2", state.to_string(-1));228state.pop(1);229}230231232ATF_TEST_CASE_WITHOUT_HEAD(files__some_with_custom_start_dir);233ATF_TEST_CASE_BODY(files__some_with_custom_start_dir)234{235lutok::state state;236state.open_table();237fs::open_fs(state, fs::current_path() / "root");238239fs::mkdir(fs::path("root"), 0755);240atf::utils::create_file("root/file1", "");241atf::utils::create_file("root/file2", "");242atf::utils::create_file("file3", "");243244lutok::do_string(state,245"names = {}\n"246"for file in fs.files('.') do\n"247" table.insert(names, file)\n"248"end\n"249"table.sort(names)\n"250"return table.concat(names, ' ')",2510, 1, 0);252ATF_REQUIRE_EQ(". .. file1 file2", state.to_string(-1));253state.pop(1);254}255256257ATF_TEST_CASE_WITHOUT_HEAD(files__fail_arg);258ATF_TEST_CASE_BODY(files__fail_arg)259{260lutok::state state;261fs::open_fs(state);262263ATF_REQUIRE_THROW_RE(lutok::error, "Need a string parameter",264lutok::do_string(state, "fs.files({})", 0, 0, 0));265ATF_REQUIRE_THROW_RE(lutok::error, "Invalid path",266lutok::do_string(state, "fs.files('')", 0, 0, 0));267}268269270ATF_TEST_CASE_WITHOUT_HEAD(files__fail_opendir);271ATF_TEST_CASE_BODY(files__fail_opendir)272{273lutok::state state;274fs::open_fs(state);275276ATF_REQUIRE_THROW_RE(lutok::error, "Failed to open directory",277lutok::do_string(state, "fs.files('root')", 0, 0, 0));278}279280281ATF_TEST_CASE_WITHOUT_HEAD(is_absolute__ok);282ATF_TEST_CASE_BODY(is_absolute__ok)283{284lutok::state state;285fs::open_fs(state);286287lutok::do_string(state, "return fs.is_absolute('my/test//file_foobar')",2880, 1, 0);289ATF_REQUIRE(!state.to_boolean(-1));290lutok::do_string(state, "return fs.is_absolute('/my/test//file_foobar')",2910, 1, 0);292ATF_REQUIRE(state.to_boolean(-1));293state.pop(2);294}295296297ATF_TEST_CASE_WITHOUT_HEAD(is_absolute__fail);298ATF_TEST_CASE_BODY(is_absolute__fail)299{300lutok::state state;301fs::open_fs(state);302303ATF_REQUIRE_THROW_RE(lutok::error, "Need a string",304lutok::do_string(state, "return fs.is_absolute({})",3050, 1, 0));306ATF_REQUIRE_THROW_RE(lutok::error, "Invalid path",307lutok::do_string(state, "return fs.is_absolute('')",3080, 1, 0));309}310311312ATF_TEST_CASE_WITHOUT_HEAD(join__ok);313ATF_TEST_CASE_BODY(join__ok)314{315lutok::state state;316fs::open_fs(state);317318lutok::do_string(state, "return fs.join('/a/b///', 'c/d')", 0, 1, 0);319ATF_REQUIRE_EQ("/a/b/c/d", state.to_string(-1));320state.pop(1);321}322323324ATF_TEST_CASE_WITHOUT_HEAD(join__fail);325ATF_TEST_CASE_BODY(join__fail)326{327lutok::state state;328fs::open_fs(state);329330ATF_REQUIRE_THROW_RE(lutok::error, "Need a string",331lutok::do_string(state, "return fs.join({}, 'a')",3320, 1, 0));333ATF_REQUIRE_THROW_RE(lutok::error, "Need a string",334lutok::do_string(state, "return fs.join('a', {})",3350, 1, 0));336337ATF_REQUIRE_THROW_RE(lutok::error, "Invalid path",338lutok::do_string(state, "return fs.join('', 'a')",3390, 1, 0));340ATF_REQUIRE_THROW_RE(lutok::error, "Invalid path",341lutok::do_string(state, "return fs.join('a', '')",3420, 1, 0));343344ATF_REQUIRE_THROW_RE(lutok::error, "Cannot join.*'a/b'.*'/c'",345lutok::do_string(state, "fs.join('a/b', '/c')",3460, 0, 0));347}348349350ATF_INIT_TEST_CASES(tcs)351{352ATF_ADD_TEST_CASE(tcs, open_fs);353354ATF_ADD_TEST_CASE(tcs, basename__ok);355ATF_ADD_TEST_CASE(tcs, basename__fail);356357ATF_ADD_TEST_CASE(tcs, dirname__ok);358ATF_ADD_TEST_CASE(tcs, dirname__fail);359360ATF_ADD_TEST_CASE(tcs, exists__ok);361ATF_ADD_TEST_CASE(tcs, exists__fail);362ATF_ADD_TEST_CASE(tcs, exists__custom_start_dir);363364ATF_ADD_TEST_CASE(tcs, files__none);365ATF_ADD_TEST_CASE(tcs, files__some);366ATF_ADD_TEST_CASE(tcs, files__some_with_custom_start_dir);367ATF_ADD_TEST_CASE(tcs, files__fail_arg);368ATF_ADD_TEST_CASE(tcs, files__fail_opendir);369370ATF_ADD_TEST_CASE(tcs, is_absolute__ok);371ATF_ADD_TEST_CASE(tcs, is_absolute__fail);372373ATF_ADD_TEST_CASE(tcs, join__ok);374ATF_ADD_TEST_CASE(tcs, join__fail);375}376377378