Skip to content
Draft
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
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,15 @@ Assuming you have luarocks installed:
Install these dependencies for core testing:

```
luarocks install moonscript
luarocks --local install moonscript
```

Then to reinstall and run tests:

```
luarocks remove busted --force
luarocks make
busted spec
luarocks --local remove busted --force
luarocks --local make
~/.luarocks/bin/busted spec
```

Docker
Expand Down
40 changes: 29 additions & 11 deletions busted/init.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local function init(busted)
local block = require 'busted.block'(busted)
local block = require 'busted.block' (busted)

local file = function(file)
busted.wrap(file.run)
Expand All @@ -20,6 +20,8 @@ local function init(busted)
local it = function(element)
local parent = busted.context.parent(element)
local finally
local attempt = 1
local max_attempts = 1

if not block.lazySetup(parent) then
-- skip test if any setup failed
Expand All @@ -31,24 +33,40 @@ local function init(busted)
block.rejectAll(element)
element.env.finally = function(fn) finally = fn end
element.env.pending = busted.pending
element.env.set_retries = function(n) max_attempts = n + 1 end

local status = busted.status('success')
local pass, ancestor = block.execAll('before_each', parent, true)
if pass and busted.safe_publish('test', { 'test', 'start' }, element, parent) then
while attempt <= max_attempts do
-- Run after_each from previous attempt before before_each (for retries)
if attempt > 1 then
block.dexecAll('after_each', ancestor, true)
pass, ancestor = block.execAll('before_each', parent, true)
end
local attempt_status = busted.safe('it', element.run, element)

if pass then
local status = busted.status('success')
if busted.safe_publish('test', { 'test', 'start' }, element, parent) then
status:update(busted.safe('it', element.run, element))
if finally then
block.reject('pending', element)
status:update(busted.safe('finally', finally, element))
end
else
status = busted.status('error')

if attempt_status:success() then
status = busted.status('success')
break
else
status = attempt_status
end

attempt = attempt + 1
end
busted.safe_publish('test', { 'test', 'end' }, element, parent, tostring(status))
end

block.dexecAll('after_each', ancestor, true)
-- Run after_each after the last try.
block.dexecAll('after_each', ancestor, true)
else
status = busted.status('error')
end
busted.safe_publish('test', { 'test', 'end' }, element, parent, tostring(status))
end

local pending = function(element)
Expand Down Expand Up @@ -93,7 +111,7 @@ local function init(busted)
local stub = busted.require 'luassert.stub'
local match = busted.require 'luassert.match'

require 'busted.fixtures' -- just load into the environment, not exposing it
require 'busted.fixtures' -- just load into the environment, not exposing it

busted.export('assert', assert)
busted.export('spy', spy)
Expand Down
9 changes: 8 additions & 1 deletion busted/outputHandlers/base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ return function()
name = handler.getFullName(element),
message = message,
randomseed = parent and parent.randomseed,
isError = isError
isError = isError,
id = tostring(element)
}
formatted.element.trace = element.trace or debug

Expand Down Expand Up @@ -120,6 +121,12 @@ return function()
local insertTable

if status == 'success' then
-- Remove any failures for this test since it succeeded
for i = #handler.failures, 1, -1 do
if handler.failures[i].id == tostring(element) then
table.remove(handler.failures, i)
Comment on lines 123 to +127

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels a bit hacky. Is there a better way for the it() implementation in busted/init.lua to avoid reporting "failures" until all retries are exhausted?

end
end
insertTable = handler.successes
handler.successesCount = handler.successesCount + 1
elseif status == 'pending' then
Expand Down
23 changes: 23 additions & 0 deletions spec/core_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,29 @@ assert(type(mock) == 'table')
assert(type(match) == 'table')
assert(type(assert) == 'table')

describe('retry test', function()

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should the set_retries tests go in a new _spec.lua file?

local attempt_count = 0

before_each(function()
attempt_count = attempt_count + 1
print("Before each for attempt " .. attempt_count)
end)

after_each(function()
print("After each for attempt " .. attempt_count)
end)

it('should succeed on second attempt', function()
set_retries(2) -- 3 total attempts
print("Running attempt " .. attempt_count)
if attempt_count < 3 then
assert.is_true(false, 'Failing attempt ' .. attempt_count)
else
assert.is_true(true, 'Succeeding on attempt ' .. attempt_count)
end
end)
end)

describe('Before each', function()
local test_val = false

Expand Down
Loading