Patch 3111264: Allow path.join() to accept any number of args

This commit is contained in:
Jason Perkins 2012-01-19 15:28:24 -05:00
parent b1df4ff7ae
commit e4d23a49bf
4 changed files with 52 additions and 25 deletions

View File

@ -52,6 +52,7 @@
* Patch 3451212: Fix Visual Studio MFC with StaticRuntime * Patch 3451212: Fix Visual Studio MFC with StaticRuntime
* Patch 3463020: Add windres environment variable for makefiles (icebreaker) * Patch 3463020: Add windres environment variable for makefiles (icebreaker)
* Bug 3413866: Incorrect VS200x .csproj relative source paths * Bug 3413866: Incorrect VS200x .csproj relative source paths
* Patch 3111264: Allow path.join() to accept any number of args
------- -------

View File

@ -218,31 +218,38 @@
end end
-- --
-- Join two pieces of a path together into a single path. -- Join one or more pieces of a path together into a single path.
--
-- @param ...
-- One or more path strings.
-- @return
-- The joined path.
-- --
function path.join(leading, trailing) function path.join(...)
leading = leading or "" local numargs = select("#", ...)
if numargs == 0 then
if (not trailing) then return "";
return leading
end end
if (path.isabsolute(trailing)) then local allparts = {}
return trailing for i = numargs, 1, -1 do
local part = select(i, ...)
if part and #part > 0 and part ~= "." then
-- trim off trailing slashes
while part:endswith("/") do
part = part:sub(1, -2)
end end
if (leading == ".") then table.insert(allparts, 1, part)
leading = "" if path.isabsolute(part) then
break
end
end
end end
if (leading:len() > 0 and not leading:endswith("/")) then return table.concat(allparts, "/")
leading = leading .. "/"
end
return leading .. trailing
end end

View File

@ -106,7 +106,6 @@
language "C++" language "C++"
kind "ConsoleApp" kind "ConsoleApp"
prepare() prepare()
test.contains(removed, "obj")
test.contains(removed, "obj/Debug") test.contains(removed, "obj/Debug")
test.contains(removed, "obj/Release") test.contains(removed, "obj/Release")
end end

View File

@ -182,23 +182,43 @@
-- --
function suite.join_OnValidParts() function suite.join_OnValidParts()
test.isequal("leading/trailing", path.join("leading", "trailing")) test.isequal("p1/p2", path.join("p1", "p2"))
end end
function suite.join_OnAbsoluteUnixPath() function suite.join_OnAbsoluteUnixPath()
test.isequal("/trailing", path.join("leading", "/trailing")) test.isequal("/p2", path.join("p1", "/p2"))
end end
function suite.join_OnAbsoluteWindowsPath() function suite.join_OnAbsoluteWindowsPath()
test.isequal("C:/trailing", path.join("leading", "C:/trailing")) test.isequal("C:/p2", path.join("p1", "C:/p2"))
end end
function suite.join_OnCurrentDirectory() function suite.join_OnCurrentDirectory()
test.isequal("trailing", path.join(".", "trailing")) test.isequal("p2", path.join(".", "p2"))
end end
function suite.join_OnNilSecondPart() function suite.join_OnNilSecondPart()
test.isequal("leading", path.join("leading", nil)) test.isequal("p1", path.join("p1", nil))
end
function suite.join_onMoreThanTwoParts()
test.isequal("p1/p2/p3", path.join("p1", "p2", "p3"))
end
function suite.join_removesExtraInternalSlashes()
test.isequal("p1/p2", path.join("p1/", "p2"))
end
function suite.join_removesTrailingSlash()
test.isequal("p1/p2", path.join("p1", "p2/"))
end
function suite.join_ignoresNilParts()
test.isequal("p2", path.join(nil, "p2", nil))
end
function suite.join_ignoresEmptyParts()
test.isequal("p2", path.join("", "p2", ""))
end end