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