aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHisham Muhammad <hisham@gobolinux.org>2018-04-16 18:00:19 -0300
committerHisham Muhammad <hisham@gobolinux.org>2018-05-07 19:27:37 -0300
commitfdc51baae37c3232f6051db0e58ebefca8fdfdc7 (patch)
tree296ef6fc68fabd34a6f6d88b528567226eadb321
parent4dfd5a5501973890af7d9842b190ad01dd5f5b1e (diff)
downloadluarocks-fdc51baae37c3232f6051db0e58ebefca8fdfdc7.tar.gz
luarocks-fdc51baae37c3232f6051db0e58ebefca8fdfdc7.tar.bz2
luarocks-fdc51baae37c3232f6051db0e58ebefca8fdfdc7.zip
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
-rwxr-xr-xsrc/bin/luarocks1
-rw-r--r--src/luarocks/cmd/test.lua50
-rw-r--r--src/luarocks/fetch.lua1
-rw-r--r--src/luarocks/test.lua62
-rw-r--r--src/luarocks/test/busted.lua31
-rw-r--r--src/luarocks/test/command.lua46
6 files changed, 191 insertions, 0 deletions
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 = {
29 upload = "luarocks.cmd.upload", 29 upload = "luarocks.cmd.upload",
30 config = "luarocks.cmd.config", 30 config = "luarocks.cmd.config",
31 which = "luarocks.cmd.which", 31 which = "luarocks.cmd.which",
32 test = "luarocks.cmd.test",
32} 33}
33 34
34command_line.run_command(...) 35command_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 @@
1
2--- Module implementing the LuaRocks "test" command.
3-- Tests a rock, compiling its C parts if any.
4local cmd_test = {}
5
6local util = require("luarocks.util")
7local test = require("luarocks.test")
8
9cmd_test.help_summary = "Run the test suite in the current directory."
10cmd_test.help_arguments = "[<rockspec>] [-- <args>]"
11cmd_test.help = [[
12Run the test suite for the Lua project in the current directory.
13If the first argument is a rockspec, it will use it to determine
14the parameters for running tests; otherwise, it will attempt to
15detect the rockspec.
16
17Any additional arguments are forwarded to the test suite.
18To make sure that any flags passed in <args> are not interpreted
19as LuaRocks flags, use -- to separate LuaRocks arguments from
20test suite arguments.
21]]..util.deps_mode_help()
22
23--- Driver function for "build" command.
24-- @param name string: A local or remote rockspec or rock file.
25-- If a package name is given, forwards the request to "search" and,
26-- if returned a result, installs the matching rock.
27-- @param version string: When passing a package name, a version number may
28-- also be given.
29-- @return boolean or (nil, string, exitcode): True if build was successful; nil and an
30-- error message otherwise. exitcode is optionally returned.
31function cmd_test.command(flags, arg, ...)
32 assert(type(arg) == "string" or not arg)
33
34 local args = { ... }
35
36 if arg and arg:match("rockspec$") then
37 return test.run_test_suite(arg, args)
38 end
39
40 table.insert(args, 1, arg)
41
42 local rockspec, err = util.get_default_rockspec()
43 if not rockspec then
44 return nil, err
45 end
46
47 return test.run_test_suite(rockspec, args)
48end
49
50return 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)
243 util.platform_overrides(rockspec.external_dependencies) 243 util.platform_overrides(rockspec.external_dependencies)
244 util.platform_overrides(rockspec.source) 244 util.platform_overrides(rockspec.source)
245 util.platform_overrides(rockspec.hooks) 245 util.platform_overrides(rockspec.hooks)
246 util.platform_overrides(rockspec.test)
246 247
247 local basename = dir.base_name(filename) 248 local basename = dir.base_name(filename)
248 if basename == "rockspec" then 249 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 @@
1
2local test = {}
3
4local fetch = require("luarocks.fetch")
5
6local test_types = {
7 "busted",
8 "command",
9}
10
11local test_modules = {}
12
13for _, test_type in ipairs(test_types) do
14 local mod = require("luarocks.test." .. test_type)
15 table.insert(test_modules, mod)
16 test_modules[test_type] = mod
17 test_modules[mod] = test_type
18end
19
20local function get_test_type(rockspec)
21 if rockspec.test and rockspec.test.type then
22 return rockspec.test.type
23 end
24
25 for _, test_module in ipairs(test_modules) do
26 if test_module.detect_type() then
27 return test_modules[test_module]
28 end
29 end
30
31 return nil, "could not detect test type -- no test suite for " .. rockspec.package .. "?"
32end
33
34-- Run test suite as configured in rockspec in the current directory.
35function test.run_test_suite(rockspec_arg, args)
36 local rockspec
37 if type(rockspec_arg) == "string" then
38 local err, errcode
39 rockspec, err, errcode = fetch.load_rockspec(rockspec_arg)
40 if err then
41 return nil, err, errcode
42 end
43 else
44 assert(type(rockspec_arg) == "table")
45 rockspec = rockspec_arg
46 end
47
48 local test_type, err = get_test_type(rockspec)
49 if not test_type then
50 return nil, err
51 end
52
53 local mod_name = "luarocks.test." .. test_type
54 local pok, test_mod = pcall(require, mod_name)
55 if not pok then
56 return nil, "failed loading test execution module " .. mod_name
57 end
58
59 return test_mod.run_tests(rockspec.test, args)
60end
61
62return 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 @@
1
2local busted = {}
3
4local fs = require("luarocks.fs")
5
6local unpack = table.unpack or unpack
7
8function busted.detect_type()
9 if fs.exists(".busted") then
10 return true
11 end
12 return false
13end
14
15function busted.run_tests(test, args)
16 if not test then
17 test = {}
18 end
19
20 if type(test.flags) == "table" then
21 -- insert any flags given in test.flags at the front of args
22 for i = 1, #test.flags do
23 table.insert(args, i, test.flags[i])
24 end
25 end
26
27 return fs.execute("busted", unpack(args))
28end
29
30
31return 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 @@
1
2local command = {}
3
4local fs = require("luarocks.fs")
5local dir = require("luarocks.dir")
6local cfg = require("luarocks.core.cfg")
7
8local unpack = table.unpack or unpack
9
10function command.detect_type()
11 if fs.exists("test.lua") then
12 return true
13 end
14 return false
15end
16
17function command.run_tests(test, args)
18 if not test then
19 test = {
20 script = "test.lua"
21 }
22 end
23
24 if not test.script and not test.command then
25 test.script = "test.lua"
26 end
27
28 if type(test.flags) == "table" then
29 -- insert any flags given in test.flags at the front of args
30 for i = 1, #test.flags do
31 table.insert(args, i, test.flags[i])
32 end
33 end
34
35 if test.script then
36 if not fs.exists(test.script) then
37 return nil, "Test script " .. test.script .. " does not exist"
38 end
39 local lua = fs.Q(dir.path(cfg.variables["LUA_BINDIR"], cfg.lua_interpreter)) -- get lua interpreter configured
40 return fs.execute(lua, test.script, unpack(args))
41 elseif test.command then
42 return fs.execute(test.command, unpack(args))
43 end
44end
45
46return command