Repository: nvim-lua/plenary.nvim
Pull request #290 (2021-12-18 22:24:09)

fix: path normalization works with esoteric usernames
Usernames with dashes at the end are perfectly valid unix user names, but they're seen as a regular expression pattern in Lua. Switch to using string.find instead of string.gsub to avoid the pattern matching issue.

Further solves https://github.com/nvim-telescope/telescope.nvim/issues/1521 for me

mattr-: :wave: This should be 🆗  to review now. Is there's something else I need to fix with this PR, please let me know. Thanks!
mattr-: @Conni2461 Would you mind taking a look at this? I'd like to get off my fork of plenary if possible. Thank you! :heart:

Patch:
diff --git a/lua/plenary/async/init.lua b/lua/plenary/async/init.lua
index f8222fc..71ccd84 100644
--- a/lua/plenary/async/init.lua
+++ b/lua/plenary/async/init.lua
@@ -36,6 +36,8 @@ exports.tests.add_globals = function()
   a.describe = exports.tests.describe
   -- must prefix with a or stack overflow
   a.it = exports.tests.it
+  a.before_each = exports.tests.before_each
+  a.after_each = exports.tests.after_each
 end
 
 exports.tests.add_to_env = function()
@@ -50,6 +52,8 @@ exports.tests.add_to_env = function()
   env.a.describe = exports.tests.describe
   -- must prefix with a or stack overflow
   env.a.it = exports.tests.it
+  a.before_each = exports.tests.before_each
+  a.after_each = exports.tests.after_each
 
   setfenv(2, env)
 end
diff --git a/lua/plenary/path.lua b/lua/plenary/path.lua
index 34607b1..4a1fd23 100644
--- a/lua/plenary/path.lua
+++ b/lua/plenary/path.lua
@@ -347,10 +347,14 @@ function Path:normalize(cwd)
   self:make_relative(cwd)
 
   -- Substitute home directory w/ "~"
-  local removed_path_sep = path.home:gsub(path.sep .. "$", "")
-  self.filename = self.filename:gsub("^" .. removed_path_sep, "~", 1)
+  -- string.gsub is not useful here because usernames with dashes at the end
+  -- will be seen as a regexp pattern rather than a raw string
+  local start, finish = string.find(self.filename, path.home, 1, true)
+  if start == 1 then
+    self.filename = "~" .. path.sep .. string.sub(self.filename, (finish + 1), -1)
+  end
 
-  return _normalize_path(self.filename, self._cwd)
+  return _normalize_path(clean(self.filename), self._cwd)
 end
 
 local function shorten_len(filename, len, exclude)


Test patch:
diff --git a/lua/plenary/async/tests.lua b/lua/plenary/async/tests.lua
index 367b83d..4895773 100644
--- a/lua/plenary/async/tests.lua
+++ b/lua/plenary/async/tests.lua
@@ -10,4 +10,12 @@ M.it = function(s, async_func)
   it(s, util.will_block(async_func))
 end
 
+M.before_each = function(async_func)
+  before_each(util.will_block(async_func))
+end
+
+M.after_each = function(async_func)
+  after_each(util.will_block(async_func))
+end
+
 return M
diff --git a/tests/plenary/async/test_spec.lua b/tests/plenary/async/test_spec.lua
new file mode 100644
index 0000000..6a37309
--- /dev/null
+++ b/tests/plenary/async/test_spec.lua
@@ -0,0 +1,52 @@
+require("plenary.async").tests.add_to_env()
+
+a.describe("a.before_each", function()
+  local counter = 0
+
+  local set_counter_to_one = a.wrap(function(callback)
+    a.util.sleep(5)
+    counter = 1
+  end, 1)
+
+  a.before_each(a.void(function()
+    set_counter_to_one()
+  end))
+
+  a.it("should run in async context", function()
+    counter = counter + 1
+    assert.are.same(counter, 2)
+  end)
+
+  a.it("should run for all tests", function()
+    counter = counter + 2
+    assert.are.same(counter, 3)
+  end)
+end)
+
+a.describe("a.after_each", function()
+  local counter = 0
+
+  local set_counter_to_one = a.wrap(function(callback)
+    a.util.sleep(5)
+    counter = 1
+  end, 1)
+
+  a.after_each(a.void(function()
+    set_counter_to_one()
+  end))
+
+  a.it("should not run before first test", function()
+    counter = counter + 1
+    assert.are.same(counter, 1)
+  end)
+
+  a.it("should run before the second test", function()
+    counter = counter + 2
+    assert.are.same(counter, 3)
+  end)
+
+  a.it("should run before the third test", function()
+    counter = counter + 3
+    assert.are.same(counter, 4)
+  end)
+end)
diff --git a/tests/plenary/path_spec.lua b/tests/plenary/path_spec.lua
index b8a1c47..2713532 100644
--- a/tests/plenary/path_spec.lua
+++ b/tests/plenary/path_spec.lua
@@ -212,7 +212,7 @@ describe("Path", function()
       assert.are.same("/tmp/lua/plenary/path.lua", p:normalize())
     end)
 
-    it("can normalize ~ when file is within home directory (traling slash)", function()
+    it("can normalize ~ when file is within home directory (trailing slash)", function()
       local home = "/home/test/"
       local p = Path:new { home, "./test_file" }
       p.path.home = home
@@ -227,6 +227,14 @@ describe("Path", function()
       p._cwd = "/tmp/lua"
       assert.are.same("~/test_file", p:normalize())
     end)
+
+    it("handles usernames with a dash at the end", function()
+      local home = "/home/mattr-"
+      local p = Path:new { home, "test_file" }
+      p.path.home = home
+      p._cwd = "/tmp/lua"
+      assert.are.same("~/test_file", p:normalize())
+    end)
   end)
 
   describe(":shorten", function()