tests/test.lua

changeset 2279
27441b099984
parent 2248
37344b18b551
child 2925
692b3c6c5bd2
equal deleted inserted replaced
2278:0b0fe49e5251 2279:27441b099984
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
147 elseif type(test) ~= "function" then 150 elseif type(test) ~= "function" then
148 if verbosity >= 1 then 151 if verbosity >= 1 then
149 print("WARNING: ", unitname.."."..name.." has no test!"); 152 print("WARNING: ", unitname.."."..name.." has no test!");
150 end 153 end
151 else 154 else
155 if verbosity >= 4 then
156 print("INFO: ", "Testing "..unitname.."."..name);
157 end
152 local line_hook, line_info = new_line_coverage_monitor(fn); 158 local line_hook, line_info = new_line_coverage_monitor(fn);
153 debug.sethook(line_hook, "l") 159 debug.sethook(line_hook, "l")
154 local success, ret = pcall(test, f, unit); 160 local success, ret = pcall(test, f, unit);
155 debug.sethook(); 161 debug.sethook();
156 if not success then 162 if not success then

mercurial