Skip to content

Commit

Permalink
[utils] Added file_exists + test #19
Browse files Browse the repository at this point in the history
  • Loading branch information
nemesifier committed May 27, 2016
1 parent 210d3b1 commit c68c5e3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
7 changes: 7 additions & 0 deletions openwisp-config/files/lib/openwisp/utils.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
-- openwisp uci utils
require('io')
require('lfs')

function starts_with_dot(str)
Expand Down Expand Up @@ -127,3 +128,9 @@ function dirtree(dir)
end
return coroutine.wrap(function() yieldtree(dir) end)
end

function file_exists(path)
local f = io.open(path, 'r')
if f ~= nil then io.close(f) return true end
return false
end
5 changes: 5 additions & 0 deletions openwisp-config/tests/test_utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -242,4 +242,9 @@ function TestUtils.test_dirtree()
luaunit.assertEquals(count, 4)
end

function TestUtils.test_file_exists()
luaunit.assertEquals(file_exists('./test_utils.lua'), true)
luaunit.assertEquals(file_exists('./WRONG'), false)
end

os.exit(luaunit.LuaUnit.run())

0 comments on commit c68c5e3

Please sign in to comment.