busted
busted copied to clipboard
Inconsistent behavior with global variables
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.
Busted uses its own sandbox. Check the command-line options, probably there is a switch to turn it off.
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.
Was this resolved with #666?