Skip to content

Commit

Permalink
Use reporter for logging from runner
Browse files Browse the repository at this point in the history
  • Loading branch information
luttje committed Dec 4, 2024
1 parent 4cbad22 commit 9e4f57d
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 74 deletions.
24 changes: 13 additions & 11 deletions gmod-addon/lua/sh_jestronaut_gmod_reporter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ end

--- Prints the name of the test.
--- @param describeOrTest DescribeOrTest
function REPORTER:testStarting(describeOrTest)
function REPORTER:onTestStarting(describeOrTest)
-- Override print so there's no interference with the test output.
print = function() end -- TODO: Store the print and output it at the end of the test.

Expand All @@ -115,7 +115,7 @@ end
--- Prints the result of the test.
--- @param describeOrTest DescribeOrTest
--- @param success boolean
function REPORTER:testFinished(describeOrTest, success)
function REPORTER:onTestFinished(describeOrTest, success)
print = originalPrint

local file = self:getFileByPath(describeOrTest.filePath)
Expand Down Expand Up @@ -147,7 +147,7 @@ end

--- Prints the skip message of the test.
--- @param describeOrTest DescribeOrTest
function REPORTER:testSkipped(describeOrTest)
function REPORTER:onTestSkipped(describeOrTest)
local file = self:getFileByPath(describeOrTest.filePath)

if file then
Expand All @@ -163,7 +163,7 @@ end
--- Prints the retry message of the test.
--- @param describeOrTest DescribeOrTest
--- @param retryCount number
function REPORTER:testRetrying(describeOrTest, retryCount)
function REPORTER:onTestRetrying(describeOrTest, retryCount)
self:redrawSummary(self.isVerbose)
end

Expand Down Expand Up @@ -198,8 +198,9 @@ end
--- Stores the tests that will be run and prints the summary with header.
--- @param rootDescribe Describe
--- @param describesByFilePath table
function REPORTER:startTestSet(rootDescribe, describesByFilePath)
local totalTestCount = rootDescribe.childCount + rootDescribe.grandChildrenCount
--- @param skippedTestCount number
function REPORTER:onStartTestSet(rootDescribe, describesByFilePath, skippedTestCount)
local totalTestCount = rootDescribe.childTestCount + rootDescribe.grandChildrenTestCount + skippedTestCount

self.summaryHeader = styledText.new(nil, STYLING_DISABLED)
:plain("🚀 Starting ")
Expand All @@ -214,12 +215,13 @@ function REPORTER:startTestSet(rootDescribe, describesByFilePath)
end

--- Prints the success message of the test.
--- @param rootDescribe Describe
--- @param processedTests DescribeOrTest[]
--- @param passedTestCount number
--- @param failedTestCount number
--- @param skippedTestCount number
--- @param duration number
function REPORTER:printEnd(rootDescribe, failedTestCount, skippedTestCount, duration)
local totalTestCount = rootDescribe.childCount + rootDescribe.grandChildrenCount
--- @param testDuration number
function REPORTER:onEndTestSet(processedTests, passedTestCount, failedTestCount, skippedTestCount, testDuration)
local totalTestCount = passedTestCount + failedTestCount + skippedTestCount
local notRunCount = failedTestCount + skippedTestCount
local relativeSuccess = 1 - (notRunCount / totalTestCount)

