From fdc51baae37c3232f6051db0e58ebefca8fdfdc7 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Mon, 16 Apr 2018 18:00:19 -0300 Subject: test: initial implementation of `luarocks.test` Adds: * `luarocks test` command * `luarocks.test` module * TODO: support running test from `build` as well * `luarocks.test.busted` test backend * TODO: add test-only dependency on Busted * `luarocks.test.command` test backend --- src/bin/luarocks | 1 + src/luarocks/cmd/test.lua | 50 ++++++++++++++++++++++++++++++++++ src/luarocks/fetch.lua | 1 + src/luarocks/test.lua | 62 +++++++++++++++++++++++++++++++++++++++++++ src/luarocks/test/busted.lua | 31 ++++++++++++++++++++++ src/luarocks/test/command.lua | 46 ++++++++++++++++++++++++++++++++ 6 files changed, 191 insertions(+) create mode 100644 src/luarocks/cmd/test.lua create mode 100644 src/luarocks/test.lua create mode 100644 src/luarocks/test/busted.lua create mode 100644 src/luarocks/test/command.lua diff --git a/src/bin/luarocks b/src/bin/luarocks index 491809dd..066c72e3 100755 --- a/src/bin/luarocks +++ b/src/bin/luarocks @@ -29,6 +29,7 @@ commands = { upload = "luarocks.cmd.upload", config = "luarocks.cmd.config", which = "luarocks.cmd.which", + test = "luarocks.cmd.test", } command_line.run_command(...) diff --git a/src/luarocks/cmd/test.lua b/src/luarocks/cmd/test.lua new file mode 100644 index 00000000..e8a05f09 --- /dev/null +++ b/src/luarocks/cmd/test.lua @@ -0,0 +1,50 @@ + +--- Module implementing the LuaRocks "test" command. +-- Tests a rock, compiling its C parts if any. +local cmd_test = {} + +local util = require("luarocks.util") +local test = require("luarocks.test") + +cmd_test.help_summary = "Run the test suite in the current directory." +cmd_test.help_arguments = "[] [-- ]" +cmd_test.help = [[ +Run the test suite for the Lua project in the current directory. +If the first argument is a rockspec, it will use it to determine +the parameters for running tests; otherwise, it will attempt to +detect the rockspec. + +Any additional arguments are forwarded to the test suite. +To make sure that any flags passed in are not interpreted +as LuaRocks flags, use -- to separate LuaRocks arguments from +test suite arguments. +]]..util.deps_mode_help() + +--- Driver function for "build" command. +-- @param name string: A local or remote rockspec or rock file. +-- If a package name is given, forwards the request to "search" and, +-- if returned a result, installs the matching rock. +-- @param version string: When passing a package name, a version number may +-- also be given. +-- @return boolean or (nil, string, exitcode): True if build was successful; nil and an +-- error message otherwise. exitcode is optionally returned. +function cmd_test.command(flags, arg, ...) + assert(type(arg) == "string" or not arg) + + local args = { ... } + + if arg and arg:match("rockspec$") then + return test.run_test_suite(arg, args) + end + + table.insert(args, 1, arg) + + local rockspec, err = util.get_default_rockspec() + if not rockspec then + return nil, err + end + + return test.run_test_suite(rockspec, args) +end + +return cmd_test diff --git a/src/luarocks/fetch.lua b/src/luarocks/fetch.lua index ce9aaff5..08998986 100644 --- a/src/luarocks/fetch.lua +++ b/src/luarocks/fetch.lua @@ -243,6 +243,7 @@ function fetch.load_local_rockspec(filename, quick) util.platform_overrides(rockspec.external_dependencies) util.platform_overrides(rockspec.source) util.platform_overrides(rockspec.hooks) + util.platform_overrides(rockspec.test) local basename = dir.base_name(filename) if basename == "rockspec" then diff --git a/src/luarocks/test.lua b/src/luarocks/test.lua new file mode 100644 index 00000000..2a64a5d8 --- /dev/null +++ b/src/luarocks/test.lua @@ -0,0 +1,62 @@ + +local test = {} + +local fetch = require("luarocks.fetch") + +local test_types = { + "busted", + "command", +} + +local test_modules = {} + +for _, test_type in ipairs(test_types) do + local mod = require("luarocks.test." .. test_type) + table.insert(test_modules, mod) + test_modules[test_type] = mod + test_modules[mod] = test_type +end + +local function get_test_type(rockspec) + 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] + end + end + + return nil, "could not detect test type -- no test suite for " .. rockspec.package .. "?" +end + +-- Run test suite as configured in rockspec in the current directory. +function test.run_test_suite(rockspec_arg, args) + local rockspec + if type(rockspec_arg) == "string" then + local err, errcode + 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 + + local test_type, err = get_test_type(rockspec) + if not test_type then + return nil, err + end + + local mod_name = "luarocks.test." .. test_type + local pok, test_mod = pcall(require, mod_name) + if not pok then + return nil, "failed loading test execution module " .. mod_name + end + + return test_mod.run_tests(rockspec.test, args) +end + +return test diff --git a/src/luarocks/test/busted.lua b/src/luarocks/test/busted.lua new file mode 100644 index 00000000..989f283f --- /dev/null +++ b/src/luarocks/test/busted.lua @@ -0,0 +1,31 @@ + +local busted = {} + +local fs = require("luarocks.fs") + +local unpack = table.unpack or unpack + +function busted.detect_type() + if fs.exists(".busted") then + return true + end + return false +end + +function busted.run_tests(test, args) + if not test then + test = {} + end + + if type(test.flags) == "table" then + -- insert any flags given in test.flags at the front of args + for i = 1, #test.flags do + table.insert(args, i, test.flags[i]) + end + end + + return fs.execute("busted", unpack(args)) +end + + +return busted diff --git a/src/luarocks/test/command.lua b/src/luarocks/test/command.lua new file mode 100644 index 00000000..396450db --- /dev/null +++ b/src/luarocks/test/command.lua @@ -0,0 +1,46 @@ + +local command = {} + +local fs = require("luarocks.fs") +local dir = require("luarocks.dir") +local cfg = require("luarocks.core.cfg") + +local unpack = table.unpack or unpack + +function command.detect_type() + if fs.exists("test.lua") then + return true + end + return false +end + +function command.run_tests(test, args) + if not test then + test = { + script = "test.lua" + } + end + + if not test.script and not test.command then + test.script = "test.lua" + end + + if type(test.flags) == "table" then + -- insert any flags given in test.flags at the front of args + for i = 1, #test.flags do + table.insert(args, i, test.flags[i]) + end + end + + if test.script then + if not fs.exists(test.script) then + return nil, "Test script " .. test.script .. " does not exist" + end + local lua = fs.Q(dir.path(cfg.variables["LUA_BINDIR"], cfg.lua_interpreter)) -- get lua interpreter configured + return fs.execute(lua, test.script, unpack(args)) + elseif test.command then + return fs.execute(test.command, unpack(args)) + end +end + +return command -- cgit v1.2.3-55-g6feb