Skip to content

Commit

Permalink
fix(strategies): handle quick exit
Browse files Browse the repository at this point in the history
  • Loading branch information
rcarriga committed Dec 28, 2024
1 parent 5da5878 commit 0dccb5e
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 10 deletions.
19 changes: 17 additions & 2 deletions lua/neotest/client/strategies/integrated/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ return function(spec)
local env, cwd = spec.env, spec.cwd

local finish_future = nio.control.future()
local output_finish_future = nio.control.future()
local result_code = nil
local command = spec.command
local output_accum = FanoutAccum(function(prev, new)
Expand Down Expand Up @@ -40,6 +41,10 @@ return function(spec)
height = spec.strategy.height,
width = spec.strategy.width,
on_stdout = function(_, data)
if #data == 1 and data[1] == "" then
output_finish_future.set()
return
end
output_accum:push(table.concat(data, "\n"))
end,
on_exit = function(_, code)
Expand Down Expand Up @@ -69,7 +74,7 @@ return function(spec)
queue.put_nowait(d)
end)
return function()
local data = nio.first({ queue.get, finish_future.wait })
local data = nio.first({ queue.get, output_finish_future.wait })
if data then
return data
end
Expand Down Expand Up @@ -114,7 +119,17 @@ return function(spec)
end,
result = function()
if result_code == nil then
finish_future:wait()
finish_future.wait()
if not output_finish_future.is_set() then
-- jobstart doesn't necessarily call on_stdout if the process
-- stops quickly, so add a timeout to prevent deadlock
nio.first({
output_finish_future.wait,
function()
nio.sleep(100)
end,
})
end
end
local close_err = nio.uv.fs_close(output_fd)
assert(not close_err, close_err)
Expand Down
8 changes: 7 additions & 1 deletion lua/neotest/consumers/output_panel/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,13 @@ local init = function(client)
for file, _ in pairs(files_to_read) do
local output = lib.files.read(file)
local dos_newlines = string.find(output, "\r\n") ~= nil
if not pcall(nio.api.nvim_chan_send, chan, dos_newlines and output or output:gsub("\n", "\r\n")) then
if
not pcall(
nio.api.nvim_chan_send,
chan,
dos_newlines and output or output:gsub("\n", "\r\n")
)
then
lib.notify(("Error sending output to term channel: %s"):format(chan), vim.log.levels.ERROR)
chan = nil
break
Expand Down
2 changes: 1 addition & 1 deletion lua/neotest/consumers/status.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ local function init(client)
virt_text = {
{ statuses[status].text .. " ", statuses[status].texthl },
},
hl_mode = "combine"
hl_mode = "combine",
})
end
end
Expand Down
8 changes: 5 additions & 3 deletions lua/neotest/consumers/watch/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ neotest.watch = {}
local init = function(client)
client.listeners.discover_positions = function(_, tree)
for _, watcher in pairs(watchers) do
if watcher.tree:data().path == tree:data().path
and not watcher.discover_positions_event.is_set() then
watcher.discover_positions_event.set()
if
watcher.tree:data().path == tree:data().path
and not watcher.discover_positions_event.is_set()
then
watcher.discover_positions_event.set()
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/consumers/output_panel_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe("neotest consumer - output_panel", function()
local files
local dirs = { dir }
local notify
local notify_msg = ''
local notify_msg = ""

---@return neotest.Tree
local get_pos = function(...)
Expand Down Expand Up @@ -136,7 +136,7 @@ describe("neotest consumer - output_panel", function()
for _, buf in ipairs(vim.api.nvim_list_bufs()) do
vim.api.nvim_buf_delete(buf, { force = true })
end
notify_msg = ''
notify_msg = ""
end)

describe("user forcefully closes the panel", function()
Expand Down Expand Up @@ -165,7 +165,7 @@ describe("neotest consumer - output_panel", function()
assert.has_no_error(function()
client:run_tree(tree, { strategy = mock_strategy })
end)
assert.is_not.matches('Error sending output to term channel:', notify_msg)
assert.is_not.matches("Error sending output to term channel:", notify_msg)
end)
exit_future_2.set()
end)
Expand Down

0 comments on commit 0dccb5e

Please sign in to comment.