aboutsummaryrefslogtreecommitdiff
path: root/spec/util
diff options
context:
space:
mode:
Diffstat (limited to 'spec/util')
-rw-r--r--spec/util/git_repo.lua107
-rw-r--r--spec/util/test_env.lua24
2 files changed, 125 insertions, 6 deletions
diff --git a/spec/util/git_repo.lua b/spec/util/git_repo.lua
new file mode 100644
index 00000000..6cccfcc4
--- /dev/null
+++ b/spec/util/git_repo.lua
@@ -0,0 +1,107 @@
1local git_repo = {}
2
3local test_env = require("spec.util.test_env")
4local lfs = require("lfs")
5
6local files = {
7----------------------------------------
8["testrock-dev-1.rockspec"] = [[
9package = "testrock"
10version = "dev-1"
11source = {
12 url = "git://localhost:20000/testrock"
13}
14description = {
15 homepage = "https://localhost",
16 license = "MIT"
17}
18dependencies = {}
19build = {
20 type = "builtin",
21 modules = {
22 testrock = "testrock.lua"
23 }
24}
25]],
26----------------------------------------
27["testrock.lua"] = [[
28local testrock = {}
29
30function testrock.say()
31 return "Hello, world!"
32end
33
34return testrock
35]],
36----------------------------------------
37["foo.c"] = [[
38#include <lua.h>
39int luaopen_foo(lua_State* L) {
40 lua_pushnumber(L, 42);
41 return 1;
42}
43]],
44----------------------------------------
45["test.lua"] = [[
46print("this should be ignored!")
47]],
48}
49
50local function write_file(filename, contents)
51 local fd = assert(io.open(filename, "w"))
52 assert(fd:write(contents))
53 fd:close()
54end
55
56local function handling(args)
57 local pok, ret = pcall(args.try)
58 if not pok then
59 pok, ret = pcall(args.catch, ret)
60 end
61 args.finally()
62 if not pok then
63 error(ret)
64 end
65 return ret
66end
67
68function git_repo.start()
69 local dir = lfs.currentdir()
70 return handling {
71 try = function()
72 local pidfile = os.tmpname()
73 local basedir = test_env.testing_paths.testrun_dir .. "/git_repo"
74 local repodir = basedir .. "/testrock"
75 test_env.remove_dir(basedir)
76 lfs.mkdir(basedir)
77 lfs.mkdir(repodir)
78 lfs.chdir(repodir)
79 assert(test_env.execute("git init"))
80 for name, contents in pairs(files) do
81 write_file(name, contents)
82 test_env.execute("git add " .. name)
83 end
84 assert(test_env.execute("git commit -a -m 'initial commit'"))
85 print("git daemon --reuseaddr --pid-file="..pidfile.." --base-path="..basedir.." --export-all "..repodir.." &")
86 assert(test_env.execute("git daemon --reuseaddr --pid-file="..pidfile.." --base-path="..basedir.." --export-all "..repodir.." &"))
87 assert(test_env.execute("sleep 0.1; netstat -ln | grep '0.0.0.0:9418 .* LISTEN'"))
88 return {
89 stop = function()
90 local fd = io.open(pidfile)
91 local pid = fd:read("*a")
92 fd:close()
93 assert(test_env.execute("kill -HUP " .. pid))
94 test_env.remove_dir(basedir)
95 end
96 }
97 end,
98 catch = function(err)
99 error(err)
100 end,
101 finally = function()
102 lfs.chdir(dir)
103 end,
104 }
105end
106
107return git_repo
diff --git a/spec/util/test_env.lua b/spec/util/test_env.lua
index 2232de87..e318128e 100644
--- a/spec/util/test_env.lua
+++ b/spec/util/test_env.lua
@@ -1,6 +1,7 @@
1local lfs = require("lfs")
2local test_env = {} 1local test_env = {}
3 2
3local lfs = require("lfs")
4
4local help_message = [[ 5local help_message = [[
5LuaRocks test-suite 6LuaRocks test-suite
6 7
@@ -135,6 +136,11 @@ function test_env.execute_helper(command, print_command, env_variables)
135 return final_command 136 return final_command
136end 137end
137 138
139function test_env.execute(cmd)
140 local ok = os.execute(cmd)
141 return (ok == true or ok == 0) -- normalize Lua 5.1 output to boolean
142end
143
138--- Execute command and returns true/false 144--- Execute command and returns true/false
139-- @return true/false boolean: status of the command execution 145-- @return true/false boolean: status of the command execution
140local function execute_bool(command, print_command, env_variables) 146local function execute_bool(command, print_command, env_variables)
@@ -147,8 +153,7 @@ local function execute_bool(command, print_command, env_variables)
147 redirect = " > "..redirect_filename 153 redirect = " > "..redirect_filename
148 os.remove(redirect_filename) 154 os.remove(redirect_filename)
149 end 155 end
150 local ok = os.execute(command .. redirect) 156 local ok = test_env.execute(command .. redirect)
151 ok = (ok == true or ok == 0) -- normalize Lua 5.1 output to boolean
152 if redirect ~= "" then 157 if redirect ~= "" then
153 if not ok then 158 if not ok then
154 local fd = io.open(redirect_filename, "r") 159 local fd = io.open(redirect_filename, "r")
@@ -530,9 +535,13 @@ function test_env.unload_luarocks()
530 package.loaded[modname] = nil 535 package.loaded[modname] = nil
531 end 536 end
532 end 537 end
538 local src_pattern = test_env.testing_paths.src_dir .. "/?.lua"
539 if not package.path:find(src_pattern, 1, true) then
540 package.path = src_pattern .. ";" .. package.path
541 end
533end 542end
534 543
535--- Function for initially setup of environment, variables, md5sums for spec files 544--- Function for initial setup of environment, variables, md5sums for spec files
536function test_env.setup_specs(extra_rocks) 545function test_env.setup_specs(extra_rocks)
537 -- if global variable about successful creation of testing environment doesn't exist, build environment 546 -- if global variable about successful creation of testing environment doesn't exist, build environment
538 if not test_env.setup_done then 547 if not test_env.setup_done then
@@ -546,6 +555,10 @@ function test_env.setup_specs(extra_rocks)
546 end 555 end
547 556
548 test_env.main() 557 test_env.main()
558
559 -- preload before meddling with package.path
560 require("spec.util.git_repo")
561
549 package.path = test_env.env_variables.LUA_PATH 562 package.path = test_env.env_variables.LUA_PATH
550 563
551 test_env.platform = execute_output(test_env.testing_paths.lua .. " -e \"print(require('luarocks.core.cfg').arch)\"", false, test_env.env_variables) 564 test_env.platform = execute_output(test_env.testing_paths.lua .. " -e \"print(require('luarocks.core.cfg').arch)\"", false, test_env.env_variables)
@@ -744,9 +757,8 @@ function test_env.mock_server_extra_rocks(more)
744end 757end
745 758
746function test_env.mock_server_init() 759function test_env.mock_server_init()
747 local assert = require("luassert")
748 local testing_paths = test_env.testing_paths 760 local testing_paths = test_env.testing_paths
749 assert.is_true(test_env.need_rock("restserver-xavante")) 761 assert(test_env.need_rock("restserver-xavante"))
750 local final_command = test_env.execute_helper(testing_paths.lua .. " " .. testing_paths.util_dir .. "/mock-server.lua &", true, test_env.env_variables) 762 local final_command = test_env.execute_helper(testing_paths.lua .. " " .. testing_paths.util_dir .. "/mock-server.lua &", true, test_env.env_variables)
751 os.execute(final_command) 763 os.execute(final_command)
752end 764end