Skip to content

Commit

Permalink
fixed internals.util.get_root_dir & added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cryi committed Mar 22, 2021
1 parent a8bcc2f commit df24226
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 7 deletions.
13 changes: 6 additions & 7 deletions lib/eli/internals/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,23 @@ local function _get_root_dir(paths)
table.insert(_rootDirSegments, _segment)
end
else
if not string.find(_path, "(.-)" .. _separator) then
return "" -- found file in root, no usable root dir
end
local j = 1
local j = 0
for _segment in string.gmatch(_path, "(.-)" .. _separator) do
if _segment ~= _rootDirSegments[j] then
_rootDirSegments = table.move(_rootDirSegments, 1, j - 1, 1, {})
if _segment ~= _rootDirSegments[j + 1] then
break
end
j = j + 1
end
if j < #_rootDirSegments then
_rootDirSegments = table.move(_rootDirSegments, 1, j, 1, {})
end
end

if #_rootDirSegments == 0 then
break
end
end
local _rootDir = _join("/", table.unpack(_rootDirSegments))
local _rootDir = _join(package.config:sub(1,1), table.unpack(_rootDirSegments))
if type(_rootDir) == "string" and #_rootDir > 0 and _rootDir:sub(#_rootDir, #_rootDir) ~= _separator then
_rootDir = _rootDir .. _separator
end
Expand Down
2 changes: 2 additions & 0 deletions lib/tests/all.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ require"elios"
require"extensions.string"
require"extensions.table"

require"internals.util"

--[[
NOTE:
elify tests has to be run at last
Expand Down
45 changes: 45 additions & 0 deletions lib/tests/internals/util.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
local _test = TEST or require "u-test"
local _ok, _internalUtil = pcall(require, "eli.internals.util")

if not _ok then
_test["eli.internals.util available"] = function()
_test.assert(false, "eli.internals.util not available")
end
if not TEST then
_test.summary()
os.exit()
else
return
end
end

_test["eli.internals.util get_root_dir"] = function()
local _paths = {
"src/__app/aaa/remove-all.lua",
"src/__app/aaa/configure.lua",
"src/__app/aaa/about.hjson",
"src/__app/specs.json",
"src/__app/ami.lua"
}
_test.assert(_internalUtil.get_root_dir(_paths) == "src/__app/")
_paths = {
"src/__app/aaa/remove-all.lua",
"src/__app/aaa/configure.lua",
"src/__app/aaa/about.hjson",
"src/__app/specs.json",
"src/ami.lua"
}
_test.assert(_internalUtil.get_root_dir(_paths) == "src/")
_paths = {
"src/__app/aaa/remove-all.lua",
"src/__app/aaa/configure.lua",
"src/__app/aaa/about.hjson",
"specs.json",
"src/ami.lua"
}
_test.assert(_internalUtil.get_root_dir(_paths) == "")
end

if not TEST then
_test.summary()
end

0 comments on commit df24226

Please sign in to comment.