From 08351f44d22f07f0eaf87fcc5732a32e7e5282df Mon Sep 17 00:00:00 2001 From: V1K1NGbg Date: Thu, 22 Aug 2024 17:48:56 -0300 Subject: Teal: convert luarocks.test --- src/luarocks/test.tl | 55 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 32 insertions(+), 23 deletions(-) (limited to 'src') diff --git a/src/luarocks/test.tl b/src/luarocks/test.tl index d074b950..d076d264 100644 --- a/src/luarocks/test.tl +++ b/src/luarocks/test.tl @@ -1,32 +1,44 @@ - -local test = {} +local record test +end local fetch = require("luarocks.fetch") local deps = require("luarocks.deps") local util = require("luarocks.util") +local type Rockspec = require("luarocks.core.types.rockspec").Rockspec +local type Dependencies = require("luarocks.core.types.rockspec").Dependencies + +local type TestRunner = require("luarocks.core.types.testrunner").TestRunner + local test_types = { "busted", "command", } -local test_modules = {} +local test_modules: {TestRunner} = {} +local typetomod: {string: TestRunner} = {} +local modtotype: {TestRunner: string} = {} for _, test_type in ipairs(test_types) do - local mod = require("luarocks.test." .. test_type) + local mod: TestRunner + if test_type == "command" then + mod = require("luarocks.test.command") + elseif test_type == "busted" then + mod = require("luarocks.test.busted") + end table.insert(test_modules, mod) - test_modules[test_type] = mod - test_modules[mod] = test_type + typetomod[test_type] = mod + modtotype[mod] = test_type end -local function get_test_type(rockspec) +local function get_test_type(rockspec: Rockspec): string, string if rockspec.test and rockspec.test.type then return rockspec.test.type end for _, test_module in ipairs(test_modules) do if test_module.detect_type() then - return test_modules[test_module] + return modtotype[test_module] end end @@ -34,27 +46,25 @@ local function get_test_type(rockspec) end -- Run test suite as configured in rockspec in the current directory. -function test.run_test_suite(rockspec_arg, test_type, args, prepare) - local rockspec - if type(rockspec_arg) == "string" then - local err, errcode +function test.run_test_suite(rockspec_arg: string | Rockspec, test_type: string, args: {string}, prepare: boolean): boolean, string, string + local rockspec: Rockspec + if rockspec_arg is string then + local err, errcode: string, string rockspec, err, errcode = fetch.load_rockspec(rockspec_arg) if err then return nil, err, errcode end else - assert(type(rockspec_arg) == "table") rockspec = rockspec_arg end if not test_type then - local err - test_type, err = get_test_type(rockspec, test_type) + local err: string + test_type, err = get_test_type(rockspec) if not test_type then return nil, err end end - assert(test_type) local all_deps = { "dependencies", @@ -62,29 +72,28 @@ function test.run_test_suite(rockspec_arg, test_type, args, prepare) "test_dependencies", } for _, dep_kind in ipairs(all_deps) do - if rockspec[dep_kind] and next(rockspec[dep_kind]) then - local ok, err, errcode = deps.fulfill_dependencies(rockspec, dep_kind, "all") + if (rockspec as {string: Dependencies})[dep_kind] and next((rockspec as {string: Dependencies})[dep_kind]) ~= nil then + local _, err, errcode = deps.fulfill_dependencies(rockspec, dep_kind, "all") if err then return nil, err, errcode end end end - local mod_name = "luarocks.test." .. test_type - local pok, test_mod = pcall(require, mod_name) + local pok, test_mod = pcall(require, "luarocks.test."..test_type) as (boolean, TestRunner) if not pok then - return nil, "failed loading test execution module " .. mod_name + return nil, "failed loading test execution module luarocks.test."..test_type end if prepare then if test_type == "busted" then - return test_mod.run_tests(rockspec_arg, {"--version"}) + return test_mod.run_tests(rockspec.test, {"--version"}) else return true end else local flags = rockspec.test and rockspec.test.flags - if type(flags) == "table" then + if flags is {string} then util.variable_substitutions(flags, rockspec.variables) -- insert any flags given in test.flags at the front of args -- cgit v1.2.3-55-g6feb