Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent behavior with global variables #585

Open
SvenMarcus opened this issue Jan 11, 2019 · 3 comments
Open

Inconsistent behavior with global variables #585

SvenMarcus opened this issue Jan 11, 2019 · 3 comments

Comments

@SvenMarcus
Copy link

SvenMarcus commented Jan 11, 2019

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.

@Tieske
Copy link
Member

Tieske commented Jan 11, 2019

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

@SvenMarcus
Copy link
Author

SvenMarcus commented Jan 17, 2019

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.

@coavins
Copy link

coavins commented Oct 24, 2021

Was this resolved with #666?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants