lunarmodules / busted

Elegant Lua unit testing.
https://lunarmodules.github.io/busted/
MIT License
1.4k stars 185 forks source link

Possible bug in busted --list: Path and line number omitted for top-level tests #743

Open MisanthropicBit opened 1 month ago

MisanthropicBit commented 1 month ago

I've encountered some strange behaviour when using busted --list. Consider the following test file:

it("top", function()
    assert.is_true(true)
end)

describe("describe 1", function()
    it("test 1", function()
        assert.is_true(true)
    end)

    it("test 2", function()
        assert.is_true(true)
    end)
end)

Running busted --list on it outputs:

> busted --list test_spec.lua
top
test_spec.lua:6: describe 1 test 1
test_spec.lua:10: describe 1 test 2

As you can see, the path and line number information is omitted for top-level tests even though they are allowed and running the tests works just fine.

> busted test_spec.lua
●●●
3 successes / 0 failures / 0 errors / 0 pending : 0.001417 seconds

I've looked at the existing issues as well as the documentation of cli options but I couldn't find anything. I've tracked down the issue to this function which only prepends the extra information if trace.what == 'Lua'. For the top-level test, trace.what is 'main' so it just prints the name of the test.

Changing the line to if trace and (trace.what == 'Lua' or trace.what == 'main') then fixes the issue but I'm not 100% if that change will break for other cases where what is 'main'. Or perhaps this is the intended behaviour? The lua docs on introspective facilities, where I assume the information is coming from, say the following.

what --- What this function is. Options are "Lua" if foo is a regular Lua function, "C" if it is a C function, or "main" if it is the main part of a Lua chunk.

I don't mind submitting a PR 🙂 In my case, this is making it difficult to consume and use the output programmatically.