tests: Use util.envload to load chunks (fixes #608)
[prosody.git] / tests / test.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 --
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9 local tests_passed = true;
10
11 function run_all_tests()
12         package.loaded["net.connlisteners"] = { get = function () return {} end };
13         dotest "util.jid"
14         dotest "util.multitable"
15         dotest "util.rfc6724"
16         dotest "util.http"
17         dotest "core.stanza_router"
18         dotest "core.s2smanager"
19         dotest "core.configmanager"
20         dotest "util.ip"
21         dotest "util.stanza"
22         dotest "util.sasl.scram"
23         dotest "util.cache"
24         dotest "util.throttle"
25         dotest "util.uuid"
26
27         dosingletest("test_sasl.lua", "latin1toutf8");
28         dosingletest("test_utf8.lua", "valid");
29 end
30
31 local verbosity = tonumber(arg[1]) or 2;
32
33 if os.getenv("WINDIR") then
34         package.path = package.path..";..\\?.lua";
35         package.cpath = package.cpath..";..\\?.dll";
36 else
37         package.path = package.path..";../?.lua";
38         package.cpath = package.cpath..";../?.so";
39 end
40
41 local _realG = _G;
42
43 require "util.import"
44
45 local envloadfile = require "util.envload".envloadfile;
46
47 local env_mt = { __index = function (t,k) return rawget(_realG, k) or print("WARNING: Attempt to access nil global '"..tostring(k).."'"); end };
48 function testlib_new_env(t)
49         return setmetatable(t or {}, env_mt);
50 end
51
52 function assert_equal(a, b, message, level)
53         if not (a == b) then
54                 error("\n   assert_equal failed: "..tostring(a).." ~= "..tostring(b)..(message and ("\n   Message: "..message) or ""), (level or 1) + 1);
55         elseif verbosity >= 4 then
56                 print("assert_equal succeeded: "..tostring(a).." == "..tostring(b));
57         end
58 end
59
60 function assert_table(a, message, level)
61         assert_equal(type(a), "table", message, (level or 1) + 1);
62 end
63 function assert_function(a, message, level)
64         assert_equal(type(a), "function", message, (level or 1) + 1);
65 end
66 function assert_string(a, message, level)
67         assert_equal(type(a), "string", message, (level or 1) + 1);
68 end
69 function assert_boolean(a, message)
70         assert_equal(type(a), "boolean", message);
71 end
72 function assert_is(a, message)
73         assert_equal(not not a, true, message);
74 end
75 function assert_is_not(a, message)
76         assert_equal(not not a, false, message);
77 end
78
79
80 function dosingletest(testname, fname)
81         local tests = setmetatable({}, { __index = _realG });
82         tests.__unit = testname;
83         tests.__test = fname;
84         local chunk, err = envloadfile(testname, tests);
85         if not chunk then
86                 print("WARNING: ", "Failed to load tests for "..testname, err);
87                 return;
88         end
89
90         local success, err = pcall(chunk);
91         if not success then
92                 print("WARNING: ", "Failed to initialise tests for "..testname, err);
93                 return;
94         end
95
96         if type(tests[fname]) ~= "function" then
97                 error(testname.." has no test '"..fname.."'", 0);
98         end
99
100
101         local line_hook, line_info = new_line_coverage_monitor(testname);
102         debug.sethook(line_hook, "l")
103         local success, ret = pcall(tests[fname]);
104         debug.sethook();
105         if not success then
106                 tests_passed = false;
107                 print("TEST FAILED! Unit: ["..testname.."] Function: ["..fname.."]");
108                 print("   Location: "..ret:gsub(":%s*\n", "\n"));
109                 line_info(fname, false, report_file);
110         elseif verbosity >= 2 then
111                 print("TEST SUCCEEDED: ", testname, fname);
112                 print(string.format("TEST COVERED %d/%d lines", line_info(fname, true, report_file)));
113         else
114                 line_info(name, success, report_file);
115         end
116 end
117
118 function dotest(unitname)
119         local _fakeG = setmetatable({}, {__index = _realG});
120         _fakeG._G = _fakeG;
121         local tests = setmetatable({}, { __index = _fakeG });
122         tests.__unit = unitname;
123         local chunk, err = envloadfile("test_"..unitname:gsub("%.", "_")..".lua", tests);
124         if not chunk then
125                 print("WARNING: ", "Failed to load tests for "..unitname, err);
126                 return;
127         end
128
129         local success, err = pcall(chunk);
130         if not success then
131                 print("WARNING: ", "Failed to initialise tests for "..unitname, err);
132                 return;
133         end
134         if tests.env then setmetatable(tests.env, { __index = _realG }); end
135         local unit = setmetatable({}, { __index = setmetatable({ _G = tests.env or _fakeG }, { __index = tests.env or _fakeG }) });
136         local fn = "../"..unitname:gsub("%.", "/")..".lua";
137         local chunk, err = envloadfile(fn, unit);
138         if not chunk then
139                 print("WARNING: ", "Failed to load module: "..unitname, err);
140                 return;
141         end
142
143         local oldmodule, old_M = _fakeG.module, _fakeG._M;
144         _fakeG.module = function ()
145                 setmetatable(unit, nil);
146                 unit._M = unit;
147         end
148         local success, ret = pcall(chunk);
149         _fakeG.module, _fakeG._M = oldmodule, old_M;
150         if not success then
151                 print("WARNING: ", "Failed to initialise module: "..unitname, ret);
152                 return;
153         end
154
155         if type(ret) == "table" then
156                 for k,v in pairs(ret) do
157                         unit[k] = v;
158                 end
159         end
160
161         for name, f in pairs(unit) do
162                 local test = rawget(tests, name);
163                 if type(f) ~= "function" then
164                         if verbosity >= 3 then
165                                 print("INFO: ", "Skipping "..unitname.."."..name.." because it is not a function");
166                         end
167                 elseif type(test) ~= "function" then
168                         if verbosity >= 1 then
169                                 print("WARNING: ", unitname.."."..name.." has no test!");
170                         end
171                 else
172                         if verbosity >= 4 then
173                                 print("INFO: ", "Testing "..unitname.."."..name);
174                         end
175                         local line_hook, line_info = new_line_coverage_monitor(fn);
176                         debug.sethook(line_hook, "l")
177                         local success, ret = pcall(test, f, unit);
178                         debug.sethook();
179                         if not success then
180                                 tests_passed = false;
181                                 print("TEST FAILED! Unit: ["..unitname.."] Function: ["..name.."]");
182                                 print("   Location: "..ret:gsub(":%s*\n", "\n"));
183                                 line_info(name, false, report_file);
184                         elseif verbosity >= 2 then
185                                 print("TEST SUCCEEDED: ", unitname, name);
186                                 print(string.format("TEST COVERED %d/%d lines", line_info(name, true, report_file)));
187                         else
188                                 line_info(name, success, report_file);
189                         end
190                 end
191         end
192 end
193
194 function runtest(f, msg)
195         if not f then print("SUBTEST NOT FOUND: "..(msg or "(no description)")); return; end
196         local success, ret = pcall(f);
197         if success and verbosity >= 2 then
198                 print("SUBTEST PASSED: "..(msg or "(no description)"));
199         elseif (not success) and verbosity >= 0 then
200                 tests_passed = false;
201                 print("SUBTEST FAILED: "..(msg or "(no description)"));
202                 error(ret, 0);
203         end
204 end
205
206 function new_line_coverage_monitor(file)
207         local lines_hit, funcs_hit = {}, {};
208         local total_lines, covered_lines = 0, 0;
209
210         for line in io.lines(file) do
211                 total_lines = total_lines + 1;
212         end
213
214         return function (event, line) -- Line hook
215                         if not lines_hit[line] then
216                                 local info = debug.getinfo(2, "fSL")
217                                 if not info.source:find(file) then return; end
218                                 if not funcs_hit[info.func] and info.activelines then
219                                         funcs_hit[info.func] = true;
220                                         for line in pairs(info.activelines) do
221                                                 lines_hit[line] = false; -- Marks it as hittable, but not hit yet
222                                         end
223                                 end
224                                 if lines_hit[line] == false then
225                                         --print("New line hit: "..line.." in "..debug.getinfo(2, "S").source);
226                                         lines_hit[line] = true;
227                                         covered_lines = covered_lines + 1;
228                                 end
229                         end
230                 end,
231                 function (test_name, success) -- Get info
232                         local fn = file:gsub("^%W*", "");
233                         local total_active_lines = 0;
234                         local coverage_file = io.open("reports/coverage_"..fn:gsub("%W+", "_")..".report", "a+");
235                         for line, active in pairs(lines_hit) do
236                                 if active ~= nil then total_active_lines = total_active_lines + 1; end
237                                 if coverage_file then
238                                         if active == false then coverage_file:write(fn, "|", line, "|", name or "", "|miss\n");
239                                         else coverage_file:write(fn, "|", line, "|", name or "", "|", tostring(success), "\n"); end
240                                 end
241                         end
242                         if coverage_file then coverage_file:close(); end
243                         return covered_lines, total_active_lines, lines_hit;
244                 end
245 end
246
247 run_all_tests()
248
249 os.exit(tests_passed and 0 or 1);