aboutsummaryrefslogtreecommitdiff
path: root/test/test_environment.lua
diff options
context:
space:
mode:
Diffstat (limited to 'test/test_environment.lua')
-rw-r--r--test/test_environment.lua781
1 files changed, 0 insertions, 781 deletions
diff --git a/test/test_environment.lua b/test/test_environment.lua
deleted file mode 100644
index d347fa59..00000000
--- a/test/test_environment.lua
+++ /dev/null
@@ -1,781 +0,0 @@
1local lfs = require("lfs")
2local test_env = {}
3
4local help_message = [[
5LuaRocks test-suite
6
7INFORMATION
8 New test-suite for LuaRocks project, using unit testing framework Busted.
9REQUIREMENTS
10 Be sure sshd is running on your system, or use '--exclude-tags=ssh',
11 to not execute tests which require sshd.
12USAGE
13 busted [-Xhelper <arguments>]
14ARGUMENTS
15 env=<type> Set type of environment to use ("minimal" or "full",
16 default: "minimal").
17 noreset Don't reset environment after each test
18 clean Remove existing testing environment.
19 travis Add if running on TravisCI.
20 appveyor Add if running on Appveyor.
21 os=<type> Set OS ("linux", "osx", or "windows").
22 lua_dir=<path> Path of Lua installation (default "/usr/local")
23 lua_interpreter=<lua> Name of the interpreter (default "lua")
24]]
25
26local function help()
27 print(help_message)
28 os.exit(1)
29end
30
31local function title(str)
32 print()
33 print(("-"):rep(#str))
34 print(str)
35 print(("-"):rep(#str))
36end
37
38function test_env.exists(path)
39 return lfs.attributes(path, "mode") ~= nil
40end
41
42--- Quote argument for shell processing. Fixes paths on Windows.
43-- Adds double quotes and escapes. Based on function in fs/win32.lua.
44-- @param arg string: Unquoted argument.
45-- @return string: Quoted argument.
46local function Q(arg)
47 if test_env.TEST_TARGET_OS == "windows" then
48 local drive_letter = "[%.a-zA-Z]?:?[\\/]"
49 -- Quote DIR for Windows
50 if arg:match("^"..drive_letter) then
51 arg = arg:gsub("/", "\\")
52 end
53
54 if arg == "\\" then
55 return '\\' -- CHDIR needs special handling for root dir
56 end
57
58 return '"' .. arg .. '"'
59 else
60 return "'" .. arg:gsub("'", "'\\''") .. "'"
61 end
62end
63
64function test_env.quiet(command)
65 if not test_env.VERBOSE then
66 if test_env.TEST_TARGET_OS == "windows" then
67 return command .. " 1> NUL 2> NUL"
68 else
69 return command .. " 1> /dev/null 2> /dev/null"
70 end
71 else
72 return command
73 end
74end
75
76function test_env.copy(source, destination)
77 local r_source, err = io.open(source, "r")
78 local r_destination, err = io.open(destination, "w")
79
80 while true do
81 local block = r_source:read(8192)
82 if not block then break end
83 r_destination:write(block)
84 end
85
86 r_source:close()
87 r_destination:close()
88end
89
90--- Helper function for execute_bool and execute_output
91-- @param command string: command to execute
92-- @param print_command boolean: print command if 'true'
93-- @param env_variables table: table of environment variables to export {FOO="bar", BAR="foo"}
94-- @return final_command string: concatenated command to execution
95function test_env.execute_helper(command, print_command, env_variables)
96 local final_command = ""
97
98 if print_command then
99 print("[EXECUTING]: " .. command)
100 end
101
102 if env_variables then
103 if test_env.TEST_TARGET_OS == "windows" then
104 for k,v in pairs(env_variables) do
105 final_command = final_command .. "set " .. k .. "=" .. v .. "&"
106 end
107 final_command = final_command:sub(1, -2) .. "&"
108 else
109 final_command = "export "
110 for k,v in pairs(env_variables) do
111 final_command = final_command .. k .. "='" .. v .. "' "
112 end
113 -- remove last space and add ';' to separate exporting variables from command
114 final_command = final_command:sub(1, -2) .. "; "
115 end
116 end
117
118 final_command = final_command .. command .. " 2>&1"
119
120 return final_command
121end
122
123--- Execute command and returns true/false
124-- @return true/false boolean: status of the command execution
125local function execute_bool(command, print_command, env_variables)
126 command = test_env.execute_helper(command, print_command, env_variables)
127
128 local redirect_filename
129 local redirect = ""
130 if print_command ~= nil then
131 redirect_filename = test_env.testing_paths.luarocks_tmp.."/output.txt"
132 redirect = " > "..redirect_filename
133 os.remove(redirect_filename)
134 end
135 local ok = os.execute(command .. redirect)
136 ok = (ok == true or ok == 0) -- normalize Lua 5.1 output to boolean
137 if redirect ~= "" then
138 if not ok then
139 local fd = io.open(redirect_filename, "r")
140 if fd then
141 print(fd:read("*a"))
142 fd:close()
143 end
144 end
145 os.remove(redirect_filename)
146 end
147 return ok
148end
149
150--- Execute command and returns output of command
151-- @return output string: output the command execution
152local function execute_output(command, print_command, env_variables)
153 command = test_env.execute_helper(command, print_command, env_variables)
154
155 local file = assert(io.popen(command))
156 local output = file:read('*all')
157 file:close()
158 return output:gsub("\n","") -- output adding new line, need to be removed
159end
160
161--- Set test_env.LUA_V or test_env.LUAJIT_V based
162-- on version of Lua used to run this script.
163function test_env.set_lua_version()
164 if _G.jit then
165 test_env.LUAJIT_V = _G.jit.version:match("(2%.%d)%.%d")
166 test_env.lua_version = "5.1"
167 else
168 test_env.LUA_V = _VERSION:match("5%.%d")
169 test_env.lua_version = test_env.LUA_V
170 end
171end
172
173--- Set all arguments from input into global variables
174function test_env.set_args()
175 -- if at least Lua/LuaJIT version argument was found on input start to parse other arguments to env. variables
176 test_env.TYPE_TEST_ENV = "minimal"
177 test_env.OPENSSL_DIRS = ""
178 test_env.RESET_ENV = true
179
180 for _, argument in ipairs(arg) do
181 if argument:find("^env=") then
182 test_env.TYPE_TEST_ENV = argument:match("^env=(.*)$")
183 elseif argument == "noreset" then
184 test_env.RESET_ENV = false
185 elseif argument == "clean" then
186 test_env.TEST_ENV_CLEAN = true
187 elseif argument == "verbose" then
188 test_env.VERBOSE = true
189 elseif argument == "travis" then
190 test_env.TRAVIS = true
191 elseif argument == "appveyor" then
192 test_env.APPVEYOR = true
193 test_env.OPENSSL_DIRS = "OPENSSL_LIBDIR=C:\\OpenSSL-Win32\\lib OPENSSL_INCDIR=C:\\OpenSSL-Win32\\include"
194 elseif argument:find("^os=") then
195 test_env.TEST_TARGET_OS = argument:match("^os=(.*)$")
196 elseif argument == "mingw" then
197 test_env.MINGW = true
198 elseif argument == "vs" then
199 test_env.MINGW = false
200 elseif argument:find("^lua_dir=") then
201 test_env.LUA_DIR = argument:match("^lua_dir=(.*)$")
202 elseif argument:find("^lua_interpreter=") then
203 test_env.LUA_INTERPRETER = argument:match("^lua_interpreter=(.*)$")
204 else
205 help()
206 end
207 end
208
209 if not test_env.TEST_TARGET_OS then
210 title("OS CHECK")
211
212 if execute_bool("sw_vers") then
213 test_env.TEST_TARGET_OS = "osx"
214 if test_env.TRAVIS then
215 test_env.OPENSSL_DIRS = "OPENSSL_LIBDIR=/usr/local/opt/openssl/lib OPENSSL_INCDIR=/usr/local/opt/openssl/include"
216 end
217 elseif execute_output("uname -s") == "Linux" then
218 test_env.TEST_TARGET_OS = "linux"
219 else
220 test_env.TEST_TARGET_OS = "windows"
221 end
222 end
223 return true
224end
225
226function test_env.copy_dir(source_path, target_path)
227 local testing_paths = test_env.testing_paths
228 if test_env.TEST_TARGET_OS == "windows" then
229 execute_bool(testing_paths.win_tools .. "/cp -R ".. source_path .. "/. " .. target_path)
230 else
231 execute_bool("cp -a ".. source_path .. "/. " .. target_path)
232 end
233end
234
235--- Remove directory recursively
236-- @param path string: directory path to delete
237function test_env.remove_dir(path)
238 if test_env.exists(path) then
239 for file in lfs.dir(path) do
240 if file ~= "." and file ~= ".." then
241 local full_path = path..'/'..file
242
243 if lfs.attributes(full_path, "mode") == "directory" then
244 test_env.remove_dir(full_path)
245 else
246 os.remove(full_path)
247 end
248 end
249 end
250 end
251 lfs.rmdir(path)
252end
253
254--- Remove subdirectories of a directory that match a pattern
255-- @param path string: path to directory
256-- @param pattern string: pattern matching basenames of subdirectories to be removed
257function test_env.remove_subdirs(path, pattern)
258 if test_env.exists(path) then
259 for file in lfs.dir(path) do
260 if file ~= "." and file ~= ".." then
261 local full_path = path..'/'..file
262
263 if lfs.attributes(full_path, "mode") == "directory" and file:find(pattern) then
264 test_env.remove_dir(full_path)
265 end
266 end
267 end
268 end
269end
270
271--- Remove files matching a pattern
272-- @param path string: directory where to delete files
273-- @param pattern string: pattern matching basenames of files to be deleted
274-- @return result_check boolean: true if one or more files deleted
275function test_env.remove_files(path, pattern)
276 local result_check = false
277 if test_env.exists(path) then
278 for file in lfs.dir(path) do
279 if file ~= "." and file ~= ".." then
280 if file:find(pattern) then
281 if os.remove(path .. "/" .. file) then
282 result_check = true
283 end
284 end
285 end
286 end
287 end
288 return result_check
289end
290
291
292--- Function for downloading rocks and rockspecs
293-- @param urls table: array of full names of rocks/rockspecs to download
294-- @param save_path string: path to directory, where to download rocks/rockspecs
295-- @return make_manifest boolean: true if new rocks downloaded
296local function download_rocks(urls, save_path)
297 local luarocks_repo = "https://luarocks.org"
298 local make_manifest = false
299
300 for _, url in ipairs(urls) do
301 -- check if already downloaded
302 if not test_env.exists(save_path .. url) then
303 if test_env.TEST_TARGET_OS == "windows" then
304 execute_bool(test_env.testing_paths.win_tools .. "/wget -cP " .. save_path .. " " .. luarocks_repo .. url .. " --no-check-certificate")
305 else
306 execute_bool("wget -cP " .. save_path .. " " .. luarocks_repo .. url)
307 end
308 make_manifest = true
309 end
310 end
311 return make_manifest
312end
313
314--- Create a file containing a string.
315-- @param path string: path to file.
316-- @param str string: content of the file.
317local function write_file(path, str)
318 local file = assert(io.open(path, "w"))
319 file:write(str)
320 file:close()
321end
322
323--- Create md5sum of directory structure recursively, based on filename and size
324-- @param path string: path to directory for generate md5sum
325-- @return md5sum string: md5sum of directory
326local function hash_environment(path)
327 if test_env.TEST_TARGET_OS == "linux" then
328 return execute_output("find " .. path .. " -printf \"%s %p\n\" | md5sum")
329 elseif test_env.TEST_TARGET_OS == "osx" then
330 return execute_output("find " .. path .. " -type f -exec stat -f \"%z %N\" {} \\; | md5")
331 elseif test_env.TEST_TARGET_OS == "windows" then
332 return execute_output("\"" .. Q(test_env.testing_paths.win_tools .. "/find") .. " " .. Q(path)
333 .. " -printf \"%s %p\"\" > temp_sum.txt && certUtil -hashfile temp_sum.txt && del temp_sum.txt")
334 end
335end
336
337--- Create environment variables needed for tests
338-- @param testing_paths table: table with paths to testing directory
339-- @return env_variables table: table with created environment variables
340local function create_env(testing_paths)
341 local luaversion_short = _VERSION:gsub("Lua ", "")
342
343 if test_env.LUAJIT_V then
344 luaversion_short="5.1"
345 end
346
347 local env_variables = {}
348 env_variables.LUA_VERSION = luaversion_short
349 env_variables.LUAROCKS_CONFIG = testing_paths.testing_dir .. "/testing_config.lua"
350 if test_env.TEST_TARGET_OS == "windows" then
351 env_variables.LUA_PATH = testing_paths.testing_lrprefix .. "\\lua\\?.lua;"
352 else
353 env_variables.LUA_PATH = testing_paths.testing_lrprefix .. "/share/lua/" .. luaversion_short .. "/?.lua;"
354 end
355 env_variables.LUA_PATH = env_variables.LUA_PATH .. testing_paths.testing_tree .. "/share/lua/" .. luaversion_short .. "/?.lua;"
356 env_variables.LUA_PATH = env_variables.LUA_PATH .. testing_paths.testing_tree .. "/share/lua/".. luaversion_short .. "/?/init.lua;"
357 env_variables.LUA_PATH = env_variables.LUA_PATH .. testing_paths.testing_sys_tree .. "/share/lua/" .. luaversion_short .. "/?.lua;"
358 env_variables.LUA_PATH = env_variables.LUA_PATH .. testing_paths.testing_sys_tree .. "/share/lua/".. luaversion_short .. "/?/init.lua;"
359 env_variables.LUA_PATH = env_variables.LUA_PATH .. testing_paths.src_dir .. "/?.lua;"
360 env_variables.LUA_CPATH = testing_paths.testing_tree .. "/lib/lua/" .. luaversion_short .. "/?.so;"
361 .. testing_paths.testing_sys_tree .. "/lib/lua/" .. luaversion_short .. "/?.so;"
362 env_variables.PATH = os.getenv("PATH") .. ";" .. testing_paths.testing_tree .. "/bin;" .. testing_paths.testing_sys_tree .. "/bin;"
363
364 return env_variables
365end
366
367--- Create md5sums of origin system and system-copy testing directory
368-- @param testing_paths table: table with paths to testing directory
369-- @return md5sums table: table of md5sums of system and system-copy testing directory
370local function create_md5sums(testing_paths)
371 local md5sums = {}
372 md5sums.testing_tree_copy_md5 = hash_environment(testing_paths.testing_tree_copy)
373 md5sums.testing_sys_tree_copy_md5 = hash_environment(testing_paths.testing_sys_tree_copy)
374
375 return md5sums
376end
377
378local function make_run_function(cmd_name, exec_function, with_coverage, do_print)
379 local cmd_prefix = Q(test_env.testing_paths.lua) .. " "
380
381 if with_coverage then
382 cmd_prefix = cmd_prefix .. "-e \"require('luacov.runner')('" .. test_env.testing_paths.testing_dir .. "/luacov.config')\" "
383 end
384
385 if test_env.TEST_TARGET_OS == "windows" then
386 cmd_prefix = cmd_prefix .. Q(test_env.testing_paths.testing_lrprefix .. "/" .. cmd_name .. ".lua") .. " "
387 else
388 cmd_prefix = cmd_prefix .. test_env.testing_paths.src_dir .. "/bin/" .. cmd_name .. " "
389 end
390
391 return function(cmd, new_vars)
392 local temp_vars = {}
393 for k, v in pairs(test_env.env_variables) do
394 temp_vars[k] = v
395 end
396 if new_vars then
397 for k, v in pairs(new_vars) do
398 temp_vars[k] = v
399 end
400 end
401 return exec_function(cmd_prefix .. cmd, do_print, temp_vars)
402 end
403end
404
405local function make_run_functions()
406 return {
407 luarocks = make_run_function("luarocks", execute_output, true, true),
408 luarocks_bool = make_run_function("luarocks", execute_bool, true, true),
409 luarocks_noprint = make_run_function("luarocks", execute_bool, true, false),
410 luarocks_nocov = make_run_function("luarocks", execute_bool, false, true),
411 luarocks_noprint_nocov = make_run_function("luarocks", execute_bool, false, false),
412 luarocks_admin = make_run_function("luarocks-admin", execute_output, true, true),
413 luarocks_admin_bool = make_run_function("luarocks-admin", execute_bool, true, true),
414 luarocks_admin_nocov = make_run_function("luarocks-admin", execute_bool, false, false)
415 }
416end
417
418--- Rebuild environment.
419-- Remove old installed rocks and install new ones,
420-- updating manifests and tree copies.
421local function build_environment(rocks, env_variables)
422 title("BUILDING ENVIRONMENT")
423 local testing_paths = test_env.testing_paths
424 test_env.remove_dir(testing_paths.testing_tree)
425 test_env.remove_dir(testing_paths.testing_sys_tree)
426 test_env.remove_dir(testing_paths.testing_tree_copy)
427 test_env.remove_dir(testing_paths.testing_sys_tree_copy)
428
429 lfs.mkdir(testing_paths.testing_tree)
430 lfs.mkdir(testing_paths.testing_sys_tree)
431
432 test_env.run.luarocks_admin_nocov("make_manifest " .. Q(testing_paths.testing_server))
433 test_env.run.luarocks_admin_nocov("make_manifest " .. Q(testing_paths.testing_cache))
434
435 for _, rock in ipairs(rocks) do
436 if not test_env.run.luarocks_nocov("install --only-server=" .. testing_paths.testing_cache .. " --tree=" .. testing_paths.testing_sys_tree .. " " .. Q(rock), env_variables) then
437 test_env.run.luarocks_nocov("build --tree=" .. Q(testing_paths.testing_sys_tree) .. " " .. Q(rock) .. "", env_variables)
438 test_env.run.luarocks_nocov("pack --tree=" .. Q(testing_paths.testing_sys_tree) .. " " .. Q(rock), env_variables)
439 if test_env.TEST_TARGET_OS == "windows" then
440 execute_bool(testing_paths.win_tools .. "/mv " .. rock .. "-*.rock " .. testing_paths.testing_cache)
441 else
442 execute_bool("mv " .. rock .. "-*.rock " .. testing_paths.testing_cache)
443 end
444 end
445 end
446
447 test_env.copy_dir(testing_paths.testing_tree, testing_paths.testing_tree_copy)
448 test_env.copy_dir(testing_paths.testing_sys_tree, testing_paths.testing_sys_tree_copy)
449end
450
451--- Reset testing environment
452local function reset_environment(testing_paths, md5sums)
453 local testing_tree_md5 = hash_environment(testing_paths.testing_tree)
454 local testing_sys_tree_md5 = hash_environment(testing_paths.testing_sys_tree)
455
456 if testing_tree_md5 ~= md5sums.testing_tree_copy_md5 then
457 test_env.remove_dir(testing_paths.testing_tree)
458 test_env.copy_dir(testing_paths.testing_tree_copy, testing_paths.testing_tree)
459 end
460
461 if testing_sys_tree_md5 ~= md5sums.testing_sys_tree_copy_md5 then
462 test_env.remove_dir(testing_paths.testing_sys_tree)
463 test_env.copy_dir(testing_paths.testing_sys_tree_copy, testing_paths.testing_sys_tree)
464 end
465end
466
467local function create_paths(luaversion_full)
468
469 local testing_paths = {}
470 testing_paths.luadir = (test_env.LUA_DIR or "/usr/local")
471 testing_paths.lua = testing_paths.luadir .. "/bin/" .. (test_env.LUA_INTERPRETER or "lua")
472
473 if test_env.TEST_TARGET_OS == "windows" then
474 testing_paths.luarocks_tmp = os.getenv("TEMP")
475 else
476 testing_paths.luarocks_tmp = "/tmp/luarocks_testing"
477 end
478
479 testing_paths.luarocks_dir = lfs.currentdir()
480
481 if test_env.TEST_TARGET_OS == "windows" then
482 testing_paths.luarocks_dir = testing_paths.luarocks_dir:gsub("\\","/")
483 end
484
485 testing_paths.testing_dir = testing_paths.luarocks_dir .. "/test"
486 testing_paths.src_dir = testing_paths.luarocks_dir .. "/src"
487 testing_paths.testing_lrprefix = testing_paths.testing_dir .. "/testing_lrprefix-" .. luaversion_full
488 testing_paths.testing_tree = testing_paths.testing_dir .. "/testing-" .. luaversion_full
489 testing_paths.testing_tree_copy = testing_paths.testing_dir .. "/testing_copy-" .. luaversion_full
490 testing_paths.testing_sys_tree = testing_paths.testing_dir .. "/testing_sys-" .. luaversion_full
491 testing_paths.testing_sys_tree_copy = testing_paths.testing_dir .. "/testing_sys_copy-" .. luaversion_full
492 testing_paths.testing_cache = testing_paths.testing_dir .. "/testing_cache-" .. luaversion_full
493 testing_paths.testing_server = testing_paths.testing_dir .. "/testing_server-" .. luaversion_full
494
495 testing_paths.testing_rocks = testing_paths.testing_tree .. "/lib/luarocks/rocks-" .. test_env.lua_version
496 testing_paths.testing_sys_rocks = testing_paths.testing_sys_tree .. "/lib/luarocks/rocks-" .. test_env.lua_version
497
498 if test_env.TEST_TARGET_OS == "windows" then
499 testing_paths.win_tools = testing_paths.testing_lrprefix .. "/tools"
500 end
501
502 return testing_paths
503end
504
505--- Helper function to unload luarocks modules from global table package.loaded
506-- Needed to load our local (testing) version of LuaRocks
507function test_env.unload_luarocks()
508 for modname, _ in pairs(package.loaded) do
509 if modname:match("^luarocks%.") then
510 package.loaded[modname] = nil
511 end
512 end
513end
514
515--- Function for initially setup of environment, variables, md5sums for spec files
516function test_env.setup_specs(extra_rocks)
517 -- if global variable about successful creation of testing environment doesn't exist, build environment
518 if not test_env.setup_done then
519 if test_env.TRAVIS then
520 if not test_env.exists(os.getenv("HOME") .. "/.ssh/id_rsa.pub") then
521 execute_bool("ssh-keygen -t rsa -P \"\" -f ~/.ssh/id_rsa")
522 execute_bool("cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys")
523 execute_bool("chmod og-wx ~/.ssh/authorized_keys")
524 execute_bool("ssh-keyscan localhost >> ~/.ssh/known_hosts")
525 end
526 end
527
528 test_env.main()
529 package.path = test_env.env_variables.LUA_PATH
530
531 test_env.platform = execute_output(test_env.testing_paths.lua .. " -e \"print(require('luarocks.core.cfg').arch)\"", false, test_env.env_variables)
532 test_env.lib_extension = execute_output(test_env.testing_paths.lua .. " -e \"print(require('luarocks.core.cfg').lib_extension)\"", false, test_env.env_variables)
533 test_env.wrapper_extension = test_env.TEST_TARGET_OS == "windows" and ".bat" or ""
534 test_env.md5sums = create_md5sums(test_env.testing_paths)
535 test_env.setup_done = true
536 title("RUNNING TESTS")
537 end
538
539 if extra_rocks then
540 local make_manifest = download_rocks(extra_rocks, test_env.testing_paths.testing_server)
541 if make_manifest then
542 test_env.run.luarocks_admin_nocov("make_manifest " .. test_env.testing_paths.testing_server)
543 end
544 end
545
546 if test_env.RESET_ENV then
547 reset_environment(test_env.testing_paths, test_env.md5sums, test_env.env_variables)
548 end
549end
550
551--- Test if required rock is installed and if not, install it.
552-- Return `true` if the rock is already installed or has been installed successfully,
553-- `false` if installation failed.
554function test_env.need_rock(rock)
555 print("Check if " .. rock .. " is installed")
556 if test_env.run.luarocks_noprint_nocov(test_env.quiet("show " .. rock)) then
557 return true
558 else
559 return test_env.run.luarocks_noprint_nocov(test_env.quiet("install " .. rock))
560 end
561end
562
563--- For each key-value pair in replacements table
564-- replace %{key} in given string with value.
565local function substitute(str, replacements)
566 return (str:gsub("%%%b{}", function(marker)
567 return replacements[marker:sub(3, -2)]
568 end))
569end
570
571
572--- Create configs for luacov and several versions of Luarocks
573-- configs needed for some tests.
574local function create_configs()
575 -- testing_config.lua and testing_config_show_downloads.lua
576 local config_content = substitute([[
577 rocks_trees = {
578 "%{testing_tree}",
579 { name = "system", root = "%{testing_sys_tree}" },
580 }
581 rocks_servers = {
582 "%{testing_server}"
583 }
584 local_cache = "%{testing_cache}"
585 upload_server = "testing"
586 upload_user = "%{user}"
587 upload_servers = {
588 testing = {
589 rsync = "localhost/tmp/luarocks_testing",
590 },
591 }
592 external_deps_dirs = {
593 "/usr/local",
594 "/usr",
595 -- These are used for a test that fails, so it
596 -- can point to invalid paths:
597 {
598 prefix = "/opt",
599 bin = "bin",
600 include = "include",
601 lib = { "lib", "lib64" },
602 }
603 }
604 ]], {
605 user = os.getenv("USER"),
606 testing_sys_tree = test_env.testing_paths.testing_sys_tree,
607 testing_tree = test_env.testing_paths.testing_tree,
608 testing_server = test_env.testing_paths.testing_server,
609 testing_cache = test_env.testing_paths.testing_cache
610 })
611
612 write_file(test_env.testing_paths.testing_dir .. "/testing_config.lua", config_content .. " \nweb_browser = \"true\"")
613 write_file(test_env.testing_paths.testing_dir .. "/testing_config_show_downloads.lua", config_content
614 .. "show_downloads = true \n rocks_servers={\"http://luarocks.org/repositories/rocks\"}")
615
616 -- testing_config_sftp.lua
617 config_content = substitute([[
618 rocks_trees = {
619 "%{testing_tree}",
620 "%{testing_sys_tree}",
621 }
622 local_cache = "%{testing_cache}"
623 upload_server = "testing"
624 upload_user = "%{user}"
625 upload_servers = {
626 testing = {
627 sftp = "localhost/tmp/luarocks_testing",
628 },
629 }
630 ]], {
631 user = os.getenv("USER"),
632 testing_sys_tree = test_env.testing_paths.testing_sys_tree,
633 testing_tree = test_env.testing_paths.testing_tree,
634 testing_cache = test_env.testing_paths.testing_cache
635 })
636
637 write_file(test_env.testing_paths.testing_dir .. "/testing_config_sftp.lua", config_content)
638
639 -- luacov.config
640 config_content = substitute([[
641 return {
642 statsfile = "%{testing_dir}/luacov.stats.out",
643 reportfile = "%{testing_dir}/luacov.report.out",
644 modules = {
645 ["luarocks"] = "src/bin/luarocks",
646 ["luarocks-admin"] = "src/bin/luarocks-admin",
647 ["luarocks.*"] = "src",
648 ["luarocks.*.*"] = "src",
649 ["luarocks.*.*.*"] = "src"
650 }
651 }
652 ]], {
653 testing_dir = test_env.testing_paths.testing_dir
654 })
655
656 write_file(test_env.testing_paths.testing_dir .. "/luacov.config", config_content)
657end
658
659--- Remove testing directories.
660local function clean()
661 print("Cleaning testing directory...")
662 test_env.remove_dir(test_env.testing_paths.luarocks_tmp)
663 test_env.remove_subdirs(test_env.testing_paths.testing_dir, "testing[_%-]")
664 test_env.remove_files(test_env.testing_paths.testing_dir, "testing_")
665 test_env.remove_files(test_env.testing_paths.testing_dir, "luacov")
666 test_env.remove_files(test_env.testing_paths.testing_dir, "upload_config")
667 print("Cleaning done!")
668end
669
670--- Install luarocks into testing prefix.
671local function install_luarocks(install_env_vars)
672 local testing_paths = test_env.testing_paths
673 title("Installing LuaRocks")
674 if test_env.TEST_TARGET_OS == "windows" then
675 local compiler_flag = test_env.MINGW and "/MW" or ""
676 assert(execute_bool("install.bat /LUA " .. testing_paths.luadir .. " " .. compiler_flag .. " /P " .. testing_paths.testing_lrprefix .. " /NOREG /NOADMIN /F /Q /CONFIG " .. testing_paths.testing_lrprefix .. "/etc/luarocks", false, install_env_vars))
677 assert(execute_bool(testing_paths.win_tools .. "/cp " .. testing_paths.testing_lrprefix .. "/lua/luarocks/core/site_config* " .. testing_paths.src_dir .. "/luarocks/core"))
678 else
679 local configure_cmd = "./configure --with-lua=" .. testing_paths.luadir .. " --prefix=" .. testing_paths.testing_lrprefix
680 assert(execute_bool(configure_cmd, false, install_env_vars))
681 assert(execute_bool("make clean", false, install_env_vars))
682 assert(execute_bool("make src/luarocks/core/site_config_"..test_env.lua_version:gsub("%.", "_")..".lua", false, install_env_vars))
683 assert(execute_bool("make dev", false, install_env_vars))
684 end
685 print("LuaRocks installed correctly!")
686end
687
688function test_env.mock_server_extra_rocks(more)
689 local rocks = {
690 -- rocks needed for mock-server
691 "/copas-2.0.1-1.src.rock",
692 "/coxpcall-1.16.0-1.src.rock",
693 "/dkjson-2.5-2.src.rock",
694 "/luafilesystem-1.6.3-1.src.rock",
695 "/luasec-0.6-1.rockspec",
696 "/luasocket-3.0rc1-2.src.rock",
697 "/luasocket-3.0rc1-2.rockspec",
698 "/restserver-0.1-1.src.rock",
699 "/restserver-xavante-0.2-1.src.rock",
700 "/rings-1.3.0-1.src.rock",
701 "/wsapi-1.6.1-1.src.rock",
702 "/wsapi-xavante-1.6.1-1.src.rock",
703 "/xavante-2.4.0-1.src.rock"
704 }
705 if more then
706 for _, rock in ipairs(more) do
707 table.insert(rocks, rock)
708 end
709 end
710 return rocks
711end
712
713function test_env.mock_server_init()
714 local assert = require("luassert")
715 local testing_paths = test_env.testing_paths
716 assert.is_true(test_env.need_rock("restserver-xavante"))
717 local final_command = test_env.execute_helper(testing_paths.lua .. " " .. testing_paths.testing_dir .. "/mock-server.lua &", true, test_env.env_variables)
718 os.execute(final_command)
719end
720
721function test_env.mock_server_done()
722 os.execute("curl localhost:8080/shutdown")
723end
724
725---
726-- Main function to create config files and testing environment
727function test_env.main()
728 local testing_paths = test_env.testing_paths
729
730 if test_env.TEST_ENV_CLEAN then
731 clean()
732 end
733
734 lfs.mkdir(testing_paths.testing_cache)
735 lfs.mkdir(testing_paths.luarocks_tmp)
736
737 create_configs()
738
739 local install_env_vars = {
740 LUAROCKS_CONFIG = test_env.testing_paths.testing_dir .. "/testing_config.lua"
741 }
742
743 install_luarocks(install_env_vars)
744
745 -- Preparation of rocks for building environment
746 local rocks = {} -- names of rocks, required for building environment
747 local urls = {} -- names of rock and rockspec files to be downloaded
748 table.insert(urls, "/luacov-0.11.0-1.rockspec")
749 table.insert(urls, "/luacov-0.11.0-1.src.rock")
750
751 if test_env.TYPE_TEST_ENV == "full" then
752 table.insert(urls, "/luafilesystem-1.6.3-1.src.rock")
753 table.insert(urls, "/luasocket-3.0rc1-1.src.rock")
754 table.insert(urls, "/luasocket-3.0rc1-1.rockspec")
755 table.insert(urls, "/luaposix-33.2.1-1.src.rock")
756 table.insert(urls, "/md5-1.2-1.src.rock")
757 table.insert(urls, "/lzlib-0.4.1.53-1.src.rock")
758 rocks = {"luafilesystem", "luasocket", "luaposix", "md5", "lzlib"}
759
760 if test_env.LUA_V ~= "5.1" then
761 table.insert(urls, "/luabitop-1.0.2-1.rockspec")
762 table.insert(urls, "/luabitop-1.0.2-1.src.rock")
763 table.insert(rocks, "luabitop")
764 end
765 end
766
767 table.insert(rocks, "luacov") -- luacov is needed for minimal or full environment
768
769 -- Download rocks needed for LuaRocks testing environment
770 lfs.mkdir(testing_paths.testing_server)
771 download_rocks(urls, testing_paths.testing_server)
772 build_environment(rocks, install_env_vars)
773end
774
775test_env.set_lua_version()
776test_env.set_args()
777test_env.testing_paths = create_paths(test_env.LUA_V or test_env.LUAJIT_V)
778test_env.env_variables = create_env(test_env.testing_paths)
779test_env.run = make_run_functions()
780
781return test_env