diff options
author | Hisham Muhammad <hisham@gobolinux.org> | 2019-07-13 17:12:58 -0300 |
---|---|---|
committer | Hisham Muhammad <hisham@gobolinux.org> | 2019-07-17 20:30:25 -0300 |
commit | 72d80a35bee56bd8f8002b199db37f89c1441c29 (patch) | |
tree | b3da27263aa3c9a20ca860753791d2ae15496cd6 | |
parent | 526664e6e54da0f129621ea6df355767259a103a (diff) | |
download | luarocks-72d80a35bee56bd8f8002b199db37f89c1441c29.tar.gz luarocks-72d80a35bee56bd8f8002b199db37f89c1441c29.tar.bz2 luarocks-72d80a35bee56bd8f8002b199db37f89c1441c29.zip |
path: keep order of existing entries in PATH
Implements suggestion by @FSMaxB:
> Add an additional flag to util.cleanup_path that specifies if the cleanup
> happens from the right or from the left. If append is true, clean up from the
> left, otherwise clean up from the right.
Fixes #763.
-rw-r--r-- | spec/util_spec.lua | 28 | ||||
-rw-r--r-- | src/luarocks/cmd/path.lua | 6 | ||||
-rw-r--r-- | src/luarocks/core/util.lua | 19 |
3 files changed, 41 insertions, 12 deletions
diff --git a/spec/util_spec.lua b/spec/util_spec.lua index 930a5aea..b1693cfa 100644 --- a/spec/util_spec.lua +++ b/spec/util_spec.lua | |||
@@ -157,16 +157,34 @@ describe("Luarocks util test #unit", function() | |||
157 | end) | 157 | end) |
158 | 158 | ||
159 | describe("core.util.cleanup_path", function() | 159 | describe("core.util.cleanup_path", function() |
160 | it("does not change order of existing items of prepended path", function() | ||
161 | local sys_path = '/usr/local/bin;/usr/bin' | ||
162 | local lr_path = '/home/user/.luarocks/bin;/usr/bin' | ||
163 | local path = lr_path .. ';' .. sys_path | ||
164 | |||
165 | local result = core_util.cleanup_path(path, ';', '5.3', false) | ||
166 | assert.are.equal('/home/user/.luarocks/bin;/usr/local/bin;/usr/bin', result) | ||
167 | end) | ||
168 | |||
169 | it("does not change order of existing items of appended path", function() | ||
170 | local sys_path = '/usr/local/bin;/usr/bin' | ||
171 | local lr_path = '/home/user/.luarocks/bin;/usr/bin' | ||
172 | local path = sys_path .. ';' .. lr_path | ||
173 | |||
174 | local result = core_util.cleanup_path(path, ';', '5.3', true) | ||
175 | assert.are.equal('/usr/local/bin;/usr/bin;/home/user/.luarocks/bin', result) | ||
176 | end) | ||
177 | |||
160 | it("rewrites versions that do not match the provided version", function() | 178 | it("rewrites versions that do not match the provided version", function() |
161 | local expected = 'a/b/lua/5.3/?.lua;a/b/c/lua/5.3/?.lua' | 179 | local expected = 'a/b/lua/5.3/?.lua;a/b/c/lua/5.3/?.lua' |
162 | local result = core_util.cleanup_path('a/b/lua/5.2/?.lua;a/b/c/lua/5.3/?.lua', ';', '5.3') | 180 | local result = core_util.cleanup_path('a/b/lua/5.2/?.lua;a/b/c/lua/5.3/?.lua', ';', '5.3') |
163 | assert.are.equal(expected, result) | 181 | assert.are.equal(expected, result) |
164 | end) | 182 | end) |
165 | 183 | ||
166 | it("does not rewrite versions for which the provided version is a substring", function() | 184 | it("does not rewrite versions for which the provided version is a substring", function() |
167 | local expected = 'a/b/lua/5.3/?.lua;a/b/c/lua/5.3.4/?.lua' | 185 | local expected = 'a/b/lua/5.3/?.lua;a/b/c/lua/5.3.4/?.lua' |
168 | local result = core_util.cleanup_path('a/b/lua/5.2/?.lua;a/b/c/lua/5.3.4/?.lua', ';', '5.3') | 186 | local result = core_util.cleanup_path('a/b/lua/5.2/?.lua;a/b/c/lua/5.3.4/?.lua', ';', '5.3') |
169 | assert.are.equal(expected, result) | 187 | assert.are.equal(expected, result) |
170 | end) | 188 | end) |
171 | end) | 189 | end) |
172 | end) | 190 | end) |
diff --git a/src/luarocks/cmd/path.lua b/src/luarocks/cmd/path.lua index 9046cd2e..bb383ad9 100644 --- a/src/luarocks/cmd/path.lua +++ b/src/luarocks/cmd/path.lua | |||
@@ -60,10 +60,10 @@ function path_cmd.command(flags) | |||
60 | 60 | ||
61 | local lpath_var, lcpath_var = util.lua_path_variables() | 61 | local lpath_var, lcpath_var = util.lua_path_variables() |
62 | 62 | ||
63 | util.printout(fs.export_cmd(lpath_var, util.cleanup_path(lr_path, ';', cfg.lua_version))) | 63 | util.printout(fs.export_cmd(lpath_var, util.cleanup_path(lr_path, ';', cfg.lua_version, flags["append"]))) |
64 | util.printout(fs.export_cmd(lcpath_var, util.cleanup_path(lr_cpath, ';', cfg.lua_version))) | 64 | util.printout(fs.export_cmd(lcpath_var, util.cleanup_path(lr_cpath, ';', cfg.lua_version, flags["append"]))) |
65 | if not flags["no-bin"] then | 65 | if not flags["no-bin"] then |
66 | util.printout(fs.export_cmd("PATH", util.cleanup_path(lr_bin, path_sep))) | 66 | util.printout(fs.export_cmd("PATH", util.cleanup_path(lr_bin, path_sep, nil, flags["append"]))) |
67 | end | 67 | end |
68 | return true | 68 | return true |
69 | end | 69 | end |
diff --git a/src/luarocks/core/util.lua b/src/luarocks/core/util.lua index 1499dc62..6a306026 100644 --- a/src/luarocks/core/util.lua +++ b/src/luarocks/core/util.lua | |||
@@ -165,13 +165,23 @@ end | |||
165 | -- @param list string: A path string (from $PATH or package.path) | 165 | -- @param list string: A path string (from $PATH or package.path) |
166 | -- @param sep string: The separator | 166 | -- @param sep string: The separator |
167 | -- @param lua_version (optional) string: The Lua version to use. | 167 | -- @param lua_version (optional) string: The Lua version to use. |
168 | function util.cleanup_path(list, sep, lua_version) | 168 | -- @param keep_last (optional) if true, keep last occurrence in case |
169 | -- of duplicates; otherwise keep first occurrence. The default is false. | ||
170 | function util.cleanup_path(list, sep, lua_version, keep_last) | ||
169 | assert(type(list) == "string") | 171 | assert(type(list) == "string") |
170 | assert(type(sep) == "string") | 172 | assert(type(sep) == "string") |
171 | local parts = util.split_string(list, sep) | 173 | local parts = util.split_string(list, sep) |
172 | local final, entries = {}, {} | 174 | local final, entries = {}, {} |
173 | for _, part in ipairs(parts) do | 175 | local start, stop, step |
174 | part = part:gsub("//", "/") | 176 | |
177 | if keep_last then | ||
178 | start, stop, step = 1, #parts, 1 | ||
179 | else | ||
180 | start, stop, step = #parts, 1, -1 | ||
181 | end | ||
182 | |||
183 | for i = start, stop, step do | ||
184 | local part = parts[i]:gsub("//", "/") | ||
175 | if lua_version then | 185 | if lua_version then |
176 | part = part:gsub("/lua/([%d.]+)/", function(part_version) | 186 | part = part:gsub("/lua/([%d.]+)/", function(part_version) |
177 | if part_version:sub(1, #lua_version) ~= lua_version then | 187 | if part_version:sub(1, #lua_version) ~= lua_version then |
@@ -180,7 +190,8 @@ function util.cleanup_path(list, sep, lua_version) | |||
180 | end) | 190 | end) |
181 | end | 191 | end |
182 | if not entries[part] then | 192 | if not entries[part] then |
183 | table.insert(final, part) | 193 | local at = keep_last and #final+1 or 1 |
194 | table.insert(final, at, part) | ||
184 | entries[part] = true | 195 | entries[part] = true |
185 | end | 196 | end |
186 | end | 197 | end |