diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/test_environment.lua | 173 |
1 files changed, 131 insertions, 42 deletions
diff --git a/test/test_environment.lua b/test/test_environment.lua index 13e548f9..42473b38 100644 --- a/test/test_environment.lua +++ b/test/test_environment.lua | |||
@@ -17,6 +17,7 @@ ARGUMENTS | |||
17 | noreset Don't reset environment after each test | 17 | noreset Don't reset environment after each test |
18 | clean Remove existing testing environment. | 18 | clean Remove existing testing environment. |
19 | travis Add if running on TravisCI. | 19 | travis Add if running on TravisCI. |
20 | appveyor Add if running on Appveyor. | ||
20 | os=<type> Set OS ("linux", "osx", or "windows"). | 21 | os=<type> Set OS ("linux", "osx", or "windows"). |
21 | ]] | 22 | ]] |
22 | 23 | ||
@@ -36,18 +37,54 @@ local function exists(path) | |||
36 | return lfs.attributes(path, "mode") ~= nil | 37 | return lfs.attributes(path, "mode") ~= nil |
37 | end | 38 | end |
38 | 39 | ||
39 | function test_env.quiet(commad) | 40 | --- Quote argument for shell processing. Fixes paths on Windows. |
41 | -- Adds double quotes and escapes. Based on function in fs/win32.lua. | ||
42 | -- @param arg string: Unquoted argument. | ||
43 | -- @return string: Quoted argument. | ||
44 | local function Q(arg) | ||
45 | if test_env.TEST_TARGET_OS == "windows" then | ||
46 | local drive_letter = "[%.a-zA-Z]?:?[\\/]" | ||
47 | -- Quote DIR for Windows | ||
48 | if arg:match("^"..drive_letter) then | ||
49 | arg = arg:gsub("/", "\\") | ||
50 | end | ||
51 | |||
52 | if arg == "\\" then | ||
53 | return '\\' -- CHDIR needs special handling for root dir | ||
54 | end | ||
55 | |||
56 | return '"' .. arg .. '"' | ||
57 | else | ||
58 | return "'" .. arg:gsub("'", "'\\''") .. "'" | ||
59 | end | ||
60 | end | ||
61 | |||
62 | function test_env.quiet(command) | ||
40 | if not test_env.VERBOSE then | 63 | if not test_env.VERBOSE then |
41 | if test_env.TEST_TARGET_OS == "linux" or test_env.TEST_TARGET_OS == "osx" then | 64 | if test_env.TEST_TARGET_OS == "windows" then |
42 | return commad .. " 1> /dev/null 2> /dev/null" | 65 | return command .. " 1> NUL 2> NUL" |
43 | elseif test_env.TEST_TARGET_OS == "windows" then | 66 | else |
44 | return commad .. " 2> NUL 1> NUL" | 67 | return command .. " 1> /dev/null 2> /dev/null" |
45 | end | 68 | end |
46 | else | 69 | else |
47 | return command | 70 | return command |
48 | end | 71 | end |
49 | end | 72 | end |
50 | 73 | ||
74 | function test_env.copy(source, destination) | ||
75 | local r_source, err = io.open(source, "r") | ||
76 | local r_destination, err = io.open(destination, "w") | ||
77 | |||
78 | while true do | ||
79 | local block = r_source:read(8192) | ||
80 | if not block then break end | ||
81 | r_destination:write(block) | ||
82 | end | ||
83 | |||
84 | r_source:close() | ||
85 | r_destination:close() | ||
86 | end | ||
87 | |||
51 | --- Helper function for execute_bool and execute_output | 88 | --- Helper function for execute_bool and execute_output |
52 | -- @param command string: command to execute | 89 | -- @param command string: command to execute |
53 | -- @param print_command boolean: print command if 'true' | 90 | -- @param print_command boolean: print command if 'true' |
@@ -61,15 +98,22 @@ function test_env.execute_helper(command, print_command, env_variables) | |||
61 | end | 98 | end |
62 | 99 | ||
63 | if env_variables then | 100 | if env_variables then |
64 | final_command = "export " | 101 | if test_env.TEST_TARGET_OS == "windows" then |
65 | for k,v in pairs(env_variables) do | 102 | for k,v in pairs(env_variables) do |
66 | final_command = final_command .. k .. "='" .. v .. "' " | 103 | final_command = final_command .. "set " .. k .. "=" .. v .. "&" |
104 | end | ||
105 | final_command = final_command:sub(1, -2) .. "&" | ||
106 | else | ||
107 | final_command = "export " | ||
108 | for k,v in pairs(env_variables) do | ||
109 | final_command = final_command .. k .. "='" .. v .. "' " | ||
110 | end | ||
111 | -- remove last space and add ';' to separate exporting variables from command | ||
112 | final_command = final_command:sub(1, -2) .. "; " | ||
67 | end | 113 | end |
68 | -- remove last space and add ';' to separate exporting variables from command | ||
69 | final_command = final_command:sub(1, -2) .. "; " | ||
70 | end | 114 | end |
71 | 115 | ||
72 | final_command = final_command .. command | 116 | final_command = final_command .. command .. " 2>&1" |
73 | 117 | ||
74 | return final_command | 118 | return final_command |
75 | end | 119 | end |
@@ -122,6 +166,9 @@ function test_env.set_args() | |||
122 | test_env.VERBOSE = true | 166 | test_env.VERBOSE = true |
123 | elseif argument == "travis" then | 167 | elseif argument == "travis" then |
124 | test_env.TRAVIS = true | 168 | test_env.TRAVIS = true |
169 | elseif argument == "appveyor" then | ||
170 | test_env.APPVEYOR = true | ||
171 | test_env.APPVEYOR_OPENSSL = "OPENSSL_LIBDIR=C:\\OpenSSL-Win32\\lib OPENSSL_INCDIR=C:\\OpenSSL-Win32\\include" | ||
125 | elseif argument:find("^os=") then | 172 | elseif argument:find("^os=") then |
126 | test_env.TEST_TARGET_OS = argument:match("^os=(.*)$") | 173 | test_env.TEST_TARGET_OS = argument:match("^os=(.*)$") |
127 | else | 174 | else |
@@ -143,6 +190,15 @@ function test_env.set_args() | |||
143 | return true | 190 | return true |
144 | end | 191 | end |
145 | 192 | ||
193 | local function copy_dir(source_path, target_path) | ||
194 | local testing_paths = test_env.testing_paths | ||
195 | if test_env.TEST_TARGET_OS == "windows" then | ||
196 | execute_bool(testing_paths.win_tools .. "/cp -R ".. source_path .. "/. " .. target_path) | ||
197 | else | ||
198 | execute_bool("cp -a ".. source_path .. "/. " .. target_path) | ||
199 | end | ||
200 | end | ||
201 | |||
146 | --- Remove directory recursively | 202 | --- Remove directory recursively |
147 | -- @param path string: directory path to delete | 203 | -- @param path string: directory path to delete |
148 | function test_env.remove_dir(path) | 204 | function test_env.remove_dir(path) |
@@ -159,7 +215,7 @@ function test_env.remove_dir(path) | |||
159 | end | 215 | end |
160 | end | 216 | end |
161 | end | 217 | end |
162 | os.remove(path) | 218 | lfs.rmdir(path) |
163 | end | 219 | end |
164 | 220 | ||
165 | --- Remove subdirectories of a directory that match a pattern | 221 | --- Remove subdirectories of a directory that match a pattern |
@@ -205,13 +261,17 @@ end | |||
205 | -- @param save_path string: path to directory, where to download rocks/rockspecs | 261 | -- @param save_path string: path to directory, where to download rocks/rockspecs |
206 | -- @return make_manifest boolean: true if new rocks downloaded | 262 | -- @return make_manifest boolean: true if new rocks downloaded |
207 | local function download_rocks(urls, save_path) | 263 | local function download_rocks(urls, save_path) |
208 | local luarocks_repo = "https://luarocks.org" | 264 | local luarocks_repo = "https://www.luarocks.org" |
209 | local make_manifest = false | 265 | local make_manifest = false |
210 | 266 | ||
211 | for _, url in ipairs(urls) do | 267 | for _, url in ipairs(urls) do |
212 | -- check if already downloaded | 268 | -- check if already downloaded |
213 | if not exists(save_path .. url) then | 269 | if not exists(save_path .. url) then |
214 | execute_bool("wget -cP " .. save_path .. " " .. luarocks_repo .. url) | 270 | if test_env.TEST_TARGET_OS == "windows" then |
271 | execute_bool(test_env.testing_paths.win_tools .. "/wget -cP " .. save_path .. " " .. luarocks_repo .. url .. " --no-check-certificate") | ||
272 | else | ||
273 | execute_bool("wget -cP " .. save_path .. " " .. luarocks_repo .. url) | ||
274 | end | ||
215 | make_manifest = true | 275 | make_manifest = true |
216 | end | 276 | end |
217 | end | 277 | end |
@@ -235,9 +295,9 @@ local function hash_environment(path) | |||
235 | return execute_output("find " .. path .. " -printf \"%s %p\n\" | md5sum") | 295 | return execute_output("find " .. path .. " -printf \"%s %p\n\" | md5sum") |
236 | elseif test_env.TEST_TARGET_OS == "osx" then | 296 | elseif test_env.TEST_TARGET_OS == "osx" then |
237 | return execute_output("find " .. path .. " -type f -exec stat -f \"%z %N\" {} \\; | md5") | 297 | return execute_output("find " .. path .. " -type f -exec stat -f \"%z %N\" {} \\; | md5") |
238 | else | 298 | elseif test_env.TEST_TARGET_OS == "windows" then |
239 | -- TODO: Windows | 299 | return execute_output("\"" .. Q(test_env.testing_paths.win_tools .. "/find") .. " " .. Q(path) |
240 | return "" | 300 | .. " -printf \"%s %p\"\" > temp_sum.txt && certUtil -hashfile temp_sum.txt && del temp_sum.txt") |
241 | end | 301 | end |
242 | end | 302 | end |
243 | 303 | ||
@@ -278,13 +338,17 @@ local function create_md5sums(testing_paths) | |||
278 | end | 338 | end |
279 | 339 | ||
280 | local function make_run_function(cmd_name, exec_function, with_coverage, do_print) | 340 | local function make_run_function(cmd_name, exec_function, with_coverage, do_print) |
281 | local cmd_prefix = test_env.testing_paths.lua .. " " | 341 | local cmd_prefix = Q(test_env.testing_paths.lua) .. " " |
282 | 342 | ||
283 | if with_coverage then | 343 | if with_coverage then |
284 | cmd_prefix = cmd_prefix .. "-e \"require('luacov.runner')('" .. test_env.testing_paths.testing_dir .. "/luacov.config')\" " | 344 | cmd_prefix = cmd_prefix .. "-e \"require('luacov.runner')('" .. test_env.testing_paths.testing_dir .. "/luacov.config')\" " |
285 | end | 345 | end |
286 | 346 | ||
287 | cmd_prefix = cmd_prefix .. test_env.testing_paths.src_dir .. "/bin/" .. cmd_name .. " " | 347 | if test_env.TEST_TARGET_OS == "windows" then |
348 | cmd_prefix = cmd_prefix .. Q(test_env.testing_paths.testing_lrprefix .. "/" .. cmd_name .. ".lua") .. " " | ||
349 | else | ||
350 | cmd_prefix = cmd_prefix .. test_env.testing_paths.src_dir .. "/bin/" .. cmd_name .. " " | ||
351 | end | ||
288 | 352 | ||
289 | return function(cmd, new_vars) | 353 | return function(cmd, new_vars) |
290 | local temp_vars = {} | 354 | local temp_vars = {} |
@@ -327,19 +391,23 @@ local function build_environment(rocks, env_variables) | |||
327 | lfs.mkdir(testing_paths.testing_tree) | 391 | lfs.mkdir(testing_paths.testing_tree) |
328 | lfs.mkdir(testing_paths.testing_sys_tree) | 392 | lfs.mkdir(testing_paths.testing_sys_tree) |
329 | 393 | ||
330 | test_env.run.luarocks_admin_nocov("make_manifest " .. testing_paths.testing_server) | 394 | test_env.run.luarocks_admin_nocov("make_manifest " .. Q(testing_paths.testing_server)) |
331 | test_env.run.luarocks_admin_nocov("make_manifest " .. testing_paths.testing_cache) | 395 | test_env.run.luarocks_admin_nocov("make_manifest " .. Q(testing_paths.testing_cache)) |
332 | 396 | ||
333 | for _, rock in ipairs(rocks) do | 397 | 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 | 398 | 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 |
335 | test_env.run.luarocks_nocov("build --tree=" .. testing_paths.testing_sys_tree .. " " .. rock, env_variables) | 399 | test_env.run.luarocks_nocov("build --tree=" .. Q(testing_paths.testing_sys_tree) .. " " .. Q(rock) .. "", env_variables) |
336 | test_env.run.luarocks_nocov("pack --tree=" .. testing_paths.testing_sys_tree .. " " .. rock, env_variables) | 400 | test_env.run.luarocks_nocov("pack --tree=" .. Q(testing_paths.testing_sys_tree) .. " " .. Q(rock), env_variables) |
337 | execute_bool("mv " .. rock .. "-*.rock " .. testing_paths.testing_cache) | 401 | if test_env.TEST_TARGET_OS == "windows" then |
402 | execute_bool(testing_paths.win_tools .. "/mv " .. rock .. "-*.rock " .. testing_paths.testing_cache) | ||
403 | else | ||
404 | execute_bool("mv " .. rock .. "-*.rock " .. testing_paths.testing_cache) | ||
405 | end | ||
338 | end | 406 | end |
339 | end | 407 | end |
340 | 408 | ||
341 | execute_bool("cp -a " .. testing_paths.testing_tree .. "/. " .. testing_paths.testing_tree_copy) | 409 | copy_dir(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) | 410 | copy_dir(testing_paths.testing_sys_tree, testing_paths.testing_sys_tree_copy) |
343 | end | 411 | end |
344 | 412 | ||
345 | --- Reset testing environment | 413 | --- Reset testing environment |
@@ -349,14 +417,13 @@ local function reset_environment(testing_paths, md5sums) | |||
349 | 417 | ||
350 | if testing_tree_md5 ~= md5sums.testing_tree_copy_md5 then | 418 | if testing_tree_md5 ~= md5sums.testing_tree_copy_md5 then |
351 | test_env.remove_dir(testing_paths.testing_tree) | 419 | test_env.remove_dir(testing_paths.testing_tree) |
352 | execute_bool("cp -a " .. testing_paths.testing_tree_copy .. "/. " .. testing_paths.testing_tree) | 420 | copy_dir(testing_paths.testing_tree_copy, testing_paths.testing_tree) |
353 | end | 421 | end |
354 | 422 | ||
355 | if testing_sys_tree_md5 ~= md5sums.testing_sys_tree_copy_md5 then | 423 | if testing_sys_tree_md5 ~= md5sums.testing_sys_tree_copy_md5 then |
356 | test_env.remove_dir(testing_paths.testing_sys_tree) | 424 | 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) | 425 | copy_dir(testing_paths.testing_sys_tree_copy, testing_paths.testing_sys_tree) |
358 | end | 426 | end |
359 | |||
360 | print("\n[ENVIRONMENT RESET]") | 427 | print("\n[ENVIRONMENT RESET]") |
361 | end | 428 | end |
362 | 429 | ||
@@ -367,9 +434,18 @@ local function create_paths(luaversion_full) | |||
367 | testing_paths.luadir = cfg.variables.LUA_BINDIR:gsub("/bin/?$", "") | 434 | testing_paths.luadir = cfg.variables.LUA_BINDIR:gsub("/bin/?$", "") |
368 | testing_paths.lua = cfg.variables.LUA_BINDIR .. "/" .. cfg.lua_interpreter | 435 | testing_paths.lua = cfg.variables.LUA_BINDIR .. "/" .. cfg.lua_interpreter |
369 | 436 | ||
370 | testing_paths.luarocks_tmp = "/tmp/luarocks_testing" --windows? | 437 | if test_env.TEST_TARGET_OS == "windows" then |
438 | testing_paths.luarocks_tmp = os.getenv("TEMP") | ||
439 | else | ||
440 | testing_paths.luarocks_tmp = "/tmp/luarocks_testing" | ||
441 | end | ||
371 | 442 | ||
372 | testing_paths.luarocks_dir = lfs.currentdir() | 443 | testing_paths.luarocks_dir = lfs.currentdir() |
444 | |||
445 | if test_env.TEST_TARGET_OS == "windows" then | ||
446 | testing_paths.luarocks_dir = testing_paths.luarocks_dir:gsub("\\","/") | ||
447 | end | ||
448 | |||
373 | testing_paths.testing_dir = testing_paths.luarocks_dir .. "/test" | 449 | testing_paths.testing_dir = testing_paths.luarocks_dir .. "/test" |
374 | testing_paths.src_dir = testing_paths.luarocks_dir .. "/src" | 450 | testing_paths.src_dir = testing_paths.luarocks_dir .. "/src" |
375 | testing_paths.testing_lrprefix = testing_paths.testing_dir .. "/testing_lrprefix-" .. luaversion_full | 451 | testing_paths.testing_lrprefix = testing_paths.testing_dir .. "/testing_lrprefix-" .. luaversion_full |
@@ -380,6 +456,10 @@ local function create_paths(luaversion_full) | |||
380 | testing_paths.testing_cache = testing_paths.testing_dir .. "/testing_cache-" .. luaversion_full | 456 | 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 | 457 | testing_paths.testing_server = testing_paths.testing_dir .. "/testing_server-" .. luaversion_full |
382 | 458 | ||
459 | if test_env.TEST_TARGET_OS == "windows" then | ||
460 | testing_paths.win_tools = testing_paths.testing_lrprefix .. "/tools" | ||
461 | end | ||
462 | |||
383 | return testing_paths | 463 | return testing_paths |
384 | end | 464 | end |
385 | 465 | ||
@@ -409,7 +489,7 @@ function test_env.setup_specs(extra_rocks) | |||
409 | test_env.main() | 489 | test_env.main() |
410 | package.path = test_env.env_variables.LUA_PATH | 490 | package.path = test_env.env_variables.LUA_PATH |
411 | 491 | ||
412 | test_env.platform = execute_output(test_env.testing_paths.lua .. " -e 'print(require(\"luarocks.cfg\").arch)'", false, test_env.env_variables) | 492 | 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) | 493 | test_env.md5sums = create_md5sums(test_env.testing_paths) |
414 | test_env.setup_done = true | 494 | test_env.setup_done = true |
415 | title("RUNNING TESTS") | 495 | title("RUNNING TESTS") |
@@ -546,13 +626,22 @@ end | |||
546 | 626 | ||
547 | --- Install luarocks into testing prefix. | 627 | --- Install luarocks into testing prefix. |
548 | local function install_luarocks(install_env_vars) | 628 | local function install_luarocks(install_env_vars) |
549 | -- Configure LuaRocks testing environment | 629 | local testing_paths = test_env.testing_paths |
550 | title("Installing LuaRocks") | 630 | title("Installing LuaRocks") |
551 | local configure_cmd = "./configure --with-lua=" .. test_env.testing_paths.luadir .. " --prefix=" .. test_env.testing_paths.testing_lrprefix | 631 | if test_env.TEST_TARGET_OS == "windows" then |
552 | assert(execute_bool(test_env.quiet(configure_cmd), false, install_env_vars)) | 632 | if test_env.LUA_V then |
553 | assert(execute_bool(test_env.quiet("make clean"), false, install_env_vars)) | 633 | assert(execute_bool("install.bat /LUA " .. testing_paths.luadir .. " /LV " .. test_env.LUA_V .. " /P " .. testing_paths.testing_lrprefix .. " /NOREG /NOADMIN /F /Q /CONFIG " .. testing_paths.testing_lrprefix .. "/etc/luarocks", false, install_env_vars)) |
554 | assert(execute_bool(test_env.quiet("make src/luarocks/site_config.lua"), false, install_env_vars)) | 634 | else |
555 | assert(execute_bool(test_env.quiet("make dev"), false, install_env_vars)) | 635 | assert(execute_bool("install.bat /LUA " .. testing_paths.luadir .. " /P " .. testing_paths.testing_lrprefix .. " /NOREG /NOADMIN /F /Q /CONFIG " .. testing_paths.testing_lrprefix .. "/etc/luarocks", false, install_env_vars)) |
636 | end | ||
637 | assert(execute_bool(testing_paths.win_tools .. "/cp " .. testing_paths.testing_lrprefix .. "/lua/luarocks/site_config* " .. testing_paths.src_dir .. "/luarocks/site_config.lua")) | ||
638 | else | ||
639 | local configure_cmd = "./configure --with-lua=" .. testing_paths.luadir .. " --prefix=" .. testing_paths.testing_lrprefix | ||
640 | assert(execute_bool(configure_cmd, false, install_env_vars)) | ||
641 | assert(execute_bool("make clean", false, install_env_vars)) | ||
642 | assert(execute_bool("make src/luarocks/site_config.lua", false, install_env_vars)) | ||
643 | assert(execute_bool("make dev", false, install_env_vars)) | ||
644 | end | ||
556 | print("LuaRocks installed correctly!") | 645 | print("LuaRocks installed correctly!") |
557 | end | 646 | end |
558 | 647 | ||
@@ -572,8 +661,8 @@ function test_env.main() | |||
572 | 661 | ||
573 | local install_env_vars = { | 662 | local install_env_vars = { |
574 | LUAROCKS_CONFIG = test_env.testing_paths.testing_dir .. "/testing_config.lua", | 663 | LUAROCKS_CONFIG = test_env.testing_paths.testing_dir .. "/testing_config.lua", |
575 | LUA_PATH = "", | 664 | LUA_PATH, |
576 | LUA_CPATH = "" | 665 | LUA_CPATH |
577 | } | 666 | } |
578 | 667 | ||
579 | install_luarocks(install_env_vars) | 668 | install_luarocks(install_env_vars) |