aboutsummaryrefslogtreecommitdiff
path: root/spec/util
diff options
context:
space:
mode:
authorHisham Muhammad <hisham@gobolinux.org>2024-02-29 00:46:06 -0300
committerHisham Muhammad <hisham@gobolinux.org>2024-02-29 15:46:16 +0000
commitf76b7a2b13e411df2e696146bb0a6396781acd92 (patch)
tree38ed32f81f05f6eacd03f36eacd5341e7b1d17dd /spec/util
parent0ca8c460e867356342180bb625760ed9201a5503 (diff)
downloadluarocks-f76b7a2b13e411df2e696146bb0a6396781acd92.tar.gz
luarocks-f76b7a2b13e411df2e696146bb0a6396781acd92.tar.bz2
luarocks-f76b7a2b13e411df2e696146bb0a6396781acd92.zip
tests: speed up and simplify
Diffstat (limited to 'spec/util')
-rw-r--r--spec/util/quick.lua46
-rw-r--r--spec/util/test_env.lua87
2 files changed, 71 insertions, 62 deletions
diff --git a/spec/util/quick.lua b/spec/util/quick.lua
index c313f575..5c49fff9 100644
--- a/spec/util/quick.lua
+++ b/spec/util/quick.lua
@@ -159,6 +159,16 @@ local function parse(filename)
159 cur_block = cur_op 159 cur_block = cur_op
160 cur_block_name = "FILE" 160 cur_block_name = "FILE"
161 table.insert(stack, "block start") 161 table.insert(stack, "block start")
162 elseif cmd == "FILE_CONTENTS" then
163 cur_op = {
164 op = "FILE_CONTENTS",
165 name = arg,
166 data = {},
167 }
168 table.insert(cur_test.ops, cur_op)
169 cur_block = cur_op
170 cur_block_name = "FILE_CONTENTS"
171 table.insert(stack, "block start")
162 elseif cmd == "RUN" then 172 elseif cmd == "RUN" then
163 local program, args = arg:match("([^ ]+)%s*(.*)$") 173 local program, args = arg:match("([^ ]+)%s*(.*)$")
164 if not program then 174 if not program then
@@ -195,6 +205,20 @@ local function parse(filename)
195 line = cur_line, 205 line = cur_line,
196 } 206 }
197 table.insert(cur_test.ops, cur_op) 207 table.insert(cur_test.ops, cur_op)
208 elseif cmd == "RMDIR" then
209 cur_op = {
210 op = "RMDIR",
211 file = dir.normalize(arg),
212 line = cur_line,
213 }
214 table.insert(cur_test.ops, cur_op)
215 elseif cmd == "RM" then
216 cur_op = {
217 op = "RM",
218 file = dir.normalize(arg),
219 line = cur_line,
220 }
221 table.insert(cur_test.ops, cur_op)
198 elseif cmd == "EXIT" then 222 elseif cmd == "EXIT" then
199 if not cur_op or cur_op.op ~= "RUN" then 223 if not cur_op or cur_op.op ~= "RUN" then
200 fail("EXIT must be given in the context of a RUN") 224 fail("EXIT must be given in the context of a RUN")
@@ -346,6 +370,28 @@ function quick.compile(filename, env)
346 op.file = native_slash(op.file) 370 op.file = native_slash(op.file)
347 write(([=[ ok, err = make_dir(%q) ]=]):format(op.file)) 371 write(([=[ ok, err = make_dir(%q) ]=]):format(op.file))
348 write(([=[ assert.truthy((lfs.attributes(%q) or {}).mode == "directory", error_message(%d, "MKDIR failed: " .. %q .. " - " .. (err or "") )) ]=]):format(op.file, op.line, op.file)) 372 write(([=[ assert.truthy((lfs.attributes(%q) or {}).mode == "directory", error_message(%d, "MKDIR failed: " .. %q .. " - " .. (err or "") )) ]=]):format(op.file, op.line, op.file))
373 elseif op.op == "RMDIR" then
374 op.file = native_slash(op.file)
375 write(([=[ ok, err = test_env.remove_dir(%q) ]=]):format(op.file))
376 write(([=[ assert.falsy((lfs.attributes(%q) or {}).mode == "directory", error_message(%d, "MKDIR failed: " .. %q .. " - " .. (err or "") )) ]=]):format(op.file, op.line, op.file))
377 elseif op.op == "RM" then
378 op.file = native_slash(op.file)
379 write(([=[ ok, err = os.remove(%q) ]=]):format(op.file))
380 write(([=[ assert.falsy((lfs.attributes(%q) or {}).mode == "file", error_message(%d, "RM failed: " .. %q .. " - " .. (err or "") )) ]=]):format(op.file, op.line, op.file))
381 elseif op.op == "FILE_CONTENTS" then
382 write(([=[ do ]=]))
383 write(([=[ local fd_file = assert(io.open(%q, "rb")) ]=]):format(op.name))
384 write(([=[ local file_data = fd_file:read("*a") ]=]))
385 write(([=[ fd_file:close() ]=]))
386 write([=[ local block_at = 1 ]=])
387 write([=[ local s, e, line ]=])
388 for i, line in ipairs(op.data) do
389 write(([=[ line = %q ]=]):format(line))
390 write(([=[ s, e = string.find(file_data, line, 1, true) ]=]))
391 write(([=[ assert(s, error_message(%d, "FILE_CONTENTS %s did not match: " .. line, file_data)) ]=]):format(op.start + i, op.name))
392 write(([=[ block_at = e + 1 ]=]):format(i))
393 end
394 write([=[ end ]=])
349 elseif op.op == "RUN" then 395 elseif op.op == "RUN" then
350 local cmd_helper = cmd_helpers[op.program] or ("%q"):format(op.program) 396 local cmd_helper = cmd_helpers[op.program] or ("%q"):format(op.program)
351 local redirs = " 1>stdout.txt 2>stderr.txt " 397 local redirs = " 1>stdout.txt 2>stderr.txt "
diff --git a/spec/util/test_env.lua b/spec/util/test_env.lua
index 8ccb494b..be75b4ec 100644
--- a/spec/util/test_env.lua
+++ b/spec/util/test_env.lua
@@ -527,21 +527,6 @@ function test_env.write_file(pathname, str, finally)
527 end 527 end
528end 528end
529 529
530--- Create md5sum of directory structure recursively, based on filename and size
531-- @param path string: path to directory for generate md5sum
532-- @return md5sum string: md5sum of directory
533local function hash_environment(path)
534 if test_env.TEST_TARGET_OS == "linux" then
535 return execute_output(C("cd", path, "&& find . -printf \"%s %p\n\" | md5sum"))
536 elseif test_env.TEST_TARGET_OS == "osx" then
537 return execute_output(C("find", path, "-type f -exec stat -f \"%z %N\" {} \\; | md5"))
538 elseif test_env.TEST_TARGET_OS == "windows" then
539 return execute_output(
540 "\"" .. C(tool("find"), Q(path), "-printf", "\"%s %p\"") .. "\"" ..
541 " > temp_sum.txt && certUtil -hashfile temp_sum.txt && del temp_sum.txt")
542 end
543end
544
545--- Create environment variables needed for tests 530--- Create environment variables needed for tests
546-- @param testing_paths table: table with paths to testing directory 531-- @param testing_paths table: table with paths to testing directory
547-- @return env_variables table: table with created environment variables 532-- @return env_variables table: table with created environment variables
@@ -551,6 +536,7 @@ local function create_env(testing_paths)
551 local lrprefix = testing_paths.testing_lrprefix 536 local lrprefix = testing_paths.testing_lrprefix
552 local tree = testing_paths.testing_tree 537 local tree = testing_paths.testing_tree
553 local sys_tree = testing_paths.testing_sys_tree 538 local sys_tree = testing_paths.testing_sys_tree
539 local deps_tree = testing_paths.testing_deps_tree
554 540
555 if test_env.LUAJIT_V then 541 if test_env.LUAJIT_V then
556 lua_v="5.1" 542 lua_v="5.1"
@@ -567,38 +553,31 @@ local function create_env(testing_paths)
567 else 553 else
568 table.insert(lua_path, dir_path(lrprefix, "share", "lua", lua_v, "?.lua")) 554 table.insert(lua_path, dir_path(lrprefix, "share", "lua", lua_v, "?.lua"))
569 end 555 end
570 table.insert(lua_path, dir_path(tree, "share", "lua", lua_v, "?.lua")) 556 table.insert(lua_path, dir_path(tree, "share", "lua", lua_v, "?.lua"))
571 table.insert(lua_path, dir_path(tree, "share", "lua", lua_v, "?", "init.lua")) 557 table.insert(lua_path, dir_path(tree, "share", "lua", lua_v, "?", "init.lua"))
572 table.insert(lua_path, dir_path(sys_tree, "share", "lua", lua_v, "?.lua")) 558 table.insert(lua_path, dir_path(sys_tree, "share", "lua", lua_v, "?.lua"))
573 table.insert(lua_path, dir_path(sys_tree, "share", "lua", lua_v, "?", "init.lua")) 559 table.insert(lua_path, dir_path(sys_tree, "share", "lua", lua_v, "?", "init.lua"))
560 table.insert(lua_path, dir_path(deps_tree, "share", "lua", lua_v, "?.lua"))
561 table.insert(lua_path, dir_path(deps_tree, "share", "lua", lua_v, "?", "init.lua"))
574 table.insert(lua_path, dir_path(testing_paths.src_dir, "?.lua")) 562 table.insert(lua_path, dir_path(testing_paths.src_dir, "?.lua"))
575 env_variables.LUA_PATH = table.concat(lua_path, ";") .. ";" 563 env_variables.LUA_PATH = table.concat(lua_path, ";") .. ";"
576 564
577 local lua_cpath = {} 565 local lua_cpath = {}
578 local lib_pattern = "?." .. test_env.lib_extension 566 local lib_pattern = "?." .. test_env.lib_extension
579 table.insert(lua_cpath, dir_path(tree, "lib", "lua", lua_v, lib_pattern)) 567 table.insert(lua_cpath, dir_path(tree, "lib", "lua", lua_v, lib_pattern))
580 table.insert(lua_cpath, dir_path(sys_tree, "lib", "lua", lua_v, lib_pattern)) 568 table.insert(lua_cpath, dir_path(sys_tree, "lib", "lua", lua_v, lib_pattern))
569 table.insert(lua_cpath, dir_path(deps_tree, "lib", "lua", lua_v, lib_pattern))
581 env_variables.LUA_CPATH = table.concat(lua_cpath, ";") .. ";" 570 env_variables.LUA_CPATH = table.concat(lua_cpath, ";") .. ";"
582 571
583 local path = { os.getenv("PATH") } 572 local path = { os.getenv("PATH") }
584 table.insert(path, dir_path(tree, "bin")) 573 table.insert(path, dir_path(tree, "bin"))
585 table.insert(path, dir_path(sys_tree, "bin")) 574 table.insert(path, dir_path(sys_tree, "bin"))
575 table.insert(path, dir_path(deps_tree, "bin"))
586 env_variables.PATH = table.concat(path, test_env.TARGET_OS == "windows" and ";" or ":") 576 env_variables.PATH = table.concat(path, test_env.TARGET_OS == "windows" and ";" or ":")
587 577
588 return env_variables 578 return env_variables
589end 579end
590 580
591--- Create md5sums of origin system and system-copy testing directory
592-- @param testing_paths table: table with paths to testing directory
593-- @return md5sums table: table of md5sums of system and system-copy testing directory
594local function create_md5sums(testing_paths)
595 local md5sums = {}
596 md5sums.testing_tree_copy_md5 = hash_environment(testing_paths.testing_tree_copy)
597 md5sums.testing_sys_tree_copy_md5 = hash_environment(testing_paths.testing_sys_tree_copy)
598
599 return md5sums
600end
601
602local function make_run_function(cmd_name, exec_function, with_coverage, do_print) 581local function make_run_function(cmd_name, exec_function, with_coverage, do_print)
603 local cmd_prefix = Q(test_env.testing_paths.lua) 582 local cmd_prefix = Q(test_env.testing_paths.lua)
604 local testrun_dir = test_env.testing_paths.testrun_dir 583 local testrun_dir = test_env.testing_paths.testrun_dir
@@ -663,43 +642,23 @@ local function build_environment(rocks, env_variables)
663 local testing_paths = test_env.testing_paths 642 local testing_paths = test_env.testing_paths
664 test_env.remove_dir(testing_paths.testing_tree) 643 test_env.remove_dir(testing_paths.testing_tree)
665 test_env.remove_dir(testing_paths.testing_sys_tree) 644 test_env.remove_dir(testing_paths.testing_sys_tree)
666 test_env.remove_dir(testing_paths.testing_tree_copy)
667 test_env.remove_dir(testing_paths.testing_sys_tree_copy)
668 645
669 lfs.mkdir(testing_paths.testing_tree) 646 lfs.mkdir(testing_paths.testing_tree)
670 lfs.mkdir(testing_paths.testing_sys_tree) 647 lfs.mkdir(testing_paths.testing_sys_tree)
648 lfs.mkdir(testing_paths.testing_deps_tree)
671 649
672 test_env.run.luarocks_admin_nocov(C("make_manifest", Q(testing_paths.testing_server))) 650 test_env.run.luarocks_admin_nocov(C("make_manifest", Q(testing_paths.testing_server)))
673 test_env.run.luarocks_admin_nocov(C("make_manifest", Q(testing_paths.testing_cache))) 651 test_env.run.luarocks_admin_nocov(C("make_manifest", Q(testing_paths.testing_cache)))
674 652
675 for _, rock in ipairs(rocks) do 653 for _, rock in ipairs(rocks) do
676 local only_server = "--only-server=" .. testing_paths.testing_cache 654 local only_server = "--only-server=" .. testing_paths.testing_cache
677 local tree = "--tree=" .. testing_paths.testing_sys_tree 655 local tree = "--tree=" .. testing_paths.testing_deps_tree
678 if not test_env.run.luarocks_nocov(test_env.quiet(C("install", only_server, tree, Q(rock)), env_variables)) then 656 if not test_env.run.luarocks_nocov(test_env.quiet(C("install", only_server, tree, Q(rock)), env_variables)) then
679 assert(test_env.run.luarocks_nocov(C("build", tree, Q(rock)), env_variables)) 657 assert(test_env.run.luarocks_nocov(C("build", tree, Q(rock)), env_variables))
680 assert(test_env.run.luarocks_nocov(C("pack", tree, Q(rock)), env_variables)) 658 assert(test_env.run.luarocks_nocov(C("pack", tree, Q(rock)), env_variables))
681 move_file(rock .. "-*.rock", testing_paths.testing_cache) 659 move_file(rock .. "-*.rock", testing_paths.testing_cache)
682 end 660 end
683 end 661 end
684
685 test_env.copy_dir(testing_paths.testing_tree, testing_paths.testing_tree_copy)
686 test_env.copy_dir(testing_paths.testing_sys_tree, testing_paths.testing_sys_tree_copy)
687end
688
689--- Reset testing environment
690local function reset_environment(testing_paths, md5sums)
691 local testing_tree_md5 = hash_environment(testing_paths.testing_tree)
692 local testing_sys_tree_md5 = hash_environment(testing_paths.testing_sys_tree)
693
694 if testing_tree_md5 ~= md5sums.testing_tree_copy_md5 then
695 test_env.remove_dir(testing_paths.testing_tree)
696 test_env.copy_dir(testing_paths.testing_tree_copy, testing_paths.testing_tree)
697 end
698
699 if testing_sys_tree_md5 ~= md5sums.testing_sys_tree_copy_md5 then
700 test_env.remove_dir(testing_paths.testing_sys_tree)
701 test_env.copy_dir(testing_paths.testing_sys_tree_copy, testing_paths.testing_sys_tree)
702 end
703end 662end
704 663
705local function find_lua() 664local function find_lua()
@@ -765,15 +724,15 @@ local function create_testing_paths(suffix)
765 paths.testrun_dir = testrun_dir 724 paths.testrun_dir = testrun_dir
766 paths.testing_lrprefix = dir_path(testrun_dir, "testing_lrprefix-" .. suffix) 725 paths.testing_lrprefix = dir_path(testrun_dir, "testing_lrprefix-" .. suffix)
767 paths.testing_tree = dir_path(testrun_dir, "testing-" .. suffix) 726 paths.testing_tree = dir_path(testrun_dir, "testing-" .. suffix)
768 paths.testing_tree_copy = dir_path(testrun_dir, "testing_copy-" .. suffix)
769 paths.testing_sys_tree = dir_path(testrun_dir, "testing_sys-" .. suffix) 727 paths.testing_sys_tree = dir_path(testrun_dir, "testing_sys-" .. suffix)
770 paths.testing_sys_tree_copy = dir_path(testrun_dir, "testing_sys_copy-" .. suffix) 728 paths.testing_deps_tree = dir_path(testrun_dir, "testing_deps-" .. suffix)
771 paths.testing_cache = dir_path(testrun_dir, "testing_cache-" .. suffix) 729 paths.testing_cache = dir_path(testrun_dir, "testing_cache-" .. suffix)
772 paths.testing_server = dir_path(testrun_dir, "testing_server-" .. suffix) 730 paths.testing_server = dir_path(testrun_dir, "testing_server-" .. suffix)
773 731
774 local rocks_v = "rocks-" .. test_env.lua_version 732 local rocks_v = "rocks-" .. test_env.lua_version
775 paths.testing_rocks = dir_path(paths.testing_tree, "lib", "luarocks", rocks_v) 733 paths.testing_rocks = dir_path(paths.testing_tree, "lib", "luarocks", rocks_v)
776 paths.testing_sys_rocks = dir_path(paths.testing_sys_tree, "lib", "luarocks", rocks_v) 734 paths.testing_sys_rocks = dir_path(paths.testing_sys_tree, "lib", "luarocks", rocks_v)
735 paths.testing_deps_rocks = dir_path(paths.testing_deps_tree, "lib", "luarocks", rocks_v)
777 736
778 if test_env.TEST_TARGET_OS == "windows" then 737 if test_env.TEST_TARGET_OS == "windows" then
779 paths.luarocks_tmp = os.getenv("TEMP") 738 paths.luarocks_tmp = os.getenv("TEMP")
@@ -853,7 +812,8 @@ local function create_configs()
853 -- testing_config_no_downloader.lua 812 -- testing_config_no_downloader.lua
854 local config_content = substitute([[ 813 local config_content = substitute([[
855 rocks_trees = { 814 rocks_trees = {
856 "%{testing_tree}", 815 { name = "user", root = "%{testing_tree}" },
816 { name = "deps", root = "%{testing_deps_tree}" },
857 { name = "system", root = "%{testing_sys_tree}" }, 817 { name = "system", root = "%{testing_sys_tree}" },
858 } 818 }
859 rocks_servers = { 819 rocks_servers = {
@@ -870,6 +830,7 @@ local function create_configs()
870 ]], { 830 ]], {
871 user = "testuser", 831 user = "testuser",
872 testing_sys_tree = test_env.testing_paths.testing_sys_tree, 832 testing_sys_tree = test_env.testing_paths.testing_sys_tree,
833 testing_deps_tree = test_env.testing_paths.testing_deps_tree,
873 testing_tree = test_env.testing_paths.testing_tree, 834 testing_tree = test_env.testing_paths.testing_tree,
874 testing_server = test_env.testing_paths.testing_server, 835 testing_server = test_env.testing_paths.testing_server,
875 testing_cache = test_env.testing_paths.testing_cache 836 testing_cache = test_env.testing_paths.testing_cache
@@ -885,6 +846,7 @@ local function create_configs()
885 config_content = substitute([[ 846 config_content = substitute([[
886 rocks_trees = { 847 rocks_trees = {
887 "%{testing_tree}", 848 "%{testing_tree}",
849 "%{testing_deps_tree}",
888 "%{testing_sys_tree}", 850 "%{testing_sys_tree}",
889 } 851 }
890 local_cache = "%{testing_cache}" 852 local_cache = "%{testing_cache}"
@@ -898,6 +860,7 @@ local function create_configs()
898 ]], { 860 ]], {
899 user = "testuser", 861 user = "testuser",
900 testing_sys_tree = test_env.testing_paths.testing_sys_tree, 862 testing_sys_tree = test_env.testing_paths.testing_sys_tree,
863 testing_deps_tree = test_env.testing_paths.testing_deps_tree,
901 testing_tree = test_env.testing_paths.testing_tree, 864 testing_tree = test_env.testing_paths.testing_tree,
902 testing_cache = test_env.testing_paths.testing_cache 865 testing_cache = test_env.testing_paths.testing_cache
903 }) 866 })
@@ -1140,7 +1103,7 @@ function test_env.main()
1140 build_environment(rocks, env_vars) 1103 build_environment(rocks, env_vars)
1141end 1104end
1142 1105
1143--- Function for initial setup of environment, variables, md5sums for spec files 1106--- Function for initial setup of environment and variables
1144function test_env.setup_specs(extra_rocks, use_mock) 1107function test_env.setup_specs(extra_rocks, use_mock)
1145 test_env.unload_luarocks() 1108 test_env.unload_luarocks()
1146 1109
@@ -1169,7 +1132,6 @@ function test_env.setup_specs(extra_rocks, use_mock)
1169 1132
1170 test_env.platform = get_luarocks_platform(test_env.env_variables) 1133 test_env.platform = get_luarocks_platform(test_env.env_variables)
1171 test_env.wrapper_extension = test_env.TEST_TARGET_OS == "windows" and ".bat" or "" 1134 test_env.wrapper_extension = test_env.TEST_TARGET_OS == "windows" and ".bat" or ""
1172 test_env.md5sums = create_md5sums(test_env.testing_paths)
1173 test_env.setup_done = true 1135 test_env.setup_done = true
1174 title("RUNNING TESTS") 1136 title("RUNNING TESTS")
1175 end 1137 end
@@ -1186,7 +1148,8 @@ function test_env.setup_specs(extra_rocks, use_mock)
1186 end 1148 end
1187 1149
1188 if test_env.RESET_ENV then 1150 if test_env.RESET_ENV then
1189 reset_environment(test_env.testing_paths, test_env.md5sums, variables) 1151 test_env.remove_dir(test_env.testing_paths.testing_tree)
1152 test_env.remove_dir(test_env.testing_paths.testing_sys_tree)
1190 end 1153 end
1191 1154
1192 lfs.chdir(testrun_dir) 1155 lfs.chdir(testrun_dir)