Repository: nvim-lua/plenary.nvim
Pull request #625 (2024-10-06 11:33:50)

feat: enable to wrap uv.fs_opendir()
`uv.fs_opendir` has an odd signature that has `callback` not in the last position. This patch enables to use `async.uv.fs_opendir` by a small hack to exchange arguments between `entries` and `callback`.


Patch:
diff --git a/.github/workflows/default.yml b/.github/workflows/default.yml
index 302ab2b..cc227fa 100644
--- a/.github/workflows/default.yml
+++ b/.github/workflows/default.yml
@@ -11,7 +11,7 @@ jobs:
       matrix:
         include:
           - os: ubuntu-22.04
-            rev: nightly/nvim-linux64.tar.gz
+            rev: nightly/nvim-linux-x86_64.tar.gz
           - os: ubuntu-22.04
             rev: v0.7.2/nvim-linux64.tar.gz
           - os: ubuntu-22.04
@@ -19,12 +19,12 @@ jobs:
           - os: ubuntu-22.04
             rev: v0.9.5/nvim-linux64.tar.gz
           - os: ubuntu-22.04
-            rev: v0.10.0/nvim-linux64.tar.gz
+            rev: v0.10.4/nvim-linux-x86_64.tar.gz
     steps:
-      - uses: actions/checkout@v3
+      - uses: actions/checkout@v4
       - run: date +%F > todays-date
       - name: Restore cache for today's nightly.
-        uses: actions/cache@v3
+        uses: actions/cache@v4
         with:
           path: _neovim
           key: ${{ runner.os }}-${{ matrix.rev }}-${{ hashFiles('todays-date') }}
@@ -46,7 +46,7 @@ jobs:
     name: stylua
     runs-on: ubuntu-22.04
     steps:
-      - uses: actions/checkout@v3
+      - uses: actions/checkout@v4
       - uses: JohnnyMorganz/stylua-action@v2
         with:
           token: ${{ secrets.GITHUB_TOKEN }}
@@ -58,7 +58,7 @@ jobs:
     name: Luacheck
     runs-on: ubuntu-22.04
     steps:
-      - uses: actions/checkout@v3
+      - uses: actions/checkout@v4
 
       - name: Prepare
         run: |
diff --git a/lua/plenary/async/uv_async.lua b/lua/plenary/async/uv_async.lua
index 4ef0eba..427e1a5 100644
--- a/lua/plenary/async/uv_async.lua
+++ b/lua/plenary/async/uv_async.lua
@@ -3,8 +3,8 @@ local uv = vim.loop
 
 local M = {}
 
-local function add(name, argc)
-  local success, ret = pcall(a.wrap, uv[name], argc)
+local function add(name, argc, custom)
+  local success, ret = pcall(a.wrap, custom or uv[name], argc)
 
   if not success then
     error("Failed to add function with name " .. name)
@@ -48,7 +48,9 @@ add("fs_chown", 4)
 add("fs_fchown", 4)
 -- 'fs_lchown',
 add("fs_copyfile", 4)
--- add('fs_opendir', 3) -- TODO: fix this one
+add("fs_opendir", 3, function(path, entries, callback)
+  return uv.fs_opendir(path, callback, entries)
+end)
 add("fs_readdir", 2)
 add("fs_closedir", 2)
 -- 'fs_statfs',
diff --git a/lua/plenary/curl.lua b/lua/plenary/curl.lua
index b7fc997..20e8cc2 100644
--- a/lua/plenary/curl.lua
+++ b/lua/plenary/curl.lua
@@ -251,15 +251,25 @@ end
 ------------------------------------------------------------
 parse.response = function(lines, dump_path, code)
   local headers = P.readlines(dump_path)
-  local status = tonumber(string.match(headers[1], "([%w+]%d+)"))
-  local body = F.join(lines, "\n")
+  local status = nil
+  local processed_headers = {}
+
+  -- Process headers in a single pass
+  for _, line in ipairs(headers) do
+    local status_match = line:match "^HTTP/%S*%s+(%d+)"
+    if status_match then
+      status = tonumber(status_match)
+    elseif line ~= "" then
+      table.insert(processed_headers, line)
+    end
+  end
 
+  local body = F.join(lines, "\n")
   vim.loop.fs_unlink(dump_path)
-  table.remove(headers, 1)
 
   return {
-    status = status,
-    headers = headers,
+    status = status or 0,
+    headers = processed_headers,
     body = body,
     exit = code,
   }
diff --git a/lua/plenary/job.lua b/lua/plenary/job.lua
index 44a7348..7f54971 100644
--- a/lua/plenary/job.lua
+++ b/lua/plenary/job.lua
@@ -361,7 +361,7 @@ local on_output = function(self, result_key, cb)
         cb(err, result_line, self)
       end
 
-      if data == nil or is_complete then
+      if is_complete then
         return
       end
 


Test patch:
diff --git a/tests/plenary/job_spec.lua b/tests/plenary/job_spec.lua
index d9213be..c2edde0 100644
--- a/tests/plenary/job_spec.lua
+++ b/tests/plenary/job_spec.lua
@@ -99,7 +99,7 @@ describe("Job", function()
       assert.are.same(job:result(), results)
     end)
 
-    pending("should split stdin across newlines with no ending newline", function()
+    it("should split stdin across newlines with no ending newline", function()
       local results = {}
       local job = Job:new {
         -- writer = "hello\nword\nthis is\ntj",
@@ -113,6 +113,8 @@ describe("Job", function()
       job:start()
       job:send "hello\nwor"
       job:send "ld"
+
+      wait_for_result(job, { "hello", "world" })
       job:shutdown()
 
       assert.are.same(job:result(), { "hello", "world" })
@@ -137,7 +139,7 @@ describe("Job", function()
       assert.are.same(job:result(), results)
     end)
 
-    pending("should return last line when there is no ending newline", function()
+    it("should return last line when there is no ending newline", function()
       local results = {}
       local job = Job:new {
         command = "printf",