Skip to content

Crash in updateStatus when setupSuite fails (currentNode is nil) #166

@daiaji

Description

@daiaji

Description

When an error occurs inside the global setupSuite function, LuaUnit crashes with attempt to index local 'node' (a nil value) inside M.LuaUnit:updateStatus.

This happens because setupSuite runs before any test starts, so self.result.currentNode is nil. However, if protectedCall catches an error in setupSuite, it calls updateStatus, which unconditionally tries to access node.

This effectively masks the original error (e.g., a setup failure) and replaces it with an internal LuaUnit crash, making debugging very difficult.

Steps to Reproduce

Save the following as repro.lua:

local lu = require('luaunit')

function setupSuite()
    error("This is the original error!")
end

function testSomething()
    lu.assertTrue(true)
end

os.exit(lu.LuaUnit.run())

Run it: lua repro.lua

Expected Behavior

LuaUnit should report a failure/error for the Suite Setup and show the message "This is the original error!", then exit gracefully.

Actual Behavior

LuaUnit crashes internally, hiding the original error:

luaunit.lua:xxxx: attempt to index local 'node' (a nil value)
stack traceback:
    luaunit.lua:xxxx: in function 'updateStatus'
    luaunit.lua:xxxx: in function 'setupSuite'
    ...

Analysis (Based on v3.4)

In M.LuaUnit:setupSuite:

    function M.LuaUnit:setupSuite( listOfNameAndInst )
        local setupSuite = getKeyInListWithGlobalFallback("setupSuite", listOfNameAndInst)
        if  self.asFunction( setupSuite ) then
            -- protectedCall returns status ERROR
            -- updateStatus is called immediately
            self:updateStatus( self:protectedCall( nil, setupSuite, 'setupSuite' ) )
        end
    end

In M.LuaUnit:updateStatus:

    function M.LuaUnit:updateStatus( err )
        if err.status == NodeStatus.SUCCESS then
            return
        end

        local node = self.result.currentNode -- <--- node is nil here because suite just started

        -- ...

        -- CRASH HERE
        if node.status ~= NodeStatus.SUCCESS then
            return
        end

Suggested Fix

In updateStatus, check if node is nil. If it is nil (meaning a suite-level error occurred), handle it separately (e.g., print to stdout/stderr immediately or store in a suite-level error field) instead of trying to update a non-existent test node.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions