busted icon indicating copy to clipboard operation
busted copied to clipboard

Inconsistent behavior with global variables

Open SvenMarcus opened this issue 6 years ago • 3 comments

I've been trying to use TDD with busted to implement my own sandboxing behavior. However to my surprise the first test after ensuring the function gets called already failed due to some weird behavior with global variables. It tells me ACTUAL is still "GLOBAL_VAR"

My test:

test("When creating global variable inside sandbox.run() -> should not be in _G afterwards", function()
    local sandbox = require "sandbox"

    sandbox.run(function()
        ACTUAL = "GLOBAL_VAR"
    end)

    assert.is_nil(ACTUAL)
end)

In good TDD fashion I'm doing the simplest thing possible in sandbox.lua:

local function run(func)
    func()
    ACTUAL = nil
end

return {
    run = run
}

As it turns out replacing ACTUAL with _G.ACTUAL in both files fixes the bug. Every other combination seems to fail.

SvenMarcus avatar Jan 11 '19 17:01 SvenMarcus

Busted uses its own sandbox. Check the command-line options, probably there is a switch to turn it off.

Tieske avatar Jan 11 '19 18:01 Tieske

Turning off busted's sandbox doesn't change anything about it, nor should it. This is all within a single test function after all.

But in this case busted produces wrong results. I'm setting ACTUAL to nil, but it still shows up as GLOBAL_VAR. Maybe the function passed into run() is not being run in the same environment? There's nothing my code does with the environment, what I posted here is all I had written at that time.

SvenMarcus avatar Jan 17 '19 16:01 SvenMarcus

Was this resolved with #666?

coavins avatar Oct 24 '21 14:10 coavins