From 72d80a35bee56bd8f8002b199db37f89c1441c29 Mon Sep 17 00:00:00 2001
From: Hisham Muhammad <hisham@gobolinux.org>
Date: Sat, 13 Jul 2019 17:12:58 -0300
Subject: 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.
---
 spec/util_spec.lua | 28 +++++++++++++++++++++++-----
 1 file changed, 23 insertions(+), 5 deletions(-)

(limited to 'spec')

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()
    end)
       
    describe("core.util.cleanup_path", function()
+     it("does not change order of existing items of prepended path", function()
+        local sys_path = '/usr/local/bin;/usr/bin'
+        local lr_path = '/home/user/.luarocks/bin;/usr/bin'
+        local path = lr_path .. ';' .. sys_path
+
+        local result = core_util.cleanup_path(path, ';', '5.3', false)
+        assert.are.equal('/home/user/.luarocks/bin;/usr/local/bin;/usr/bin', result)
+     end)
+
+     it("does not change order of existing items of appended path", function()
+        local sys_path = '/usr/local/bin;/usr/bin'
+        local lr_path = '/home/user/.luarocks/bin;/usr/bin'
+        local path = sys_path .. ';' .. lr_path
+
+        local result = core_util.cleanup_path(path, ';', '5.3', true)
+        assert.are.equal('/usr/local/bin;/usr/bin;/home/user/.luarocks/bin', result)
+     end)
+
      it("rewrites versions that do not match the provided version", function()
         local expected = 'a/b/lua/5.3/?.lua;a/b/c/lua/5.3/?.lua'
         local result = core_util.cleanup_path('a/b/lua/5.2/?.lua;a/b/c/lua/5.3/?.lua', ';', '5.3')
         assert.are.equal(expected, result)
      end)
 
-      it("does not rewrite versions for which the provided version is a substring", function()
-         local expected = 'a/b/lua/5.3/?.lua;a/b/c/lua/5.3.4/?.lua'
-         local result = core_util.cleanup_path('a/b/lua/5.2/?.lua;a/b/c/lua/5.3.4/?.lua', ';', '5.3')
-         assert.are.equal(expected, result)
-      end)
+     it("does not rewrite versions for which the provided version is a substring", function()
+        local expected = 'a/b/lua/5.3/?.lua;a/b/c/lua/5.3.4/?.lua'
+        local result = core_util.cleanup_path('a/b/lua/5.2/?.lua;a/b/c/lua/5.3.4/?.lua', ';', '5.3')
+        assert.are.equal(expected, result)
+     end)
    end)
 end)
-- 
cgit v1.2.3-55-g6feb