From c68c5e30a7acaeb3fbb46152c09f683c337456ad Mon Sep 17 00:00:00 2001 From: Federico Capoano Date: Fri, 27 May 2016 17:08:22 +0200 Subject: [PATCH] [utils] Added file_exists + test #19 --- openwisp-config/files/lib/openwisp/utils.lua | 7 +++++++ openwisp-config/tests/test_utils.lua | 5 +++++ 2 files changed, 12 insertions(+) diff --git a/openwisp-config/files/lib/openwisp/utils.lua b/openwisp-config/files/lib/openwisp/utils.lua index c22b411..5d336e8 100644 --- a/openwisp-config/files/lib/openwisp/utils.lua +++ b/openwisp-config/files/lib/openwisp/utils.lua @@ -1,4 +1,5 @@ -- openwisp uci utils +require('io') require('lfs') function starts_with_dot(str) @@ -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 diff --git a/openwisp-config/tests/test_utils.lua b/openwisp-config/tests/test_utils.lua index 4e0e613..d38732f 100644 --- a/openwisp-config/tests/test_utils.lua +++ b/openwisp-config/tests/test_utils.lua @@ -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())