diff --git a/.luacheckrc b/.luacheckrc index a3b85faa..71aa4c39 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -42,6 +42,7 @@ files = { "clear", "assert", "print", + "RUN_INSIDE_COROUTINE", }, }, ["lua/plenary/async/init.lua"] = { diff --git a/lua/plenary/busted.lua b/lua/plenary/busted.lua index 1b15fcec..684b74ba 100644 --- a/lua/plenary/busted.lua +++ b/lua/plenary/busted.lua @@ -43,6 +43,7 @@ end local mod = {} +local describes = {} local results = {} local current_description = {} local current_before_each = {} @@ -116,16 +117,18 @@ mod.describe = function(desc, func) results.fail = results.fail or {} results.errs = results.errs or {} - describe = mod.inner_describe - local ok, msg, desc_stack = call_inner(desc, func) - describe = mod.describe + table.insert(describes, function() + describe = mod.inner_describe + local ok, msg, desc_stack = call_inner(desc, func) + describe = mod.describe - if not ok then - table.insert(results.errs, { - descriptions = desc_stack, - msg = msg, - }) - end + if not ok then + table.insert(results.errs, { + descriptions = desc_stack, + msg = msg, + }) + end + end) end mod.inner_describe = function(desc, func) @@ -214,13 +217,47 @@ after_each = mod.after_each clear = mod.clear assert = require "luassert" +local main = function() + for _, d in ipairs(describes) do + d() + end + + -- If nothing runs (empty file without top level describe) + if not results.pass then + if is_headless then + return vim.cmd "0cq" + else + return + end + end + + mod.format_results(results) + + if #results.errs ~= 0 then + print("We had an unexpected error: ", vim.inspect(results.errs), vim.inspect(results)) + if is_headless then + return vim.cmd "2cq" + end + elseif #results.fail > 0 then + print "Tests Failed. Exit: 1" + + if is_headless then + return vim.cmd "1cq" + end + else + if is_headless then + return vim.cmd "0cq" + end + end +end + mod.run = function(file) print("\n" .. HEADER) print("Testing: ", file) - local loaded, msg = loadfile(file) + local ok, msg = pcall(dofile, file) - if not loaded then + if not ok then print(HEADER) print "FAILED TO LOAD FILE" print(color_string("red", msg)) @@ -232,37 +269,11 @@ mod.run = function(file) end end - coroutine.wrap(function() - loaded() - - -- If nothing runs (empty file without top level describe) - if not results.pass then - if is_headless then - return vim.cmd "0cq" - else - return - end - end - - mod.format_results(results) - - if #results.errs ~= 0 then - print("We had an unexpected error: ", vim.inspect(results.errs), vim.inspect(results)) - if is_headless then - return vim.cmd "2cq" - end - elseif #results.fail > 0 then - print "Tests Failed. Exit: 1" - - if is_headless then - return vim.cmd "1cq" - end - else - if is_headless then - return vim.cmd "0cq" - end - end - end)() + if RUN_INSIDE_COROUTINE then + coroutine.wrap(main)() + else + main() + end end return mod diff --git a/tests/plenary/async_testing_spec.lua b/tests/plenary/async_testing_spec.lua index c1c991be..2167e25f 100644 --- a/tests/plenary/async_testing_spec.lua +++ b/tests/plenary/async_testing_spec.lua @@ -1,5 +1,7 @@ local Job = require "plenary.job" +RUN_INSIDE_COROUTINE = true + local Timing = {} function Timing:log(name)