From 8555ca548d6a0061e0818a7c320804c11050d73a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=B3n=C3=A1n=20Carrigan?= Date: Mon, 4 Jul 2022 09:35:48 +0100 Subject: [PATCH] feat(strategies/integrated): async write --- lua/neotest/client/init.lua | 10 ++++--- .../client/strategies/integrated/init.lua | 27 +++++++++++-------- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/lua/neotest/client/init.lua b/lua/neotest/client/init.lua index e4614a28..e0f83cdd 100644 --- a/lua/neotest/client/init.lua +++ b/lua/neotest/client/init.lua @@ -213,9 +213,11 @@ function NeotestClient:_update_positions(path, args) -- If existing tree then we have to find the point to merge the trees and update that path rather than trying to -- merge an orphan. This happens when a whole new directory is found (e.g. renamed an existing one). local existing_root = self:get_position(nil, { adapter = adapter_id }) - while existing_root - and vim.startswith(path, existing_root:data().path) - and not self:get_position(path, { adapter = adapter_id }) do + while + existing_root + and vim.startswith(path, existing_root:data().path) + and not self:get_position(path, { adapter = adapter_id }) + do path = lib.files.parent(path) if not vim.startswith(path, existing_root:data().path) then return @@ -383,7 +385,7 @@ end function NeotestClient:_update_adapters(path) local adapters_with_root = lib.files.is_dir(path) and self._adapter_group:adapters_with_root_dir(path) - or {} + or {} local adapters_with_bufs = self._adapter_group:adapters_matching_open_bufs() local found = {} for _, adapter in pairs(self._adapters) do diff --git a/lua/neotest/client/strategies/integrated/init.lua b/lua/neotest/client/strategies/integrated/init.lua index d6068c0a..2620d962 100644 --- a/lua/neotest/client/strategies/integrated/init.lua +++ b/lua/neotest/client/strategies/integrated/init.lua @@ -18,8 +18,8 @@ return function(spec) local unread_data = "" local attach_win, attach_buf, attach_chan local output_path = async.fn.tempname() - -- TODO: Resolve permissions issues with opening file with luv - local output_file = assert(io.open(output_path, "w")) + local open_err, output_fd = async.uv.fs_open(output_path, "w", 438) + assert(not open_err, open_err) local success, job = pcall(async.fn.jobstart, command, { cwd = cwd, env = env, @@ -27,13 +27,16 @@ return function(spec) height = spec.strategy.height, width = spec.strategy.width, on_stdout = function(_, data) - data = table.concat(data, "\n") - unread_data = unread_data .. data - output_file:write(data) - if attach_chan then - async.api.nvim_chan_send(attach_chan, unread_data) - unread_data = "" - end + async.run(function() + data = table.concat(data, "\n") + unread_data = unread_data .. data + local write_err, _ = async.uv.fs_write(output_fd, data) + assert(not write_err, write_err) + if attach_chan then + async.api.nvim_chan_send(attach_chan, unread_data) + unread_data = "" + end + end) end, on_exit = function(_, code) result_code = code @@ -41,7 +44,8 @@ return function(spec) end, }) if not success then - output_file:write(job) + local write_err, _ = async.uv.fs_write(output_fd, job) + assert(not write_err, write_err) result_code = 1 finish_cond:notify_all() end @@ -86,7 +90,8 @@ return function(spec) if result_code == nil then finish_cond:wait() end - output_file:close() + local close_err = async.uv.fs_close(output_fd) + assert(not close_err, close_err) pcall(async.fn.chanclose, job) if attach_win then attach_win:listen("close", function()