Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .luacheckrc
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ files = {
"clear",
"assert",
"print",
"RUN_INSIDE_COROUTINE",
},
},
["lua/plenary/async/init.lua"] = {
Expand Down
95 changes: 53 additions & 42 deletions lua/plenary/busted.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ end

local mod = {}

local describes = {}
local results = {}
local current_description = {}
local current_before_each = {}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand All @@ -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
2 changes: 2 additions & 0 deletions tests/plenary/async_testing_spec.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
local Job = require "plenary.job"

RUN_INSIDE_COROUTINE = true

local Timing = {}

function Timing:log(name)
Expand Down