Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix path.join to remove inner slashes when joining
  • Loading branch information
creationix committed Aug 29, 2012
1 parent c810e71 commit e4f3818
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
18 changes: 17 additions & 1 deletion lib/luvit/path.lua
Expand Up @@ -90,7 +90,23 @@ function Path:normalize(filepath)
end
function Path:join(...)
return table.concat({...}, self.sep)
local parts = {...}
for i, part in ipairs(parts) do
-- Strip leading slashes on all but first item
if i > 1 then
while part:sub(1, 1) == self.sep do
part = part:sub(2)
end
end
-- Strip trailing slashes on all but last item
if i < #parts then
while part:sub(#part) == self.sep do
part = part:sub(1, #part - 1)
end
end
parts[i] = part
end
return table.concat(parts, self.sep)
end
function Path:resolve(root, filepath)
Expand Down
13 changes: 13 additions & 0 deletions tests/test-path.lua
Expand Up @@ -39,3 +39,16 @@ assert(path.posix:dirname('/usr/bin') == '/usr')
assert(path.nt:dirname('C:\\Users\\philips\\vim.exe') == 'C:\\Users\\philips')
assert(path.nt:dirname('C:\\Users\\philips\\') == 'C:\\Users')
assert(path.nt:dirname('C:\\Users\\philips\\') == 'C:\\Users')
assert(path.posix:join('foo', '/bar') == "foo/bar")
assert(path.posix:join('foo', 'bar') == "foo/bar")
assert(path.posix:join('foo/', 'bar') == "foo/bar")
assert(path.posix:join('foo/', '/bar') == "foo/bar")
assert(path.posix:join('/foo', '/bar') == "/foo/bar")
assert(path.posix:join('/foo', 'bar') == "/foo/bar")
assert(path.posix:join('/foo/', 'bar') == "/foo/bar")
assert(path.posix:join('/foo/', '/bar') == "/foo/bar")
assert(path.posix:join('foo', '/bar/') == "foo/bar/")
assert(path.posix:join('foo', 'bar/') == "foo/bar/")
assert(path.posix:join('foo/', 'bar/') == "foo/bar/")
assert(path.posix:join('foo/', '/bar/') == "foo/bar/")

0 comments on commit e4f3818

Please sign in to comment.