aboutsummaryrefslogtreecommitdiff
path: root/spec/util
diff options
context:
space:
mode:
Diffstat (limited to 'spec/util')
-rw-r--r--spec/util/mock-server.lua97
-rw-r--r--spec/util/test_env.lua795
2 files changed, 892 insertions, 0 deletions
diff --git a/spec/util/mock-server.lua b/spec/util/mock-server.lua
new file mode 100644
index 00000000..244aceae
--- /dev/null
+++ b/spec/util/mock-server.lua
@@ -0,0 +1,97 @@
1#!/usr/bin/env lua
2
3--- A simple LuaRocks mock-server for testing.
4local restserver = require("restserver")
5local server = restserver:new():port(8080)
6
7server:add_resource("api/tool_version", {
8 {
9 method = "GET",
10 path = "/",
11 produces = "application/json",
12 handler = function(query)
13 local json = { version = query.current }
14 return restserver.response():status(200):entity(json)
15 end
16 }
17})
18
19server:add_resource("api/1/{id:[0-9]+}/status", {
20 {
21 method = "GET",
22 path = "/",
23 produces = "application/json",
24 handler = function(query)
25 local json = { user_id = "123", created_at = "29.1.1993" }
26 return restserver.response():status(200):entity(json)
27 end
28 }
29})
30
31server:add_resource("/api/1/{id:[0-9]+}/check_rockspec", {
32 {
33 method = "GET",
34 path = "/",
35 produces = "application/json",
36 handler = function(query)
37 local json = {}
38 return restserver.response():status(200):entity(json)
39 end
40 }
41})
42
43server:add_resource("/api/1/{id:[0-9]+}/upload", {
44 {
45 method = "POST",
46 path = "/",
47 produces = "application/json",
48 handler = function(query)
49 local json = {module = "luasocket", version = {id = "1.0"}, module_url = "http://localhost/luasocket", manifests = "root", is_new = "is_new"}
50 return restserver.response():status(200):entity(json)
51 end
52 }
53})
54
55server:add_resource("/api/1/{id:[0-9]+}/upload_rock/{id:[0-9]+}", {
56 {
57 method = "POST",
58 path = "/",
59 produces = "application/json",
60 handler = function(query)
61 local json = {"rock","module_url"}
62 return restserver.response():status(200):entity(json)
63 end
64 }
65})
66
67server:add_resource("/file/{name:[^/]+}", {
68 {
69 method = "GET",
70 path = "/",
71 produces = "text/plain",
72 handler = function(query, name)
73 local fd = io.open("spec/fixtures/"..name, "r")
74 if not fd then
75 return restserver.response():status(404)
76 end
77 local data = fd:read("*a")
78 fd:close()
79 return restserver.response():status(200):entity(data)
80 end
81 }
82})
83
84-- SHUTDOWN this mock-server
85server:add_resource("/shutdown", {
86 {
87 method = "GET",
88 path = "/",
89 handler = function(query)
90 os.exit()
91 return restserver.response():status(200):entity()
92 end
93 }
94})
95
96-- This loads the restserver.xavante plugin
97server:enable("restserver.xavante"):start() \ No newline at end of file
diff --git a/spec/util/test_env.lua b/spec/util/test_env.lua
new file mode 100644
index 00000000..9d9ae843
--- /dev/null
+++ b/spec/util/test_env.lua
@@ -0,0 +1,795 @@
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.testrun_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.testrun_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.fixtures_dir = testing_paths.luarocks_dir .. "/spec/fixtures"
486 testing_paths.util_dir = testing_paths.luarocks_dir .. "/spec/util"
487 testing_paths.testrun_dir = testing_paths.luarocks_dir .. "/testrun"
488 testing_paths.src_dir = testing_paths.luarocks_dir .. "/src"
489 testing_paths.testing_lrprefix = testing_paths.testrun_dir .. "/testing_lrprefix-" .. luaversion_full
490 testing_paths.testing_tree = testing_paths.testrun_dir .. "/testing-" .. luaversion_full
491 testing_paths.testing_tree_copy = testing_paths.testrun_dir .. "/testing_copy-" .. luaversion_full
492 testing_paths.testing_sys_tree = testing_paths.testrun_dir .. "/testing_sys-" .. luaversion_full
493 testing_paths.testing_sys_tree_copy = testing_paths.testrun_dir .. "/testing_sys_copy-" .. luaversion_full
494 testing_paths.testing_cache = testing_paths.testrun_dir .. "/testing_cache-" .. luaversion_full
495 testing_paths.testing_server = testing_paths.testrun_dir .. "/testing_server-" .. luaversion_full
496
497 testing_paths.testing_rocks = testing_paths.testing_tree .. "/lib/luarocks/rocks-" .. test_env.lua_version
498 testing_paths.testing_sys_rocks = testing_paths.testing_sys_tree .. "/lib/luarocks/rocks-" .. test_env.lua_version
499
500 if test_env.TEST_TARGET_OS == "windows" then
501 testing_paths.win_tools = testing_paths.testing_lrprefix .. "/tools"
502 end
503
504 return testing_paths
505end
506
507--- Helper function to unload luarocks modules from global table package.loaded
508-- Needed to load our local (testing) version of LuaRocks
509function test_env.unload_luarocks()
510 for modname, _ in pairs(package.loaded) do
511 if modname:match("^luarocks%.") then
512 package.loaded[modname] = nil
513 end
514 end
515end
516
517--- Function for initially setup of environment, variables, md5sums for spec files
518function test_env.setup_specs(extra_rocks)
519 -- if global variable about successful creation of testing environment doesn't exist, build environment
520 if not test_env.setup_done then
521 if test_env.TRAVIS then
522 if not test_env.exists(os.getenv("HOME") .. "/.ssh/id_rsa.pub") then
523 execute_bool("ssh-keygen -t rsa -P \"\" -f ~/.ssh/id_rsa")
524 execute_bool("cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys")
525 execute_bool("chmod og-wx ~/.ssh/authorized_keys")
526 execute_bool("ssh-keyscan localhost >> ~/.ssh/known_hosts")
527 end
528 end
529
530 test_env.main()
531 package.path = test_env.env_variables.LUA_PATH
532
533 test_env.platform = execute_output(test_env.testing_paths.lua .. " -e \"print(require('luarocks.core.cfg').arch)\"", false, test_env.env_variables)
534 test_env.lib_extension = execute_output(test_env.testing_paths.lua .. " -e \"print(require('luarocks.core.cfg').lib_extension)\"", false, test_env.env_variables)
535 test_env.wrapper_extension = test_env.TEST_TARGET_OS == "windows" and ".bat" or ""
536 test_env.md5sums = create_md5sums(test_env.testing_paths)
537 test_env.setup_done = true
538 title("RUNNING TESTS")
539 end
540
541 if extra_rocks then
542 local make_manifest = download_rocks(extra_rocks, test_env.testing_paths.testing_server)
543 if make_manifest then
544 test_env.run.luarocks_admin_nocov("make_manifest " .. test_env.testing_paths.testing_server)
545 end
546 end
547
548 if test_env.RESET_ENV then
549 reset_environment(test_env.testing_paths, test_env.md5sums, test_env.env_variables)
550 end
551end
552
553--- Test if required rock is installed and if not, install it.
554-- Return `true` if the rock is already installed or has been installed successfully,
555-- `false` if installation failed.
556function test_env.need_rock(rock)
557 print("Check if " .. rock .. " is installed")
558 if test_env.run.luarocks_noprint_nocov(test_env.quiet("show " .. rock)) then
559 return true
560 else
561 return test_env.run.luarocks_noprint_nocov(test_env.quiet("install " .. rock))
562 end
563end
564
565--- For each key-value pair in replacements table
566-- replace %{key} in given string with value.
567local function substitute(str, replacements)
568 return (str:gsub("%%%b{}", function(marker)
569 return replacements[marker:sub(3, -2)]
570 end))
571end
572
573
574--- Create configs for luacov and several versions of Luarocks
575-- configs needed for some tests.
576local function create_configs()
577 -- testing_config.lua and testing_config_show_downloads.lua
578 local config_content = substitute([[
579 rocks_trees = {
580 "%{testing_tree}",
581 { name = "system", root = "%{testing_sys_tree}" },
582 }
583 rocks_servers = {
584 "%{testing_server}"
585 }
586 local_cache = "%{testing_cache}"
587 upload_server = "testing"
588 upload_user = "%{user}"
589 upload_servers = {
590 testing = {
591 rsync = "localhost/tmp/luarocks_testing",
592 },
593 }
594 external_deps_dirs = {
595 "/usr/local",
596 "/usr",
597 -- These are used for a test that fails, so it
598 -- can point to invalid paths:
599 {
600 prefix = "/opt",
601 bin = "bin",
602 include = "include",
603 lib = { "lib", "lib64" },
604 }
605 }
606 ]], {
607 user = os.getenv("USER"),
608 testing_sys_tree = test_env.testing_paths.testing_sys_tree,
609 testing_tree = test_env.testing_paths.testing_tree,
610 testing_server = test_env.testing_paths.testing_server,
611 testing_cache = test_env.testing_paths.testing_cache
612 })
613
614 write_file(test_env.testing_paths.testrun_dir .. "/testing_config.lua", config_content .. " \nweb_browser = \"true\"")
615 write_file(test_env.testing_paths.testrun_dir .. "/testing_config_show_downloads.lua", config_content
616 .. "show_downloads = true \n rocks_servers={\"http://luarocks.org/repositories/rocks\"}")
617
618 -- testing_config_sftp.lua
619 config_content = substitute([[
620 rocks_trees = {
621 "%{testing_tree}",
622 "%{testing_sys_tree}",
623 }
624 local_cache = "%{testing_cache}"
625 upload_server = "testing"
626 upload_user = "%{user}"
627 upload_servers = {
628 testing = {
629 sftp = "localhost/tmp/luarocks_testing",
630 },
631 }
632 ]], {
633 user = os.getenv("USER"),
634 testing_sys_tree = test_env.testing_paths.testing_sys_tree,
635 testing_tree = test_env.testing_paths.testing_tree,
636 testing_cache = test_env.testing_paths.testing_cache
637 })
638
639 write_file(test_env.testing_paths.testrun_dir .. "/testing_config_sftp.lua", config_content)
640
641 -- luacov.config
642 config_content = substitute([[
643 return {
644 statsfile = "%{testrun_dir}/luacov.stats.out",
645 reportfile = "%{testrun_dir}/luacov.report.out",
646 modules = {
647 ["luarocks"] = "src/bin/luarocks",
648 ["luarocks-admin"] = "src/bin/luarocks-admin",
649 ["luarocks.*"] = "src",
650 ["luarocks.*.*"] = "src",
651 ["luarocks.*.*.*"] = "src"
652 }
653 }
654 ]], {
655 testrun_dir = test_env.testing_paths.testrun_dir
656 })
657
658 write_file(test_env.testing_paths.testrun_dir .. "/luacov.config", config_content)
659
660 config_content = [[
661 -- Config file of mock LuaRocks.org site for tests
662 upload = {
663 server = "http://localhost:8080",
664 tool_version = "1.0.0",
665 api_version = "1",
666 }
667 ]]
668 write_file(test_env.testing_paths.testrun_dir .. "/luarocks_site.lua", config_content)
669end
670
671--- Remove testing directories.
672local function clean()
673 print("Cleaning testing directory...")
674 test_env.remove_dir(test_env.testing_paths.luarocks_tmp)
675 test_env.remove_subdirs(test_env.testing_paths.testrun_dir, "testing[_%-]")
676 test_env.remove_files(test_env.testing_paths.testrun_dir, "testing_")
677 test_env.remove_files(test_env.testing_paths.testrun_dir, "luacov")
678 test_env.remove_files(test_env.testing_paths.testrun_dir, "upload_config")
679 test_env.remove_files(test_env.testing_paths.testrun_dir, "luarocks_site")
680 print("Cleaning done!")
681end
682
683--- Install luarocks into testing prefix.
684local function install_luarocks(install_env_vars)
685 local testing_paths = test_env.testing_paths
686 title("Installing LuaRocks")
687 if test_env.TEST_TARGET_OS == "windows" then
688 local compiler_flag = test_env.MINGW and "/MW" or ""
689 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))
690 assert(execute_bool(testing_paths.win_tools .. "/cp " .. testing_paths.testing_lrprefix .. "/lua/luarocks/core/site_config* " .. testing_paths.src_dir .. "/luarocks/core"))
691 else
692 local configure_cmd = "./configure --with-lua=" .. testing_paths.luadir .. " --prefix=" .. testing_paths.testing_lrprefix
693 assert(execute_bool(configure_cmd, false, install_env_vars))
694 assert(execute_bool("make clean", false, install_env_vars))
695 assert(execute_bool("make src/luarocks/core/site_config_"..test_env.lua_version:gsub("%.", "_")..".lua", false, install_env_vars))
696 assert(execute_bool("make dev", false, install_env_vars))
697 end
698 print("LuaRocks installed correctly!")
699end
700
701function test_env.mock_server_extra_rocks(more)
702 local rocks = {
703 -- rocks needed for mock-server
704 "/copas-2.0.1-1.src.rock",
705 "/coxpcall-1.16.0-1.src.rock",
706 "/dkjson-2.5-2.src.rock",
707 "/luafilesystem-1.6.3-1.src.rock",
708 "/luasec-0.6-1.rockspec",
709 "/luasocket-3.0rc1-2.src.rock",
710 "/luasocket-3.0rc1-2.rockspec",
711 "/restserver-0.1-1.src.rock",
712 "/restserver-xavante-0.2-1.src.rock",
713 "/rings-1.3.0-1.src.rock",
714 "/wsapi-1.6.1-1.src.rock",
715 "/wsapi-xavante-1.6.1-1.src.rock",
716 "/xavante-2.4.0-1.src.rock"
717 }
718 if more then
719 for _, rock in ipairs(more) do
720 table.insert(rocks, rock)
721 end
722 end
723 return rocks
724end
725
726function test_env.mock_server_init()
727 local assert = require("luassert")
728 local testing_paths = test_env.testing_paths
729 assert.is_true(test_env.need_rock("restserver-xavante"))
730 local final_command = test_env.execute_helper(testing_paths.lua .. " " .. testing_paths.util_dir .. "/mock-server.lua &", true, test_env.env_variables)
731 os.execute(final_command)
732end
733
734function test_env.mock_server_done()
735 os.execute("curl localhost:8080/shutdown")
736end
737
738---
739-- Main function to create config files and testing environment
740function test_env.main()
741 local testing_paths = test_env.testing_paths
742
743 if test_env.TEST_ENV_CLEAN then
744 clean()
745 end
746
747 lfs.mkdir(testing_paths.testrun_dir)
748 lfs.mkdir(testing_paths.testing_cache)
749 lfs.mkdir(testing_paths.luarocks_tmp)
750
751 create_configs()
752
753 local install_env_vars = {
754 LUAROCKS_CONFIG = test_env.testing_paths.testrun_dir .. "/testing_config.lua"
755 }
756
757 install_luarocks(install_env_vars)
758
759 -- Preparation of rocks for building environment
760 local rocks = {} -- names of rocks, required for building environment
761 local urls = {} -- names of rock and rockspec files to be downloaded
762 table.insert(urls, "/luacov-0.11.0-1.rockspec")
763 table.insert(urls, "/luacov-0.11.0-1.src.rock")
764
765 if test_env.TYPE_TEST_ENV == "full" then
766 table.insert(urls, "/luafilesystem-1.6.3-1.src.rock")
767 table.insert(urls, "/luasocket-3.0rc1-1.src.rock")
768 table.insert(urls, "/luasocket-3.0rc1-1.rockspec")
769 table.insert(urls, "/luaposix-33.2.1-1.src.rock")
770 table.insert(urls, "/md5-1.2-1.src.rock")
771 table.insert(urls, "/lzlib-0.4.1.53-1.src.rock")
772 rocks = {"luafilesystem", "luasocket", "luaposix", "md5", "lzlib"}
773
774 if test_env.LUA_V ~= "5.1" then
775 table.insert(urls, "/luabitop-1.0.2-1.rockspec")
776 table.insert(urls, "/luabitop-1.0.2-1.src.rock")
777 table.insert(rocks, "luabitop")
778 end
779 end
780
781 table.insert(rocks, "luacov") -- luacov is needed for minimal or full environment
782
783 -- Download rocks needed for LuaRocks testing environment
784 lfs.mkdir(testing_paths.testing_server)
785 download_rocks(urls, testing_paths.testing_server)
786 build_environment(rocks, install_env_vars)
787end
788
789test_env.set_lua_version()
790test_env.set_args()
791test_env.testing_paths = create_paths(test_env.LUA_V or test_env.LUAJIT_V)
792test_env.env_variables = create_env(test_env.testing_paths)
793test_env.run = make_run_functions()
794
795return test_env