Skip to content

Commit 7c7c2be

Browse files
committed
feat: optional busted async runtime
1 parent 9d81624 commit 7c7c2be

File tree

2 files changed

+55
-42
lines changed

2 files changed

+55
-42
lines changed

lua/plenary/busted.lua

Lines changed: 53 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ end
4343

4444
local mod = {}
4545

46+
local describes = {}
4647
local results = {}
4748
local current_description = {}
4849
local current_before_each = {}
@@ -116,16 +117,18 @@ mod.describe = function(desc, func)
116117
results.fail = results.fail or {}
117118
results.errs = results.errs or {}
118119

119-
describe = mod.inner_describe
120-
local ok, msg, desc_stack = call_inner(desc, func)
121-
describe = mod.describe
120+
table.insert(describes, function()
121+
describe = mod.inner_describe
122+
local ok, msg, desc_stack = call_inner(desc, func)
123+
describe = mod.describe
122124

123-
if not ok then
124-
table.insert(results.errs, {
125-
descriptions = desc_stack,
126-
msg = msg,
127-
})
128-
end
125+
if not ok then
126+
table.insert(results.errs, {
127+
descriptions = desc_stack,
128+
msg = msg,
129+
})
130+
end
131+
end)
129132
end
130133

131134
mod.inner_describe = function(desc, func)
@@ -214,13 +217,47 @@ after_each = mod.after_each
214217
clear = mod.clear
215218
assert = require "luassert"
216219

220+
local main = function()
221+
for _, d in ipairs(describes) do
222+
d()
223+
end
224+
225+
-- If nothing runs (empty file without top level describe)
226+
if not results.pass then
227+
if is_headless then
228+
return vim.cmd "0cq"
229+
else
230+
return
231+
end
232+
end
233+
234+
mod.format_results(results)
235+
236+
if #results.errs ~= 0 then
237+
print("We had an unexpected error: ", vim.inspect(results.errs), vim.inspect(results))
238+
if is_headless then
239+
return vim.cmd "2cq"
240+
end
241+
elseif #results.fail > 0 then
242+
print "Tests Failed. Exit: 1"
243+
244+
if is_headless then
245+
return vim.cmd "1cq"
246+
end
247+
else
248+
if is_headless then
249+
return vim.cmd "0cq"
250+
end
251+
end
252+
end
253+
217254
mod.run = function(file)
218255
print("\n" .. HEADER)
219256
print("Testing: ", file)
220257

221-
local loaded, msg = loadfile(file)
258+
local ok, msg = pcall(dofile, file)
222259

223-
if not loaded then
260+
if not ok then
224261
print(HEADER)
225262
print "FAILED TO LOAD FILE"
226263
print(color_string("red", msg))
@@ -232,37 +269,11 @@ mod.run = function(file)
232269
end
233270
end
234271

235-
coroutine.wrap(function()
236-
loaded()
237-
238-
-- If nothing runs (empty file without top level describe)
239-
if not results.pass then
240-
if is_headless then
241-
return vim.cmd "0cq"
242-
else
243-
return
244-
end
245-
end
246-
247-
mod.format_results(results)
248-
249-
if #results.errs ~= 0 then
250-
print("We had an unexpected error: ", vim.inspect(results.errs), vim.inspect(results))
251-
if is_headless then
252-
return vim.cmd "2cq"
253-
end
254-
elseif #results.fail > 0 then
255-
print "Tests Failed. Exit: 1"
256-
257-
if is_headless then
258-
return vim.cmd "1cq"
259-
end
260-
else
261-
if is_headless then
262-
return vim.cmd "0cq"
263-
end
264-
end
265-
end)()
272+
if RUN_INSIDE_COROUTINE then
273+
coroutine.wrap(main)()
274+
else
275+
main()
276+
end
266277
end
267278

268279
return mod

tests/plenary/async_testing_spec.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
local Job = require "plenary.job"
22

3+
RUN_INSIDE_COROUTINE = true
4+
35
local Timing = {}
46

57
function Timing:log(name)

0 commit comments

Comments
 (0)