Skip to content

Commit 58eae75

Browse files
Oleg Chaplashkinylobankov
authored andcommitted
Autorequire luatest in server instances
Luatest module available without explicit require. ``` g.server:exec(function() t.assert(...) end) ``` The module is also available for the eval method: ``` g.server:eval([[ t.assert(...) ]]) ``` This also works with replica set instances. Note: This will work if the value for the LUA_PATH environment variable has been set correctly. If it is wrong or undefined, you will see a warning: ``` LUA_PATH is unset or incorrect, module 'luatest' not found ``` Resolves #233
1 parent 8b14346 commit 58eae75

File tree

6 files changed

+142
-7
lines changed

6 files changed

+142
-7
lines changed

.luacheckrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
include_files = {"**/*.lua", "*.rockspec", "*.luacheckrc"}
22
exclude_files = {"lua_modules/", ".luarocks/", ".rocks/", "tmp/"}
33

4+
globals = {"t"}
5+
46
max_line_length = 120

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
- Print Tarantool version used by luatest.
4242
- Add new module `replica_proxy.lua`.
4343
- Add new module `tarantool.lua`.
44+
- Autorequire `luatest` module in the server instance as `t` variable.
4445

4546
## 0.5.7
4647

luatest/init.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,8 @@ function luatest.defaults(...)
9595
return luatest.configure(...)
9696
end
9797

98+
if not rawget(_G, 't') then
99+
rawset(_G, 't', luatest)
100+
end
101+
98102
return luatest

luatest/server.lua

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -472,15 +472,25 @@ end
472472

473473
--- Evaluate Lua code on the server.
474474
--
475-
-- This is a shortcut for `server.net_box:eval()`.
475+
-- This is a wrapper for `server.net_box:eval()`.
476476
-- @string code
477477
-- @tab[opt] args
478478
-- @tab[opt] options
479-
function Server:eval(...)
479+
function Server:eval(code, ...)
480480
if self.net_box == nil then
481481
error('net_box is not connected', 2)
482482
end
483-
return self.net_box:eval(...)
483+
local preamble = [[
484+
if not rawget(_G, 't') then
485+
local is_ok, _t = pcall(require, 'luatest')
486+
if is_ok then
487+
rawset(_G, 't', _t)
488+
else
489+
require('log').warn('LUA_PATH is unset or incorrect, ' .. _t)
490+
end
491+
end
492+
]]
493+
return self.net_box:eval(preamble .. code, ...)
484494
end
485495

486496
--- Call remote function on the server by name.
@@ -526,7 +536,7 @@ end
526536
-- -- sum == 3
527537
--
528538
-- server:exec(function()
529-
-- local t = require('luatest')
539+
-- -- luatest is always available as `t`
530540
-- t.assert_equals(math.pi, 3)
531541
-- end)
532542
-- -- mytest.lua:12: expected: 3, actual: 3.1415926535898
@@ -546,7 +556,7 @@ function Server:exec(fn, args, options)
546556
error(err, 2)
547557
end
548558