Expand Down Expand Up @@ -259,7 +261,7 @@ function REPORTER:printEnd(rootDescribe, failedTestCount, skippedTestCount, dura

originalPrint(
styledText.new(nil, STYLING_DISABLED)
:plain("Time: " .. duration .. "s")
:plain("Time: " .. testDuration .. "s")
)

self:printNewline()
Expand Down
65 changes: 34 additions & 31 deletions libs/jestronaut/environment/runner.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ local function newTestRunner(runnerOptions)

self.timeout = 5
self.isCompleted = false
self.isStarted = false

self.preTestCallback = nil
self.modifyTestResultCallback = nil
Expand Down Expand Up @@ -74,6 +75,7 @@ end

function TEST_RUNNER:reset()
self.isCompleted = false
self.isStarted = false

for _, test in ipairs(self.processedTests) do
test.status = "starting"
Expand All @@ -86,20 +88,17 @@ function TEST_RUNNER:reset()
self.processedTests = {}
end

function TEST_RUNNER:markFinished(test, status, err)
test.status = status
test.error = err
function TEST_RUNNER:markFinished(queuedTest, status, err)
queuedTest.status = status
queuedTest.error = err

-- TODO: Use reporter instead
if status == "passed" then
print(string.format("✅ PASSED %s", test.name))
elseif status == "skipped" then
print(string.format("⏭️ SKIPPED %s", test.name))
table.insert(self.processedTests, queuedTest)

if (status == nil) then
self.reporter:onTestSkipped(queuedTest.test)
else
print(string.format("❌ FAILED %s - %s", test.name, test.error or "Unknown error"))
self.reporter:onTestFinished(queuedTest.test, status, err)
end

table.insert(self.processedTests, test)
end

function TEST_RUNNER:runTest(queuedTest)
Expand Down Expand Up @@ -130,22 +129,28 @@ function TEST_RUNNER:runTest(queuedTest)
end

-- TODO: Should this also run here for async tests? Even though those may not have finished yet?
if self.postTestCallback then
self.postTestCallback(queuedTest.test, queuedTest.status == "passed")
if self.postTestCallback and queuedTest.status ~= nil then
self.postTestCallback(queuedTest.test, queuedTest.status == true)
end

return status, errorMessage
end

function TEST_RUNNER:statusToPassFailText(status)
return status and "passed" or "failed"
function TEST_RUNNER:start(rootDescribe, describesByFilePath, skippedTestCount)
self.isStarted = true
self.startTime = os.time()
self.reporter:onStartTestSet(rootDescribe, describesByFilePath, skippedTestCount)
end

function TEST_RUNNER:tick()
if self.isCompleted then
return false
end

if not self.isStarted then
error("Test runner not started! Did you forget to call `:start()`?")
end

local remainingTests = {}

-- Process queued tests
Expand All @@ -156,7 +161,7 @@ function TEST_RUNNER:tick()
-- Run sync tests immediately
local success, errorMessage = self:runTest(queuedTest)

self:markFinished(queuedTest, self:statusToPassFailText(success), errorMessage)
self:markFinished(queuedTest, success, errorMessage)
elseif queuedTest.type == "async" then
-- Start async test if not started
if queuedTest.status == "starting" then
Expand All @@ -167,7 +172,7 @@ function TEST_RUNNER:tick()

-- Failed even while starting the test
if not success then
self:markFinished(queuedTest, self:statusToPassFailText(success), errorMessage)
self:markFinished(queuedTest, success, errorMessage)
else
-- Test started, might need further processing
queuedTest.result = queuedTest
Expand All @@ -187,7 +192,7 @@ function TEST_RUNNER:tick()
status, errorMessage = self.modifyTestResultCallback(queuedTest.test, status, errorMessage)
end

self:markFinished(queuedTest, status and "passed" or "failed", errorMessage)
self:markFinished(queuedTest, status, errorMessage)
elseif (queuedTest.asyncWrapper.isDone) then
local errorMessage = queuedTest.asyncWrapper.errorMessage
local success = not errorMessage
Expand All @@ -196,7 +201,7 @@ function TEST_RUNNER:tick()
success, errorMessage = self.modifyTestResultCallback(queuedTest.test, success, errorMessage)
end

self:markFinished(queuedTest, self:statusToPassFailText(success), errorMessage)
self:markFinished(queuedTest, success, errorMessage)
else
-- Test still in progress
table.insert(remainingTests, queuedTest)
Expand All @@ -221,32 +226,30 @@ function TEST_RUNNER:finalize()
return
end

self:printResults()
self.isCompleted = true
end
local testDuration = os.time() - self.startTime

self.isCompleted = true
self.isStarted = false

function TEST_RUNNER:printResults()
print("\n================================")
print("Test Results:")
self:printResults(testDuration)
end

function TEST_RUNNER:printResults(testDuration)
local passed = 0
local failed = 0
local skipped = 0

for _, test in ipairs(self.processedTests) do
if test.status == "passed" then
if test.status == true then
passed = passed + 1
elseif test.status == "skipped" then
elseif test.status == nil then
skipped = skipped + 1
else
failed = failed + 1
end
end

print(string.format("✅ Passed: %d", passed))
print(string.format("⏭️ Skipped: %d", skipped))
print(string.format("❌ Failed: %d", failed))
print("================================\n")
self.reporter:onEndTestSet(self.processedTests, passed, failed, skipped, testDuration)
end

-- --[[
Expand Down
29 changes: 19 additions & 10 deletions libs/jestronaut/environment/state.lua
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,9 @@ local DESCRIBE_OR_TEST_META = {
assertionCount = 0,
parent = nil,
childCount = 0,
childTestCount = 0,
grandChildrenCount = 0,
grandChildrenTestCount = 0,

--- @type DescribeOrTest[]
children = nil
Expand All @@ -222,13 +224,21 @@ DESCRIBE_OR_TEST_META.__index = DESCRIBE_OR_TEST_META
function DESCRIBE_OR_TEST_META:addChild(child)
self.childCount = self.childCount + 1

if child.isTest then
self.childTestCount = self.childTestCount + 1
end

self.children[self.childCount] = child
self.childrenLookup[child.name] = self.childCount

child.parent = self

if self.parent then
self.parent.grandChildrenCount = self.parent.grandChildrenCount + 1

if child.isTest then
self.parent.grandChildrenTestCount = self.parent.grandChildrenTestCount + 1
end
end
end

Expand All @@ -253,7 +263,7 @@ end
-- local failedTestCount = 0

-- if self.toSkip then
-- reporter:testSkipped(self)
-- reporter:onTestSkipped(self)

-- return failedTestCount
-- end
Expand All @@ -269,7 +279,7 @@ end
-- end

-- if self.isTest then
-- reporter:testStarting(self)
-- reporter:onTestStarting(self)
-- self.isRunning = true

-- local testLocalState = getTestLocalState(self.filePath)
Expand All @@ -296,7 +306,7 @@ end
-- end

-- if (success or (not retrySettings or retrySettings.options.logErrorsBeforeRetry)) then
-- reporter:testFinished(self, success)
-- reporter:onTestFinished(self, success)
-- end

-- if not success then
Expand All @@ -306,14 +316,14 @@ end
-- if retrySettings.timesRemaining > 0 then
-- retrySettings.timesRemaining = retrySettings.timesRemaining - 1

-- reporter:testRetrying(self, retrySettings.timesRemaining + 1)
-- reporter:onTestRetrying(self, retrySettings.timesRemaining + 1)

-- return self:run(reporter, runnerOptions)
-- end
-- end

-- if runnerOptions.bail ~= nil and failedTestCount >= runnerOptions.bail then
-- reporter:testFinished(self, success)
-- reporter:onTestFinished(self, success)

-- error(
-- "Bail after " .. failedTestCount .. " failed "
Expand All @@ -336,7 +346,7 @@ end
-- self.success = failedTestCount == 0
-- end

-- reporter:testFinished(self, self.success)
-- reporter:onTestFinished(self, self.success)
-- end

-- return failedTestCount
Expand Down Expand Up @@ -420,7 +430,7 @@ FILE_FOR_RUN.__index = FILE_FOR_RUN
--- TODO: table is that useful anymore.
--- @param describeOrTest DescribeOrTest
--- @param runnerOptions RunnerOptions
--- @return DescribeOrTestForRun, table, number, table
--- @return DescribeOrTestForRun, table, number
local function copyDescribeOrTestForRun(describeOrTest, runnerOptions)
local describeOrTestForRun, skippedTestCount = makeDescribeOrTestForRun(describeOrTest, runnerOptions)
local describesByFilePath = {}
Expand Down Expand Up @@ -457,17 +467,15 @@ local function copyDescribeOrTestForRun(describeOrTest, runnerOptions)
table.insert(describesByFilePath[fileIndex].describesOrTests, child)
end

if describeOrTest.isDescribe then
if describeOrTestForRun.children then
for _, child in pairs(describeOrTest.children) do
local child, childDescribesByFilePath, childSkippedCount = copyDescribeOrTestForRun(child, runnerOptions)

describeOrTestForRun:addChild(child)

skippedTestCount = skippedTestCount + childSkippedCount
end
end

if describeOrTestForRun.children then
for _, child in pairs(describeOrTestForRun.children) do
insertChildWithFile(child)
end
Expand Down Expand Up @@ -716,6 +724,7 @@ local function runTests(runnerOptions)
i = i + 1
end

runner:start(testSetRoot, describesByFilePath, skippedTestCount)
runnerOptions.eventLoopTicker(function()
return runner:tick()
end)
Expand Down
Loading

0 comments on commit 9e4f57d

Please sign in to comment.