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 if type(f) == "string" then
local p = os.matchfiles(f) local p = os.matchfiles(f)
for _, v in pairs(p) do for _, v in pairs(p) do
local ok, err = builtin_remove(v) local ok, err, code = builtin_remove(v)
if not ok then if not ok then
return ok, err return ok, err, code
end end
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 -- in case of table, match files for every table entry
elseif type(f) == "table" then elseif type(f) == "table" then
for _, v in pairs(f) do for _, v in pairs(f) do
local ok, err = os.remove(v) local ok, err, code = os.remove(v)
if not ok then if not ok then
return ok, err return ok, err, code
end end
end end
end end
return true
end end
-- --
-- Remove a directory, along with any contained files or subdirectories. -- 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 local builtin_rmdir = os.rmdir
function os.rmdir(p) function os.rmdir(p)
-- recursively remove subdirectories -- recursively remove subdirectories
local dirs = os.matchdirs(p .. "/*") local dirs = os.matchdirs(p .. "/*")
for _, dname in ipairs(dirs) do for _, dname in ipairs(dirs) do
os.rmdir(dname) local ok, err = os.rmdir(dname)
if not ok then
return ok, err
end
end end
-- remove any files -- remove any files
local files = os.matchfiles(p .. "/*") local files = os.matchfiles(p .. "/*")
for _, fname in ipairs(files) do for _, fname in ipairs(files) do
os.remove(fname) local ok, err = os.remove(fname)
if not ok then
return ok, err
end
end end
-- remove this directory -- remove this directory
builtin_rmdir(p) return builtin_rmdir(p)
end end

View File

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

View File

@ -1,6 +1,6 @@
/** /**
* \file os_rmdir.c * \file os_rename.c
* \brief Remove a subdirectory. * \brief Rename a path on Windows.
* \author Copyright (c) 2002-2013 Jason Perkins and the Premake project * \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')) test.isequal('cmdtool "../foo/path1" "../foo/path2/"', os.translateCommandsAndPaths("cmdtool %[path1] %[path2/]", '../foo', '.', 'osx'))
end 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