Skip to content

Commit 0dccb5e

Browse files
committed
fix(strategies): handle quick exit
1 parent 5da5878 commit 0dccb5e

File tree

5 files changed

+33
-10
lines changed

5 files changed

+33
-10
lines changed

lua/neotest/client/strategies/integrated/init.lua

+17-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ return function(spec)
1313
local env, cwd = spec.env, spec.cwd
1414

1515
local finish_future = nio.control.future()
16+
local output_finish_future = nio.control.future()
1617
local result_code = nil
1718
local command = spec.command
1819
local output_accum = FanoutAccum(function(prev, new)
@@ -40,6 +41,10 @@ return function(spec)
4041
height = spec.strategy.height,
4142
width = spec.strategy.width,
4243
on_stdout = function(_, data)
44+
if #data == 1 and data[1] == "" then
45+
output_finish_future.set()
46+
return
47+
end
4348
output_accum:push(table.concat(data, "\n"))
4449
end,
4550
on_exit = function(_, code)
@@ -69,7 +74,7 @@ return function(spec)
6974
queue.put_nowait(d)
7075
end)
7176
return function()
72-
local data = nio.first({ queue.get, finish_future.wait })
77+
local data = nio.first({ queue.get, output_finish_future.wait })
7378
if data then
7479
return data
7580
end
@@ -114,7 +119,17 @@ return function(spec)
114119
end,
115120
result = function()
116121
if result_code == nil then
117-
finish_future:wait()
122+
finish_future.wait()
123+
if not output_finish_future.is_set() then
124+
-- jobstart doesn't necessarily call on_stdout if the process
125+
-- stops quickly, so add a timeout to prevent deadlock
126+
nio.first({
127+
output_finish_future.wait,
128+
function()
129+
nio.sleep(100)
130+
end,
131+
})
132+
end
118133
end
119134
local close_err = nio.uv.fs_close(output_fd)
120135
assert(not close_err, close_err)

lua/neotest/consumers/output_panel/init.lua

+7-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,13 @@ local init = function(client)
5555
for file, _ in pairs(files_to_read) do
5656
local output = lib.files.read(file)
5757
local dos_newlines = string.find(output, "\r\n") ~= nil
58-
if not pcall(nio.api.nvim_chan_send, chan, dos_newlines and output or output:gsub("\n", "\r\n")) then
58+
if
59+
not pcall(
60+
nio.api.nvim_chan_send,
61+
chan,
62+
dos_newlines and output or output:gsub("\n", "\r\n")
63+
)
64+
then
5965
lib.notify(("Error sending output to term channel: %s"):format(chan), vim.log.levels.ERROR)
6066
chan = nil
6167
break

lua/neotest/consumers/status.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ local function init(client)
4545
virt_text = {
4646
{ statuses[status].text .. " ", statuses[status].texthl },
4747
},
48-
hl_mode = "combine"
48+
hl_mode = "combine",
4949
})
5050
end
5151
end

lua/neotest/consumers/watch/init.lua

+5-3
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@ neotest.watch = {}
3232
local init = function(client)
3333
client.listeners.discover_positions = function(_, tree)
3434
for _, watcher in pairs(watchers) do
35-
if watcher.tree:data().path == tree:data().path
36-
and not watcher.discover_positions_event.is_set() then
37-
watcher.discover_positions_event.set()
35+
if
36+
watcher.tree:data().path == tree:data().path
37+
and not watcher.discover_positions_event.is_set()
38+
then
39+
watcher.discover_positions_event.set()
3840
end
3941
end
4042
end

tests/unit/consumers/output_panel_spec.lua

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe("neotest consumer - output_panel", function()
2323
local files
2424
local dirs = { dir }
2525
local notify
26-
local notify_msg = ''
26+
local notify_msg = ""
2727

2828
---@return neotest.Tree
2929
local get_pos = function(...)
@@ -136,7 +136,7 @@ describe("neotest consumer - output_panel", function()
136136
for _, buf in ipairs(vim.api.nvim_list_bufs()) do
137137
vim.api.nvim_buf_delete(buf, { force = true })
138138
end
139-
notify_msg = ''
139+
notify_msg = ""
140140
end)
141141

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

0 commit comments

Comments
 (0)