diff options
Diffstat (limited to 'test/test_environment.lua')
-rw-r--r-- | test/test_environment.lua | 625 |
1 files changed, 625 insertions, 0 deletions
diff --git a/test/test_environment.lua b/test/test_environment.lua new file mode 100644 index 00000000..6de501fb --- /dev/null +++ b/test/test_environment.lua | |||
@@ -0,0 +1,625 @@ | |||
1 | local lfs = require("lfs") | ||
2 | local test_env = {} | ||
3 | local arg = arg or { ... } | ||
4 | |||
5 | local function help() | ||
6 | print("LuaRocks test-suite\n\n".. | ||
7 | [[ | ||
8 | INFORMATION | ||
9 | New test-suite for LuaRocks project, using unit testing framework Busted. | ||
10 | REQUIREMENTS | ||
11 | Tests require to have Lua installed and added to PATH. Be sure sshd is runnig on your system, or | ||
12 | use '--exclude-tags=ssh', to not execute tests which require sshd. | ||
13 | USAGE -Xhelper <arguments> | ||
14 | lua=<version> (mandatory) type your full version of Lua (e.g. --lua 5.2.4) | ||
15 | OR | ||
16 | luajit=<version> (mandatory) type your full version of LuaJIT (e.g. --luajit 2.0.3) | ||
17 | |||
18 | env=<type> (default:"minimal") type what kind of environment to use ["minimal", "full"] | ||
19 | clean remove existing testing environment | ||
20 | travis add just if running on TravisCI | ||
21 | os=<version> type your OS ["linux", "osx", "windows"] | ||
22 | ]]); | ||
23 | os.exit(1) | ||
24 | end | ||
25 | |||
26 | --- Helper function for execute_bool and execute_output | ||
27 | -- @param command string: command to execute | ||
28 | -- @param print_command boolean: print command if 'true' | ||
29 | -- @param env_variables table: table of environment variables to export {FOO="bar", BAR="foo"} | ||
30 | -- @return final_command string: concatenated command to execution | ||
31 | local function execute_helper(command, print_command, env_variables) | ||
32 | local final_command = "" | ||
33 | |||
34 | if print_command then | ||
35 | print("\n[EXECUTING]: " .. command) | ||
36 | end | ||
37 | |||
38 | if env_variables then | ||
39 | final_command = "export " | ||
40 | for k,v in pairs(env_variables) do | ||
41 | final_command = final_command .. k .. "='" .. v .. "' " | ||
42 | end | ||
43 | -- remove last space and add ';' to separate exporting variables from command | ||
44 | final_command = final_command:sub(1, -2) .. "; " | ||
45 | end | ||
46 | |||
47 | final_command = final_command .. command | ||
48 | |||
49 | return final_command | ||
50 | end | ||
51 | |||
52 | --- Execute command and returns true/false | ||
53 | -- In Lua5.1 os.execute returns numeric value, but in Lua5.2+ returns boolean | ||
54 | -- @return true/false boolean: status of the command execution | ||
55 | local function execute_bool(command, print_command, env_variables) | ||
56 | command = execute_helper(command, print_command, env_variables) | ||
57 | |||
58 | local ok = os.execute(command) | ||
59 | return ok == true or ok == 0 | ||
60 | end | ||
61 | |||
62 | --- Execute command and returns output of command | ||
63 | -- @return output string: output the command execution | ||
64 | local function execute_output(command, print_command, env_variables) | ||
65 | command = execute_helper(command, print_command, env_variables) | ||
66 | |||
67 | local file = assert(io.popen(command)) | ||
68 | local output = file:read('*all') | ||
69 | file:close() | ||
70 | return output:gsub("\n","") -- output adding new line, need to be removed | ||
71 | end | ||
72 | |||
73 | --- Set all arguments from input into global variables | ||
74 | function test_env.set_args() | ||
75 | if arg[1] == nil then | ||
76 | help() | ||
77 | end | ||
78 | |||
79 | local args_position | ||
80 | |||
81 | for i=1, #arg do | ||
82 | if arg[i]:find("-Xhelper") and arg[i+1]:find("lua=") and not arg[i+1]:find("luajit=") then | ||
83 | args_position = i+1 | ||
84 | test_env.LUA_V = arg[args_position]:gsub("(.*)lua=([^%,]+)(.*)","%2") | ||
85 | break | ||
86 | elseif arg[i]:find("-Xhelper") and not arg[i+1]:find("lua=") and arg[i+1]:find("luajit=") then | ||
87 | args_position = i+1 | ||
88 | test_env.LUAJIT_V = arg[args_position]:gsub("(.*)luajit=([^%,]+)(.*)","%2") | ||
89 | break | ||
90 | elseif arg[i]:find("-Xhelper") and arg[i+1]:find("lua=") and arg[i+1]:find("luajit=") then | ||
91 | print("Please specify just Lua or LuaJIT version for testing in format 'lua=X.X.X' or 'luajit=X.X.X', for -Xhelper flag") | ||
92 | os.exit(1) | ||
93 | elseif arg[i]:find("-Xhelper") and not arg[i+1]:find("lua=") and not arg[i+1]:find("luajit=") then | ||
94 | print("Please add mandatory argument - version of Lua or LuaJIT in format 'lua=X.X.X' or 'luajit=X.X.X', for -Xhelper flag") | ||
95 | os.exit(1) | ||
96 | end | ||
97 | end | ||
98 | |||
99 | if not args_position then | ||
100 | help() | ||
101 | end | ||
102 | |||
103 | -- if at least Lua/LuaJIT version argument was found on input start to parse other arguments to env. variables | ||
104 | test_env.TYPE_TEST_ENV = "minimal" | ||
105 | |||
106 | if arg[args_position]:find("env=") then | ||
107 | test_env.TYPE_TEST_ENV = arg[args_position]:gsub("(.*)env=([^%,]+)(.*)","%2") | ||
108 | end | ||
109 | if arg[args_position]:find("clean") then | ||
110 | test_env.TEST_ENV_CLEAN = true | ||
111 | end | ||
112 | if arg[args_position]:find("travis") then | ||
113 | test_env.TRAVIS = true | ||
114 | end | ||
115 | if arg[args_position]:find("os=") then | ||
116 | test_env.TEST_TARGET_OS = arg[args_position]:gsub("(.*)os=([^%,]+)(.*)","%2") | ||
117 | end | ||
118 | |||
119 | if not test_env.TEST_TARGET_OS then | ||
120 | print("[OS CHECK]") | ||
121 | if execute_bool("sw_vers") then | ||
122 | test_env.TEST_TARGET_OS = "osx" | ||
123 | elseif execute_bool("uname -s") then | ||
124 | test_env.TEST_TARGET_OS = "linux" | ||
125 | else | ||
126 | test_env.TEST_TARGET_OS = "windows" | ||
127 | end | ||
128 | print("--------------") | ||
129 | end | ||
130 | return true | ||
131 | end | ||
132 | |||
133 | --- Remove directory recursively | ||
134 | -- @param path string: directory path to delete | ||
135 | function test_env.remove_dir(path) | ||
136 | if lfs.attributes(path) then | ||
137 | for file in lfs.dir(path) do | ||
138 | if file ~= "." and file ~= ".." then | ||
139 | local full_path = path..'/'..file | ||
140 | local attr = lfs.attributes(full_path) | ||
141 | |||
142 | if attr.mode == "directory" then | ||
143 | test_env.remove_dir(full_path) | ||
144 | os.remove(full_path) | ||
145 | else | ||
146 | os.remove(full_path) | ||
147 | end | ||
148 | end | ||
149 | end | ||
150 | end | ||
151 | os.remove(path) | ||
152 | end | ||
153 | |||
154 | --- Remove directory recursively | ||
155 | -- @param path string: directory path to delete | ||
156 | -- @param pattern string: pattern in directories | ||
157 | function test_env.remove_dir_pattern(path, pattern) | ||
158 | if lfs.attributes(path) then | ||
159 | for file in lfs.dir(path) do | ||
160 | if file ~= "." and file ~= ".." then | ||
161 | local full_path = path..'/'..file | ||
162 | local attr = lfs.attributes(full_path) | ||
163 | |||
164 | if attr.mode == "directory" and file:find(pattern) then | ||
165 | test_env.remove_dir(full_path) | ||
166 | os.remove(full_path) | ||
167 | end | ||
168 | end | ||
169 | end | ||
170 | end | ||
171 | end | ||
172 | |||
173 | --- Remove files based on filename | ||
174 | -- @param path string: directory where to delete files | ||
175 | -- @param pattern string: pattern in filenames | ||
176 | -- @return result_check boolean: true if one or more files deleted | ||
177 | function test_env.remove_files(path, pattern) | ||
178 | local result_check = false | ||
179 | if lfs.attributes(path) then | ||
180 | for file in lfs.dir(path) do | ||
181 | if file ~= "." and file ~= ".." then | ||
182 | if file:find(pattern) then | ||
183 | if os.remove(path .. "/" .. file) then | ||
184 | result_check = true | ||
185 | end | ||
186 | end | ||
187 | end | ||
188 | end | ||
189 | end | ||
190 | return result_check | ||
191 | end | ||
192 | |||
193 | |||
194 | --- Function for downloading rocks and rockspecs | ||
195 | -- @param rocks table: table with full name of rocks/rockspecs to download | ||
196 | -- @param save_path string: path to directory, where to download rocks/rockspecs | ||
197 | -- @return make_manifest boolean: true if new rocks downloaded | ||
198 | local function download_rocks(rocks, save_path) | ||
199 | local luarocks_repo = "https://luarocks.org" | ||
200 | local make_manifest = false | ||
201 | |||
202 | for _,rock in ipairs(rocks) do | ||
203 | -- check if already downloaded | ||
204 | if not os.rename( save_path .. rock, save_path .. rock) then | ||
205 | execute_bool("wget -cP " .. save_path .. " " .. luarocks_repo .. rock) | ||
206 | make_manifest = true | ||
207 | end | ||
208 | end | ||
209 | return make_manifest | ||
210 | end | ||
211 | |||
212 | --- Create config files for testing | ||
213 | -- @param config_path string: path where to save config file | ||
214 | -- @param config_content string: content of this config file | ||
215 | local function create_config(config_path, config_content) | ||
216 | local file, err = io.open(config_path, "w+") | ||
217 | if not file then return nil, err end | ||
218 | file:write(config_content) | ||
219 | file:close() | ||
220 | end | ||
221 | |||
222 | --- Create md5sum of directory structure recursively, based on filename and size | ||
223 | -- @param path string: path to directory for generate md5sum | ||
224 | -- @param testing_os string(optional): version of PC OS | ||
225 | -- @return md5sum string: md5sum of directory | ||
226 | local function hash_environment(path, testing_os) | ||
227 | local md5sum = "" | ||
228 | testing_os = testing_os or test_env.TEST_TARGET_OS | ||
229 | |||
230 | if testing_os == "linux" then | ||
231 | md5sum = execute_output("find " .. path .. " -printf \"%s %p\n\" | md5sum") | ||
232 | end | ||
233 | if testing_os == "osx" then | ||
234 | md5sum = execute_output("find " .. path .. " -type f -exec stat -f \"%z %N\" {} \\; | md5") | ||
235 | end | ||
236 | --TODO if testing_os == "windows" then | ||
237 | -- md5sum = execute_output("find . -printf \"%s %p\n\" | md5sum") | ||
238 | -- end | ||
239 | return md5sum | ||
240 | end | ||
241 | |||
242 | --- Create environment variables needed for tests | ||
243 | -- @param testing_paths table: table with paths to testing directory | ||
244 | -- @return env_variables table: table with created environment variables | ||
245 | local function create_env(testing_paths) | ||
246 | local luaversion_short = _VERSION:gsub("Lua ", "") | ||
247 | |||
248 | if test_env.LUAJIT_V then | ||
249 | luaversion_short="5.1" | ||
250 | end | ||
251 | |||
252 | local env_variables = {} | ||
253 | env_variables.LUA_VERSION = luaversion_short | ||
254 | env_variables.LUAROCKS_CONFIG = testing_paths.testing_dir .. "/testing_config.lua" | ||
255 | env_variables.LUA_PATH = testing_paths.testing_tree .. "/share/lua/" .. luaversion_short .. "/?.lua;" | ||
256 | env_variables.LUA_PATH = env_variables.LUA_PATH .. testing_paths.testing_tree .. "/share/lua/".. luaversion_short .. "/?/init.lua;" | ||
257 | env_variables.LUA_PATH = env_variables.LUA_PATH .. testing_paths.testing_sys_tree .. "/share/lua/" .. luaversion_short .. "/?.lua;" | ||
258 | env_variables.LUA_PATH = env_variables.LUA_PATH .. testing_paths.testing_sys_tree .. "/share/lua/".. luaversion_short .. "/?/init.lua;" | ||
259 | env_variables.LUA_PATH = env_variables.LUA_PATH .. testing_paths.src_dir .. "/?.lua;" | ||
260 | env_variables.LUA_CPATH = testing_paths.testing_tree .. "/lib/lua/" .. luaversion_short .. "/?.so;" | ||
261 | .. testing_paths.testing_sys_tree .. "/lib/lua/" .. luaversion_short .. "/?.so;" | ||
262 | env_variables.PATH = os.getenv("PATH") .. ":" .. testing_paths.testing_tree .. "/bin:" .. testing_paths.testing_sys_tree .. "/bin" | ||
263 | |||
264 | return env_variables | ||
265 | end | ||
266 | |||
267 | --- Create md5sums of origin system and system-copy testing directory | ||
268 | -- @param testing_paths table: table with paths to testing directory | ||
269 | -- @return md5sums table: table of md5sums of system and system-copy testing directory | ||
270 | local function create_md5sums(testing_paths) | ||
271 | local md5sums = {} | ||
272 | md5sums.testing_tree_copy_md5 = hash_environment(testing_paths.testing_tree_copy) | ||
273 | md5sums.testing_sys_tree_copy_md5 = hash_environment(testing_paths.testing_sys_tree_copy) | ||
274 | |||
275 | return md5sums | ||
276 | end | ||
277 | |||
278 | local function run_luarocks(testing_paths, env_variables) | ||
279 | |||
280 | local function make_command_function(exec_function, lua_cmd, do_print) | ||
281 | return function(cmd, new_vars) | ||
282 | local temp_vars = {} | ||
283 | for k, v in pairs(env_variables) do | ||
284 | temp_vars[k] = v | ||
285 | end | ||
286 | if new_vars then | ||
287 | for k, v in pairs(new_vars) do | ||
288 | temp_vars[k] = v | ||
289 | end | ||
290 | end | ||
291 | return exec_function(lua_cmd .. cmd, do_print, temp_vars) | ||
292 | end | ||
293 | end | ||
294 | |||
295 | local run = {} | ||
296 | |||
297 | local cov_str = testing_paths.lua .. " -e\"require('luacov.runner')('" .. testing_paths.testing_dir .. "/luacov.config')\" " | ||
298 | |||
299 | local luarocks_cmd = cov_str .. testing_paths.src_dir .. "/bin/luarocks " | ||
300 | run.luarocks = make_command_function(execute_output, luarocks_cmd, true) | ||
301 | run.luarocks_bool = make_command_function(execute_bool, luarocks_cmd, true) | ||
302 | run.luarocks_noprint = make_command_function(execute_bool, luarocks_cmd, false) | ||
303 | |||
304 | local luarocks_nocov_cmd = testing_paths.lua .. " " .. testing_paths.src_dir .. "/bin/luarocks " | ||
305 | run.luarocks_nocov = make_command_function(execute_bool, luarocks_nocov_cmd, true) | ||
306 | run.luarocks_noprint_nocov = make_command_function(execute_bool, luarocks_nocov_cmd, false) | ||
307 | |||
308 | local luarocks_admin_cmd = cov_str .. testing_paths.src_dir .. "/bin/luarocks-admin " | ||
309 | run.luarocks_admin = make_command_function(execute_output, luarocks_admin_cmd, true) | ||
310 | run.luarocks_admin_bool = make_command_function(execute_bool, luarocks_admin_cmd, true) | ||
311 | |||
312 | local luarocks_admin_nocov_cmd = testing_paths.lua .. " " .. testing_paths.src_dir .. "/bin/luarocks-admin " | ||
313 | run.luarocks_admin_nocov = make_command_function(execute_bool, luarocks_admin_nocov_cmd, false) | ||
314 | |||
315 | return run | ||
316 | end | ||
317 | |||
318 | --- Build environment for testing | ||
319 | local function build_environment(env_rocks, testing_paths, env_variables) | ||
320 | print("\n--------------------") | ||
321 | print("BUILDING ENVIRONMENT") | ||
322 | print("--------------------") | ||
323 | test_env.remove_dir(testing_paths.testing_tree) | ||
324 | test_env.remove_dir(testing_paths.testing_sys_tree) | ||
325 | test_env.remove_dir(testing_paths.testing_tree_copy) | ||
326 | test_env.remove_dir(testing_paths.testing_sys_tree_copy) | ||
327 | |||
328 | lfs.mkdir(testing_paths.testing_tree) | ||
329 | lfs.mkdir(testing_paths.testing_sys_tree) | ||
330 | |||
331 | local run = run_luarocks(testing_paths, env_variables) | ||
332 | run.luarocks_admin_nocov("make_manifest " .. testing_paths.testing_server) | ||
333 | run.luarocks_admin_nocov("make_manifest " .. testing_paths.testing_cache) | ||
334 | |||
335 | for _,package in ipairs(env_rocks) do | ||
336 | if not run.luarocks_nocov("install --only-server=" .. testing_paths.testing_cache .. " --tree=" .. testing_paths.testing_sys_tree .. " " .. package, env_variables) then | ||
337 | run.luarocks_nocov("build --tree=" .. testing_paths.testing_sys_tree .. " " .. package, env_variables) | ||
338 | run.luarocks_nocov("pack --tree=" .. testing_paths.testing_sys_tree .. " " .. package .. "; mv " .. package .. "-*.rock " .. testing_paths.testing_cache, env_variables) | ||
339 | end | ||
340 | end | ||
341 | |||
342 | execute_bool("cp -a " .. testing_paths.testing_tree .. "/. " .. testing_paths.testing_tree_copy) | ||
343 | execute_bool("cp -a " .. testing_paths.testing_sys_tree .. "/. " .. testing_paths.testing_sys_tree_copy) | ||
344 | end | ||
345 | |||
346 | --- Reset testing environment | ||
347 | local function reset_environment(testing_paths, md5sums) | ||
348 | local testing_tree_md5 = hash_environment(testing_paths.testing_tree) | ||
349 | local testing_sys_tree_md5 = hash_environment(testing_paths.testing_sys_tree) | ||
350 | |||
351 | if testing_tree_md5 ~= md5sums.testing_tree_copy_md5 then | ||
352 | test_env.remove_dir(testing_paths.testing_tree) | ||
353 | execute_bool("cp -a " .. testing_paths.testing_tree_copy .. "/. " .. testing_paths.testing_tree) | ||
354 | end | ||
355 | if testing_sys_tree_md5 ~= md5sums.testing_sys_tree_copy_md5 then | ||
356 | test_env.remove_dir(testing_paths.testing_sys_tree) | ||
357 | execute_bool("cp -a " .. testing_paths.testing_sys_tree_copy .. "/. " .. testing_paths.testing_sys_tree) | ||
358 | end | ||
359 | |||
360 | print("\n[ENVIRONMENT RESET]") | ||
361 | end | ||
362 | |||
363 | local function create_paths(luaversion_full) | ||
364 | local testing_paths = {} | ||
365 | |||
366 | testing_paths.luadir = "" | ||
367 | |||
368 | if test_env.TRAVIS then | ||
369 | testing_paths.luadir = lfs.currentdir() .. "/lua_install" | ||
370 | testing_paths.lua = testing_paths.luadir .. "/bin/lua" | ||
371 | end | ||
372 | |||
373 | if test_env.LUA_V and not test_env.TRAVIS then | ||
374 | if lfs.attributes("/usr/bin/lua") then | ||
375 | testing_paths.luadir = "/usr" | ||
376 | testing_paths.lua = testing_paths.luadir .. "/bin/lua" | ||
377 | elseif lfs.attributes("/usr/local/bin/lua") then | ||
378 | testing_paths.luadir = "/usr/local" | ||
379 | testing_paths.lua = testing_paths.luadir .. "/bin/lua" | ||
380 | end | ||
381 | elseif test_env.LUAJIT_V and not test_env.TRAVIS then | ||
382 | if lfs.attributes("/usr/bin/luajit") then | ||
383 | testing_paths.luadir = "/usr" | ||
384 | testing_paths.lua = testing_paths.luadir .. "/bin/luajit" | ||
385 | elseif lfs.attributes("/usr/local/bin/luajit") then | ||
386 | testing_paths.luadir = "/usr/local" | ||
387 | testing_paths.lua = testing_paths.luadir .. "/bin/luajit" | ||
388 | end | ||
389 | end | ||
390 | |||
391 | testing_paths.luarocks_tmp = "/tmp/luarocks_testing" --windows? | ||
392 | |||
393 | testing_paths.luarocks_dir = lfs.currentdir() | ||
394 | testing_paths.testing_dir = testing_paths.luarocks_dir .. "/test" | ||
395 | testing_paths.src_dir = testing_paths.luarocks_dir .. "/src" | ||
396 | testing_paths.testing_lrprefix = testing_paths.testing_dir .. "/testing_lrprefix-" .. luaversion_full | ||
397 | testing_paths.testing_tree = testing_paths.testing_dir .. "/testing-" .. luaversion_full | ||
398 | testing_paths.testing_tree_copy = testing_paths.testing_dir .. "/testing_copy-" .. luaversion_full | ||
399 | testing_paths.testing_sys_tree = testing_paths.testing_dir .. "/testing_sys-" .. luaversion_full | ||
400 | testing_paths.testing_sys_tree_copy = testing_paths.testing_dir .. "/testing_sys_copy-" .. luaversion_full | ||
401 | testing_paths.testing_cache = testing_paths.testing_dir .. "/testing_cache-" .. luaversion_full | ||
402 | testing_paths.testing_server = testing_paths.testing_dir .. "/testing_server-" .. luaversion_full | ||
403 | |||
404 | return testing_paths | ||
405 | end | ||
406 | |||
407 | --- Helper function to unload luarocks modules from global table package.loaded | ||
408 | -- Needed to load our local (testing) version of LuaRocks | ||
409 | function test_env.unload_luarocks() | ||
410 | for modname, _ in pairs(package.loaded) do | ||
411 | if modname:match("^luarocks%.") then | ||
412 | package.loaded[modname] = nil | ||
413 | end | ||
414 | end | ||
415 | end | ||
416 | |||
417 | --- Function for initially setup of environment, variables, md5sums for spec files | ||
418 | function test_env.setup_specs(extra_rocks, luaversion_full) | ||
419 | -- if global variable about successful creation of testing environment doesn't exists, build environment | ||
420 | if not test_env.setup_done then | ||
421 | test_env.set_args() | ||
422 | |||
423 | if test_env.TRAVIS then | ||
424 | if not os.rename(os.getenv("HOME") .. "/.ssh/id_rsa.pub", os.getenv("HOME") .. "/.ssh/id_rsa.pub") then | ||
425 | execute_bool("ssh-keygen -t rsa -P \"\" -f ~/.ssh/id_rsa") | ||
426 | execute_bool("cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys") | ||
427 | execute_bool("chmod og-wx ~/.ssh/authorized_keys") | ||
428 | execute_bool("ssh-keyscan localhost >> ~/.ssh/known_hosts") | ||
429 | end | ||
430 | end | ||
431 | |||
432 | luaversion_full = luaversion_full or test_env.LUA_V or test_env.LUAJIT_V | ||
433 | |||
434 | test_env.main() | ||
435 | |||
436 | -- Set paths, env_vars and functions for specs | ||
437 | test_env.testing_paths = create_paths(luaversion_full) | ||
438 | test_env.env_variables = create_env(test_env.testing_paths) | ||
439 | package.path = test_env.env_variables.LUA_PATH | ||
440 | |||
441 | test_env.run = run_luarocks(test_env.testing_paths, test_env.env_variables) | ||
442 | test_env.platform = execute_output(test_env.testing_paths.lua .. " -e 'print(require(\"luarocks.cfg\").arch)'", false, test_env.env_variables) | ||
443 | test_env.md5sums = create_md5sums(test_env.testing_paths) | ||
444 | test_env.setup_done = true | ||
445 | end | ||
446 | |||
447 | if extra_rocks then | ||
448 | local make_manifest = download_rocks(extra_rocks, test_env.testing_paths.testing_server) | ||
449 | if make_manifest then | ||
450 | local run = run_luarocks(test_env.testing_paths, test_env.env_variables) | ||
451 | run.luarocks_admin_nocov("make_manifest " .. test_env.testing_paths.testing_server) | ||
452 | end | ||
453 | end | ||
454 | |||
455 | reset_environment(test_env.testing_paths, test_env.md5sums, test_env.env_variables) | ||
456 | |||
457 | return true | ||
458 | end | ||
459 | |||
460 | --- Helper function for tests which needs luasocket installed | ||
461 | function test_env.need_luasocket(luarocks_nocov, testing_cache, platform) | ||
462 | luarocks_nocov = luarocks_nocov or test_env.run.luarocks_nocov | ||
463 | testing_cache = testing_cache or test_env.testing_paths.testing_cache | ||
464 | platform = platform or test_env.platform | ||
465 | |||
466 | if luarocks_nocov("show luasocket") then | ||
467 | return true | ||
468 | else | ||
469 | testing_cache = testing_cache .. "/" | ||
470 | local luasocket_rock = "luasocket-3.0rc1-1." .. platform .. ".rock" | ||
471 | if not os.rename( testing_cache .. luasocket_rock, testing_cache .. luasocket_rock) then | ||
472 | luarocks_nocov("build --pack-binary-rock luasocket 3.0rc1-1") | ||
473 | os.rename(luasocket_rock, testing_cache .. luasocket_rock) | ||
474 | end | ||
475 | luarocks_nocov("install " .. testing_cache .. luasocket_rock) | ||
476 | end | ||
477 | return true | ||
478 | end | ||
479 | |||
480 | --- | ||
481 | -- Main function to create config files and testing environment | ||
482 | function test_env.main(luaversion_full, env_type, env_clean) | ||
483 | luaversion_full = luaversion_full or test_env.LUA_V or test_env.LUAJIT_V | ||
484 | local testing_paths = create_paths(luaversion_full) | ||
485 | |||
486 | env_clean = env_clean or test_env.TEST_ENV_CLEAN | ||
487 | if env_clean then | ||
488 | print("Cleaning testing directory...") | ||
489 | test_env.remove_dir(testing_paths.luarocks_tmp) | ||
490 | test_env.remove_dir_pattern(testing_paths.testing_dir, "testing_") | ||
491 | test_env.remove_dir_pattern(testing_paths.testing_dir, "testing-") | ||
492 | test_env.remove_files(testing_paths.testing_dir, "testing_") | ||
493 | test_env.remove_files(testing_paths.testing_dir, "luacov") | ||
494 | print("Cleaning done!") | ||
495 | end | ||
496 | |||
497 | lfs.mkdir(testing_paths.testing_cache) | ||
498 | lfs.mkdir(testing_paths.luarocks_tmp) | ||
499 | |||
500 | --- CONFIG FILES | ||
501 | -- testing_config.lua and testing_config_show_downloads.lua | ||
502 | local config_content = ([[rocks_trees = { | ||
503 | "%{testing_tree}", | ||
504 | { name = "system", root = "%{testing_sys_tree}" }, | ||
505 | } | ||
506 | rocks_servers = { | ||
507 | "%{testing_server}" | ||
508 | } | ||
509 | local_cache = "%{testing_cache}" | ||
510 | upload_server = "testing" | ||
511 | upload_user = "%{user}" | ||
512 | upload_servers = { | ||
513 | testing = { | ||
514 | rsync = "localhost/tmp/luarocks_testing", | ||
515 | }, | ||
516 | } | ||
517 | external_deps_dirs = { | ||
518 | "/usr/local", | ||
519 | "/usr", | ||
520 | -- These are used for a test that fails, so it | ||
521 | -- can point to invalid paths: | ||
522 | { | ||
523 | prefix = "/opt", | ||
524 | bin = "bin", | ||
525 | include = "include", | ||
526 | lib = { "lib", "lib64" }, | ||
527 | } | ||
528 | }]]):gsub("%%%b{}", { | ||
529 | ["%{user}"] = os.getenv("USER"), | ||
530 | ["%{testing_sys_tree}"] = testing_paths.testing_sys_tree, | ||
531 | ["%{testing_tree}"] = testing_paths.testing_tree, | ||
532 | ["%{testing_server}"] = testing_paths.testing_server, | ||
533 | ["%{testing_cache}"] = testing_paths.testing_cache}) | ||
534 | |||
535 | create_config(testing_paths.testing_dir .. "/testing_config.lua", config_content .. " \nweb_browser = \"true\"") | ||
536 | create_config(testing_paths.testing_dir .. "/testing_config_show_downloads.lua", config_content | ||
537 | .. "show_downloads = true \n rocks_servers={\"http://luarocks.org/repositories/rocks\"}") | ||
538 | |||
539 | -- testing_config_sftp.lua | ||
540 | config_content=([[rocks_trees = { | ||
541 | "%{testing_tree}", | ||
542 | "%{testing_sys_tree}", | ||
543 | } | ||
544 | local_cache = "%{testing_cache}" | ||
545 | upload_server = "testing" | ||
546 | upload_user = "%{user}" | ||
547 | upload_servers = { | ||
548 | testing = { | ||
549 | sftp = "localhost/tmp/luarocks_testing", | ||
550 | }, | ||
551 | }]]):gsub("%%%b{}", { | ||
552 | ["%{user}"] = os.getenv("USER"), | ||
553 | ["%{testing_sys_tree}"] = testing_paths.testing_sys_tree, | ||
554 | ["%{testing_tree}"] = testing_paths.testing_tree, | ||
555 | ["%{testing_cache}"] = testing_paths.testing_cache}) | ||
556 | |||
557 | create_config(testing_paths.testing_dir .. "/testing_config_sftp.lua", config_content) | ||
558 | |||
559 | -- luacov.config | ||
560 | config_content=([[return { | ||
561 | statsfile = "%{testing_dir}/luacov.stats.out", | ||
562 | reportfile = "%{testing_dir}/luacov.report.out", | ||
563 | modules = { | ||
564 | ["luarocks"] = "src/bin/luarocks", | ||
565 | ["luarocks-admin"] = "src/bin/luarocks-admin", | ||
566 | ["luarocks.*"] = "src", | ||
567 | ["luarocks.*.*"] = "src", | ||
568 | ["luarocks.*.*.*"] = "src" | ||
569 | } | ||
570 | }]]):gsub("%%%b{}", { | ||
571 | ["%{testing_dir}"] = testing_paths.testing_dir}) | ||
572 | |||
573 | create_config(testing_paths.testing_dir .. "/luacov.config", config_content) | ||
574 | |||
575 | -- Create environment variables for configuration | ||
576 | local temp_env_variables = {LUAROCKS_CONFIG = testing_paths.testing_dir .. "/testing_config.lua",LUA_PATH="",LUA_CPATH=""} | ||
577 | |||
578 | -- Configure LuaRocks testing environment | ||
579 | local configure_cmd = "./configure --with-lua=" .. testing_paths.luadir .. " --prefix=" .. testing_paths.testing_lrprefix | ||
580 | configure_cmd = configure_cmd .. " && make clean" | ||
581 | |||
582 | if not execute_bool(configure_cmd, false, temp_env_variables) then | ||
583 | os.exit(1) | ||
584 | end | ||
585 | if not execute_bool("make src/luarocks/site_config.lua && make dev", false, temp_env_variables) then | ||
586 | os.exit(1) | ||
587 | end | ||
588 | |||
589 | -- Preparation of rocks for building environment | ||
590 | env_type = env_type or test_env.TYPE_TEST_ENV | ||
591 | |||
592 | local env_rocks = {} -- short names of rocks, required for building environment | ||
593 | local rocks = {} -- full names of rocks required for download | ||
594 | rocks[#rocks+1] = "/luacov-0.11.0-1.rockspec" | ||
595 | rocks[#rocks+1] = "/luacov-0.11.0-1.src.rock" | ||
596 | |||
597 | if env_type == "full" then | ||
598 | rocks[#rocks+1] = "/luafilesystem-1.6.3-1.src.rock" | ||
599 | rocks[#rocks+1] = "/luasocket-3.0rc1-1.src.rock" | ||
600 | rocks[#rocks+1] = "/luasocket-3.0rc1-1.rockspec" | ||
601 | rocks[#rocks+1] = "/luaposix-33.2.1-1.src.rock" | ||
602 | rocks[#rocks+1] = "/md5-1.2-1.src.rock" | ||
603 | rocks[#rocks+1] = "/lzlib-0.4.1.53-1.src.rock" | ||
604 | env_rocks = {"luafilesystem", "luasocket", "luaposix", "md5", "lzlib"} | ||
605 | end | ||
606 | if env_type == "full" and luaversion_full ~= "5.1.5" then | ||
607 | rocks[#rocks+1] = "/luabitop-1.0.2-1.rockspec" | ||
608 | rocks[#rocks+1] = "/luabitop-1.0.2-1.src.rock" | ||
609 | table.insert(env_rocks, "luabitop") | ||
610 | end | ||
611 | |||
612 | table.insert(env_rocks, "luacov") -- luacov is needed for minimal or full environment | ||
613 | |||
614 | -- Download rocks needed for LuaRocks testing environment | ||
615 | lfs.mkdir(testing_paths.testing_server) | ||
616 | download_rocks(rocks, testing_paths.testing_server) | ||
617 | |||
618 | build_environment(env_rocks, testing_paths, temp_env_variables) | ||
619 | |||
620 | print("----------------") | ||
621 | print(" RUNNING TESTS") | ||
622 | print("----------------") | ||
623 | end | ||
624 | |||
625 | return test_env | ||