Skip to content

Commit

Permalink
Add fail() function (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonsturgeon authored Apr 3, 2024
1 parent 86093eb commit 0bf7738
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
5 changes: 3 additions & 2 deletions lua/gluatest/runner/helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ function Helpers.FailCallback( reason )
}
end

function Helpers.MakeAsyncEnv( onDone, onFailedExpectation )
function Helpers.MakeAsyncEnv( done, fail, onFailedExpectation )
-- TODO: How can we make Stubs safer in Async environments?
local stub, stubCleanup = stubMaker()
local testEnv, envCleanup = makeTestLibStubs()
Expand Down Expand Up @@ -227,7 +227,8 @@ function Helpers.MakeAsyncEnv( onDone, onFailedExpectation )
return built
end,

done = onDone,
done = done,
fail = fail,
stub = stub,
},
{
Expand Down
13 changes: 11 additions & 2 deletions lua/gluatest/runner/runner.lua
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ return function( allTestGroups )
checkComplete()
end

local onDone = function()
local done = function()
if callbacks[case.id] ~= nil then return end

if not expectationFailure then
Expand All @@ -221,6 +221,14 @@ return function( allTestGroups )
case.testComplete()
end

local fail = function( reason )
if callbacks[case.id] ~= nil then return end

setFailed( case, { reason = reason or "fail() called" } )
callbacks[case.id] = false
case.testComplete()
end

-- Received an expectation failure
-- We will record it here, but still expect them
-- to call done().
Expand All @@ -234,7 +242,7 @@ return function( allTestGroups )
expectationFailure = true
end

local asyncEnv, asyncCleanupFunc = MakeAsyncEnv( onDone, onFailedExpectation )
local asyncEnv, asyncCleanupFunc = MakeAsyncEnv( done, fail, onFailedExpectation )
asyncCleanup = asyncCleanupFunc

setfenv( testGroup.beforeEach, asyncEnv )
Expand Down Expand Up @@ -270,6 +278,7 @@ return function( allTestGroups )
for id, case in pairs( asyncCases ) do
if callbacks[id] == nil then
setTimedOut( case )
callbacks[case.id] = false

local shouldCheckComplete = false
case.testComplete( shouldCheckComplete )
Expand Down

0 comments on commit 0bf7738

Please sign in to comment.