Browse files

Fix path.join to remove inner slashes when joining

  • Loading branch information...
1 parent c810e71 commit e4f3818e840e3c48e19b9418805378aad98d2af6 @creationix creationix committed Aug 29, 2012
Showing with 30 additions and 1 deletion.
  1. +17 −1 lib/luvit/path.lua
  2. +13 −0 tests/test-path.lua
View
18 lib/luvit/path.lua
@@ -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)
View
13 tests/test-path.lua
@@ -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.