549-
return exec_tail(self.net_box:eval([[
559+
return exec_tail(self:eval([[
550560
local dump, args = ...
551561
local fn = loadstring(dump)
552562
if args == nil then

test/autorequire_luatest_test.lua

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
local fio = require('fio')
2+
3+
local g = t.group()
4+
local Server = t.Server
5+
6+
local root = fio.dirname(fio.dirname(fio.abspath(package.search('test.helper'))))
7+
local datadir = fio.pathjoin(root, 'tmp', 'luatest_module')
8+
local command = fio.pathjoin(root, 'test', 'server_instance.lua')
9+
10+
g.before_all(function()
11+
fio.rmtree(datadir)
12+
13+
g.server = Server:new({
14+
command = command,
15+
workdir = fio.pathjoin(datadir, 'common'),
16+
env = {
17+
LUA_PATH =
18+
root .. '/?.lua;' ..
19+
root .. '/?/init.lua;' ..
20+
root .. '/.rocks/share/tarantool/?.lua'
21+
},
22+
http_port = 8182,
23+
net_box_port = 3133,
24+
})
25+
fio.mktree(g.server.workdir)
26+
27+
g.server:start()
28+
t.helpers.retrying({timeout = 2}, function()
29+
g.server:http_request('get', '/ping')
30+
end)
31+
32+
g.server:connect_net_box()
33+
end)
34+
35+
g.after_all(function()
36+
g.server:stop()
37+
fio.rmtree(datadir)
38+
end)
39+
40+
g.test_exec_without_t = function()
41+
local actual = g.server:exec(function()
42+
return 1 + 1
43+
end)
44+
t.assert_equals(actual, 2)
45+
end
46+
47+
g.test_exec_with_global_variable = function()
48+
g.server:exec(function()
49+
t.assert_equals(1, 1)
50+
end)
51+
t.assert_equals(1, 1)
52+
end
53+
54+
g.test_exec_with_local_variable = function()
55+
g.server:exec(function()
56+
local t = require('luatest')
57+
t.assert_equals(1, 1)
58+
end)
59+
t.assert_equals(1, 1)
60+
end
61+
62+
g.test_exec_with_local_duplicate = function()
63+
g.server:exec(function()
64+
local tt = require('luatest')
65+
t.assert_equals(1, 1)
66+
tt.assert_equals(1, 1)
67+
t.assert_equals(tt, t)
68+
end)
69+
end
70+
71+
g.test_eval_with_t = function()
72+
local actual = g.server:eval([[
73+
t.assert_equals(1, 1)
74+
return 1
75+
]])
76+
t.assert_equals(actual, 1)
77+
end
78+
79+
g.before_test('test_exec_when_lua_path_is_unset', function()
80+
-- Setup custom server without LUA_PATH variable
81+
local workdir = fio.tempdir()
82+
local log = fio.pathjoin(workdir, 'bad_env_server.log')
83+
g.bad_env_server = Server:new({
84+
command = command,
85+
workdir = workdir,
86+
env = {
87+
TARANTOOL_LOG = log
88+
},
89+
http_port = 8183,
90+
net_box_port = 3134,
91+
})
92+
93+
fio.mktree(g.bad_env_server.workdir)
94+
95+
g.bad_env_server:start()
96+
97+
t.helpers.retrying({timeout = 2}, function()
98+
g.bad_env_server:http_request('get', '/ping')
99+
end)
100+
101+
g.bad_env_server:connect_net_box()
102+
end)
103+
104+
g.test_exec_when_lua_path_is_unset = function()
105+
g.bad_env_server:exec(function() return 1 + 1 end)
106+
107+
t.assert(
108+
g.bad_env_server:grep_log(
109+
"W> LUA_PATH is unset or incorrect, module 'luatest' not found"
110+
)
111+
)
112+
end
113+
114+
g.after_test('test_exec_when_lua_path_is_unset', function()
115+
g.bad_env_server:drop()
116+
end)

test/server_instance.lua

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ local json = require('json')
55
local workdir = os.getenv('TARANTOOL_WORKDIR')
66
local listen = os.getenv('TARANTOOL_LISTEN')
77
local http_port = os.getenv('TARANTOOL_HTTP_PORT')
8+
local log = os.getenv('TARANTOOL_LOG')
89

9-
box.cfg({work_dir = workdir})
10+
local httpd = require('http.server').new('0.0.0.0', http_port)
11+
12+
box.cfg({work_dir = workdir, log = log})
1013
box.schema.user.grant('guest', 'super', nil, nil, {if_not_exists=true})
1114
box.cfg({listen = listen})
1215

13-
local httpd = require('http.server').new('0.0.0.0', http_port)
1416

1517
httpd:route({path = '/ping', method = 'GET'}, function()
1618
return {status = 200, body = 'pong'}

0 commit comments

Comments
 (0)