2014-11-06 19:54:49 -07:00
|
|
|
require('coxpcall')
|
2015-11-17 15:31:22 -07:00
|
|
|
local lfs = require('lfs')
|
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-10-23 05:20:38 -07:00
|
|
|
'--cmd', 'set shortmess+=I background=light noswapfile noautoindent laststatus=1 encoding=utf-8 undodir=. directory=. viewdir=. backupdir=.',
|
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
|
2015-04-26 05:31:39 -07:00
|
|
|
local new_nvim_argv = {}
|
2014-11-22 08:29:03 -07:00
|
|
|
local len = #prepend_argv
|
2015-04-26 05:31:39 -07:00
|
|
|
for i = 1, len do
|
|
|
|
new_nvim_argv[i] = prepend_argv[i]
|
|
|
|
end
|
2014-10-08 08:56:28 -07:00
|
|
|
for i = 1, #nvim_argv do
|
2015-04-26 05:31:39 -07:00
|
|
|
new_nvim_argv[i + len] = nvim_argv[i]
|
2014-10-08 08:56:28 -07:00
|
|
|
end
|
2015-04-26 05:31:39 -07:00
|
|
|
nvim_argv = new_nvim_argv
|
2014-10-08 08:56:28 -07:00
|
|
|
end
|
|
|
|
|
2015-11-17 15:31:22 -07:00
|
|
|
local session, loop_running, 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(...)
|
2015-11-17 15:31:22 -07:00
|
|
|
local status, result = copcall(...) -- luacheck: ignore
|
2014-11-06 19:54:49 -07:00
|
|
|
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-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()
|
|
|
|
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-08-15 07:25:10 -07:00
|
|
|
local function merge_args(...)
|
|
|
|
local i = 1
|
|
|
|
local argv = {}
|
|
|
|
for anum = 1,select('#', ...) do
|
|
|
|
local args = select(anum, ...)
|
|
|
|
if args then
|
|
|
|
for _, arg in ipairs(args) do
|
|
|
|
argv[i] = arg
|
|
|
|
i = i + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return argv
|
|
|
|
end
|
|
|
|
|
|
|
|
local function spawn(argv, merge)
|
2015-03-17 04:45:13 -07:00
|
|
|
local loop = Loop.new()
|
|
|
|
local msgpack_stream = MsgpackStream.new(loop)
|
|
|
|
local async_session = AsyncSession.new(msgpack_stream)
|
2015-11-17 15:31:22 -07:00
|
|
|
local sess = Session.new(async_session)
|
2015-08-15 07:25:10 -07:00
|
|
|
loop:spawn(merge and merge_args(prepend_argv, argv) or argv)
|
2015-11-17 15:31:22 -07:00
|
|
|
return sess
|
2015-03-17 04:45:13 -07:00
|
|
|
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.
|
2015-07-25 15:52:50 -07:00
|
|
|
local function write_file(name, text, dont_dedent)
|
2015-06-30 05:10:34 -07:00
|
|
|
local file = io.open(name, 'w')
|
2015-07-25 15:52:50 -07:00
|
|
|
if not dont_dedent then
|
|
|
|
text = dedent(text)
|
|
|
|
end
|
|
|
|
file:write(text)
|
2015-06-30 05:10:34 -07:00
|
|
|
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-12-29 12:23:51 -07:00
|
|
|
local function os_is_windows()
|
|
|
|
return nvim_eval('has("win32")') == 1
|
|
|
|
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
|
2015-11-17 15:31:22 -07:00
|
|
|
local ret, err = os.remove(path..'/'..file)
|
2015-07-20 07:51:53 -07:00
|
|
|
if not ret then
|
|
|
|
error('os.remove: '..err)
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
::continue::
|
|
|
|
end
|
2015-11-17 15:31:22 -07:00
|
|
|
local ret, err = os.remove(path)
|
2015-07-20 07:51:53 -07:00
|
|
|
if not ret then
|
|
|
|
error('os.remove: '..err)
|
|
|
|
end
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
2015-09-18 15:53:58 -07:00
|
|
|
local exc_exec = function(cmd)
|
|
|
|
nvim_command(([[
|
|
|
|
try
|
|
|
|
execute "%s"
|
|
|
|
catch
|
|
|
|
let g:__exception = v:exception
|
|
|
|
endtry
|
|
|
|
]]):format(cmd:gsub('\n', '\\n'):gsub('[\\"]', '\\%0')))
|
|
|
|
local ret = nvim_eval('get(g:, "__exception", 0)')
|
|
|
|
nvim_command('unlet! g:__exception')
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
2015-09-26 16:49:48 -07:00
|
|
|
local function redir_exec(cmd)
|
|
|
|
nvim_command(([[
|
|
|
|
redir => g:__output
|
|
|
|
silent! execute "%s"
|
|
|
|
redir END
|
|
|
|
]]):format(cmd:gsub('\n', '\\n'):gsub('[\\"]', '\\%0')))
|
|
|
|
local ret = nvim_eval('get(g:, "__output", 0)')
|
|
|
|
nvim_command('unlet! g:__output')
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
|
|
|
local function create_callindex(func)
|
2015-11-17 15:31:22 -07:00
|
|
|
local table = {}
|
|
|
|
setmetatable(table, {
|
2015-09-26 16:49:48 -07:00
|
|
|
__index = function(tbl, arg1)
|
2015-11-17 15:31:22 -07:00
|
|
|
local ret = function(...) return func(arg1, ...) end
|
2015-09-26 16:49:48 -07:00
|
|
|
tbl[arg1] = ret
|
|
|
|
return ret
|
|
|
|
end,
|
|
|
|
})
|
2015-11-17 15:31:22 -07:00
|
|
|
return table
|
2015-09-26 16:49:48 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
local funcs = create_callindex(nvim_call)
|
|
|
|
local meths = create_callindex(nvim)
|
|
|
|
local bufmeths = create_callindex(buffer)
|
|
|
|
local winmeths = create_callindex(window)
|
|
|
|
local tabmeths = create_callindex(tabpage)
|
|
|
|
local curbufmeths = create_callindex(curbuf)
|
|
|
|
local curwinmeths = create_callindex(curwin)
|
|
|
|
local curtabmeths = create_callindex(curtab)
|
|
|
|
|
2014-09-29 05:43:52 -07:00
|
|
|
return {
|
2015-04-26 05:31:39 -07:00
|
|
|
prepend_argv = prepend_argv,
|
2014-09-29 05:43:52 -07:00
|
|
|
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,
|
2015-12-29 12:23:51 -07:00
|
|
|
os_is_windows = os_is_windows,
|
2015-09-18 15:53:58 -07:00
|
|
|
rmdir = rmdir,
|
2015-09-18 17:17:06 -07:00
|
|
|
mkdir = lfs.mkdir,
|
2015-09-18 15:53:58 -07:00
|
|
|
exc_exec = exc_exec,
|
2015-09-26 16:49:48 -07:00
|
|
|
redir_exec = redir_exec,
|
2015-08-15 07:25:10 -07:00
|
|
|
merge_args = merge_args,
|
2015-09-26 16:49:48 -07:00
|
|
|
funcs = funcs,
|
|
|
|
meths = meths,
|
|
|
|
bufmeths = bufmeths,
|
|
|
|
winmeths = winmeths,
|
|
|
|
tabmeths = tabmeths,
|
|
|
|
curbufmeths = curbufmeths,
|
|
|
|
curwinmeths = curwinmeths,
|
|
|
|
curtabmeths = curtabmeths,
|
2014-09-29 05:43:52 -07:00
|
|
|
}
|