# HG changeset patch # User Matthew Wild # Date 1255783000 -3600 # Node ID 26d4b99ba211ea89790478e278fc78fcc9605e0c # Parent 91420df04d5b88166c46cb420306ce5828a528ba tests: More environment magic to help get stuff working in a sandbox diff -r 91420df04d5b -r 26d4b99ba211 tests/test.lua --- a/tests/test.lua Sat Oct 17 13:33:23 2009 +0100 +++ b/tests/test.lua Sat Oct 17 13:36:40 2009 +0100 @@ -30,9 +30,11 @@ package.cpath = package.cpath..";../?.so"; end +local _realG = _G; + require "util.import" -local env_mt = { __index = function (t,k) return rawget(_G, k) or print("WARNING: Attempt to access nil global '"..tostring(k).."'"); end }; +local env_mt = { __index = function (t,k) return rawget(_realG, k) or print("WARNING: Attempt to access nil global '"..tostring(k).."'"); end }; function testlib_new_env(t) return setmetatable(t or {}, env_mt); end @@ -66,7 +68,7 @@ function dosingletest(testname, fname) - local tests = setmetatable({}, { __index = _G }); + local tests = setmetatable({}, { __index = _realG }); tests.__unit = testname; tests.__test = fname; local chunk, err = loadfile(testname); @@ -104,7 +106,7 @@ end function dotest(unitname) - local tests = setmetatable({}, { __index = _G }); + local tests = setmetatable({}, { __index = _realG }); tests.__unit = unitname; local chunk, err = loadfile("test_"..unitname:gsub("%.", "_")..".lua"); if not chunk then @@ -119,8 +121,9 @@ return; end - local unit = setmetatable({}, { __index = setmetatable({ module = function () _M = getfenv(2); end }, { __index = _G }) }); - + if tests.env then setmetatable(tests.env, { __index = _realG }); end + local unit = setmetatable({}, { __index = setmetatable({ _G = tests.env or _G }, { __index = tests.env or _G }) }); + unit._G = unit; _realG._G = unit; local fn = "../"..unitname:gsub("%.", "/")..".lua"; local chunk, err = loadfile(fn); if not chunk then