Merge pull request #1360 from sp-jordi-vilalta/feature/fix-os-return

Fix the return value of some os functions
This commit is contained in:
Samuel Surtees 2019-11-11 00:07:15 +10:00 committed by GitHub
commit fe962f898c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 100 additions and 11 deletions

View File

@ -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

View File

@ -1,6 +1,6 @@
/**
* \file os_rmdir.c
* \brief Remove a subdirectory.
* \file os_remove.c
* \brief Remove a file on Windows.
* \author Copyright (c) 2002-2013 Jason Perkins and the Premake project
*/

View File

@ -1,6 +1,6 @@
/**
* \file os_rmdir.c
* \brief Remove a subdirectory.
* \file os_rename.c
* \brief Rename a path on Windows.
* \author Copyright (c) 2002-2013 Jason Perkins and the Premake project
*/

View File

@ -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