From 03e401231972d7b4db0145a71ce74d5d18d1a901 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Mon, 26 Feb 2024 18:30:31 -0300 Subject: tests: reorganize and speed up unit tests --- spec/README.md | 2 +- spec/add_spec.lua | 2 - spec/build_spec.lua | 16 ++-- spec/cmd_spec.lua | 4 +- spec/config_spec.lua | 6 +- spec/deps_spec.lua | 145 ------------------------------------ spec/doc_spec.lua | 4 +- spec/download_spec.lua | 2 - spec/dummy_spec.lua | 3 +- spec/external_spec.lua | 7 +- spec/fs_spec.lua | 76 +++++++++++++++++++ spec/help_spec.lua | 2 - spec/init_spec.lua | 4 +- spec/install_spec.lua | 14 ++-- spec/lint_spec.lua | 2 - spec/list_spec.lua | 2 - spec/loader_spec.lua | 26 ++----- spec/make_manifest_spec.lua | 2 - spec/make_spec.lua | 24 +++--- spec/new_version_spec.lua | 9 +-- spec/pack_spec.lua | 7 +- spec/path_spec.lua | 2 - spec/purge_spec.lua | 2 - spec/quick/path.q | 22 ++++++ spec/refresh_cache_spec.lua | 2 - spec/remove_spec.lua | 2 - spec/search_spec.lua | 2 - spec/show_spec.lua | 2 - spec/test_spec.lua | 173 ------------------------------------------ spec/unit/build_spec.lua | 3 - spec/unit/deps_spec.lua | 144 +++++++++++++++++++++++++++++++++++ spec/unit/dir_spec.lua | 1 - spec/unit/fetch_spec.lua | 1 - spec/unit/fs_spec.lua | 66 +--------------- spec/unit/fun_spec.lua | 1 - spec/unit/loader_spec.lua | 21 ++++++ spec/unit/persist_spec.lua | 1 - spec/unit/test_spec.lua | 174 +++++++++++++++++++++++++++++++++++++++++++ spec/unit/tools_spec.lua | 1 - spec/unit/util_spec.lua | 161 +++++++++++++++++++++++++++++++++++++++ spec/unpack_spec.lua | 2 - spec/upload_spec.lua | 45 +++++------ spec/util/test_env.lua | 117 ++++++++++++++++------------- spec/util_spec.lua | 161 --------------------------------------- spec/which_spec.lua | 5 +- spec/write_rockspec_spec.lua | 9 ++- 46 files changed, 743 insertions(+), 736 deletions(-) create mode 100644 spec/fs_spec.lua create mode 100644 spec/quick/path.q create mode 100644 spec/unit/deps_spec.lua create mode 100644 spec/unit/loader_spec.lua create mode 100644 spec/unit/test_spec.lua create mode 100644 spec/unit/util_spec.lua diff --git a/spec/README.md b/spec/README.md index b55550e6..89e13ea1 100644 --- a/spec/README.md +++ b/spec/README.md @@ -5,7 +5,7 @@ Test suite for LuaRocks project with Busted unit testing framework(http://olivinelabs.com/busted/). -* Contains unit & integration tests +* Contains unit and integration tests * Easy setup for your purpose on command line or from configuration file ## Dependencies diff --git a/spec/add_spec.lua b/spec/add_spec.lua index b17cadac..0a345753 100644 --- a/spec/add_spec.lua +++ b/spec/add_spec.lua @@ -2,8 +2,6 @@ local test_env = require("spec.util.test_env") local run = test_env.run local testing_paths = test_env.testing_paths -test_env.unload_luarocks() - local extra_rocks = { "/luasocket-${LUASOCKET}.src.rock", "/luasocket-${LUASOCKET}.rockspec" diff --git a/spec/build_spec.lua b/spec/build_spec.lua index a2c09119..d07447c1 100644 --- a/spec/build_spec.lua +++ b/spec/build_spec.lua @@ -6,9 +6,7 @@ local testing_paths = test_env.testing_paths local write_file = test_env.write_file local git_repo = require("spec.util.git_repo") -test_env.unload_luarocks() -local cfg = require("luarocks.core.cfg") -local fs = require("luarocks.fs") +local cfg, fs local extra_rocks = { "/lmathx-20120430.51-1.src.rock", @@ -40,13 +38,12 @@ local c_module_source = [[ ]] describe("LuaRocks build #integration", function() - lazy_setup(function() - cfg.init() - fs.init() - end) - before_each(function() test_env.setup_specs(extra_rocks) + cfg = require("luarocks.core.cfg") + fs = require("luarocks.fs") + cfg.init() + fs.init() end) describe("building with flags", function() @@ -154,7 +151,7 @@ describe("LuaRocks build #integration", function() assert.is_true(run.luarocks_bool("build test-1.0-1.rockspec --deps-mode=none")) assert.is.truthy(lfs.attributes(testing_paths.testing_sys_rocks .. "/test/1.0-1/test-1.0-1.rockspec")) - end) + end, finally) end) it("supports --pin #pinning", function() @@ -459,6 +456,7 @@ describe("LuaRocks build #integration", function() describe("#mock external dependencies", function() lazy_setup(function() + test_env.setup_specs(nil, "mock") test_env.mock_server_init() end) diff --git a/spec/cmd_spec.lua b/spec/cmd_spec.lua index 5d6e729b..9067a160 100644 --- a/spec/cmd_spec.lua +++ b/spec/cmd_spec.lua @@ -2,11 +2,9 @@ local test_env = require("spec.util.test_env") local lfs = require("lfs") local run = test_env.run -test_env.unload_luarocks() - describe("LuaRocks command line #integration", function() - setup(function() + lazy_setup(function() test_env.setup_specs() end) diff --git a/spec/config_spec.lua b/spec/config_spec.lua index 9615f050..6bfac433 100644 --- a/spec/config_spec.lua +++ b/spec/config_spec.lua @@ -7,13 +7,11 @@ local write_file = test_env.write_file local get_tmp_path = test_env.get_tmp_path local hardcoded -test_env.unload_luarocks() - describe("LuaRocks config tests #integration", function() - setup(function() + lazy_setup(function() test_env.setup_specs() - test_env.unload_luarocks() -- need to be required here, because hardcoded is created after first loading of specs + -- needs to be required here, because hardcoded is created after first loading of specs hardcoded = require("luarocks.core.hardcoded") end) diff --git a/spec/deps_spec.lua b/spec/deps_spec.lua index 32f68f51..dd8fea64 100644 --- a/spec/deps_spec.lua +++ b/spec/deps_spec.lua @@ -3,8 +3,6 @@ local lfs = require("lfs") local run = test_env.run local testing_paths = test_env.testing_paths -test_env.unload_luarocks() - local extra_rocks = { "/lxsh-${LXSH}.src.rock", "/lxsh-${LXSH}.rockspec", @@ -116,146 +114,3 @@ describe("LuaRocks deps-mode #integration", function() assert.is.truthy(lfs.attributes(testing_paths.testing_sys_rocks .. "/lxsh/${LXSH}/lxsh-${LXSH}.rockspec")) end) end) - -test_env.unload_luarocks() -test_env.setup_specs() -local cfg = require("luarocks.core.cfg") -local deps = require("luarocks.deps") -local fs = require("luarocks.fs") - -describe("LuaRocks deps #unit", function() - local runner - - lazy_setup(function() - runner = require("luacov.runner") - runner.init(testing_paths.testrun_dir .. "/luacov.config") - runner.tick = true - cfg.init() - fs.init() - deps.check_lua_incdir(cfg.variables) - deps.check_lua_libdir(cfg.variables) - end) - - lazy_teardown(function() - runner.shutdown() - end) - - describe("deps", function() - describe("deps.autodetect_external_dependencies", function() - it("returns false if the given build table has no external dependencies", function() - local build_table = { - type = "builtin" - } - - assert.falsy(deps.autodetect_external_dependencies(build_table)) - end) - - it("returns a table of the external dependencies found in the given build table", function() - local build_table = { - type = "builtin", - modules = { - module1 = { - libraries = { "foo1", "foo2" }, - }, - module2 = { - libraries = "foo3" - }, - } - } - - local extdeps = deps.autodetect_external_dependencies(build_table) - assert.same(extdeps["FOO1"], { library = "foo1" }) - assert.same(extdeps["FOO2"], { library = "foo2" }) - assert.same(extdeps["FOO3"], { library = "foo3" }) - end) - - it("adds proper include and library dirs to the given build table", function() - local build_table - - build_table = { - type = "builtin", - modules = { - module1 = { - libraries = "foo" - } - } - } - deps.autodetect_external_dependencies(build_table) - assert.same(build_table, { - type = "builtin", - modules = { - module1 = { - libraries = "foo", - incdirs = { "$(FOO_INCDIR)" }, - libdirs = { "$(FOO_LIBDIR)" } - } - } - }) - - build_table = { - type = "builtin", - modules = { - module1 = { - libraries = "foo", - incdirs = { "INCDIRS" } - } - } - } - deps.autodetect_external_dependencies(build_table) - assert.same(build_table, { - type = "builtin", - modules = { - module1 = { - libraries = "foo", - incdirs = { "INCDIRS" }, - libdirs = { "$(FOO_LIBDIR)" } - } - } - }) - - build_table = { - type = "builtin", - modules = { - module1 = { - libraries = "foo", - libdirs = { "LIBDIRS" } - } - } - } - deps.autodetect_external_dependencies(build_table) - assert.same(build_table, { - type = "builtin", - modules = { - module1 = { - libraries = "foo", - incdirs = { "$(FOO_INCDIR)" }, - libdirs = { "LIBDIRS" } - } - } - }) - - build_table = { - type = "builtin", - modules = { - module1 = { - libraries = "foo", - incdirs = { "INCDIRS" }, - libdirs = { "LIBDIRS" } - } - } - } - deps.autodetect_external_dependencies(build_table) - assert.same(build_table, { - type = "builtin", - modules = { - module1 = { - libraries = "foo", - incdirs = { "INCDIRS" }, - libdirs = { "LIBDIRS" } - } - } - }) - end) - end) - end) -end) diff --git a/spec/doc_spec.lua b/spec/doc_spec.lua index a10bb501..062b36e7 100644 --- a/spec/doc_spec.lua +++ b/spec/doc_spec.lua @@ -2,8 +2,6 @@ local test_env = require("spec.util.test_env") local run = test_env.run local testing_paths = test_env.testing_paths -test_env.unload_luarocks() - describe("luarocks doc #integration", function() before_each(function() test_env.setup_specs() @@ -129,7 +127,7 @@ describe("luarocks doc #integration", function() end) it("with --local", function() - assert.is_true(run.luarocks_bool("install --server=" .. testing_paths.fixtures_dir .. "/a_repo a_rock")) + assert.is_true(run.luarocks_bool("install --local --server=" .. testing_paths.fixtures_dir .. "/a_repo a_rock")) assert.is_true(run.luarocks_bool("doc --server=" .. testing_paths.fixtures_dir .. "/a_repo a_rock --local")) end) diff --git a/spec/download_spec.lua b/spec/download_spec.lua index 66b12d6f..f7c10e85 100644 --- a/spec/download_spec.lua +++ b/spec/download_spec.lua @@ -3,8 +3,6 @@ local lfs = require("lfs") local run = test_env.run local testing_paths = test_env.testing_paths -test_env.unload_luarocks() - local extra_rocks = { "/say-1.3-1.rockspec", } diff --git a/spec/dummy_spec.lua b/spec/dummy_spec.lua index 80f04ffb..85fc8932 100644 --- a/spec/dummy_spec.lua +++ b/spec/dummy_spec.lua @@ -3,8 +3,7 @@ local test_env = require("spec.util.test_env") local testing_paths = test_env.testing_paths -test_env.unload_luarocks() - +test_env.setup_specs() local runner = require("luacov.runner") runner.init(testing_paths.testrun_dir .. "/luacov.config") diff --git a/spec/external_spec.lua b/spec/external_spec.lua index 63363b76..5a69a678 100644 --- a/spec/external_spec.lua +++ b/spec/external_spec.lua @@ -2,14 +2,13 @@ local test_env = require("spec.util.test_env") local run = test_env.run local testing_paths = test_env.testing_paths -test_env.setup_specs() - describe("luarocks external commands #integration", function() - setup(function() + lazy_setup(function() + test_env.setup_specs() test_env.mock_server_init() end) - teardown(function() + lazy_teardown(function() test_env.mock_server_done() end) diff --git a/spec/fs_spec.lua b/spec/fs_spec.lua new file mode 100644 index 00000000..d5e93c4b --- /dev/null +++ b/spec/fs_spec.lua @@ -0,0 +1,76 @@ +local test_env = require("spec.util.test_env") + +local lfs = require("lfs") +local testing_paths = test_env.testing_paths +local get_tmp_path = test_env.get_tmp_path + +describe("luarocks.fs #integration", function() + + local fs + + describe("fs.download #mock", function() + local tmpfile + local tmpdir + + lazy_setup(function() + test_env.setup_specs(nil, "mock") + local cfg = require("luarocks.core.cfg") + fs = require("luarocks.fs") + cfg.init() + fs.init() + test_env.mock_server_init() + end) + + lazy_teardown(function() + test_env.mock_server_done() + end) + + after_each(function() + if tmpfile then + os.remove(tmpfile) + tmpfile = nil + end + if tmpdir then + lfs.rmdir(tmpdir) + tmpdir = nil + end + end) + + it("returns true and fetches the url argument into the specified filename", function() + tmpfile = get_tmp_path() + assert.truthy(fs.download("http://localhost:8080/file/a_rock.lua", tmpfile)) + local fd = assert(io.open(tmpfile, "r")) + local downloadcontent = assert(fd:read("*a")) + fd:close() + fd = assert(io.open(testing_paths.fixtures_dir .. "/a_rock.lua", "r")) + local originalcontent = assert(fd:read("*a")) + fd:close() + assert.same(downloadcontent, originalcontent) + end) + + it("returns true and fetches the url argument into a file whose name matches the basename of the url if the filename argument is not given", function() + tmpdir = get_tmp_path() + lfs.mkdir(tmpdir) + fs.change_dir(tmpdir) + assert.truthy(fs.download("http://localhost:8080/file/a_rock.lua")) + tmpfile = tmpdir .. "/a_rock.lua" + local fd = assert(io.open(tmpfile, "r")) + local downloadcontent = assert(fd:read("*a")) + fd:close() + fd = assert(io.open(testing_paths.fixtures_dir .. "/a_rock.lua", "r")) + local originalcontent = assert(fd:read("*a")) + fd:close() + assert.same(downloadcontent, originalcontent) + fs.pop_dir() + end) + + it("returns false and does nothing if the url argument contains a nonexistent file", function() + tmpfile = get_tmp_path() + assert.falsy(fs.download("http://localhost:8080/file/nonexistent", tmpfile)) + end) + + it("returns false and does nothing if the url argument is invalid", function() + assert.falsy(fs.download("invalidurl")) + end) + end) +end) diff --git a/spec/help_spec.lua b/spec/help_spec.lua index 08ce87a9..e13f1c30 100644 --- a/spec/help_spec.lua +++ b/spec/help_spec.lua @@ -1,8 +1,6 @@ local test_env = require("spec.util.test_env") local run = test_env.run -test_env.unload_luarocks() - describe("luarocks help #integration", function() before_each(function() diff --git a/spec/init_spec.lua b/spec/init_spec.lua index be10b5b1..88bd23a5 100644 --- a/spec/init_spec.lua +++ b/spec/init_spec.lua @@ -6,11 +6,9 @@ local is_win = test_env.TEST_TARGET_OS == "windows" local write_file = test_env.write_file local lfs = require("lfs") -test_env.unload_luarocks() - describe("luarocks init #integration", function() - setup(function() + lazy_setup(function() test_env.setup_specs() end) diff --git a/spec/install_spec.lua b/spec/install_spec.lua index 45979b9c..b4b064f0 100644 --- a/spec/install_spec.lua +++ b/spec/install_spec.lua @@ -7,8 +7,6 @@ local write_file = test_env.write_file local git_repo = require("spec.util.git_repo") local V = test_env.V -test_env.unload_luarocks() - local extra_rocks = { "/cprint-${CPRINT}.src.rock", "/cprint-${CPRINT}.rockspec", @@ -244,15 +242,15 @@ describe("luarocks install #integration", function() test = "test.lua" } } - ]], finally) - write_file("test.lua", "return {}", finally) + ]]) + write_file("test.lua", "return {}") write_file("luarocks.lock", [[ return { dependencies = { ["a_rock"] = "1.0-1", } } - ]], finally) + ]]) assert.is_true(run.luarocks_bool("make --pack-binary-rock --server=" .. testing_paths.fixtures_dir .. "/a_repo test-1.0-1.rockspec")) assert.is_true(os.remove("luarocks.lock")) @@ -268,17 +266,17 @@ describe("luarocks install #integration", function() assert.is.truthy(lfs.attributes("./lua_modules/lib/luarocks/rocks-" .. test_env.lua_version .. "/test/1.0-1/luarocks.lock")) assert.is.truthy(lfs.attributes("./lua_modules/lib/luarocks/rocks-" .. test_env.lua_version .. "/a_rock/1.0-1/a_rock-1.0-1.rockspec")) assert.is.falsy(lfs.attributes("./lua_modules/lib/luarocks/rocks-" .. test_env.lua_version .. "/a_rock/2.0-1")) - end) + end, finally) end) describe("#unix install runs build from #git", function() local git - setup(function() + lazy_setup(function() git = git_repo.start() end) - teardown(function() + lazy_teardown(function() if git then git:stop() end diff --git a/spec/lint_spec.lua b/spec/lint_spec.lua index 847c9800..919e4e71 100644 --- a/spec/lint_spec.lua +++ b/spec/lint_spec.lua @@ -2,8 +2,6 @@ local test_env = require("spec.util.test_env") local run = test_env.run local get_tmp_path = test_env.get_tmp_path local write_file = test_env.write_file - -test_env.unload_luarocks() local lfs = require("lfs") local extra_rocks = { diff --git a/spec/list_spec.lua b/spec/list_spec.lua index 4c7fc776..eeeb79d2 100644 --- a/spec/list_spec.lua +++ b/spec/list_spec.lua @@ -3,8 +3,6 @@ local V = test_env.V local run = test_env.run local testing_paths = test_env.testing_paths -test_env.unload_luarocks() - local extra_rocks = { "/say-1.0-1.src.rock", "/say-1.2-1.src.rock" diff --git a/spec/loader_spec.lua b/spec/loader_spec.lua index c8526117..bd95a292 100644 --- a/spec/loader_spec.lua +++ b/spec/loader_spec.lua @@ -9,22 +9,10 @@ describe("luarocks.loader", function() test_env.setup_specs() end) - describe("#unit", function() - it("starts", function() - assert(run.lua_bool([[-e "require 'luarocks.loader'; print(package.loaded['luarocks.loaded'])"]])) - end) - - describe("which", function() - it("finds modules using package.path", function() - assert(run.lua_bool([[-e "loader = require 'luarocks.loader'; local x,y,z,p = loader.which('luarocks.loader', 'p'); assert(p == 'p')"]])) - end) - end) - end) - describe("#integration", function() it("respects version constraints", function() test_env.run_in_tmp(function(tmpdir) - write_file("rock_b_01.lua", "print('ROCK B 0.1'); return {}", finally) + write_file("rock_b_01.lua", "print('ROCK B 0.1'); return {}") write_file("rock_b-0.1-1.rockspec", [[ package = "rock_b" version = "0.1-1" @@ -37,9 +25,9 @@ describe("luarocks.loader", function() rock_b = "rock_b_01.lua" } } - ]], finally) + ]]) - write_file("rock_b_10.lua", "print('ROCK B 1.0'); return {}", finally) + write_file("rock_b_10.lua", "print('ROCK B 1.0'); return {}") write_file("rock_b-1.0-1.rockspec", [[ package = "rock_b" version = "1.0-1" @@ -52,9 +40,9 @@ describe("luarocks.loader", function() rock_b = "rock_b_10.lua" } } - ]], finally) + ]]) - write_file("rock_a.lua", "require('rock_b'); return {}", finally) + write_file("rock_a.lua", "require('rock_b'); return {}") write_file("rock_a-2.0-1.rockspec", [[ package = "rock_a" version = "2.0-1" @@ -70,7 +58,7 @@ describe("luarocks.loader", function() rock_a = "rock_a.lua" } } - ]], finally) + ]]) print(run.luarocks("make --server=" .. testing_paths.fixtures_dir .. "/a_repo --tree=" .. testing_paths.testing_tree .. " ./rock_b-0.1-1.rockspec")) print(run.luarocks("make --server=" .. testing_paths.fixtures_dir .. "/a_repo --tree=" .. testing_paths.testing_tree .. " ./rock_b-1.0-1.rockspec --keep")) @@ -79,7 +67,7 @@ describe("luarocks.loader", function() local output = run.lua([[-e "require 'luarocks.loader'; require('rock_a')"]]) assert.matches("ROCK B 0.1", output, 1, true) - end) + end, finally) end) end) end) diff --git a/spec/make_manifest_spec.lua b/spec/make_manifest_spec.lua index a3d015c9..146b6178 100644 --- a/spec/make_manifest_spec.lua +++ b/spec/make_manifest_spec.lua @@ -1,8 +1,6 @@ local test_env = require("spec.util.test_env") local run = test_env.run -test_env.unload_luarocks() - describe("luarocks make_manifest #integration", function() before_each(function() diff --git a/spec/make_spec.lua b/spec/make_spec.lua index 626d84df..6cce0543 100644 --- a/spec/make_spec.lua +++ b/spec/make_spec.lua @@ -5,8 +5,6 @@ local testing_paths = test_env.testing_paths local env_variables = test_env.env_variables local write_file = test_env.write_file -test_env.unload_luarocks() - local extra_rocks = { "/luasocket-${LUASOCKET}.src.rock", "/luasocket-${LUASOCKET}.rockspec", @@ -156,8 +154,8 @@ describe("luarocks make #integration", function() test = "test.lua" } } - ]], finally) - write_file("test.lua", "return {}", finally) + ]]) + write_file("test.lua", "return {}") assert.is_true(run.luarocks_bool("make --server=" .. testing_paths.fixtures_dir .. "/a_repo --pin --tree=lua_modules")) assert.is.truthy(lfs.attributes("./lua_modules/lib/luarocks/rocks-" .. test_env.lua_version .. "/test/1.0-1/test-1.0-1.rockspec")) @@ -171,7 +169,7 @@ describe("luarocks make #integration", function() ["lua"] = test_env.lua_version .. "-1", } }, lockdata) - end) + end, finally) end) it("respects luarocks.lock when present #pinning", function() @@ -191,15 +189,15 @@ describe("luarocks make #integration", function() test = "test.lua" } } - ]], finally) - write_file("test.lua", "return {}", finally) + ]]) + write_file("test.lua", "return {}") write_file("luarocks.lock", [[ return { dependencies = { ["a_rock"] = "1.0-1", } } - ]], finally) + ]]) print(run.luarocks("make --server=" .. testing_paths.fixtures_dir .. "/a_repo --tree=lua_modules")) assert.is.truthy(lfs.attributes("./lua_modules/lib/luarocks/rocks-" .. test_env.lua_version .. "/test/2.0-1/test-2.0-1.rockspec")) @@ -212,7 +210,7 @@ describe("luarocks make #integration", function() ["a_rock"] = "1.0-1", } }, lockdata) - end) + end, finally) end) it("overrides luarocks.lock with --pin #pinning", function() @@ -232,15 +230,15 @@ describe("luarocks make #integration", function() test = "test.lua" } } - ]], finally) - write_file("test.lua", "return {}", finally) + ]]) + write_file("test.lua", "return {}") write_file("luarocks.lock", [[ return { dependencies = { ["a_rock"] = "1.0-1", } } - ]], finally) + ]]) print(run.luarocks("make --server=" .. testing_paths.fixtures_dir .. "/a_repo --tree=lua_modules --pin")) assert.is.truthy(lfs.attributes("./lua_modules/lib/luarocks/rocks-" .. test_env.lua_version .. "/test/2.0-1/test-2.0-1.rockspec")) @@ -254,7 +252,7 @@ describe("luarocks make #integration", function() ["lua"] = test_env.lua_version .. "-1", } }, lockdata) - end) + end, finally) end) describe("#ddt upgrading rockspecs with double deploy types", function() diff --git a/spec/new_version_spec.lua b/spec/new_version_spec.lua index d47cfd05..18271176 100644 --- a/spec/new_version_spec.lua +++ b/spec/new_version_spec.lua @@ -3,8 +3,6 @@ local lfs = require("lfs") local run = test_env.run local testing_paths = test_env.testing_paths -test_env.unload_luarocks() - local extra_rocks = { "/abelhas-1.1-1.rockspec", "/lpeg-${LPEG}.rockspec" @@ -12,7 +10,7 @@ local extra_rocks = { describe("luarocks new_version #integration", function() - setup(function() + lazy_setup(function() test_env.setup_specs(extra_rocks) end) @@ -69,10 +67,11 @@ describe("luarocks new_version #integration", function() end) describe("remote tests #mock", function() - setup(function() + lazy_setup(function() + test_env.setup_specs(extra_rocks, "mock") test_env.mock_server_init() end) - teardown(function() + lazy_teardown(function() test_env.mock_server_done() end) it("with remote spec", function() diff --git a/spec/pack_spec.lua b/spec/pack_spec.lua index 0a4e0d8a..20a2b3ef 100644 --- a/spec/pack_spec.lua +++ b/spec/pack_spec.lua @@ -4,8 +4,6 @@ local run = test_env.run local testing_paths = test_env.testing_paths local write_file = test_env.write_file -test_env.unload_luarocks() - local extra_rocks = { "/luassert-1.7.0-1.src.rock", "/luasocket-${LUASOCKET}.src.rock", @@ -64,11 +62,12 @@ describe("luarocks pack #integration", function() describe("#mock", function() - setup(function() + lazy_setup(function() + test_env.setup_specs(extra_rocks, "mock") test_env.mock_server_init() end) - teardown(function() + lazy_teardown(function() test_env.mock_server_done() end) diff --git a/spec/path_spec.lua b/spec/path_spec.lua index ba2ecdde..1ad6b3ac 100644 --- a/spec/path_spec.lua +++ b/spec/path_spec.lua @@ -1,8 +1,6 @@ local test_env = require("spec.util.test_env") local run = test_env.run -test_env.unload_luarocks() - describe("luarocks path #integration", function() before_each(function() test_env.setup_specs() diff --git a/spec/purge_spec.lua b/spec/purge_spec.lua index ccd26bdf..7fdb74f4 100644 --- a/spec/purge_spec.lua +++ b/spec/purge_spec.lua @@ -2,8 +2,6 @@ local test_env = require("spec.util.test_env") local run = test_env.run local testing_paths = test_env.testing_paths -test_env.unload_luarocks() - local extra_rocks = { "/say-1.0-1.src.rock", } diff --git a/spec/quick/path.q b/spec/quick/path.q new file mode 100644 index 00000000..77fc188b --- /dev/null +++ b/spec/quick/path.q @@ -0,0 +1,22 @@ +================================================================================ +TEST: luarocks path: --project-tree + + +RUN: luarocks path --project-tree=foo +STDOUT: +-------------------------------------------------------------------------------- +%{path(foo/share/lua/%{lua_version}/?.lua)} +%{path(foo/share/lua/%{lua_version}/?/init.lua)} +-------------------------------------------------------------------------------- + +RUN: luarocks path --project-tree=foo --tree=bar +NOT_STDOUT: +-------------------------------------------------------------------------------- +%{path(foo/share/lua/%{lua_version}/?.lua)} +%{path(foo/share/lua/%{lua_version}/?/init.lua)} +-------------------------------------------------------------------------------- +STDOUT: +-------------------------------------------------------------------------------- +%{path(bar/share/lua/%{lua_version}/?.lua)} +%{path(bar/share/lua/%{lua_version}/?/init.lua)} +-------------------------------------------------------------------------------- diff --git a/spec/refresh_cache_spec.lua b/spec/refresh_cache_spec.lua index c0877371..73ba9a91 100644 --- a/spec/refresh_cache_spec.lua +++ b/spec/refresh_cache_spec.lua @@ -1,8 +1,6 @@ local test_env = require("spec.util.test_env") local run = test_env.run -test_env.unload_luarocks() - describe("luarocks-admin refresh_cache #integration", function() before_each(function() diff --git a/spec/remove_spec.lua b/spec/remove_spec.lua index ddeb0644..3bcfbb25 100644 --- a/spec/remove_spec.lua +++ b/spec/remove_spec.lua @@ -6,8 +6,6 @@ local env_variables = test_env.env_variables local V = test_env.V local P = test_env.P -test_env.unload_luarocks() - local extra_rocks = { "/abelhas-1.1-1.src.rock", "/copas-${COPAS}.src.rock", diff --git a/spec/search_spec.lua b/spec/search_spec.lua index bb6adf3e..6d71e008 100644 --- a/spec/search_spec.lua +++ b/spec/search_spec.lua @@ -1,8 +1,6 @@ local test_env = require("spec.util.test_env") local run = test_env.run -test_env.unload_luarocks() - local extra_rocks = { "/lzlib-0.4.1.53-1.src.rock" } diff --git a/spec/show_spec.lua b/spec/show_spec.lua index d07a8495..cd34b5a5 100644 --- a/spec/show_spec.lua +++ b/spec/show_spec.lua @@ -2,8 +2,6 @@ local test_env = require("spec.util.test_env") local run = test_env.run local testing_paths = test_env.testing_paths -test_env.unload_luarocks() - describe("luarocks show #integration", function() before_each(function() diff --git a/spec/test_spec.lua b/spec/test_spec.lua index 4be0567f..194dcfd1 100644 --- a/spec/test_spec.lua +++ b/spec/test_spec.lua @@ -1,12 +1,9 @@ local test_env = require("spec.util.test_env") local lfs = require("lfs") -local get_tmp_path = test_env.get_tmp_path local run = test_env.run local testing_paths = test_env.testing_paths local write_file = test_env.write_file -test_env.unload_luarocks() - local extra_rocks = { "/busted-2.0.0-1.rockspec", "/lua_cliargs-3.0-1.src.rock", @@ -109,173 +106,3 @@ describe("luarocks test #integration", function() end) end) -test_env.unload_luarocks() - -local fs = require("luarocks.fs") -local cfg = require("luarocks.core.cfg") -local path = require("luarocks.path") -local test = require("luarocks.test") -local test_busted = require("luarocks.test.busted") -local test_command = require("luarocks.test.command") - -describe("LuaRocks test #unit", function() - local runner - - lazy_setup(function() - cfg.init() - fs.init() - runner = require("luacov.runner") - runner.init(testing_paths.testrun_dir .. "/luacov.config") - runner.tick = true - end) - - lazy_teardown(function() - runner.shutdown() - end) - - local tmpdir - local olddir - - local create_tmp_dir = function() - tmpdir = get_tmp_path() - olddir = lfs.currentdir() - lfs.mkdir(tmpdir) - lfs.chdir(tmpdir) - fs.change_dir(tmpdir) - end - - local destroy_tmp_dir = function() - if olddir then - lfs.chdir(olddir) - if tmpdir then - lfs.rmdir(tmpdir) - end - end - end - - describe("test.command", function() - describe("command.detect_type", function() - before_each(function() - create_tmp_dir() - end) - - after_each(function() - destroy_tmp_dir() - end) - - it("returns true if test.lua exists", function() - write_file("test.lua", "", finally) - assert.truthy(test_command.detect_type()) - end) - - it("returns false if test.lua doesn't exist", function() - assert.falsy(test_command.detect_type()) - end) - end) - - describe("command.run_tests", function() - before_each(function() - create_tmp_dir() - end) - - after_each(function() - destroy_tmp_dir() - end) - - it("returns the result of the executed tests", function() - write_file("test.lua", "assert(1==1)", finally) - assert.truthy(test_command.run_tests(nil, {})) - - write_file("test.lua", "assert(1==2)", finally) - assert.falsy(test_command.run_tests(nil, {})) - end) - - it("returns the result of the executed tests with custom arguments and test command", function() - write_file("test.lua", "assert(1==1)", finally) - - local test = { - script = "test.lua", - flags = { - arg1 = "1", - arg2 = "2" - }, - command = fs.Q(testing_paths.lua) - } - assert.truthy(test_command.run_tests(test, {})) - end) - - it("returns false and does nothing if the test script doesn't exist", function() - assert.falsy(test_command.run_tests(nil, {})) - end) - end) - end) - - describe("test.busted", function() - describe("busted.detect_type", function() - before_each(function() - create_tmp_dir() - end) - - after_each(function() - destroy_tmp_dir() - end) - - it("returns true if .busted exists", function() - write_file(".busted", "", finally) - assert.truthy(test_busted.detect_type()) - end) - - it("returns false if .busted doesn't exist", function() - assert.falsy(test_busted.detect_type()) - end) - end) - - describe("busted.run_tests", function() - before_each(function() - path.use_tree(testing_paths.testing_sys_tree) - create_tmp_dir() - end) - - after_each(function() - destroy_tmp_dir() - end) - - pending("returns the result of the executed tests", function() - -- FIXME: busted issue - write_file("test_spec.lua", "assert(1==1)", finally) - assert.truthy(test_busted.run_tests(nil, {})) - - write_file("test_spec.lua", "assert(1==2)", finally) - assert.falsy(test_busted.run_tests()) - end) - end) - end) - - describe("test", function() - describe("test.run_test_suite", function() - before_each(function() - create_tmp_dir() - end) - - after_each(function() - destroy_tmp_dir() - end) - - it("returns false if the given rockspec cannot be loaded", function() - assert.falsy(test.run_test_suite("invalid", nil, {})) - end) - - it("returns false if no test type was detected", function() - assert.falsy(test.run_test_suite({ package = "test" }, nil, {})) - end) - - it("returns the result of executing the tests specified in the given rockspec", function() - write_file("test.lua", "assert(1==1)", finally) - assert.truthy(test.run_test_suite({ test_dependencies = {} }, nil, {})) - - write_file("test.lua", "assert(1==2)", finally) - assert.falsy(test.run_test_suite({ test_dependencies = {} }, nil, {})) - end) - end) - end) -end) diff --git a/spec/unit/build_spec.lua b/spec/unit/build_spec.lua index 6ab143c6..fb680858 100644 --- a/spec/unit/build_spec.lua +++ b/spec/unit/build_spec.lua @@ -6,9 +6,6 @@ local testing_paths = test_env.testing_paths local write_file = test_env.write_file local P = test_env.P -test_env.unload_luarocks() - -test_env.unload_luarocks() test_env.setup_specs() local cfg = require("luarocks.core.cfg") local deps = require("luarocks.deps") diff --git a/spec/unit/deps_spec.lua b/spec/unit/deps_spec.lua new file mode 100644 index 00000000..b8c89a2d --- /dev/null +++ b/spec/unit/deps_spec.lua @@ -0,0 +1,144 @@ +local test_env = require("spec.util.test_env") +local testing_paths = test_env.testing_paths + +local cfg = require("luarocks.core.cfg") +local deps = require("luarocks.deps") +local fs = require("luarocks.fs") + +describe("LuaRocks deps #unit", function() + local runner + + lazy_setup(function() + cfg.init() + fs.init() + deps.check_lua_incdir(cfg.variables) + deps.check_lua_libdir(cfg.variables) + + runner = require("luacov.runner") + runner.init(testing_paths.testrun_dir .. "/luacov.config") + runner.tick = true + end) + + lazy_teardown(function() + runner.shutdown() + end) + + describe("deps", function() + describe("deps.autodetect_external_dependencies", function() + it("returns false if the given build table has no external dependencies", function() + local build_table = { + type = "builtin" + } + + assert.falsy(deps.autodetect_external_dependencies(build_table)) + end) + + it("returns a table of the external dependencies found in the given build table", function() + local build_table = { + type = "builtin", + modules = { + module1 = { + libraries = { "foo1", "foo2" }, + }, + module2 = { + libraries = "foo3" + }, + } + } + + local extdeps = deps.autodetect_external_dependencies(build_table) + assert.same(extdeps["FOO1"], { library = "foo1" }) + assert.same(extdeps["FOO2"], { library = "foo2" }) + assert.same(extdeps["FOO3"], { library = "foo3" }) + end) + + it("adds proper include and library dirs to the given build table", function() + local build_table + + build_table = { + type = "builtin", + modules = { + module1 = { + libraries = "foo" + } + } + } + deps.autodetect_external_dependencies(build_table) + assert.same(build_table, { + type = "builtin", + modules = { + module1 = { + libraries = "foo", + incdirs = { "$(FOO_INCDIR)" }, + libdirs = { "$(FOO_LIBDIR)" } + } + } + }) + + build_table = { + type = "builtin", + modules = { + module1 = { + libraries = "foo", + incdirs = { "INCDIRS" } + } + } + } + deps.autodetect_external_dependencies(build_table) + assert.same(build_table, { + type = "builtin", + modules = { + module1 = { + libraries = "foo", + incdirs = { "INCDIRS" }, + libdirs = { "$(FOO_LIBDIR)" } + } + } + }) + + build_table = { + type = "builtin", + modules = { + module1 = { + libraries = "foo", + libdirs = { "LIBDIRS" } + } + } + } + deps.autodetect_external_dependencies(build_table) + assert.same(build_table, { + type = "builtin", + modules = { + module1 = { + libraries = "foo", + incdirs = { "$(FOO_INCDIR)" }, + libdirs = { "LIBDIRS" } + } + } + }) + + build_table = { + type = "builtin", + modules = { + module1 = { + libraries = "foo", + incdirs = { "INCDIRS" }, + libdirs = { "LIBDIRS" } + } + } + } + deps.autodetect_external_dependencies(build_table) + assert.same(build_table, { + type = "builtin", + modules = { + module1 = { + libraries = "foo", + incdirs = { "INCDIRS" }, + libdirs = { "LIBDIRS" } + } + } + }) + end) + end) + end) +end) diff --git a/spec/unit/dir_spec.lua b/spec/unit/dir_spec.lua index b5dadda8..f340beea 100644 --- a/spec/unit/dir_spec.lua +++ b/spec/unit/dir_spec.lua @@ -2,7 +2,6 @@ local test_env = require("spec.util.test_env") local testing_paths = test_env.testing_paths local P = test_env.P -test_env.unload_luarocks() test_env.setup_specs() local dir = require("luarocks.dir") diff --git a/spec/unit/fetch_spec.lua b/spec/unit/fetch_spec.lua index 38da379e..c58e2cea 100644 --- a/spec/unit/fetch_spec.lua +++ b/spec/unit/fetch_spec.lua @@ -1,6 +1,5 @@ local test_env = require("spec.util.test_env") -test_env.unload_luarocks() test_env.setup_specs() local cfg = require("luarocks.core.cfg") local fetch = require("luarocks.fetch") diff --git a/spec/unit/fs_spec.lua b/spec/unit/fs_spec.lua index aea86af3..7643e9b5 100644 --- a/spec/unit/fs_spec.lua +++ b/spec/unit/fs_spec.lua @@ -1,6 +1,5 @@ local test_env = require("spec.util.test_env") -test_env.unload_luarocks() test_env.setup_specs() local fs = require("luarocks.fs") local path = require("luarocks.path") @@ -542,7 +541,7 @@ describe("luarocks.fs #unit", function() if is_win then assert.same(tmpdir, fs.current_dir()) else - assert.same(lfs.attributes(tmpdir).ino, lfs.attributes(fs.current_dir()).ino) + assert.same(lfs.attributes(tmpdir).ino, lfs.attributes((fs.current_dir())).ino) end end) end) @@ -578,7 +577,7 @@ describe("luarocks.fs #unit", function() if is_win then assert.same(tmpdir, fs.current_dir()) else - assert.same(lfs.attributes(tmpdir).ino, lfs.attributes(lfs.currentdir()).ino) + assert.same(lfs.attributes(tmpdir).ino, lfs.attributes(fs.current_dir()).ino) end end) @@ -1283,67 +1282,6 @@ describe("luarocks.fs #unit", function() end) end) - describe("fs.download #mock", function() - local tmpfile - local tmpdir - - setup(function() - test_env.mock_server_init() - end) - - teardown(function() - test_env.mock_server_done() - end) - - after_each(function() - if tmpfile then - os.remove(tmpfile) - tmpfile = nil - end - if tmpdir then - lfs.rmdir(tmpdir) - tmpdir = nil - end - end) - - it("returns true and fetches the url argument into the specified filename", function() - tmpfile = get_tmp_path() - assert.truthy(fs.download("http://localhost:8080/file/a_rock.lua", tmpfile)) - local fd = assert(io.open(tmpfile, "r")) - local downloadcontent = assert(fd:read("*a")) - fd:close() - fd = assert(io.open(testing_paths.fixtures_dir .. "/a_rock.lua", "r")) - local originalcontent = assert(fd:read("*a")) - fd:close() - assert.same(downloadcontent, originalcontent) - end) - - it("returns true and fetches the url argument into a file whose name matches the basename of the url if the filename argument is not given", function() - tmpdir = get_tmp_path() - lfs.mkdir(tmpdir) - fs.change_dir(tmpdir) - assert.truthy(fs.download("http://localhost:8080/file/a_rock.lua")) - tmpfile = tmpdir .. "/a_rock.lua" - local fd = assert(io.open(tmpfile, "r")) - local downloadcontent = assert(fd:read("*a")) - fd:close() - fd = assert(io.open(testing_paths.fixtures_dir .. "/a_rock.lua", "r")) - local originalcontent = assert(fd:read("*a")) - fd:close() - assert.same(downloadcontent, originalcontent) - fs.pop_dir() - end) - - it("returns false and does nothing if the url argument contains a nonexistent file", function() - tmpfile = get_tmp_path() - assert.falsy(fs.download("http://localhost:8080/file/nonexistent", tmpfile)) - end) - - it("returns false and does nothing if the url argument is invalid", function() - assert.falsy(fs.download("invalidurl")) - end) - end) - describe("fs.zip", function() local tmpdir local olddir diff --git a/spec/unit/fun_spec.lua b/spec/unit/fun_spec.lua index 9844ec27..c2b3e864 100644 --- a/spec/unit/fun_spec.lua +++ b/spec/unit/fun_spec.lua @@ -1,7 +1,6 @@ local test_env = require("spec.util.test_env") local testing_paths = test_env.testing_paths -test_env.unload_luarocks() local fun = require("luarocks.fun") describe("luarocks.fun #unit", function() diff --git a/spec/unit/loader_spec.lua b/spec/unit/loader_spec.lua new file mode 100644 index 00000000..7650c80c --- /dev/null +++ b/spec/unit/loader_spec.lua @@ -0,0 +1,21 @@ +local test_env = require("spec.util.test_env") +local run = test_env.run + +describe("luarocks.loader", function() + + before_each(function() + test_env.setup_specs() + end) + + describe("#unit", function() + it("starts", function() + assert(run.lua_bool([[-e "require 'luarocks.loader'; print(package.loaded['luarocks.loaded'])"]])) + end) + + describe("which", function() + it("finds modules using package.path", function() + assert(run.lua_bool([[-e "loader = require 'luarocks.loader'; local x,y,z,p = loader.which('luarocks.loader', 'p'); assert(p == 'p')"]])) + end) + end) + end) +end) diff --git a/spec/unit/persist_spec.lua b/spec/unit/persist_spec.lua index f426fd83..ea5985aa 100644 --- a/spec/unit/persist_spec.lua +++ b/spec/unit/persist_spec.lua @@ -1,7 +1,6 @@ local test_env = require("spec.util.test_env") local testing_paths = test_env.testing_paths -test_env.unload_luarocks() local persist = require("luarocks.persist") describe("luarocks.persist #unit", function() diff --git a/spec/unit/test_spec.lua b/spec/unit/test_spec.lua new file mode 100644 index 00000000..4d338984 --- /dev/null +++ b/spec/unit/test_spec.lua @@ -0,0 +1,174 @@ +local test_env = require("spec.util.test_env") +local lfs = require("lfs") +local get_tmp_path = test_env.get_tmp_path +local testing_paths = test_env.testing_paths +local write_file = test_env.write_file + +local fs = require("luarocks.fs") +local cfg = require("luarocks.core.cfg") +local path = require("luarocks.path") +local test = require("luarocks.test") +local test_busted = require("luarocks.test.busted") +local test_command = require("luarocks.test.command") + +describe("LuaRocks test #unit", function() + local runner + + lazy_setup(function() + cfg.init() + fs.init() + runner = require("luacov.runner") + runner.init(testing_paths.testrun_dir .. "/luacov.config") + runner.tick = true + end) + + lazy_teardown(function() + runner.shutdown() + end) + + local tmpdir + local olddir + + local create_tmp_dir = function() + tmpdir = get_tmp_path() + olddir = lfs.currentdir() + lfs.mkdir(tmpdir) + lfs.chdir(tmpdir) + fs.change_dir(tmpdir) + end + + local destroy_tmp_dir = function() + if olddir then + lfs.chdir(olddir) + if tmpdir then + lfs.rmdir(tmpdir) + end + end + end + + describe("test.command", function() + describe("command.detect_type", function() + before_each(function() + create_tmp_dir() + end) + + after_each(function() + destroy_tmp_dir() + end) + + it("returns true if test.lua exists", function() + write_file("test.lua", "", finally) + assert.truthy(test_command.detect_type()) + end) + + it("returns false if test.lua doesn't exist", function() + assert.falsy(test_command.detect_type()) + end) + end) + + describe("command.run_tests", function() + before_each(function() + create_tmp_dir() + end) + + after_each(function() + destroy_tmp_dir() + end) + + it("returns the result of the executed tests", function() + write_file("test.lua", "assert(1==1)", finally) + assert.truthy(test_command.run_tests(nil, {})) + + write_file("test.lua", "assert(1==2)", finally) + assert.falsy(test_command.run_tests(nil, {})) + end) + + it("returns the result of the executed tests with custom arguments and test command", function() + write_file("test.lua", "assert(1==1)", finally) + + local test = { + script = "test.lua", + flags = { + arg1 = "1", + arg2 = "2" + }, + command = fs.Q(testing_paths.lua) + } + assert.truthy(test_command.run_tests(test, {})) + end) + + it("returns false and does nothing if the test script doesn't exist", function() + assert.falsy(test_command.run_tests(nil, {})) + end) + end) + end) + + describe("test.busted", function() + describe("busted.detect_type", function() + before_each(function() + create_tmp_dir() + end) + + after_each(function() + destroy_tmp_dir() + end) + + it("returns true if .busted exists", function() + write_file(".busted", "", finally) + assert.truthy(test_busted.detect_type()) + end) + + it("returns false if .busted doesn't exist", function() + assert.falsy(test_busted.detect_type()) + end) + end) + + describe("busted.run_tests", function() + before_each(function() + path.use_tree(testing_paths.testing_sys_tree) + create_tmp_dir() + end) + + after_each(function() + destroy_tmp_dir() + end) + + pending("returns the result of the executed tests", function() + -- FIXME: busted issue + write_file("test_spec.lua", "assert(1==1)", finally) + assert.truthy(test_busted.run_tests(nil, {})) + + write_file("test_spec.lua", "assert(1==2)", finally) + assert.falsy(test_busted.run_tests()) + end) + end) + end) + + describe("test", function() + describe("test.run_test_suite", function() + before_each(function() + create_tmp_dir() + end) + + after_each(function() + destroy_tmp_dir() + end) + + it("returns false if the given rockspec cannot be loaded", function() + assert.falsy(test.run_test_suite("invalid", nil, {})) + end) + + it("returns false if no test type was detected", function() + assert.falsy(test.run_test_suite({ package = "test" }, nil, {})) + end) + + it("returns the result of executing the tests specified in the given rockspec", function() + write_file("test.lua", "assert(1==1)", finally) + assert.truthy(test.run_test_suite({ test_dependencies = {} }, nil, {})) + + write_file("test.lua", "assert(1==2)", finally) + assert.falsy(test.run_test_suite({ test_dependencies = {} }, nil, {})) + end) + end) + end) +end) diff --git a/spec/unit/tools_spec.lua b/spec/unit/tools_spec.lua index 29e21740..0863c316 100644 --- a/spec/unit/tools_spec.lua +++ b/spec/unit/tools_spec.lua @@ -3,7 +3,6 @@ local get_tmp_path = test_env.get_tmp_path local testing_paths = test_env.testing_paths local write_file = test_env.write_file -test_env.unload_luarocks() local fs = require("luarocks.fs") local cfg = require("luarocks.core.cfg") local patch = require("luarocks.tools.patch") diff --git a/spec/unit/util_spec.lua b/spec/unit/util_spec.lua new file mode 100644 index 00000000..0e380f2a --- /dev/null +++ b/spec/unit/util_spec.lua @@ -0,0 +1,161 @@ +local test_env = require("spec.util.test_env") +local testing_paths = test_env.testing_paths +local P = test_env.P + +local util = require("luarocks.util") +local core_util = require("luarocks.core.util") + +describe("luarocks.util #unit", function() + local runner + + setup(function() + runner = require("luacov.runner") + runner.init(testing_paths.testrun_dir .. "/luacov.config") + runner.tick = true + end) + + teardown(function() + runner.shutdown() + end) + + describe("util.variable_substitutions", function() + it("replaces variables", function() + local t = { + ["hello"] = "$(KIND) world", + } + util.variable_substitutions(t, { + ["KIND"] = "happy", + }) + assert.are.same({ + ["hello"] = "happy world", + }, t) + end) + + it("missing variables are empty", function() + local t = { + ["hello"] = "$(KIND) world", + } + util.variable_substitutions(t, { + }) + assert.are.same({ + ["hello"] = " world", + }, t) + end) + end) + + describe("util.sortedpairs", function() + local function collect(iter, state, var) + local collected = {} + + while true do + local returns = {iter(state, var)} + + if returns[1] == nil then + return collected + else + table.insert(collected, returns) + var = returns[1] + end + end + end + + it("default sort", function() + assert.are.same({}, collect(util.sortedpairs({}))) + assert.are.same({ + {1, "v1"}, + {2, "v2"}, + {3, "v3"}, + {"bar", "v5"}, + {"foo", "v4"} + }, collect(util.sortedpairs({"v1", "v2", "v3", foo = "v4", bar = "v5"}))) + end) + + it("sort by function", function() + local function compare(a, b) return a > b end + assert.are.same({}, collect(util.sortedpairs({}, compare))) + assert.are.same({ + {3, "v3"}, + {2, "v2"}, + {1, "v1"} + }, collect(util.sortedpairs({"v1", "v2", "v3"}, compare))) + end) + + it("sort by priority table", function() + assert.are.same({}, collect(util.sortedpairs({}, {"k1", "k2"}))) + assert.are.same({ + {"k3", "v3"}, + {"k2", "v2", {"sub order"}}, + {"k1", "v1"}, + {"k4", "v4"}, + {"k5", "v5"}, + }, collect(util.sortedpairs({ + k1 = "v1", k2 = "v2", k3 = "v3", k4 = "v4", k5 = "v5" + }, {"k3", {"k2", {"sub order"}}, "k1"}))) + end) + end) + + describe("core.util.show_table", function() + it("returns a pretty-printed string containing the representation of the given table", function() + local result + + local t1 = {1, 2, 3} + result = core_util.show_table(t1) + assert.truthy(result:find("[1] = 1", 1, true)) + assert.truthy(result:find("[2] = 2", 1, true)) + assert.truthy(result:find("[3] = 3", 1, true)) + + local t2 = {a = 1, b = 2, c = 3} + result = core_util.show_table(t2) + assert.truthy(result:find("[\"a\"] = 1", 1, true)) + assert.truthy(result:find("[\"b\"] = 2", 1, true)) + assert.truthy(result:find("[\"c\"] = 3", 1, true)) + + local t3 = {a = 1, b = "2", c = {3}} + result = core_util.show_table(t3) + assert.truthy(result:find("[\"a\"] = 1", 1, true)) + assert.truthy(result:find("[\"b\"] = \"2\"", 1, true)) + assert.truthy(result:find("[\"c\"] = {", 1, true)) + assert.truthy(result:find("[1] = 3", 1, true)) + + local t4 = {a = 1, b = {c = 2, d = {e = "4"}}} + result = core_util.show_table(t4) + assert.truthy(result:find("[\"a\"] = 1", 1, true)) + assert.truthy(result:find("[\"b\"] = {", 1, true)) + assert.truthy(result:find("[\"c\"] = 2", 1, true)) + assert.truthy(result:find("[\"d\"] = {", 1, true)) + assert.truthy(result:find("[\"e\"] = \"4\"", 1, true)) + end) + end) + + describe("core.util.cleanup_path", function() + it("does not change order of existing items of prepended path", function() + local sys_path = P'/usr/local/bin;/usr/bin' + local lr_path = P'/home/user/.luarocks/bin;/usr/bin' + local path = lr_path .. ';' .. sys_path + + local result = core_util.cleanup_path(path, ';', '5.3', false) + assert.are.equal(P'/home/user/.luarocks/bin;/usr/local/bin;/usr/bin', result) + end) + + it("does not change order of existing items of appended path", function() + local sys_path = P'/usr/local/bin;/usr/bin' + local lr_path = P'/home/user/.luarocks/bin;/usr/bin' + local path = sys_path .. ';' .. lr_path + + local result = core_util.cleanup_path(path, ';', '5.3', true) + assert.are.equal(P'/usr/local/bin;/usr/bin;/home/user/.luarocks/bin', result) + end) + + it("rewrites versions that do not match the provided version", function() + local expected = P'a/b/lua/5.3/?.lua;a/b/c/lua/5.3/?.lua' + local result = core_util.cleanup_path(P'a/b/lua/5.2/?.lua;a/b/c/lua/5.3/?.lua', ';', '5.3') + assert.are.equal(expected, result) + end) + + it("does not rewrite versions for which the provided version is a substring", function() + local expected = P'a/b/lua/5.3/?.lua;a/b/c/lua/5.3.4/?.lua' + local result = core_util.cleanup_path(P'a/b/lua/5.2/?.lua;a/b/c/lua/5.3.4/?.lua', ';', '5.3') + assert.are.equal(expected, result) + end) + end) +end) diff --git a/spec/unpack_spec.lua b/spec/unpack_spec.lua index 00d6781f..59f7eb0b 100644 --- a/spec/unpack_spec.lua +++ b/spec/unpack_spec.lua @@ -3,8 +3,6 @@ local lfs = require("lfs") local run = test_env.run local testing_paths = test_env.testing_paths -test_env.unload_luarocks() - local extra_rocks = { "/cprint-${CPRINT}.src.rock", "/cprint-${CPRINT}.rockspec", diff --git a/spec/upload_spec.lua b/spec/upload_spec.lua index 119d34b6..cc0b606c 100644 --- a/spec/upload_spec.lua +++ b/spec/upload_spec.lua @@ -2,38 +2,41 @@ local test_env = require("spec.util.test_env") local run = test_env.run local testing_paths = test_env.testing_paths -test_env.unload_luarocks() - describe("luarocks upload #integration", function() - before_each(function() - test_env.setup_specs() - end) + describe("general tests", function() + before_each(function() + test_env.setup_specs(nil) + end) - it("with no flags/arguments", function() - assert.is_false(run.luarocks_bool("upload")) - end) + it("with no flags/arguments", function() + assert.is_false(run.luarocks_bool("upload")) + end) - it("invalid rockspec", function() - assert.is_false(run.luarocks_bool("upload invalid.rockspec")) - end) + it("invalid rockspec", function() + assert.is_false(run.luarocks_bool("upload invalid.rockspec")) + end) - it("api key invalid", function() - assert.is_false(run.luarocks_bool("upload --api-key=invalid invalid.rockspec")) - end) + it("api key invalid", function() + assert.is_false(run.luarocks_bool("upload --api-key=invalid invalid.rockspec")) + end) - it("api key invalid and skip-pack", function() - assert.is_false(run.luarocks_bool("upload --api-key=\"invalid\" --skip-pack " .. testing_paths.testing_server .. "/luasocket-${LUASOCKET}.rockspec")) - end) + it("api key invalid and skip-pack", function() + assert.is_false(run.luarocks_bool("upload --api-key=\"invalid\" --skip-pack " .. testing_paths.testing_server .. "/luasocket-${LUASOCKET}.rockspec")) + end) - it("force #unix", function() - assert.is_false(run.luarocks_bool("upload --api-key=\"invalid\" --force " .. testing_paths.testing_server .. "/luasocket-${LUASOCKET}.rockspec")) + it("force #unix", function() + assert.is_false(run.luarocks_bool("upload --api-key=\"invalid\" --force " .. testing_paths.testing_server .. "/luasocket-${LUASOCKET}.rockspec")) + end) end) describe("tests with Xavante server #mock", function() - before_each(test_env.mock_server_init) + lazy_setup(function() + test_env.setup_specs(nil, "mock") + test_env.mock_server_init() + end) - after_each(test_env.mock_server_done) + lazy_teardown(test_env.mock_server_done) it("rockspec with api-key", function() assert.is_true(run.luarocks_bool("upload " .. testing_paths.fixtures_dir .. "/a_rock-1.0-1.rockspec " .. test_env.openssl_dirs .. " --api-key=123", {LUAROCKS_CONFIG = testing_paths.testrun_dir .. "/luarocks_site.lua"})) diff --git a/spec/util/test_env.lua b/spec/util/test_env.lua index f311a370..15aa554d 100644 --- a/spec/util/test_env.lua +++ b/spec/util/test_env.lua @@ -809,52 +809,6 @@ local function get_luarocks_platform(variables) return execute_output(cmd, false, variables) end ---- Function for initial setup of environment, variables, md5sums for spec files -function test_env.setup_specs(extra_rocks) - local testrun_dir = test_env.testing_paths.testrun_dir - local variables = test_env.env_variables - - -- if global variable about successful creation of testing environment doesn't exist, build environment - if not test_env.setup_done then - if test_env.CI then - if not exists(os.getenv("HOME"), ".ssh/id_rsa.pub") then - execute_bool("ssh-keygen -t rsa -P \"\" -f ~/.ssh/id_rsa") - execute_bool("cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys") - execute_bool("chmod og-wx ~/.ssh/authorized_keys") - execute_bool("ssh-keyscan localhost >> ~/.ssh/known_hosts") - end - end - - test_env.main() - - -- preload before meddling with package.path - require("spec.util.git_repo") - require("spec.util.quick") - - package.path = variables.LUA_PATH - package.cpath = variables.LUA_CPATH - - test_env.platform = get_luarocks_platform(test_env.env_variables) - test_env.wrapper_extension = test_env.TEST_TARGET_OS == "windows" and ".bat" or "" - test_env.md5sums = create_md5sums(test_env.testing_paths) - test_env.setup_done = true - title("RUNNING TESTS") - end - - if extra_rocks then - local make_manifest = download_rocks(extra_rocks, test_env.testing_paths.testing_server) - if make_manifest then - test_env.run.luarocks_admin_nocov("make_manifest " .. test_env.testing_paths.testing_server) - end - end - - if test_env.RESET_ENV then - reset_environment(test_env.testing_paths, test_env.md5sums, variables) - end - - lfs.chdir(testrun_dir) -end - --- Test if required rock is installed and if not, install it. -- Return `true` if the rock is already installed or has been installed successfully, -- `false` if installation failed. @@ -1024,10 +978,14 @@ local function setup_luarocks() end local function mock_api_call(path) - test_env.execute(C(tool("wget"), "--quiet --timeout=5 --tries=1 localhost:8080" .. path)) + return test_env.execute(C(tool("wget"), "--timeout=0.1 --quiet --tries=10 http://localhost:8080" .. path)) end function test_env.mock_server_init() + if not test_env.mock_prepared then + error("need to setup_specs with with_mock set to true") + end + local testing_paths = test_env.testing_paths assert(test_env.need_rock("restserver-xavante")) @@ -1043,14 +1001,13 @@ function test_env.mock_server_init() os.execute(test_env.execute_helper(bg_cmd, true, test_env.env_variables)) - for _ = 1, 10 do + for _ = 1, 100 do if mock_api_call("/api/tool_version") then break end - os.execute(test_env.TEST_TARGET_OS == "windows" - and "timeout 1 > NUL" - or "sleep 1") + and "ping 192.0.2.0 -n 1 -w 250 > NUL" + or "sleep 0.1") end end @@ -1070,6 +1027,10 @@ local function find_binary_rock(src_rock, dirname) end local function prepare_mock_server_binary_rocks() + if test_env.mock_prepared then + return + end + local testing_paths = test_env.testing_paths local rocks = { @@ -1106,6 +1067,8 @@ local function prepare_mock_server_binary_rocks() if make_manifest then test_env.run.luarocks_admin_nocov(C("make_manifest", Q(testing_paths.testing_server))) end + + test_env.mock_prepared = true end --- @@ -1169,8 +1132,58 @@ function test_env.main() download_rocks(urls, testing_paths.testing_server) build_environment(rocks, env_vars) +end + +--- Function for initial setup of environment, variables, md5sums for spec files +function test_env.setup_specs(extra_rocks, use_mock) + test_env.unload_luarocks() + + local testrun_dir = test_env.testing_paths.testrun_dir + local variables = test_env.env_variables + + -- if global variable about successful creation of testing environment doesn't exist, build environment + if not test_env.setup_done then + if test_env.CI then + if not exists(os.getenv("HOME"), ".ssh/id_rsa.pub") then + execute_bool("ssh-keygen -t rsa -P \"\" -f ~/.ssh/id_rsa") + execute_bool("cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys") + execute_bool("chmod og-wx ~/.ssh/authorized_keys") + execute_bool("ssh-keyscan localhost >> ~/.ssh/known_hosts") + end + end + + test_env.main() + + -- preload before meddling with package.path + require("spec.util.git_repo") + require("spec.util.quick") - prepare_mock_server_binary_rocks() + package.path = variables.LUA_PATH + package.cpath = variables.LUA_CPATH + + test_env.platform = get_luarocks_platform(test_env.env_variables) + test_env.wrapper_extension = test_env.TEST_TARGET_OS == "windows" and ".bat" or "" + test_env.md5sums = create_md5sums(test_env.testing_paths) + test_env.setup_done = true + title("RUNNING TESTS") + end + + if use_mock == "mock" then + prepare_mock_server_binary_rocks() + end + + if extra_rocks then + local make_manifest = download_rocks(extra_rocks, test_env.testing_paths.testing_server) + if make_manifest then + test_env.run.luarocks_admin_nocov("make_manifest " .. test_env.testing_paths.testing_server) + end + end + + if test_env.RESET_ENV then + reset_environment(test_env.testing_paths, test_env.md5sums, variables) + end + + lfs.chdir(testrun_dir) end test_env.set_lua_version() diff --git a/spec/util_spec.lua b/spec/util_spec.lua index abc0da57..0f199c9c 100644 --- a/spec/util_spec.lua +++ b/spec/util_spec.lua @@ -1,8 +1,6 @@ local test_env = require("spec.util.test_env") local lfs = require("lfs") local run = test_env.run -local testing_paths = test_env.testing_paths -local P = test_env.P describe("Basic tests #integration", function() @@ -55,162 +53,3 @@ describe("Basic tests #integration", function() end) end) - -test_env.unload_luarocks() -local util = require("luarocks.util") -local core_util = require("luarocks.core.util") - -describe("luarocks.util #unit", function() - local runner - - setup(function() - runner = require("luacov.runner") - runner.init(testing_paths.testrun_dir .. "/luacov.config") - runner.tick = true - end) - - teardown(function() - runner.shutdown() - end) - - describe("util.variable_substitutions", function() - it("replaces variables", function() - local t = { - ["hello"] = "$(KIND) world", - } - util.variable_substitutions(t, { - ["KIND"] = "happy", - }) - assert.are.same({ - ["hello"] = "happy world", - }, t) - end) - - it("missing variables are empty", function() - local t = { - ["hello"] = "$(KIND) world", - } - util.variable_substitutions(t, { - }) - assert.are.same({ - ["hello"] = " world", - }, t) - end) - end) - - describe("util.sortedpairs", function() - local function collect(iter, state, var) - local collected = {} - - while true do - local returns = {iter(state, var)} - - if returns[1] == nil then - return collected - else - table.insert(collected, returns) - var = returns[1] - end - end - end - - it("default sort", function() - assert.are.same({}, collect(util.sortedpairs({}))) - assert.are.same({ - {1, "v1"}, - {2, "v2"}, - {3, "v3"}, - {"bar", "v5"}, - {"foo", "v4"} - }, collect(util.sortedpairs({"v1", "v2", "v3", foo = "v4", bar = "v5"}))) - end) - - it("sort by function", function() - local function compare(a, b) return a > b end - assert.are.same({}, collect(util.sortedpairs({}, compare))) - assert.are.same({ - {3, "v3"}, - {2, "v2"}, - {1, "v1"} - }, collect(util.sortedpairs({"v1", "v2", "v3"}, compare))) - end) - - it("sort by priority table", function() - assert.are.same({}, collect(util.sortedpairs({}, {"k1", "k2"}))) - assert.are.same({ - {"k3", "v3"}, - {"k2", "v2", {"sub order"}}, - {"k1", "v1"}, - {"k4", "v4"}, - {"k5", "v5"}, - }, collect(util.sortedpairs({ - k1 = "v1", k2 = "v2", k3 = "v3", k4 = "v4", k5 = "v5" - }, {"k3", {"k2", {"sub order"}}, "k1"}))) - end) - end) - - describe("core.util.show_table", function() - it("returns a pretty-printed string containing the representation of the given table", function() - local result - - local t1 = {1, 2, 3} - result = core_util.show_table(t1) - assert.truthy(result:find("[1] = 1", 1, true)) - assert.truthy(result:find("[2] = 2", 1, true)) - assert.truthy(result:find("[3] = 3", 1, true)) - - local t2 = {a = 1, b = 2, c = 3} - result = core_util.show_table(t2) - assert.truthy(result:find("[\"a\"] = 1", 1, true)) - assert.truthy(result:find("[\"b\"] = 2", 1, true)) - assert.truthy(result:find("[\"c\"] = 3", 1, true)) - - local t3 = {a = 1, b = "2", c = {3}} - result = core_util.show_table(t3) - assert.truthy(result:find("[\"a\"] = 1", 1, true)) - assert.truthy(result:find("[\"b\"] = \"2\"", 1, true)) - assert.truthy(result:find("[\"c\"] = {", 1, true)) - assert.truthy(result:find("[1] = 3", 1, true)) - - local t4 = {a = 1, b = {c = 2, d = {e = "4"}}} - result = core_util.show_table(t4) - assert.truthy(result:find("[\"a\"] = 1", 1, true)) - assert.truthy(result:find("[\"b\"] = {", 1, true)) - assert.truthy(result:find("[\"c\"] = 2", 1, true)) - assert.truthy(result:find("[\"d\"] = {", 1, true)) - assert.truthy(result:find("[\"e\"] = \"4\"", 1, true)) - end) - end) - - describe("core.util.cleanup_path", function() - it("does not change order of existing items of prepended path", function() - local sys_path = P'/usr/local/bin;/usr/bin' - local lr_path = P'/home/user/.luarocks/bin;/usr/bin' - local path = lr_path .. ';' .. sys_path - - local result = core_util.cleanup_path(path, ';', '5.3', false) - assert.are.equal(P'/home/user/.luarocks/bin;/usr/local/bin;/usr/bin', result) - end) - - it("does not change order of existing items of appended path", function() - local sys_path = P'/usr/local/bin;/usr/bin' - local lr_path = P'/home/user/.luarocks/bin;/usr/bin' - local path = sys_path .. ';' .. lr_path - - local result = core_util.cleanup_path(path, ';', '5.3', true) - assert.are.equal(P'/usr/local/bin;/usr/bin;/home/user/.luarocks/bin', result) - end) - - it("rewrites versions that do not match the provided version", function() - local expected = P'a/b/lua/5.3/?.lua;a/b/c/lua/5.3/?.lua' - local result = core_util.cleanup_path(P'a/b/lua/5.2/?.lua;a/b/c/lua/5.3/?.lua', ';', '5.3') - assert.are.equal(expected, result) - end) - - it("does not rewrite versions for which the provided version is a substring", function() - local expected = P'a/b/lua/5.3/?.lua;a/b/c/lua/5.3.4/?.lua' - local result = core_util.cleanup_path(P'a/b/lua/5.2/?.lua;a/b/c/lua/5.3.4/?.lua', ';', '5.3') - assert.are.equal(expected, result) - end) - end) -end) diff --git a/spec/which_spec.lua b/spec/which_spec.lua index 9ccb1040..5712511b 100644 --- a/spec/which_spec.lua +++ b/spec/which_spec.lua @@ -2,17 +2,14 @@ local test_env = require("spec.util.test_env") local run = test_env.run local P = test_env.P -test_env.unload_luarocks() - local extra_rocks = { "/say-1.2-1.src.rock", } describe("luarocks which #integration", function() - setup(function() + lazy_setup(function() test_env.setup_specs(extra_rocks) - test_env.unload_luarocks() -- need to be required here, because hardcoded is created after first loading of specs end) it("fails on missing arguments", function() diff --git a/spec/write_rockspec_spec.lua b/spec/write_rockspec_spec.lua index 5f9faa6e..abd5c80b 100644 --- a/spec/write_rockspec_spec.lua +++ b/spec/write_rockspec_spec.lua @@ -5,7 +5,7 @@ local run = test_env.run describe("luarocks write_rockspec tests #integration", function() - before_each(function() + lazy_setup(function() test_env.setup_specs() end) @@ -20,7 +20,7 @@ describe("luarocks write_rockspec tests #integration", function() describe("from #git #unix", function() local git - setup(function() + lazy_setup(function() git = git_repo.start() end) @@ -79,10 +79,11 @@ describe("luarocks write_rockspec tests #integration", function() describe("from tarball #mock", function() - setup(function() + lazy_setup(function() + test_env.setup_specs(nil, "mock") test_env.mock_server_init() end) - teardown(function() + lazy_teardown(function() test_env.mock_server_done() end) -- cgit v1.2.3-55-g6feb