This is actually the answer of @EgorSkriptunoff, but he placed it as a comment.
For me, it was good enough to just make a shallow-copy of bot the package.loaded
and the _G
. Shallow-copy can be found here: http://lua-users.org/wiki/CopyTable:
function shallowcopy(orig)
local orig_type = type(orig)
local copy
if orig_type == 'table' then
copy = {}
for orig_key, orig_value in pairs(orig) do
copy[orig_key] = orig_value
end
else -- number, string, boolean, etc
copy = orig
end
return copy
end
The actual testing then looks like this:
local packageLoadedCopy = shallowcopy(package.loaded)
local gCopy = shallowcopy(_G)
for _, test in ipairs(tests) do
-- Do the actual test
-- Clean up the environment
local markDeletion = {}
local markModify = {}
for name in pairs(package.loaded) do
if not packageLoadedCopy[name] then
table.insert(markDeletion, name)
elseif packageLoadedCopy[name] ~= package.loaded[name] then
table.insert(markModify, name)
end
end
for _, name in pairs(markDeletion) do
package.loaded[name] = nil
end
for _, name in pairs(markModify) do
package.loaded[name] = packageLoadedCopy[name]
end
markDeletion, markModify = {}, {}
for name in pairs(_G) do
if not gCopy[name] then
table.insert(markDeletion, name)
elseif _G[name] ~= gCopy[name] then
table.insert(markModify, name)
end
end
for _, name in pairs(markDeletion) do
_G[name] = nil
end
for _, name in pairs(markModify) do
_G[name] = gCopy[name]
end
end
Do note that simply
package.loaded = packageLoadedCopy
_G = gCopy
Does not work, and I am not sure why.