diff options
| author | Hisham Muhammad <hisham@gobolinux.org> | 2022-04-11 19:16:10 -0300 |
|---|---|---|
| committer | Hisham Muhammad <hisham@gobolinux.org> | 2022-04-12 13:19:12 -0300 |
| commit | 238ba6b21cfcb1af56f1e70dd50de201fc3482a7 (patch) | |
| tree | c948b6d3e12e3a518a69d0c844e016c590b31825 /spec/util/test_env.lua | |
| parent | df8a3cd3c957db337bfc3ab4d404c33066a24923 (diff) | |
| download | luarocks-238ba6b21cfcb1af56f1e70dd50de201fc3482a7.tar.gz luarocks-238ba6b21cfcb1af56f1e70dd50de201fc3482a7.tar.bz2 luarocks-238ba6b21cfcb1af56f1e70dd50de201fc3482a7.zip | |
tests: avoid repeated hardcoded package version numbers all over
Diffstat (limited to 'spec/util/test_env.lua')
| -rw-r--r-- | spec/util/test_env.lua | 131 |
1 files changed, 107 insertions, 24 deletions
diff --git a/spec/util/test_env.lua b/spec/util/test_env.lua index 737d90a5..6a466bae 100644 --- a/spec/util/test_env.lua +++ b/spec/util/test_env.lua | |||
| @@ -1,6 +1,7 @@ | |||
| 1 | local test_env = {} | 1 | local test_env = {} |
| 2 | 2 | ||
| 3 | local lfs = require("lfs") | 3 | local lfs = require("lfs") |
| 4 | local versions = require("spec.util.versions") | ||
| 4 | 5 | ||
| 5 | local help_message = [[ | 6 | local help_message = [[ |
| 6 | LuaRocks test-suite | 7 | LuaRocks test-suite |
| @@ -36,14 +37,6 @@ local function title(str) | |||
| 36 | print(("-"):rep(#str)) | 37 | print(("-"):rep(#str)) |
| 37 | end | 38 | end |
| 38 | 39 | ||
| 39 | function test_env.exists(path) | ||
| 40 | return lfs.attributes(path, "mode") ~= nil | ||
| 41 | end | ||
| 42 | |||
| 43 | function test_env.file_if_exists(path) | ||
| 44 | return lfs.attributes(path, "mode") and path | ||
| 45 | end | ||
| 46 | |||
| 47 | --- Quote argument for shell processing. Fixes paths on Windows. | 40 | --- Quote argument for shell processing. Fixes paths on Windows. |
| 48 | -- Adds double quotes and escapes. Based on function in fs/win32.lua. | 41 | -- Adds double quotes and escapes. Based on function in fs/win32.lua. |
| 49 | -- @param arg string: Unquoted argument. | 42 | -- @param arg string: Unquoted argument. |
| @@ -66,6 +59,62 @@ local function Q(arg) | |||
| 66 | end | 59 | end |
| 67 | end | 60 | end |
| 68 | 61 | ||
| 62 | local function V(str) | ||
| 63 | return (str:gsub("${([^}]-)}", function(name) | ||
| 64 | name = name:lower() | ||
| 65 | local prefix, suffix = name:match("^(.*)_(.)$") | ||
| 66 | if suffix then | ||
| 67 | name = prefix | ||
| 68 | local d = assert(versions[name]) | ||
| 69 | local v, r = d:match("^([^-]*)%-(%d*)$") | ||
| 70 | if suffix == "d" then | ||
| 71 | return d | ||
| 72 | elseif suffix == "v" then | ||
| 73 | return v | ||
| 74 | elseif suffix == "r" then | ||
| 75 | return v | ||
| 76 | else | ||
| 77 | print("Test error: invalid suffix " .. suffix .. " in variable " .. name) | ||
| 78 | os.exit(1) | ||
| 79 | end | ||
| 80 | else | ||
| 81 | if not versions[name] then | ||
| 82 | print("Test error: no version definition for " .. name) | ||
| 83 | os.exit(1) | ||
| 84 | end | ||
| 85 | return versions[name] | ||
| 86 | end | ||
| 87 | end)) | ||
| 88 | end | ||
| 89 | |||
| 90 | local os_remove = os.remove | ||
| 91 | os.remove = function(f) -- luacheck: ignore | ||
| 92 | return os_remove(V(f)) | ||
| 93 | end | ||
| 94 | |||
| 95 | local os_rename = os.rename | ||
| 96 | os.rename = function(a, b) -- luacheck: ignore | ||
| 97 | return os_rename(V(a), V(b)) | ||
| 98 | end | ||
| 99 | |||
| 100 | local lfs_chdir = lfs.chdir | ||
| 101 | lfs.chdir = function(d) -- luacheck: ignore | ||
| 102 | return lfs_chdir(V(d)) | ||
| 103 | end | ||
| 104 | |||
| 105 | local lfs_attributes = lfs.attributes | ||
| 106 | lfs.attributes = function(f, ...) -- luacheck: ignore | ||
| 107 | return lfs_attributes(V(f), ...) | ||
| 108 | end | ||
| 109 | |||
| 110 | function test_env.exists(path) | ||
| 111 | return lfs.attributes(path, "mode") ~= nil | ||
| 112 | end | ||
| 113 | |||
| 114 | function test_env.file_if_exists(path) | ||
| 115 | return lfs.attributes(path, "mode") and path | ||
| 116 | end | ||
| 117 | |||
| 69 | function test_env.quiet(command) | 118 | function test_env.quiet(command) |
| 70 | if not test_env.VERBOSE then | 119 | if not test_env.VERBOSE then |
| 71 | if test_env.TEST_TARGET_OS == "windows" then | 120 | if test_env.TEST_TARGET_OS == "windows" then |
| @@ -79,6 +128,9 @@ function test_env.quiet(command) | |||
| 79 | end | 128 | end |
| 80 | 129 | ||
| 81 | function test_env.copy(source, destination) | 130 | function test_env.copy(source, destination) |
| 131 | source = V(source) | ||
| 132 | destination = V(destination) | ||
| 133 | |||
| 82 | local r_source, err = io.open(source, "r") | 134 | local r_source, err = io.open(source, "r") |
| 83 | local r_destination, err = io.open(destination, "w") | 135 | local r_destination, err = io.open(destination, "w") |
| 84 | 136 | ||
| @@ -303,6 +355,9 @@ function test_env.set_args() | |||
| 303 | end | 355 | end |
| 304 | 356 | ||
| 305 | function test_env.copy_dir(source_path, target_path) | 357 | function test_env.copy_dir(source_path, target_path) |
| 358 | source_path = V(source_path) | ||
| 359 | target_path = V(target_path) | ||
| 360 | |||
| 306 | local testing_paths = test_env.testing_paths | 361 | local testing_paths = test_env.testing_paths |
| 307 | if test_env.TEST_TARGET_OS == "windows" then | 362 | if test_env.TEST_TARGET_OS == "windows" then |
| 308 | execute_bool(testing_paths.win_tools .. "/cp -R ".. source_path .. "/. " .. target_path) | 363 | execute_bool(testing_paths.win_tools .. "/cp -R ".. source_path .. "/. " .. target_path) |
| @@ -314,6 +369,8 @@ end | |||
| 314 | --- Remove directory recursively | 369 | --- Remove directory recursively |
| 315 | -- @param path string: directory path to delete | 370 | -- @param path string: directory path to delete |
| 316 | function test_env.remove_dir(path) | 371 | function test_env.remove_dir(path) |
| 372 | path = V(path) | ||
| 373 | |||
| 317 | if test_env.exists(path) then | 374 | if test_env.exists(path) then |
| 318 | for file in lfs.dir(path) do | 375 | for file in lfs.dir(path) do |
| 319 | if file ~= "." and file ~= ".." then | 376 | if file ~= "." and file ~= ".." then |
| @@ -334,6 +391,8 @@ end | |||
| 334 | -- @param path string: path to directory | 391 | -- @param path string: path to directory |
| 335 | -- @param pattern string: pattern matching basenames of subdirectories to be removed | 392 | -- @param pattern string: pattern matching basenames of subdirectories to be removed |
| 336 | function test_env.remove_subdirs(path, pattern) | 393 | function test_env.remove_subdirs(path, pattern) |
| 394 | path = V(path) | ||
| 395 | |||
| 337 | if test_env.exists(path) then | 396 | if test_env.exists(path) then |
| 338 | for file in lfs.dir(path) do | 397 | for file in lfs.dir(path) do |
| 339 | if file ~= "." and file ~= ".." then | 398 | if file ~= "." and file ~= ".." then |
| @@ -352,6 +411,8 @@ end | |||
| 352 | -- @param pattern string: pattern matching basenames of files to be deleted | 411 | -- @param pattern string: pattern matching basenames of files to be deleted |
| 353 | -- @return result_check boolean: true if one or more files deleted | 412 | -- @return result_check boolean: true if one or more files deleted |
| 354 | function test_env.remove_files(path, pattern) | 413 | function test_env.remove_files(path, pattern) |
| 414 | path = V(path) | ||
| 415 | |||
| 355 | local result_check = false | 416 | local result_check = false |
| 356 | if test_env.exists(path) then | 417 | if test_env.exists(path) then |
| 357 | for file in lfs.dir(path) do | 418 | for file in lfs.dir(path) do |
| @@ -378,6 +439,8 @@ local function download_rocks(urls, save_path) | |||
| 378 | local to_download = {} | 439 | local to_download = {} |
| 379 | local fixtures = {} | 440 | local fixtures = {} |
| 380 | for _, url in ipairs(urls) do | 441 | for _, url in ipairs(urls) do |
| 442 | url = V(url) | ||
| 443 | |||
| 381 | if url:match("^spec/fixtures") then | 444 | if url:match("^spec/fixtures") then |
| 382 | table.insert(fixtures, (url:gsub("^spec/fixtures", test_env.testing_paths.fixtures_dir))) | 445 | table.insert(fixtures, (url:gsub("^spec/fixtures", test_env.testing_paths.fixtures_dir))) |
| 383 | else | 446 | else |
| @@ -399,7 +462,10 @@ local function download_rocks(urls, save_path) | |||
| 399 | else | 462 | else |
| 400 | cmd = "wget -cP " .. save_path | 463 | cmd = "wget -cP " .. save_path |
| 401 | end | 464 | end |
| 402 | assert(execute_bool(cmd.." "..table.concat(to_download, " "))) | 465 | local ok = execute_bool(cmd.." "..table.concat(to_download, " ")) |
| 466 | if not ok then | ||
| 467 | os.exit(1) | ||
| 468 | end | ||
| 403 | end | 469 | end |
| 404 | 470 | ||
| 405 | return (#fixtures > 0) or (#to_download > 0) | 471 | return (#fixtures > 0) or (#to_download > 0) |
| @@ -409,6 +475,8 @@ end | |||
| 409 | -- @param pathname string: path to file. | 475 | -- @param pathname string: path to file. |
| 410 | -- @param str string: content of the file. | 476 | -- @param str string: content of the file. |
| 411 | function test_env.write_file(pathname, str, finally) | 477 | function test_env.write_file(pathname, str, finally) |
| 478 | pathname = V(pathname) | ||
| 479 | |||
| 412 | local file = assert(io.open(pathname, "w")) | 480 | local file = assert(io.open(pathname, "w")) |
| 413 | file:write(str) | 481 | file:write(str) |
| 414 | file:close() | 482 | file:close() |
| @@ -487,6 +555,7 @@ local function make_run_function(cmd_name, exec_function, with_coverage, do_prin | |||
| 487 | end | 555 | end |
| 488 | 556 | ||
| 489 | return function(cmd, new_vars) | 557 | return function(cmd, new_vars) |
| 558 | cmd = V(cmd) | ||
| 490 | local temp_vars = {} | 559 | local temp_vars = {} |
| 491 | for k, v in pairs(test_env.env_variables) do | 560 | for k, v in pairs(test_env.env_variables) do |
| 492 | temp_vars[k] = v | 561 | temp_vars[k] = v |
| @@ -519,7 +588,11 @@ local function move_file(src, dst) | |||
| 519 | if test_env.TEST_TARGET_OS == "windows" then | 588 | if test_env.TEST_TARGET_OS == "windows" then |
| 520 | execute_bool(test_env.testing_paths.win_tools .. "/mv " .. src .. " " .. dst) | 589 | execute_bool(test_env.testing_paths.win_tools .. "/mv " .. src .. " " .. dst) |
| 521 | else | 590 | else |
| 522 | execute_bool("mv " .. src .. " " .. dst) | 591 | local ok = execute_bool("mv " .. src .. " " .. dst) |
| 592 | if not ok then | ||
| 593 | print(debug.traceback()) | ||
| 594 | os.exit(1) | ||
| 595 | end | ||
| 523 | end | 596 | end |
| 524 | end | 597 | end |
| 525 | 598 | ||
| @@ -541,9 +614,9 @@ local function build_environment(rocks, env_variables) | |||
| 541 | test_env.run.luarocks_admin_nocov("make_manifest " .. Q(testing_paths.testing_cache)) | 614 | test_env.run.luarocks_admin_nocov("make_manifest " .. Q(testing_paths.testing_cache)) |
| 542 | 615 | ||
| 543 | for _, rock in ipairs(rocks) do | 616 | for _, rock in ipairs(rocks) do |
| 544 | 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 | 617 | if not test_env.run.luarocks_nocov(test_env.quiet("install --only-server=" .. testing_paths.testing_cache .. " --tree=" .. testing_paths.testing_sys_tree .. " " .. Q(rock), env_variables)) then |
| 545 | test_env.run.luarocks_nocov("build --tree=" .. Q(testing_paths.testing_sys_tree) .. " " .. Q(rock), env_variables) | 618 | assert(test_env.run.luarocks_nocov("build --tree=" .. Q(testing_paths.testing_sys_tree) .. " " .. Q(rock), env_variables)) |
| 546 | test_env.run.luarocks_nocov("pack --tree=" .. Q(testing_paths.testing_sys_tree) .. " " .. Q(rock), env_variables) | 619 | assert(test_env.run.luarocks_nocov("pack --tree=" .. Q(testing_paths.testing_sys_tree) .. " " .. Q(rock), env_variables)) |
| 547 | move_file(rock .. "-*.rock", testing_paths.testing_cache) | 620 | move_file(rock .. "-*.rock", testing_paths.testing_cache) |
| 548 | end | 621 | end |
| 549 | end | 622 | end |
| @@ -685,6 +758,8 @@ end | |||
| 685 | -- Return `true` if the rock is already installed or has been installed successfully, | 758 | -- Return `true` if the rock is already installed or has been installed successfully, |
| 686 | -- `false` if installation failed. | 759 | -- `false` if installation failed. |
| 687 | function test_env.need_rock(rock) | 760 | function test_env.need_rock(rock) |
| 761 | rock = V(rock) | ||
| 762 | |||
| 688 | print("Check if " .. rock .. " is installed") | 763 | print("Check if " .. rock .. " is installed") |
| 689 | if test_env.run.luarocks_noprint_nocov(test_env.quiet("show " .. rock)) then | 764 | if test_env.run.luarocks_noprint_nocov(test_env.quiet("show " .. rock)) then |
| 690 | return true | 765 | return true |
| @@ -887,10 +962,12 @@ local function prepare_mock_server_binary_rocks() | |||
| 887 | 962 | ||
| 888 | local rocks = { | 963 | local rocks = { |
| 889 | -- rocks needed for mock-server | 964 | -- rocks needed for mock-server |
| 890 | "luasocket-3.0rc1-2.src.rock", | 965 | "luasocket-${LUASOCKET}.src.rock", |
| 891 | "coxpcall-1.16.0-1.src.rock", | 966 | "coxpcall-1.16.0-1.src.rock", |
| 892 | "copas-2.0.1-1.src.rock", | 967 | "binaryheap-${BINARYHEAP}.src.rock", |
| 893 | "luafilesystem-1.7.0-2.src.rock", | 968 | "timerwheel-${TIMERWHEEL}.src.rock", |
| 969 | "copas-${COPAS}.src.rock", | ||
| 970 | "luafilesystem-${LUAFILESYSTEM}.src.rock", | ||
| 894 | "xavante-2.4.0-1.src.rock", | 971 | "xavante-2.4.0-1.src.rock", |
| 895 | "wsapi-1.6.1-1.src.rock", | 972 | "wsapi-1.6.1-1.src.rock", |
| 896 | "rings-1.3.0-1.src.rock", | 973 | "rings-1.3.0-1.src.rock", |
| @@ -901,6 +978,7 @@ local function prepare_mock_server_binary_rocks() | |||
| 901 | } | 978 | } |
| 902 | local make_manifest = download_rocks(rocks, testing_paths.testing_server) | 979 | local make_manifest = download_rocks(rocks, testing_paths.testing_server) |
| 903 | for _, rock in ipairs(rocks) do | 980 | for _, rock in ipairs(rocks) do |
| 981 | rock = V(rock) | ||
| 904 | local rockname = rock:gsub("%-[^-]+%-%d+%.[%a.]+$", "") | 982 | local rockname = rock:gsub("%-[^-]+%-%d+%.[%a.]+$", "") |
| 905 | if not find_binary_rock(rock, testing_paths.testing_server) then | 983 | if not find_binary_rock(rock, testing_paths.testing_server) then |
| 906 | test_env.run.luarocks_nocov("build " .. Q(testing_paths.testing_server .. "/" .. rock) .. " --tree=" .. testing_paths.testing_cache) | 984 | test_env.run.luarocks_nocov("build " .. Q(testing_paths.testing_server .. "/" .. rock) .. " --tree=" .. testing_paths.testing_cache) |
| @@ -937,25 +1015,29 @@ function test_env.main() | |||
| 937 | local urls = {} -- names of rock and rockspec files to be downloaded | 1015 | local urls = {} -- names of rock and rockspec files to be downloaded |
| 938 | 1016 | ||
| 939 | if test_env.TYPE_TEST_ENV == "full" then | 1017 | if test_env.TYPE_TEST_ENV == "full" then |
| 940 | table.insert(urls, "/luafilesystem-1.6.3-1.src.rock") | 1018 | table.insert(urls, "/luafilesystem-${LUAFILESYSTEM}.src.rock") |
| 941 | table.insert(urls, "/luasocket-3.0rc1-1.src.rock") | 1019 | table.insert(urls, "/luasocket-${LUASOCKET}.src.rock") |
| 942 | table.insert(urls, "/luasocket-3.0rc1-1.rockspec") | 1020 | table.insert(urls, "/luasocket-${LUASOCKET}.rockspec") |
| 943 | table.insert(urls, "/md5-1.2-1.src.rock") | 1021 | table.insert(urls, "/md5-1.2-1.src.rock") |
| 944 | --table.insert(urls, "/lzlib-0.4.1.53-1.src.rock") | 1022 | --table.insert(urls, "/lzlib-0.4.1.53-1.src.rock") |
| 945 | table.insert(urls, "/lua-zlib-1.2-0.src.rock") | 1023 | table.insert(urls, "/lua-zlib-1.2-0.src.rock") |
| 946 | table.insert(urls, "/lua-bz2-0.1.0-1.src.rock") | 1024 | table.insert(urls, "/lua-bz2-0.1.0-1.src.rock") |
| 947 | rocks = {"luafilesystem", "luasocket", "md5", "lua-zlib", "lua-bz2"} | 1025 | rocks = {"luafilesystem", "luasocket", "md5", "lua-zlib", "lua-bz2"} |
| 948 | if test_env.TEST_TARGET_OS ~= "windows" then | 1026 | if test_env.TEST_TARGET_OS ~= "windows" then |
| 949 | table.insert(urls, "/luaposix-33.2.1-1.src.rock") | 1027 | if test_env.lua_version == "5.1" then |
| 1028 | table.insert(urls, "/bit32-${BIT32}.src.rock") | ||
| 1029 | table.insert(rocks, "bit32") | ||
| 1030 | end | ||
| 1031 | table.insert(urls, "/luaposix-${LUAPOSIX}.src.rock") | ||
| 950 | table.insert(rocks, "luaposix") | 1032 | table.insert(rocks, "luaposix") |
| 951 | end | 1033 | end |
| 952 | end | 1034 | end |
| 953 | 1035 | ||
| 954 | -- luacov is needed for both minimal or full environment | 1036 | -- luacov is needed for both minimal or full environment |
| 955 | table.insert(urls, "/luacov-0.15.0-1.rockspec") | 1037 | table.insert(urls, "/luacov-${LUACOV}.rockspec") |
| 956 | table.insert(urls, "/luacov-0.15.0-1.src.rock") | 1038 | table.insert(urls, "/luacov-${LUACOV}.src.rock") |
| 957 | table.insert(urls, "/cluacov-0.1.1-1.rockspec") | 1039 | table.insert(urls, "/cluacov-${CLUACOV}.rockspec") |
| 958 | table.insert(urls, "/cluacov-0.1.1-1.src.rock") | 1040 | table.insert(urls, "/cluacov-${CLUACOV}.src.rock") |
| 959 | table.insert(rocks, "luacov") | 1041 | table.insert(rocks, "luacov") |
| 960 | table.insert(rocks, "cluacov") | 1042 | table.insert(rocks, "cluacov") |
| 961 | 1043 | ||
| @@ -977,5 +1059,6 @@ test_env.set_args() | |||
| 977 | test_env.testing_paths = create_paths(test_env.LUA_V or test_env.LUAJIT_V) | 1059 | test_env.testing_paths = create_paths(test_env.LUA_V or test_env.LUAJIT_V) |
| 978 | test_env.env_variables = create_env(test_env.testing_paths) | 1060 | test_env.env_variables = create_env(test_env.testing_paths) |
| 979 | test_env.run = make_run_functions() | 1061 | test_env.run = make_run_functions() |
| 1062 | test_env.V = V | ||
| 980 | 1063 | ||
| 981 | return test_env | 1064 | return test_env |
