diff options
author | Hisham Muhammad <hisham@gobolinux.org> | 2021-03-20 23:44:27 -0300 |
---|---|---|
committer | Hisham Muhammad <hisham@gobolinux.org> | 2021-03-22 10:33:10 -0300 |
commit | dcaca50d670b3f5654c109339b6f37c766558eb1 (patch) | |
tree | 638faf5df7570aa874bafdb4179d00f18f410bac | |
parent | fbd857e1a0b2aae5beb3875427612e41bf67b35e (diff) | |
download | luarocks-dcaca50d670b3f5654c109339b6f37c766558eb1.tar.gz luarocks-dcaca50d670b3f5654c109339b6f37c766558eb1.tar.bz2 luarocks-dcaca50d670b3f5654c109339b6f37c766558eb1.zip |
fix: pack: rockspec with a bare file in the url
-rw-r--r-- | spec/dir_spec.lua | 10 | ||||
-rw-r--r-- | spec/fetch_spec.lua | 21 | ||||
-rw-r--r-- | spec/make_spec.lua | 5 | ||||
-rw-r--r-- | spec/pack_spec.lua | 56 | ||||
-rw-r--r-- | src/luarocks/core/dir.lua | 22 | ||||
-rw-r--r-- | src/luarocks/fetch.lua | 21 |
6 files changed, 129 insertions, 6 deletions
diff --git a/spec/dir_spec.lua b/spec/dir_spec.lua index 1b568c8f..9f05c664 100644 --- a/spec/dir_spec.lua +++ b/spec/dir_spec.lua | |||
@@ -46,8 +46,18 @@ describe("luarocks.dir #unit", function() | |||
46 | describe("dir.normalize", function() | 46 | describe("dir.normalize", function() |
47 | it("converts backslashes and removes trailing slashes", function() | 47 | it("converts backslashes and removes trailing slashes", function() |
48 | assert.are.same("/foo/ovo", dir.normalize("\\foo\\ovo\\")) | 48 | assert.are.same("/foo/ovo", dir.normalize("\\foo\\ovo\\")) |
49 | assert.are.same("c:/some/dir", dir.normalize("c:\\..\\some\\foo\\..\\dir")) | ||
49 | assert.are.same("http://example.com/foo/ovo", dir.normalize("http://example.com/foo\\ovo\\")) | 50 | assert.are.same("http://example.com/foo/ovo", dir.normalize("http://example.com/foo\\ovo\\")) |
50 | end) | 51 | end) |
52 | it("strips unneeded /../ and /./", function() | ||
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")) | ||
55 | end) | ||
56 | it("respects relative paths", function() | ||
57 | assert.are.same("boo", dir.normalize("./boo")) | ||
58 | assert.are.same("/boo", dir.normalize("/./boo")) | ||
59 | assert.are.same("../../../../boo", dir.normalize("../../../hello/world/../../../boo")) | ||
60 | end) | ||
51 | end) | 61 | end) |
52 | 62 | ||
53 | end) | 63 | end) |
diff --git a/spec/fetch_spec.lua b/spec/fetch_spec.lua index 8194a298..008a91c8 100644 --- a/spec/fetch_spec.lua +++ b/spec/fetch_spec.lua | |||
@@ -52,8 +52,25 @@ describe("luarocks fetch #unit #mock", function() | |||
52 | end) | 52 | end) |
53 | 53 | ||
54 | it("returns the absolute path of the filename argument if the url represents a file", function() | 54 | it("returns the absolute path of the filename argument if the url represents a file", function() |
55 | local file = fetch.fetch_url("file://a_rock.lua") | 55 | test_env.run_in_tmp(function(tmpdir) |
56 | assert.truthy(are_same_files(file, lfs.currentdir() .. "/a_rock.lua")) | 56 | write_file("test.lua", "return {}", finally) |
57 | |||
58 | fs.change_dir(tmpdir) | ||
59 | local file, err = fetch.fetch_url("file://test.lua") | ||
60 | assert.truthy(file, err) | ||
61 | assert.truthy(are_same_files(file, lfs.currentdir() .. "/test.lua")) | ||
62 | fs.pop_dir() | ||
63 | end, finally) | ||
64 | end) | ||
65 | |||
66 | it("fails if local path is invalid and returns a helpful hint for relative paths", function() | ||
67 | test_env.run_in_tmp(function(tmpdir) | ||
68 | write_file("test.lua", "return {}", finally) | ||
69 | |||
70 | local ok, err = fetch.fetch_url("file://boo.lua") | ||
71 | assert.falsy(ok) | ||
72 | assert.match("note that given path in rockspec is not absolute: file://boo.lua", err) | ||
73 | end, finally) | ||
57 | end) | 74 | end) |
58 | 75 | ||
59 | it("returns false and does nothing if the url argument contains a nonexistent file", function() | 76 | it("returns false and does nothing if the url argument contains a nonexistent file", function() |
diff --git a/spec/make_spec.lua b/spec/make_spec.lua index 791eeb89..e14eb66b 100644 --- a/spec/make_spec.lua +++ b/spec/make_spec.lua | |||
@@ -67,10 +67,11 @@ describe("luarocks make #integration", function() | |||
67 | end) | 67 | end) |
68 | 68 | ||
69 | it("--only-deps", function() | 69 | it("--only-deps", function() |
70 | local rockspec = testing_paths.fixtures_dir .. "/build_only_deps-0.1-1.rockspec" | 70 | local rockspec = "build_only_deps-0.1-1.rockspec" |
71 | local src_rock = testing_paths.fixtures_dir .. "/build_only_deps-0.1-1.src.rock" | ||
71 | 72 | ||
72 | test_env.remove_dir("build_only_deps-0.1-1/") | 73 | test_env.remove_dir("build_only_deps-0.1-1/") |
73 | assert.is_true(run.luarocks_bool("unpack " .. rockspec)) | 74 | assert.is_true(run.luarocks_bool("unpack " .. src_rock)) |
74 | lfs.chdir("build_only_deps-0.1-1/") | 75 | lfs.chdir("build_only_deps-0.1-1/") |
75 | assert.is_true(run.luarocks_bool("make " .. rockspec .. " --only-deps")) | 76 | assert.is_true(run.luarocks_bool("make " .. rockspec .. " --only-deps")) |
76 | assert.is_false(run.luarocks_bool("show build_only_deps")) | 77 | assert.is_false(run.luarocks_bool("show build_only_deps")) |
diff --git a/spec/pack_spec.lua b/spec/pack_spec.lua index 55071839..beab7e8a 100644 --- a/spec/pack_spec.lua +++ b/spec/pack_spec.lua | |||
@@ -2,6 +2,7 @@ local test_env = require("spec.util.test_env") | |||
2 | local lfs = require("lfs") | 2 | local lfs = require("lfs") |
3 | local run = test_env.run | 3 | local run = test_env.run |
4 | local testing_paths = test_env.testing_paths | 4 | local testing_paths = test_env.testing_paths |
5 | local write_file = test_env.write_file | ||
5 | 6 | ||
6 | test_env.unload_luarocks() | 7 | test_env.unload_luarocks() |
7 | 8 | ||
@@ -80,7 +81,60 @@ describe("luarocks pack #integration", function() | |||
80 | assert.is_truthy(lfs.attributes("a_rock-1.0-1.src.rock")) | 81 | assert.is_truthy(lfs.attributes("a_rock-1.0-1.src.rock")) |
81 | end) | 82 | end) |
82 | 83 | ||
83 | it("fails packing a rockspec into a .src.rock dir if doesn't exist", function() | 84 | it("can pack a rockspec with a bare file:// in the url", function() |
85 | test_env.run_in_tmp(function(tmpdir) | ||
86 | write_file("test-1.0-1.rockspec", [[ | ||
87 | package = "test" | ||
88 | version = "1.0-1" | ||
89 | source = { | ||
90 | url = "file://]] .. tmpdir:gsub("\\", "/") .. [[/test.lua" | ||
91 | } | ||
92 | dependencies = { | ||
93 | "a_rock 1.0" | ||
94 | } | ||
95 | build = { | ||
96 | type = "builtin", | ||
97 | modules = { | ||
98 | test = "test.lua" | ||
99 | } | ||
100 | } | ||
101 | ]], finally) | ||
102 | write_file("test.lua", "return {}", finally) | ||
103 | |||
104 | assert.is.truthy(run.luarocks_bool("pack test-1.0-1.rockspec")) | ||
105 | assert.is.truthy(lfs.attributes("test-1.0-1.src.rock")) | ||
106 | |||
107 | assert.is.truthy(run.luarocks_bool("unpack test-1.0-1.src.rock")) | ||
108 | assert.is.truthy(lfs.attributes("test-1.0-1/test.lua")) | ||
109 | end, finally) | ||
110 | end) | ||
111 | |||
112 | it("can pack a rockspec with a bare file:// fails if doesn't exist", function() | ||
113 | test_env.run_in_tmp(function(tmpdir) | ||
114 | write_file("test-1.0-1.rockspec", [[ | ||
115 | package = "test" | ||
116 | version = "1.0-1" | ||
117 | source = { | ||
118 | url = "file://]] .. tmpdir:gsub("\\", "/") .. [[/test_doesnt_exist.lua" | ||
119 | } | ||
120 | dependencies = { | ||
121 | "a_rock 1.0" | ||
122 | } | ||
123 | build = { | ||
124 | type = "builtin", | ||
125 | modules = { | ||
126 | test = "test.lua" | ||
127 | } | ||
128 | } | ||
129 | ]], finally) | ||
130 | |||
131 | assert.is.falsy(run.luarocks_bool("pack test-1.0-1.rockspec")) | ||
132 | assert.is.falsy(lfs.attributes("test-1.0-1.src.rock")) | ||
133 | end, finally) | ||
134 | end) | ||
135 | |||
136 | |||
137 | it("fails packing a rockspec into a .src.rock if dir doesn't exist", function() | ||
84 | local output = run.luarocks("pack " .. testing_paths.fixtures_dir .. "/bad_pack-0.1-1.rockspec") | 138 | local output = run.luarocks("pack " .. testing_paths.fixtures_dir .. "/bad_pack-0.1-1.rockspec") |
85 | assert.match("Directory invalid_dir not found", output) | 139 | assert.match("Directory invalid_dir not found", output) |
86 | assert.is_falsy(lfs.attributes("bad_pack-0.1-1.src.rock")) | 140 | assert.is_falsy(lfs.attributes("bad_pack-0.1-1.src.rock")) |
diff --git a/src/luarocks/core/dir.lua b/src/luarocks/core/dir.lua index 765a0e58..0da88997 100644 --- a/src/luarocks/core/dir.lua +++ b/src/luarocks/core/dir.lua | |||
@@ -58,6 +58,28 @@ end | |||
58 | function dir.normalize(name) | 58 | function dir.normalize(name) |
59 | local protocol, pathname = dir.split_url(name) | 59 | local protocol, pathname = dir.split_url(name) |
60 | pathname = pathname:gsub("\\", "/"):gsub("(.)/*$", "%1"):gsub("//", "/") | 60 | pathname = pathname:gsub("\\", "/"):gsub("(.)/*$", "%1"):gsub("//", "/") |
61 | local pieces = {} | ||
62 | local drive = "" | ||
63 | if pathname:match("^.:") then | ||
64 | drive, pathname = pathname:match("^(.:)(.*)$") | ||
65 | end | ||
66 | for piece in pathname:gmatch("(.-)/") do | ||
67 | if piece == ".." then | ||
68 | local prev = pieces[#pieces] | ||
69 | if not prev or prev == ".." then | ||
70 | table.insert(pieces, "..") | ||
71 | elseif prev ~= "" then | ||
72 | table.remove(pieces) | ||
73 | end | ||
74 | elseif piece ~= "." then | ||
75 | table.insert(pieces, piece) | ||
76 | end | ||
77 | end | ||
78 | local basename = pathname:match("[^/]+$") | ||
79 | if basename then | ||
80 | table.insert(pieces, basename) | ||
81 | end | ||
82 | pathname = drive .. table.concat(pieces, "/") | ||
61 | if protocol ~= "file" then pathname = protocol .."://"..pathname end | 83 | if protocol ~= "file" then pathname = protocol .."://"..pathname end |
62 | return pathname | 84 | return pathname |
63 | end | 85 | end |
diff --git a/src/luarocks/fetch.lua b/src/luarocks/fetch.lua index c8b15d61..1882c348 100644 --- a/src/luarocks/fetch.lua +++ b/src/luarocks/fetch.lua | |||
@@ -63,7 +63,26 @@ function fetch.fetch_url(url, filename, cache) | |||
63 | 63 | ||
64 | local protocol, pathname = dir.split_url(url) | 64 | local protocol, pathname = dir.split_url(url) |
65 | if protocol == "file" then | 65 | if protocol == "file" then |
66 | return fs.absolute_name(pathname) | 66 | local fullname = dir.normalize(fs.absolute_name(pathname)) |
67 | if not fs.exists(fullname) then | ||
68 | local hint = (not pathname:match("^/")) | ||
69 | and (" - note that given path in rockspec is not absolute: " .. url) | ||
70 | or "" | ||
71 | return nil, "Local file not found: " .. fullname .. hint | ||
72 | end | ||
73 | filename = filename or dir.base_name(pathname) | ||
74 | local dstname = dir.normalize(fs.absolute_name(dir.path(".", filename))) | ||
75 | local ok, err | ||
76 | if fullname == dstname then | ||
77 | ok = true | ||
78 | else | ||
79 | ok, err = fs.copy(fullname, dstname) | ||
80 | end | ||
81 | if ok then | ||
82 | return dstname | ||
83 | else | ||
84 | return nil, "Failed copying local file " .. fullname .. " to " .. dstname .. ": " .. err | ||
85 | end | ||
67 | elseif dir.is_basic_protocol(protocol) then | 86 | elseif dir.is_basic_protocol(protocol) then |
68 | local ok, name, from_cache = fs.download(url, filename, cache) | 87 | local ok, name, from_cache = fs.download(url, filename, cache) |
69 | if not ok then | 88 | if not ok then |