Fix the return values of os.rmdir() and os.remove()
This commit is contained in:
parent
55abe47caf
commit
75ceeb50c6
@ -497,43 +497,55 @@
|
||||
if type(f) == "string" then
|
||||
local p = os.matchfiles(f)
|
||||
for _, v in pairs(p) do
|
||||
local ok, err = builtin_remove(v)
|
||||
local ok, err, code = builtin_remove(v)
|
||||
if not ok then
|
||||
return ok, err
|
||||
return ok, err, code
|
||||
end
|
||||
end
|
||||
if #p == 0 then
|
||||
return nil, "Couldn't find any file matching: " .. f, 1
|
||||
end
|
||||
-- in case of table, match files for every table entry
|
||||
elseif type(f) == "table" then
|
||||
for _, v in pairs(f) do
|
||||
local ok, err = os.remove(v)
|
||||
local ok, err, code = os.remove(v)
|
||||
if not ok then
|
||||
return ok, err
|
||||
return ok, err, code
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
|
||||
--
|
||||
-- Remove a directory, along with any contained files or subdirectories.
|
||||
--
|
||||
-- @return true on success, false and an appropriate error message on error
|
||||
|
||||
local builtin_rmdir = os.rmdir
|
||||
function os.rmdir(p)
|
||||
-- recursively remove subdirectories
|
||||
local dirs = os.matchdirs(p .. "/*")
|
||||
for _, dname in ipairs(dirs) do
|
||||
os.rmdir(dname)
|
||||
local ok, err = os.rmdir(dname)
|
||||
if not ok then
|
||||
return ok, err
|
||||
end
|
||||
end
|
||||
|
||||
-- remove any files
|
||||
local files = os.matchfiles(p .. "/*")
|
||||
for _, fname in ipairs(files) do
|
||||
os.remove(fname)
|
||||
local ok, err = os.remove(fname)
|
||||
if not ok then
|
||||
return ok, err
|
||||
end
|
||||
end
|
||||
|
||||
-- remove this directory
|
||||
builtin_rmdir(p)
|
||||
return builtin_rmdir(p)
|
||||
end
|
||||
|
||||
|
||||
|
@ -373,3 +373,80 @@
|
||||
test.isequal('cmdtool "../foo/path1" "../foo/path2/"', os.translateCommandsAndPaths("cmdtool %[path1] %[path2/]", '../foo', '.', 'osx'))
|
||||
end
|
||||
|
||||
|
||||
--
|
||||
-- Helpers
|
||||
--
|
||||
|
||||
local tmpname = function()
|
||||
local p = os.tmpname()
|
||||
os.remove(p) -- just needed on POSIX
|
||||
return p
|
||||
end
|
||||
|
||||
local tmpfile = function()
|
||||
local p = tmpname()
|
||||
if os.ishost("windows") then
|
||||
os.execute("type nul >" .. p)
|
||||
else
|
||||
os.execute("touch " .. p)
|
||||
end
|
||||
return p
|
||||
end
|
||||
|
||||
local tmpdir = function()
|
||||
local p = tmpname()
|
||||
os.mkdir(p)
|
||||
return p
|
||||
end
|
||||
|
||||
|
||||
--
|
||||
-- os.remove() tests.
|
||||
--
|
||||
|
||||
function suite.remove_ReturnsError_OnNonExistingPath()
|
||||
local ok, err, exitcode = os.remove(tmpname())
|
||||
test.isnil(ok)
|
||||
test.isequal("string", type(err))
|
||||
test.isequal("number", type(exitcode))
|
||||
test.istrue(0 ~= exitcode)
|
||||
end
|
||||
|
||||
function suite.remove_ReturnsError_OnDirectory()
|
||||
local ok, err, exitcode = os.remove(tmpdir())
|
||||
test.isnil(ok)
|
||||
test.isequal("string", type(err))
|
||||
test.isequal("number", type(exitcode))
|
||||
test.istrue(0 ~= exitcode)
|
||||
end
|
||||
|
||||
function suite.remove_ReturnsTrue_OnFile()
|
||||
local ok, err, exitcode = os.remove(tmpfile())
|
||||
test.isequal(true, ok)
|
||||
test.isnil(err)
|
||||
test.isnil(exitcode)
|
||||
end
|
||||
|
||||
|
||||
--
|
||||
-- os.rmdir() tests.
|
||||
--
|
||||
|
||||
function suite.rmdir_ReturnsError_OnNonExistingPath()
|
||||
local ok, err = os.rmdir(tmpname())
|
||||
test.isnil(ok)
|
||||
test.isequal("string", type(err))
|
||||
end
|
||||
|
||||
function suite.rmdir_ReturnsError_OnFile()
|
||||
local ok, err = os.rmdir(tmpfile())
|
||||
test.isnil(ok)
|
||||
test.isequal("string", type(err))
|
||||
end
|
||||
|
||||
function suite.rmdir_ReturnsTrue_OnDirectory()
|
||||
local ok, err = os.rmdir(tmpdir())
|
||||
test.isequal(true, ok)
|
||||
test.isnil(err)
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user