Commit acac466e authored by Michal 'vorner' Vaner's avatar Michal 'vorner' Vaner

lua: Tests for the file manipulation utils

With the addition of few test cleanups
parent 7bcb0d4e
......@@ -18,6 +18,11 @@ along with Updater. If not, see <http://www.gnu.org/licenses/>.
]]--
local pairs = pairs
local next = next
local error = error
local unpack = unpack
local events_wait = events_wait
local run_command = run_command
module "utils"
......@@ -49,4 +54,15 @@ function map(table, fun)
return result
end
-- Run rm -rf on all dirs in the provided table
function cleanup_dirs(dirs)
if next(dirs) then
events_wait(run_command(function (ecode, killed, stdout, stderr)
if ecode ~= 0 then
error("rm -rf failed: " .. stderr)
end
end, nil, nil, -1, -1, "/bin/rm", "-rf", unpack(dirs)));
end
end
return _M
......@@ -427,8 +427,11 @@ static int lua_ls(lua_State *L) {
errno = 0;
lua_newtable(L);
while ((ent = readdir(d))) {
lua_pushboolean(L, true);
lua_setfield(L, -2, ent->d_name);
// Skip the . and .. directories
if (strcmp(ent->d_name, "..") && strcmp(ent->d_name, ".")) {
lua_pushboolean(L, true);
lua_setfield(L, -2, ent->d_name);
}
}
int old_errno = errno;
int result = closedir(d);
......
......@@ -383,7 +383,5 @@ function teardown()
-- Clean up, return the original file name
B.status_file = orig_status_file
B.info_dir = orig_info_dir
if next(tmp_dirs) then
events_wait(run_command(function (ecode) assert_equal(0, ecode) end, nil, nil, -1, -1, '/bin/rm', '-rf', unpack(tmp_dirs)))
end
utils.cleanup_dirs(tmp_dirs)
end
......@@ -18,10 +18,14 @@ along with Updater. If not, see <http://www.gnu.org/licenses/>.
]]--
require 'lunit'
require 'utils'
-- Some of the interpreter tests are in C, some are easier written in lua
module("interpreter-tests", package.seeall, lunit.testcase)
local tmp_dirs = {}
-- Test work with working directory
function test_dirs()
local top = os.getenv("S") or "."
chdir(top)
......@@ -30,3 +34,35 @@ function test_dirs()
-- The current directory should be in tests now
assert_equal("/tests", dir:sub(-6))
end
-- Test some FS utilities
function test_fsutils()
local dir = mkdtemp()
table.insert(tmp_dirs, dir)
-- ls on empty directory
assert_table_equal({}, ls(dir))
-- We can create a directory
mkdir(dir .. "/d1")
assert_table_equal({["d1"] = true}, ls(dir))
-- Parent directory doesn't exist
assert_error(function () mkdir(dir .. "/d2/d3") end)
-- Already exists
assert_error(function () mkdir(dir .. "/d1") end)
move(dir .. "/d1", dir .. "/d2")
assert_table_equal({["d2"] = true}, ls(dir))
-- Create a file
local f = io.open(dir .. "/d2/x", "w")
assert(f)
-- The file exists
assert_table_equal({["x"] = true}, ls(dir .. "/d2"))
-- Can't remove non-empty dir
assert_error(function () rmdir(dir .. "/d2") end)
unlink(dir .. "/d2/x")
rmdir(dir .. "/d2")
-- Nothing left now
assert_table_equal({}, ls(dir))
end
function teardown()
utils.cleanup_dirs(tmp_dirs)
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment