2014-11-06 19:54:49 -07:00
|
|
|
require('coxpcall')
|
2015-03-01 07:00:27 -07:00
|
|
|
local assert = require('luassert')
|
2014-10-08 08:56:28 -07:00
|
|
|
local Loop = require('nvim.loop')
|
|
|
|
local MsgpackStream = require('nvim.msgpack_stream')
|
|
|
|
local AsyncSession = require('nvim.async_session')
|
|
|
|
local Session = require('nvim.session')
|
|
|
|
|
|
|
|
local nvim_prog = os.getenv('NVIM_PROG') or 'build/bin/nvim'
|
2015-01-22 04:53:04 -07:00
|
|
|
local nvim_argv = {nvim_prog, '-u', 'NONE', '-i', 'NONE', '-N',
|
2015-06-30 02:32:14 -07:00
|
|
|
'--cmd', 'set shortmess+=I background=light noswapfile noautoindent laststatus=1 encoding=utf-8',
|
2015-01-23 14:06:52 -07:00
|
|
|
'--embed'}
|
2015-02-28 07:39:04 -07:00
|
|
|
|
|
|
|
-- Formulate a path to the directory containing nvim. We use this to
|
|
|
|
-- help run test executables. It helps to keep the tests working, even
|
|
|
|
-- when the build is not in the default location.
|
|
|
|
local nvim_dir = nvim_prog:gsub("[/\\][^/\\]+$", "")
|
|
|
|
if nvim_dir == nvim_prog then
|
|
|
|
nvim_dir = "."
|
|
|
|
end
|
|
|
|
|
2015-04-11 21:48:16 -07:00
|
|
|
-- Nvim "Unit Under Test" http://en.wikipedia.org/wiki/Device_under_test
|
|
|
|
local NvimUUT = {}
|
|
|
|
NvimUUT.__index = NvimUUT
|
|
|
|
|
2014-11-22 08:29:03 -07:00
|
|
|
local prepend_argv
|
2014-10-08 08:56:28 -07:00
|
|
|
|
|
|
|
if os.getenv('VALGRIND') then
|
|
|
|
local log_file = os.getenv('VALGRIND_LOG') or 'valgrind-%p.log'
|
2014-11-22 08:29:03 -07:00
|
|
|
prepend_argv = {'valgrind', '-q', '--tool=memcheck',
|
|
|
|
'--leak-check=yes', '--track-origins=yes',
|
|
|
|
'--show-possibly-lost=no',
|
|
|
|
'--suppressions=.valgrind.supp',
|
|
|
|
'--log-file='..log_file}
|
|
|
|
if os.getenv('GDB') then
|
|
|
|
table.insert(prepend_argv, '--vgdb=yes')
|
|
|
|
table.insert(prepend_argv, '--vgdb-error=0')
|
2014-10-08 08:56:28 -07:00
|
|
|
end
|
2014-11-22 08:29:03 -07:00
|
|
|
elseif os.getenv('GDB') then
|
|
|
|
local gdbserver_port = '7777'
|
|
|
|
if os.getenv('GDBSERVER_PORT') then
|
|
|
|
gdbserver_port = os.getenv('GDBSERVER_PORT')
|
|
|
|
end
|
|
|
|
prepend_argv = {'gdbserver', 'localhost:'..gdbserver_port}
|
|
|
|
end
|
|
|
|
|
|
|
|
if prepend_argv then
|
|
|
|
local len = #prepend_argv
|
2014-10-08 08:56:28 -07:00
|
|
|
for i = 1, #nvim_argv do
|
2014-11-22 08:29:03 -07:00
|
|
|
prepend_argv[i + len] = nvim_argv[i]
|
2014-10-08 08:56:28 -07:00
|
|
|
end
|
2014-11-22 08:29:03 -07:00
|
|
|
nvim_argv = prepend_argv
|
2014-10-08 08:56:28 -07:00
|
|
|
end
|
|
|
|
|
2014-11-07 07:01:59 -07:00
|
|
|
local session, loop_running, loop_stopped, last_error
|
2014-10-08 08:56:28 -07:00
|
|
|
|
2015-04-11 21:48:16 -07:00
|
|
|
local function set_session(s)
|
|
|
|
session = s
|
|
|
|
end
|
|
|
|
|
2014-10-08 08:56:28 -07:00
|
|
|
local function request(method, ...)
|
|
|
|
local status, rv = session:request(method, ...)
|
|
|
|
if not status then
|
2014-10-27 14:34:42 -07:00
|
|
|
if loop_running then
|
|
|
|
last_error = rv[2]
|
|
|
|
session:stop()
|
|
|
|
else
|
|
|
|
error(rv[2])
|
|
|
|
end
|
2014-10-08 08:56:28 -07:00
|
|
|
end
|
|
|
|
return rv
|
|
|
|
end
|
|
|
|
|
2014-10-08 09:56:01 -07:00
|
|
|
local function next_message()
|
|
|
|
return session:next_message()
|
|
|
|
end
|
|
|
|
|
2014-11-06 19:54:49 -07:00
|
|
|
local function call_and_stop_on_error(...)
|
|
|
|
local status, result = copcall(...)
|
|
|
|
if not status then
|
|
|
|
session:stop()
|
|
|
|
last_error = result
|
|
|
|
return ''
|
|
|
|
end
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
2014-12-08 18:31:45 -07:00
|
|
|
local function run(request_cb, notification_cb, setup_cb, timeout)
|
|
|
|
local on_request, on_notification, on_setup
|
2014-11-06 19:54:49 -07:00
|
|
|
|
2014-12-08 18:31:45 -07:00
|
|
|
if request_cb then
|
|
|
|
function on_request(method, args)
|
|
|
|
return call_and_stop_on_error(request_cb, method, args)
|
|
|
|
end
|
2014-11-06 19:54:49 -07:00
|
|
|
end
|
|
|
|
|
2014-12-08 18:31:45 -07:00
|
|
|
if notification_cb then
|
|
|
|
function on_notification(method, args)
|
|
|
|
call_and_stop_on_error(notification_cb, method, args)
|
|
|
|
end
|
2014-11-06 19:54:49 -07:00
|
|
|
end
|
|
|
|
|
2014-12-08 18:31:45 -07:00
|
|
|
if setup_cb then
|
|
|
|
function on_setup()
|
|
|
|
call_and_stop_on_error(setup_cb)
|
|
|
|
end
|
2014-11-06 19:54:49 -07:00
|
|
|
end
|
|
|
|
|
2014-11-07 07:01:59 -07:00
|
|
|
loop_stopped = false
|
2014-10-27 14:34:42 -07:00
|
|
|
loop_running = true
|
2014-12-08 18:31:45 -07:00
|
|
|
session:run(on_request, on_notification, on_setup, timeout)
|
2014-10-27 14:34:42 -07:00
|
|
|
loop_running = false
|
|
|
|
if last_error then
|
|
|
|
local err = last_error
|
|
|
|
last_error = nil
|
|
|
|
error(err)
|
|
|
|
end
|
2014-10-08 09:56:01 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
local function stop()
|
2014-11-07 07:01:59 -07:00
|
|
|
loop_stopped = true
|
2014-10-08 09:56:01 -07:00
|
|
|
session:stop()
|
|
|
|
end
|
|
|
|
|
2014-10-08 08:56:28 -07:00
|
|
|
local function nvim_command(cmd)
|
|
|
|
request('vim_command', cmd)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function nvim_eval(expr)
|
|
|
|
return request('vim_eval', expr)
|
|
|
|
end
|
|
|
|
|
2015-08-23 05:16:46 -07:00
|
|
|
local function nvim_call(name, ...)
|
|
|
|
return request('vim_call_function', name, {...})
|
|
|
|
end
|
|
|
|
|
2014-11-21 09:06:03 -07:00
|
|
|
local function nvim_feed(input)
|
|
|
|
while #input > 0 do
|
|
|
|
local written = request('vim_input', input)
|
|
|
|
input = input:sub(written + 1)
|
2014-10-08 08:56:28 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function dedent(str)
|
|
|
|
-- find minimum common indent across lines
|
|
|
|
local indent = nil
|
|
|
|
for line in str:gmatch('[^\n]+') do
|
|
|
|
local line_indent = line:match('^%s+') or ''
|
|
|
|
if indent == nil or #line_indent < #indent then
|
|
|
|
indent = line_indent
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if #indent == 0 then
|
|
|
|
-- no minimum common indent
|
|
|
|
return str
|
|
|
|
end
|
|
|
|
-- create a pattern for the indent
|
|
|
|
indent = indent:gsub('%s', '%%s')
|
|
|
|
-- strip it from the first line
|
|
|
|
str = str:gsub('^'..indent, '')
|
|
|
|
-- strip it from the remaining lines
|
|
|
|
str = str:gsub('[\n]'..indent, '\n')
|
|
|
|
return str
|
|
|
|
end
|
|
|
|
|
2014-09-29 05:43:52 -07:00
|
|
|
local function feed(...)
|
|
|
|
for _, v in ipairs({...}) do
|
2014-12-08 18:31:45 -07:00
|
|
|
nvim_feed(dedent(v))
|
2014-09-29 05:43:52 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-11-06 19:54:49 -07:00
|
|
|
local function rawfeed(...)
|
2014-09-29 05:43:52 -07:00
|
|
|
for _, v in ipairs({...}) do
|
2014-11-21 09:06:03 -07:00
|
|
|
nvim_feed(dedent(v))
|
2014-09-29 05:43:52 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-03-17 04:45:13 -07:00
|
|
|
local function spawn(argv)
|
|
|
|
local loop = Loop.new()
|
|
|
|
local msgpack_stream = MsgpackStream.new(loop)
|
|
|
|
local async_session = AsyncSession.new(msgpack_stream)
|
|
|
|
local session = Session.new(async_session)
|
|
|
|
loop:spawn(argv)
|
|
|
|
return session
|
|
|
|
end
|
|
|
|
|
2015-08-29 08:10:06 -07:00
|
|
|
local function clear(extra_cmd)
|
2014-11-06 19:54:49 -07:00
|
|
|
if session then
|
2015-01-23 14:06:52 -07:00
|
|
|
session:exit(0)
|
2014-11-06 19:54:49 -07:00
|
|
|
end
|
2015-08-29 08:10:06 -07:00
|
|
|
local args = {unpack(nvim_argv)}
|
|
|
|
if extra_cmd ~= nil then
|
|
|
|
table.insert(args, '--cmd')
|
|
|
|
table.insert(args, extra_cmd)
|
|
|
|
end
|
|
|
|
session = spawn(args)
|
2014-11-06 19:54:49 -07:00
|
|
|
end
|
|
|
|
|
2014-09-29 05:43:52 -07:00
|
|
|
local function insert(...)
|
2014-11-21 09:06:03 -07:00
|
|
|
nvim_feed('i')
|
2014-12-08 18:31:45 -07:00
|
|
|
for _, v in ipairs({...}) do
|
|
|
|
local escaped = v:gsub('<', '<lt>')
|
|
|
|
rawfeed(escaped)
|
|
|
|
end
|
|
|
|
nvim_feed('<ESC>')
|
2014-09-29 05:43:52 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
local function execute(...)
|
|
|
|
for _, v in ipairs({...}) do
|
|
|
|
if v:sub(1, 1) ~= '/' then
|
|
|
|
-- not a search command, prefix with colon
|
2014-11-21 09:06:03 -07:00
|
|
|
nvim_feed(':')
|
2014-09-29 05:43:52 -07:00
|
|
|
end
|
2014-12-08 18:31:45 -07:00
|
|
|
nvim_feed(v:gsub('<', '<lt>'))
|
|
|
|
nvim_feed('<CR>')
|
2014-09-29 05:43:52 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-06-30 05:10:34 -07:00
|
|
|
-- Dedent the given text and write it to the file name.
|
|
|
|
local function write_file(name, text)
|
|
|
|
local file = io.open(name, 'w')
|
|
|
|
file:write(dedent(text))
|
|
|
|
file:flush()
|
|
|
|
file:close()
|
|
|
|
end
|
|
|
|
|
2014-11-11 02:12:19 -07:00
|
|
|
local function source(code)
|
|
|
|
local tmpname = os.tmpname()
|
2015-06-30 05:11:44 -07:00
|
|
|
write_file(tmpname, code)
|
2014-11-11 02:12:19 -07:00
|
|
|
nvim_command('source '..tmpname)
|
|
|
|
os.remove(tmpname)
|
|
|
|
end
|
|
|
|
|
2014-10-01 05:31:57 -07:00
|
|
|
local function eq(expected, actual)
|
|
|
|
return assert.are.same(expected, actual)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function neq(expected, actual)
|
|
|
|
return assert.are_not.same(expected, actual)
|
|
|
|
end
|
|
|
|
|
2014-10-08 09:56:01 -07:00
|
|
|
local function ok(expr)
|
|
|
|
assert.is_true(expr)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function nvim(method, ...)
|
|
|
|
return request('vim_'..method, ...)
|
|
|
|
end
|
|
|
|
|
2015-07-27 04:39:38 -07:00
|
|
|
local function nvim_async(method, ...)
|
|
|
|
session:notify('vim_'..method, ...)
|
|
|
|
end
|
|
|
|
|
2014-10-08 09:56:01 -07:00
|
|
|
local function buffer(method, ...)
|
|
|
|
return request('buffer_'..method, ...)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function window(method, ...)
|
|
|
|
return request('window_'..method, ...)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function tabpage(method, ...)
|
|
|
|
return request('tabpage_'..method, ...)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function curbuf(method, ...)
|
|
|
|
local buf = nvim('get_current_buffer')
|
|
|
|
if not method then
|
|
|
|
return buf
|
|
|
|
end
|
|
|
|
return buffer(method, buf, ...)
|
|
|
|
end
|
|
|
|
|
2015-02-12 09:29:48 -07:00
|
|
|
local function wait()
|
|
|
|
session:request('vim_eval', '1')
|
|
|
|
end
|
|
|
|
|
2014-10-08 09:56:01 -07:00
|
|
|
local function curbuf_contents()
|
2014-11-21 09:06:03 -07:00
|
|
|
-- Before inspecting the buffer, execute 'vim_eval' to wait until all
|
|
|
|
-- previously sent keys are processed(vim_eval is a deferred function, and
|
|
|
|
-- only processed after all input)
|
2015-02-12 09:29:48 -07:00
|
|
|
wait()
|
2014-10-08 09:56:01 -07:00
|
|
|
return table.concat(curbuf('get_line_slice', 0, -1, true, true), '\n')
|
|
|
|
end
|
|
|
|
|
|
|
|
local function curwin(method, ...)
|
|
|
|
local win = nvim('get_current_window')
|
|
|
|
if not method then
|
|
|
|
return win
|
|
|
|
end
|
|
|
|
return window(method, win, ...)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function curtab(method, ...)
|
|
|
|
local tab = nvim('get_current_tabpage')
|
|
|
|
if not method then
|
|
|
|
return tab
|
|
|
|
end
|
|
|
|
return tabpage(method, tab, ...)
|
|
|
|
end
|
|
|
|
|
2014-11-21 09:06:03 -07:00
|
|
|
local function expect(contents)
|
|
|
|
return eq(dedent(contents), curbuf_contents())
|
|
|
|
end
|
|
|
|
|
2015-07-20 07:51:53 -07:00
|
|
|
local function rmdir(path)
|
|
|
|
if lfs.attributes(path, 'mode') ~= 'directory' then
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
for file in lfs.dir(path) do
|
|
|
|
if file == '.' or file == '..' then
|
|
|
|
goto continue
|
|
|
|
end
|
|
|
|
ret, err = os.remove(path..'/'..file)
|
|
|
|
if not ret then
|
|
|
|
error('os.remove: '..err)
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
::continue::
|
|
|
|
end
|
|
|
|
ret, err = os.remove(path)
|
|
|
|
if not ret then
|
|
|
|
error('os.remove: '..err)
|
|
|
|
end
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
2014-09-29 05:43:52 -07:00
|
|
|
return {
|
|
|
|
clear = clear,
|
2015-03-17 04:45:13 -07:00
|
|
|
spawn = spawn,
|
2014-10-23 14:20:33 -07:00
|
|
|
dedent = dedent,
|
2014-11-11 02:12:19 -07:00
|
|
|
source = source,
|
2014-09-29 05:43:52 -07:00
|
|
|
rawfeed = rawfeed,
|
|
|
|
insert = insert,
|
|
|
|
feed = feed,
|
|
|
|
execute = execute,
|
2014-10-27 14:34:42 -07:00
|
|
|
eval = nvim_eval,
|
2015-08-23 05:16:46 -07:00
|
|
|
call = nvim_call,
|
2014-10-27 14:34:42 -07:00
|
|
|
command = nvim_command,
|
2014-10-08 09:56:01 -07:00
|
|
|
request = request,
|
|
|
|
next_message = next_message,
|
|
|
|
run = run,
|
|
|
|
stop = stop,
|
2014-10-01 05:31:57 -07:00
|
|
|
eq = eq,
|
|
|
|
neq = neq,
|
2014-10-08 09:56:01 -07:00
|
|
|
expect = expect,
|
|
|
|
ok = ok,
|
|
|
|
nvim = nvim,
|
2015-07-27 04:39:38 -07:00
|
|
|
nvim_async = nvim_async,
|
2015-03-17 04:45:13 -07:00
|
|
|
nvim_prog = nvim_prog,
|
2015-02-28 07:39:04 -07:00
|
|
|
nvim_dir = nvim_dir,
|
2014-10-08 09:56:01 -07:00
|
|
|
buffer = buffer,
|
|
|
|
window = window,
|
|
|
|
tabpage = tabpage,
|
|
|
|
curbuf = curbuf,
|
|
|
|
curwin = curwin,
|
|
|
|
curtab = curtab,
|
2015-02-12 09:29:48 -07:00
|
|
|
curbuf_contents = curbuf_contents,
|
2015-04-11 21:48:16 -07:00
|
|
|
wait = wait,
|
2015-06-30 05:10:34 -07:00
|
|
|
set_session = set_session,
|
2015-07-20 07:51:53 -07:00
|
|
|
write_file = write_file,
|
|
|
|
rmdir = rmdir
|
2014-09-29 05:43:52 -07:00
|
|
|
}
|