Skip to content

Commit 27e4f0a

Browse files
committed
fix(test): harden child lifecycle cleanup
1 parent 593ff07 commit 27e4f0a

6 files changed

Lines changed: 113 additions & 5 deletions

File tree

‎tests/e2e/astrocore_spec.lua‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,10 @@ T["MAP-08 uses terminal navigation mappings in ordinary and floating terminal wi
576576
assert.equals(50, child.lua_get "vim.api.nvim_win_get_config(0).zindex")
577577
child.lua [[
578578
for _, job_id in ipairs(vim.g.astronvim_test_terminal_jobs or {}) do
579-
if job_id then vim.fn.jobstop(job_id) end
579+
if job_id then
580+
vim.fn.jobstop(job_id)
581+
vim.fn.jobwait({ job_id }, 1000)
582+
end
580583
end
581584
]]
582585
end

‎tests/e2e/dap_spec.lua‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ local T = MiniTest.new_set {
77
hooks = {
88
pre_case = function() child = nil end,
99
post_case = function()
10+
if child then
11+
pcall(
12+
child.lua,
13+
[[
14+
local dap = require "dap"
15+
if dap.session() then dap.terminate { all = true } end
16+
]]
17+
)
18+
pcall(helpers.wait_until, child, "require('dap').session() == nil", "DAP fixture shutdown", 1000)
19+
end
1020
helpers.stop_child(child)
1121
child = nil
1222
end,

‎tests/e2e/lsp_spec.lua‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,23 @@ local T = MiniTest.new_set {
77
hooks = {
88
pre_case = function() child = nil end,
99
post_case = function()
10+
if child then
11+
pcall(
12+
child.lua,
13+
[[
14+
for _, client in ipairs(vim.lsp.get_clients { name = "astronvim-e2e-lsp" }) do
15+
client:stop()
16+
end
17+
]]
18+
)
19+
pcall(
20+
helpers.wait_until,
21+
child,
22+
"#vim.lsp.get_clients({ name = 'astronvim-e2e-lsp' }) == 0",
23+
"LSP fixture shutdown",
24+
1000
25+
)
26+
end
1027
helpers.stop_child(child)
1128
child = nil
1229
end,

‎tests/e2e/workflows_spec.lua‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,12 @@ T["TERM-05 owns base options and bounds terminal lifecycle through AstroNvim map
160160
return true
161161
end, 20)
162162
163+
local terminal_job = terminal_buffer and vim.b[terminal_buffer].terminal_job_id
164+
if terminal_job then
165+
vim.fn.jobstop(terminal_job)
166+
vim.fn.jobwait({ terminal_job }, 1000)
167+
end
168+
163169
return {
164170
mapping_rhs = mapping.rhs,
165171
size = spec.opts.size,

‎tests/helpers.lua‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,15 @@ end
287287

288288
function M.start_ready_child(plugins)
289289
local child = M.start_child()
290-
M.wait_until(child, "vim.g.astronvim_test_ready == true", "VimEnter and LazyDone")
291-
if plugins then child.lua(([=[require("lazy").load { plugins = %s }]=]):format(vim.inspect(plugins))) end
290+
local ok, err = xpcall(function()
291+
M.wait_until(child, "vim.g.astronvim_test_ready == true", "VimEnter and LazyDone")
292+
if plugins then child.lua(([=[require("lazy").load { plugins = %s }]=]):format(vim.inspect(plugins))) end
293+
end, debug.traceback)
294+
if not ok then
295+
local cleanup_ok, cleanup_error = pcall(M.stop_child, child)
296+
if not cleanup_ok then err = err .. "\nFailed child cleanup: " .. cleanup_error_message(cleanup_error) end
297+
error(err, 0)
298+
end
292299
return child
293300
end
294301

@@ -304,17 +311,26 @@ end
304311
function M.wait_until(child, expression, description, timeout)
305312
local timeout_ms = timeout or config.wait_timeout
306313
local deadline = vim.uv.hrtime() + timeout_ms * 1000000
314+
local last_error
307315

308316
while true do
309317
local ok, value = pcall(child.lua_get, expression)
310318
if ok and value == true then return true end
319+
if not ok then
320+
last_error = value
321+
local running_ok, running = pcall(child.is_running)
322+
if running_ok and not running then
323+
error(("Child Neovim exited while waiting for %s: %s"):format(description, cleanup_error_message(value)), 0)
324+
end
325+
end
311326

312327
local remaining_ms = math.floor((deadline - vim.uv.hrtime()) / 1000000)
313328
if remaining_ms <= 0 then break end
314329
vim.wait(math.min(20, remaining_ms))
315330
end
316331

317-
error(("Timed out waiting for %s"):format(description), 0)
332+
local detail = last_error and (": " .. cleanup_error_message(last_error)) or ""
333+
error(("Timed out waiting for %s%s"):format(description, detail), 0)
318334
end
319335

320336
function M.stop_child(child)

‎tests/unit/e2e_helpers_spec.lua‎

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ local function with_helpers(options, callback)
3939
if value == "error" then error "child RPC failed" end
4040
return value == nil and false or value
4141
end,
42-
lua = function(expression) table.insert(state.lua_calls, expression) end,
42+
lua = function(expression)
43+
table.insert(state.lua_calls, expression)
44+
if options.lua_error then error(options.lua_error) end
45+
end,
4346
start = function(arguments)
4447
state.start_arguments = arguments
4548
if options.start_error then error(options.start_error) end
@@ -165,6 +168,33 @@ T["WAIT-01 retries child RPC conditions after a false result"] = function()
165168
end)
166169
end
167170

171+
T["WAIT-02 reports a dead child instead of masking the RPC error as a timeout"] = function()
172+
with_helpers({ wait_values = { "error" }, child_running = false }, function(helpers, state)
173+
local child = helpers.start_child()
174+
local ok, err = pcall(helpers.wait_until, child, "ready", "child readiness", 100)
175+
176+
assert.is_false(ok)
177+
assert_contains(err, "Child Neovim exited while waiting for child readiness")
178+
assert_contains(err, "child RPC failed")
179+
assert.equals(1, state.lua_get_calls)
180+
assert.equals(0, state.wait_calls)
181+
helpers.stop_child(child)
182+
end)
183+
end
184+
185+
T["WAIT-03 retains the final RPC error when a live child never becomes ready"] = function()
186+
with_helpers({ wait_values = { "error" } }, function(helpers, state)
187+
local child = helpers.start_child()
188+
local ok, err = pcall(helpers.wait_until, child, "ready", "child readiness", 100)
189+
190+
assert.is_false(ok)
191+
assert_contains(err, "Timed out waiting for child readiness")
192+
assert_contains(err, "child RPC failed")
193+
assert.is_true(state.lua_get_calls > 1)
194+
helpers.stop_child(child)
195+
end)
196+
end
197+
168198
T["CHILD-01 starts a ready child and loads requested plugins"] = function()
169199
with_helpers({ wait_values = { true } }, function(helpers, state, root)
170200
local child = helpers.start_ready_child { "astrolsp" }
@@ -177,6 +207,32 @@ T["CHILD-01 starts a ready child and loads requested plugins"] = function()
177207
end)
178208
end
179209

210+
T["CHILD-02 cleans a child when readiness fails before the caller receives it"] = function()
211+
with_helpers({ wait_values = { false } }, function(helpers, state, root)
212+
local ok, err = pcall(helpers.start_ready_child)
213+
local fixture_available = pcall(helpers.fixture_project, state.child)
214+
215+
assert.is_false(ok)
216+
assert_contains(err, "Timed out waiting for VimEnter and LazyDone")
217+
assert.equals(1, state.stop_attempts)
218+
assert.same({ root }, state.deleted_roots)
219+
assert.is_false(fixture_available)
220+
end)
221+
end
222+
223+
T["CHILD-03 cleans a child when requested plugin loading fails"] = function()
224+
with_helpers({ wait_values = { true }, lua_error = "plugin loading failed" }, function(helpers, state, root)
225+
local ok, err = pcall(helpers.start_ready_child, { "astrolsp" })
226+
local fixture_available = pcall(helpers.fixture_project, state.child)
227+
228+
assert.is_false(ok)
229+
assert_contains(err, "plugin loading failed")
230+
assert.equals(1, state.stop_attempts)
231+
assert.same({ root }, state.deleted_roots)
232+
assert.is_false(fixture_available)
233+
end)
234+
end
235+
180236
T["PORT-01 creates fixtures under the platform temp directory and copies them through libuv"] = function()
181237
with_helpers({}, function(helpers, state, root)
182238
local child = helpers.start_child()

0 commit comments

Comments
 (0)