aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--spec/dir_spec.lua8
-rw-r--r--src/luarocks/core/dir.lua11
2 files changed, 15 insertions, 4 deletions
diff --git a/spec/dir_spec.lua b/spec/dir_spec.lua
index 9f05c664..f6ef172e 100644
--- a/spec/dir_spec.lua
+++ b/spec/dir_spec.lua
@@ -52,12 +52,20 @@ describe("luarocks.dir #unit", function()
52 it("strips unneeded /../ and /./", function() 52 it("strips unneeded /../ and /./", function()
53 assert.are.same("/some/dir/file.txt", dir.normalize("/../../../some/./foo/bar/.././../dir/bla/../file.txt")) 53 assert.are.same("/some/dir/file.txt", dir.normalize("/../../../some/./foo/bar/.././../dir/bla/../file.txt"))
54 assert.are.same("/some/dir/file.txt", dir.normalize("/../../../some/./foo/bar/.././../dir/bla/../file.txt")) 54 assert.are.same("/some/dir/file.txt", dir.normalize("/../../../some/./foo/bar/.././../dir/bla/../file.txt"))
55 assert.are.same("/some/dir", dir.normalize("/../../../some/./foo/bar/.././../dir/./some/subdir/../.."))
56 assert.are.same("/some/dir", dir.normalize("/../../../some/./foo/bar/.././../dir/./."))
55 end) 57 end)
56 it("respects relative paths", function() 58 it("respects relative paths", function()
59 assert.are.same(".", dir.normalize("."))
57 assert.are.same("boo", dir.normalize("./boo")) 60 assert.are.same("boo", dir.normalize("./boo"))
58 assert.are.same("/boo", dir.normalize("/./boo")) 61 assert.are.same("/boo", dir.normalize("/./boo"))
59 assert.are.same("../../../../boo", dir.normalize("../../../hello/world/../../../boo")) 62 assert.are.same("../../../../boo", dir.normalize("../../../hello/world/../../../boo"))
60 end) 63 end)
64 it("respects root directory", function()
65 assert.are.same("/", dir.normalize("/"))
66 assert.are.same("/", dir.normalize("/////"))
67 assert.are.same("/", dir.normalize("/a/b/.././../c/./../../"))
68 end)
61 end) 69 end)
62 70
63end) 71end)
diff --git a/src/luarocks/core/dir.lua b/src/luarocks/core/dir.lua
index 0da88997..46dbeafd 100644
--- a/src/luarocks/core/dir.lua
+++ b/src/luarocks/core/dir.lua
@@ -63,6 +63,7 @@ function dir.normalize(name)
63 if pathname:match("^.:") then 63 if pathname:match("^.:") then
64 drive, pathname = pathname:match("^(.:)(.*)$") 64 drive, pathname = pathname:match("^(.:)(.*)$")
65 end 65 end
66 pathname = pathname .. "/"
66 for piece in pathname:gmatch("(.-)/") do 67 for piece in pathname:gmatch("(.-)/") do
67 if piece == ".." then 68 if piece == ".." then
68 local prev = pieces[#pieces] 69 local prev = pieces[#pieces]
@@ -75,11 +76,13 @@ function dir.normalize(name)
75 table.insert(pieces, piece) 76 table.insert(pieces, piece)
76 end 77 end
77 end 78 end
78 local basename = pathname:match("[^/]+$") 79 if #pieces == 0 then
79 if basename then 80 pathname = drive .. "."
80 table.insert(pieces, basename) 81 elseif #pieces == 1 and pieces[1] == "" then
82 pathname = drive .. "/"
83 else
84 pathname = drive .. table.concat(pieces, "/")
81 end 85 end
82 pathname = drive .. table.concat(pieces, "/")
83 if protocol ~= "file" then pathname = protocol .."://"..pathname end 86 if protocol ~= "file" then pathname = protocol .."://"..pathname end
84 return pathname 87 return pathname
85end 88end