Comparison

tests/test.lua @ 2248:37344b18b551

tests/test.lua: Changes to environment handling of tests, and replace module() with dummy function that doesn't alter the current environment
author Matthew Wild <mwild1@gmail.com>
date Fri, 27 Nov 2009 18:00:47 +0000
parent 2246:eb047fe305aa
child 2925:692b3c6c5bd2
comparison
equal deleted inserted replaced
2247:f62af2a9974e 2248:37344b18b551
14 dotest "core.modulemanager" 14 dotest "core.modulemanager"
15 dotest "core.stanza_router" 15 dotest "core.stanza_router"
16 dotest "core.s2smanager" 16 dotest "core.s2smanager"
17 dotest "core.configmanager" 17 dotest "core.configmanager"
18 dotest "util.stanza" 18 dotest "util.stanza"
19 19
20 dosingletest("test_sasl.lua", "latin1toutf8"); 20 dosingletest("test_sasl.lua", "latin1toutf8");
21 end 21 end
22 22
23 local verbosity = tonumber(arg[1]) or 2; 23 local verbosity = tonumber(arg[1]) or 2;
24 24
104 line_info(name, success, report_file); 104 line_info(name, success, report_file);
105 end 105 end
106 end 106 end
107 107
108 function dotest(unitname) 108 function dotest(unitname)
109 local tests = setmetatable({}, { __index = _realG }); 109 local _fakeG = setmetatable({}, {__index = _realG});
110 _fakeG._G = _fakeG;
111 local tests = setmetatable({}, { __index = _fakeG });
110 tests.__unit = unitname; 112 tests.__unit = unitname;
111 local chunk, err = loadfile("test_"..unitname:gsub("%.", "_")..".lua"); 113 local chunk, err = loadfile("test_"..unitname:gsub("%.", "_")..".lua");
112 if not chunk then 114 if not chunk then
113 print("WARNING: ", "Failed to load tests for "..unitname, err); 115 print("WARNING: ", "Failed to load tests for "..unitname, err);
114 return; 116 return;
118 local success, err = pcall(chunk); 120 local success, err = pcall(chunk);
119 if not success then 121 if not success then
120 print("WARNING: ", "Failed to initialise tests for "..unitname, err); 122 print("WARNING: ", "Failed to initialise tests for "..unitname, err);
121 return; 123 return;
122 end 124 end
123
124 if tests.env then setmetatable(tests.env, { __index = _realG }); end 125 if tests.env then setmetatable(tests.env, { __index = _realG }); end
125 local unit = setmetatable({}, { __index = setmetatable({ _G = tests.env or _G }, { __index = tests.env or _G }) }); 126 local unit = setmetatable({}, { __index = setmetatable({ _G = tests.env or _fakeG }, { __index = tests.env or _fakeG }) });
126 unit._G = unit; _realG._G = unit;
127 local fn = "../"..unitname:gsub("%.", "/")..".lua"; 127 local fn = "../"..unitname:gsub("%.", "/")..".lua";
128 local chunk, err = loadfile(fn); 128 local chunk, err = loadfile(fn);
129 if not chunk then 129 if not chunk then
130 print("WARNING: ", "Failed to load module: "..unitname, err); 130 print("WARNING: ", "Failed to load module: "..unitname, err);
131 return; 131 return;
132 end 132 end
133 133
134 local oldmodule, old_M = _fakeG.module, _fakeG._M;
135 _fakeG.module = function () _M = _G end
134 setfenv(chunk, unit); 136 setfenv(chunk, unit);
135 local success, err = pcall(chunk); 137 local success, err = pcall(chunk);
138 _fakeG.module, _fakeG._M = oldmodule, old_M;
136 if not success then 139 if not success then
137 print("WARNING: ", "Failed to initialise module: "..unitname, err); 140 print("WARNING: ", "Failed to initialise module: "..unitname, err);
138 return; 141 return;
139 end 142 end
140 143