Skip to content

Commit

Permalink
[utils] Added basename and dirname #19
Browse files Browse the repository at this point in the history
  • Loading branch information
nemesifier committed May 27, 2016
1 parent 035502c commit ab1a7bb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
17 changes: 17 additions & 0 deletions openwisp-config/files/lib/openwisp/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,23 @@ function split(input, sep)
return t
end

function basename(path)
local parts = split(path, '/')
return parts[table.getn(parts)]
end

function dirname(path)
local parts = split(path, '/')
local path = '/'
local length = table.getn(parts)
for i, part in ipairs(parts) do
if i < length then
path = path..part..'/'
end
end
return path
end

function add_values_to_set(set, values)
for i, el in pairs(values) do
set[el] = true
Expand Down
9 changes: 9 additions & 0 deletions openwisp-config/tests/test_utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -275,4 +275,13 @@ function TestUtils.test_split()
luaunit.assertEquals(split('a/b/c', '/'), t)
end

function TestUtils.test_basename()
luaunit.assertEquals(basename('/etc/config/network'), 'network')
luaunit.assertEquals(basename('./profile'), 'profile')
end

function TestUtils.test_dirname()
luaunit.assertEquals(dirname('/etc/config/network'), '/etc/config/')
end

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

0 comments on commit ab1a7bb

Please sign in to comment.