Mercurial > prosody-hg
comparison tests/test.lua @ 7108:bb8a38c68191
tests: Use util.envload to load chunks (fixes #608)
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Thu, 28 Jan 2016 14:45:44 +0100 |
| parents | 31fa6770019c |
| children | 32b74ad54432 |
comparison
equal
deleted
inserted
replaced
| 7106:74798480b52e | 7108:bb8a38c68191 |
|---|---|
| 40 | 40 |
| 41 local _realG = _G; | 41 local _realG = _G; |
| 42 | 42 |
| 43 require "util.import" | 43 require "util.import" |
| 44 | 44 |
| 45 local envloadfile = require "util.envload".envloadfile; | |
| 46 | |
| 45 local env_mt = { __index = function (t,k) return rawget(_realG, k) or print("WARNING: Attempt to access nil global '"..tostring(k).."'"); end }; | 47 local env_mt = { __index = function (t,k) return rawget(_realG, k) or print("WARNING: Attempt to access nil global '"..tostring(k).."'"); end }; |
| 46 function testlib_new_env(t) | 48 function testlib_new_env(t) |
| 47 return setmetatable(t or {}, env_mt); | 49 return setmetatable(t or {}, env_mt); |
| 48 end | 50 end |
| 49 | 51 |
| 77 | 79 |
| 78 function dosingletest(testname, fname) | 80 function dosingletest(testname, fname) |
| 79 local tests = setmetatable({}, { __index = _realG }); | 81 local tests = setmetatable({}, { __index = _realG }); |
| 80 tests.__unit = testname; | 82 tests.__unit = testname; |
| 81 tests.__test = fname; | 83 tests.__test = fname; |
| 82 local chunk, err = loadfile(testname); | 84 local chunk, err = envloadfile(testname, tests); |
| 83 if not chunk then | 85 if not chunk then |
| 84 print("WARNING: ", "Failed to load tests for "..testname, err); | 86 print("WARNING: ", "Failed to load tests for "..testname, err); |
| 85 return; | 87 return; |
| 86 end | 88 end |
| 87 | 89 |
| 88 setfenv(chunk, tests); | |
| 89 local success, err = pcall(chunk); | 90 local success, err = pcall(chunk); |
| 90 if not success then | 91 if not success then |
| 91 print("WARNING: ", "Failed to initialise tests for "..testname, err); | 92 print("WARNING: ", "Failed to initialise tests for "..testname, err); |
| 92 return; | 93 return; |
| 93 end | 94 end |
| 117 function dotest(unitname) | 118 function dotest(unitname) |
| 118 local _fakeG = setmetatable({}, {__index = _realG}); | 119 local _fakeG = setmetatable({}, {__index = _realG}); |
| 119 _fakeG._G = _fakeG; | 120 _fakeG._G = _fakeG; |
| 120 local tests = setmetatable({}, { __index = _fakeG }); | 121 local tests = setmetatable({}, { __index = _fakeG }); |
| 121 tests.__unit = unitname; | 122 tests.__unit = unitname; |
| 122 local chunk, err = loadfile("test_"..unitname:gsub("%.", "_")..".lua"); | 123 local chunk, err = envloadfile("test_"..unitname:gsub("%.", "_")..".lua", tests); |
| 123 if not chunk then | 124 if not chunk then |
| 124 print("WARNING: ", "Failed to load tests for "..unitname, err); | 125 print("WARNING: ", "Failed to load tests for "..unitname, err); |
| 125 return; | 126 return; |
| 126 end | 127 end |
| 127 | 128 |
| 128 setfenv(chunk, tests); | |
| 129 local success, err = pcall(chunk); | 129 local success, err = pcall(chunk); |
| 130 if not success then | 130 if not success then |
| 131 print("WARNING: ", "Failed to initialise tests for "..unitname, err); | 131 print("WARNING: ", "Failed to initialise tests for "..unitname, err); |
| 132 return; | 132 return; |
| 133 end | 133 end |
| 134 if tests.env then setmetatable(tests.env, { __index = _realG }); 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 }) }); | 135 local unit = setmetatable({}, { __index = setmetatable({ _G = tests.env or _fakeG }, { __index = tests.env or _fakeG }) }); |
| 136 local fn = "../"..unitname:gsub("%.", "/")..".lua"; | 136 local fn = "../"..unitname:gsub("%.", "/")..".lua"; |
| 137 local chunk, err = loadfile(fn); | 137 local chunk, err = envloadfile(fn, unit); |
| 138 if not chunk then | 138 if not chunk then |
| 139 print("WARNING: ", "Failed to load module: "..unitname, err); | 139 print("WARNING: ", "Failed to load module: "..unitname, err); |
| 140 return; | 140 return; |
| 141 end | 141 end |
| 142 | 142 |
| 143 local oldmodule, old_M = _fakeG.module, _fakeG._M; | 143 local oldmodule, old_M = _fakeG.module, _fakeG._M; |
| 144 _fakeG.module = function () | 144 _fakeG.module = function () |
| 145 setmetatable(unit, nil); | 145 setmetatable(unit, nil); |
| 146 unit._M = unit; | 146 unit._M = unit; |
| 147 end | 147 end |
| 148 setfenv(chunk, unit); | |
| 149 local success, ret = pcall(chunk); | 148 local success, ret = pcall(chunk); |
| 150 _fakeG.module, _fakeG._M = oldmodule, old_M; | 149 _fakeG.module, _fakeG._M = oldmodule, old_M; |
| 151 if not success then | 150 if not success then |
| 152 print("WARNING: ", "Failed to initialise module: "..unitname, ret); | 151 print("WARNING: ", "Failed to initialise module: "..unitname, ret); |
| 153 return; | 152 return; |
