Skip to content
Open
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
17 changes: 17 additions & 0 deletions lute/std/libs/test.luau
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type FailedTest = {
error: string, -- the error message from the assertion
filename: string, -- source file where the failure occurred
linenumber: number, -- line number of the failure
stacktrace: string, -- full stacktrace at the point of failure
Copy link
Collaborator

Choose a reason for hiding this comment

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

If you look at tests/cli/test/test.test.luau (sorry for horrific naming lol), can you add a test for this? Moreover is it possible to trim the stacktrace? E.g. show the last 5 frames before the crash?

}

type TestRunResult = {
Expand All @@ -74,6 +75,10 @@ local function printFailedTest(failed: FailedTest)
print(`❌ {failed.test}`)
print(` {failed.filename}:{failed.linenumber}`)
print(` {failed.assertion}: {failed.error}`)
if failed.stacktrace and failed.stacktrace ~= "" then
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
if failed.stacktrace and failed.stacktrace ~= "" then
if failed.stacktrace ~= "" then

if the stacktrace is always present and just empty string when not set, we don't need to check if it's present

print("\tstacktrace:")
print(failed.stacktrace)
end
print()
end

Expand Down Expand Up @@ -142,6 +147,7 @@ function test.run()
-- Error handler for beforeall hook
local function beforeallErrorHandler(suite: TestSuite)
return function(err)
local stacktrace = debug.traceback(tostring(err))
-- If beforeall fails, skip all tests in the suite
total += #suite.cases
failed += #suite.cases
Expand All @@ -152,6 +158,7 @@ function test.run()
error = `beforeall hook failed: {err}`,
filename = "",
linenumber = 0,
stacktrace = stacktrace,
}
table.insert(failures, failedtest)
end
Expand All @@ -162,13 +169,15 @@ function test.run()
-- Error handler for beforeeach hook
local function beforeeachErrorHandler(testFullName: string)
return function(err)
local stacktrace = debug.traceback(tostring(err))
failed += 1
local failedtest: FailedTest = {
test = testFullName,
assertion = "beforeeach",
error = `beforeeach hook failed: {err}`,
filename = "",
linenumber = 0,
stacktrace = stacktrace,
}
table.insert(failures, failedtest)
return tostring(err)
Expand All @@ -178,12 +187,14 @@ function test.run()
-- Error handler for aftereach hook
local function afterachErrorHandler(testFullName: string)
return function(err)
local stacktrace = debug.traceback(tostring(err))
local failedtest: FailedTest = {
test = testFullName,
assertion = "aftereach",
error = `aftereach hook failed: {err}`,
filename = "",
linenumber = 0,
stacktrace = stacktrace,
}
table.insert(failures, failedtest)
return tostring(err)
Expand All @@ -193,13 +204,15 @@ function test.run()
-- Error handler for afterall hook
local function afterallErrorHandler(suiteName: string)
return function(err)
local stacktrace = debug.traceback(tostring(err))
failed += 1
local failedtest: FailedTest = {
test = suiteName,
assertion = "afterall",
error = `afterall hook failed: {err}`,
filename = "",
linenumber = 0,
stacktrace = stacktrace,
}
table.insert(failures, failedtest)
return tostring(err)
Expand All @@ -212,13 +225,15 @@ function test.run()
local function handlefailure(err)
local fileName, lineNumber = debug.info(4, "sl")
local assertName = debug.info(3, "n")
local stacktrace = debug.traceback(tostring(err))

local failedtest: FailedTest = {
test = tc.name,
assertion = assertName,
error = err.msg,
filename = fileName,
linenumber = lineNumber,
stacktrace = stacktrace,
}
table.insert(failures, failedtest)
failed += 1
Expand Down Expand Up @@ -247,13 +262,15 @@ function test.run()
local function handlefailure(err)
local fileName, lineNumber = debug.info(4, "sl")
local assertName = debug.info(3, "n")
local stacktrace = debug.traceback(tostring(err))

local failedtest: FailedTest = {
test = testFullName,
assertion = assertName,
error = err.msg,
filename = fileName,
linenumber = lineNumber,
stacktrace = stacktrace,
}
table.insert(failures, failedtest)
end
Expand Down