2014-11-06 19:54:49 -07:00
|
|
|
require('coxpcall')
|
2019-10-19 14:15:07 -07:00
|
|
|
local busted = require('busted')
|
2016-12-14 08:56:00 -07:00
|
|
|
local luv = require('luv')
|
2015-11-17 15:31:22 -07:00
|
|
|
local lfs = require('lfs')
|
2019-08-08 07:03:25 -07:00
|
|
|
local mpack = require('mpack')
|
2016-04-23 16:53:11 -07:00
|
|
|
local global_helpers = require('test.helpers')
|
|
|
|
|
2016-10-22 09:15:46 -07:00
|
|
|
-- nvim client: Found in .deps/usr/share/lua/<version>/nvim/ if "bundled".
|
|
|
|
local Session = require('nvim.session')
|
|
|
|
local TcpStream = require('nvim.tcp_stream')
|
|
|
|
local SocketStream = require('nvim.socket_stream')
|
|
|
|
local ChildProcessStream = require('nvim.child_process_stream')
|
|
|
|
|
2016-11-04 08:20:58 -07:00
|
|
|
local check_cores = global_helpers.check_cores
|
2016-04-23 16:53:11 -07:00
|
|
|
local check_logs = global_helpers.check_logs
|
2018-04-27 01:07:26 -07:00
|
|
|
local dedent = global_helpers.dedent
|
2016-04-23 16:53:11 -07:00
|
|
|
local eq = global_helpers.eq
|
2020-02-18 01:41:29 -07:00
|
|
|
local filter = global_helpers.tbl_filter
|
2019-09-04 06:58:04 -07:00
|
|
|
local is_os = global_helpers.is_os
|
2020-02-18 01:41:29 -07:00
|
|
|
local map = global_helpers.tbl_map
|
2018-04-27 01:07:26 -07:00
|
|
|
local ok = global_helpers.ok
|
|
|
|
local sleep = global_helpers.sleep
|
2019-05-18 07:00:06 -07:00
|
|
|
local tbl_contains = global_helpers.tbl_contains
|
2020-09-21 01:37:28 -07:00
|
|
|
local fail = global_helpers.fail
|
2014-10-08 08:56:28 -07:00
|
|
|
|
2019-08-08 07:03:25 -07:00
|
|
|
local module = {
|
|
|
|
NIL = mpack.NIL,
|
|
|
|
mkdir = lfs.mkdir,
|
|
|
|
}
|
|
|
|
|
2017-01-03 22:35:21 -07:00
|
|
|
local start_dir = lfs.currentdir()
|
2022-09-24 17:20:47 -07:00
|
|
|
local runtime_set = 'set runtimepath^=./build/lib/nvim/'
|
2019-08-08 07:03:25 -07:00
|
|
|
module.nvim_prog = (
|
2019-10-19 18:04:08 -07:00
|
|
|
os.getenv('NVIM_PRG')
|
2018-11-29 15:26:21 -07:00
|
|
|
or global_helpers.test_build_dir .. '/bin/nvim'
|
2017-04-08 15:55:19 -07:00
|
|
|
)
|
2017-02-09 19:39:00 -07:00
|
|
|
-- Default settings for the test session.
|
2019-08-08 07:03:25 -07:00
|
|
|
module.nvim_set = (
|
2019-10-01 07:14:00 -07:00
|
|
|
'set shortmess+=IS background=light noswapfile noautoindent startofline'
|
2019-08-08 07:03:25 -07:00
|
|
|
..' laststatus=1 undodir=. directory=. viewdir=. backupdir=.'
|
2021-08-17 11:12:47 -07:00
|
|
|
..' belloff= wildoptions-=pum joinspaces noshowcmd noruler nomore redrawdebug=invalid')
|
2019-08-08 07:03:25 -07:00
|
|
|
module.nvim_argv = {
|
|
|
|
module.nvim_prog, '-u', 'NONE', '-i', 'NONE',
|
2022-09-24 17:20:47 -07:00
|
|
|
-- XXX: find treesitter parsers.
|
|
|
|
'--cmd', runtime_set,
|
2021-08-15 21:24:59 -07:00
|
|
|
'--cmd', module.nvim_set,
|
2022-05-12 17:28:10 -07:00
|
|
|
'--cmd', 'mapclear',
|
|
|
|
'--cmd', 'mapclear!',
|
2021-08-16 08:31:14 -07:00
|
|
|
'--embed'}
|
|
|
|
|
2017-04-09 16:38:20 -07:00
|
|
|
-- Directory containing nvim.
|
2019-08-08 07:03:25 -07:00
|
|
|
module.nvim_dir = module.nvim_prog:gsub("[/\\][^/\\]+$", "")
|
|
|
|
if module.nvim_dir == module.nvim_prog then
|
|
|
|
module.nvim_dir = "."
|
2015-02-28 07:39:04 -07:00
|
|
|
end
|
|
|
|
|
2019-09-01 21:21:26 -07:00
|
|
|
local iswin = global_helpers.iswin
|
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',
|
2016-06-07 21:24:23 -07:00
|
|
|
'--suppressions=src/.valgrind.supp',
|
2014-11-22 08:29:03 -07:00
|
|
|
'--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
|
2019-08-08 07:03:25 -07:00
|
|
|
for i = 1, #module.nvim_argv do
|
|
|
|
new_nvim_argv[i + len] = module.nvim_argv[i]
|
2014-10-08 08:56:28 -07:00
|
|
|
end
|
2019-08-08 07:03:25 -07:00
|
|
|
module.nvim_argv = new_nvim_argv
|
|
|
|
module.prepend_argv = prepend_argv
|
2014-10-08 08:56:28 -07:00
|
|
|
end
|
|
|
|
|
2019-01-24 11:15:39 -07:00
|
|
|
local session, loop_running, last_error, method_error
|
2014-10-08 08:56:28 -07:00
|
|
|
|
2019-08-08 07:03:25 -07:00
|
|
|
function module.get_session()
|
2017-12-09 03:26:06 -07:00
|
|
|
return session
|
|
|
|
end
|
|
|
|
|
2021-06-12 14:23:05 -07:00
|
|
|
function module.set_session(s)
|
2015-04-11 21:48:16 -07:00
|
|
|
session = s
|
|
|
|
end
|
|
|
|
|
2019-08-08 07:03:25 -07:00
|
|
|
function module.request(method, ...)
|
2014-10-08 08:56:28 -07:00
|
|
|
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
|
|
|
|
|
2019-08-08 07:03:25 -07:00
|
|
|
function module.next_msg(timeout)
|
2018-03-08 16:29:20 -07:00
|
|
|
return session:next_message(timeout and timeout or 10000)
|
2014-10-08 09:56:01 -07:00
|
|
|
end
|
|
|
|
|
2019-08-08 07:03:25 -07:00
|
|
|
function module.expect_twostreams(msgs1, msgs2)
|
2017-06-10 06:25:23 -07:00
|
|
|
local pos1, pos2 = 1, 1
|
|
|
|
while pos1 <= #msgs1 or pos2 <= #msgs2 do
|
2019-08-08 07:03:25 -07:00
|
|
|
local msg = module.next_msg()
|
2017-06-10 06:25:23 -07:00
|
|
|
if pos1 <= #msgs1 and pcall(eq, msgs1[pos1], msg) then
|
|
|
|
pos1 = pos1 + 1
|
|
|
|
elseif pos2 <= #msgs2 then
|
|
|
|
eq(msgs2[pos2], msg)
|
|
|
|
pos2 = pos2 + 1
|
|
|
|
else
|
|
|
|
-- already failed, but show the right error message
|
|
|
|
eq(msgs1[pos1], msg)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-08 16:24:38 -07:00
|
|
|
-- Expects a sequence of next_msg() results. If multiple sequences are
|
2018-02-16 11:42:05 -07:00
|
|
|
-- passed they are tried until one succeeds, in order of shortest to longest.
|
2019-04-12 17:13:29 -07:00
|
|
|
--
|
|
|
|
-- Can be called with positional args (list of sequences only):
|
|
|
|
-- expect_msg_seq(seq1, seq2, ...)
|
|
|
|
-- or keyword args:
|
|
|
|
-- expect_msg_seq{ignore={...}, seqs={seq1, seq2, ...}}
|
|
|
|
--
|
|
|
|
-- ignore: List of ignored event names.
|
|
|
|
-- seqs: List of one or more potential event sequences.
|
2019-08-08 07:03:25 -07:00
|
|
|
function module.expect_msg_seq(...)
|
2018-02-16 11:42:05 -07:00
|
|
|
if select('#', ...) < 1 then
|
|
|
|
error('need at least 1 argument')
|
|
|
|
end
|
2019-04-12 17:13:29 -07:00
|
|
|
local arg1 = select(1, ...)
|
|
|
|
if (arg1['seqs'] and select('#', ...) > 1) or type(arg1) ~= 'table' then
|
|
|
|
error('invalid args')
|
|
|
|
end
|
|
|
|
local ignore = arg1['ignore'] and arg1['ignore'] or {}
|
|
|
|
local seqs = arg1['seqs'] and arg1['seqs'] or {...}
|
|
|
|
if type(ignore) ~= 'table' then
|
|
|
|
error("'ignore' arg must be a list of strings")
|
|
|
|
end
|
2018-02-16 11:42:05 -07:00
|
|
|
table.sort(seqs, function(a, b) -- Sort ascending, by (shallow) length.
|
|
|
|
return #a < #b
|
|
|
|
end)
|
|
|
|
|
|
|
|
local actual_seq = {}
|
2019-04-12 17:13:29 -07:00
|
|
|
local nr_ignored = 0
|
2018-02-16 11:42:05 -07:00
|
|
|
local final_error = ''
|
|
|
|
local function cat_err(err1, err2)
|
|
|
|
if err1 == nil then
|
|
|
|
return err2
|
|
|
|
end
|
|
|
|
return string.format('%s\n%s\n%s', err1, string.rep('=', 78), err2)
|
|
|
|
end
|
2019-08-09 06:32:38 -07:00
|
|
|
local msg_timeout = module.load_adjust(10000) -- Big timeout for ASAN/valgrind.
|
2018-02-16 11:42:05 -07:00
|
|
|
for anum = 1, #seqs do
|
|
|
|
local expected_seq = seqs[anum]
|
|
|
|
-- Collect enough messages to compare the next expected sequence.
|
|
|
|
while #actual_seq < #expected_seq do
|
2019-08-09 06:32:38 -07:00
|
|
|
local msg = module.next_msg(msg_timeout)
|
2019-04-12 17:13:29 -07:00
|
|
|
local msg_type = msg and msg[2] or nil
|
2018-02-16 11:42:05 -07:00
|
|
|
if msg == nil then
|
|
|
|
error(cat_err(final_error,
|
2019-04-12 17:13:29 -07:00
|
|
|
string.format('got %d messages (ignored %d), expected %d',
|
|
|
|
#actual_seq, nr_ignored, #expected_seq)))
|
2019-05-18 07:00:06 -07:00
|
|
|
elseif tbl_contains(ignore, msg_type) then
|
2019-04-12 17:13:29 -07:00
|
|
|
nr_ignored = nr_ignored + 1
|
|
|
|
else
|
|
|
|
table.insert(actual_seq, msg)
|
2018-02-16 11:42:05 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
local status, result = pcall(eq, expected_seq, actual_seq)
|
|
|
|
if status then
|
|
|
|
return result
|
|
|
|
end
|
2018-11-09 07:52:57 -07:00
|
|
|
local message = result
|
|
|
|
if type(result) == "table" then
|
|
|
|
-- 'eq' returns several things
|
|
|
|
message = result.message
|
|
|
|
end
|
|
|
|
final_error = cat_err(final_error, message)
|
2018-02-16 11:42:05 -07:00
|
|
|
end
|
|
|
|
error(final_error)
|
|
|
|
end
|
|
|
|
|
2017-12-09 03:26:06 -07:00
|
|
|
local function call_and_stop_on_error(lsession, ...)
|
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
|
2017-12-09 03:26:06 -07:00
|
|
|
lsession:stop()
|
2014-11-06 19:54:49 -07:00
|
|
|
last_error = result
|
|
|
|
return ''
|
|
|
|
end
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
2019-08-08 07:03:25 -07:00
|
|
|
function module.set_method_error(err)
|
2019-01-24 11:15:39 -07:00
|
|
|
method_error = err
|
|
|
|
end
|
|
|
|
|
2019-08-08 07:03:25 -07:00
|
|
|
function module.run_session(lsession, request_cb, notification_cb, setup_cb, timeout)
|
2014-12-08 18:31:45 -07:00
|
|
|
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)
|
2019-01-24 11:15:39 -07:00
|
|
|
method_error = nil
|
|
|
|
local result = call_and_stop_on_error(lsession, request_cb, method, args)
|
|
|
|
if method_error ~= nil then
|
|
|
|
return method_error, true
|
|
|
|
end
|
|
|
|
return result
|
2014-12-08 18:31:45 -07:00
|
|
|
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)
|
2017-12-09 03:26:06 -07:00
|
|
|
call_and_stop_on_error(lsession, notification_cb, method, args)
|
2014-12-08 18:31:45 -07:00
|
|
|
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()
|
2017-12-09 03:26:06 -07:00
|
|
|
call_and_stop_on_error(lsession, setup_cb)
|
2014-12-08 18:31:45 -07:00
|
|
|
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
|
2022-07-25 01:16:33 -07:00
|
|
|
|
|
|
|
return session.eof_err
|
2014-10-08 09:56:01 -07:00
|
|
|
end
|
|
|
|
|
2019-08-08 07:03:25 -07:00
|
|
|
function module.run(request_cb, notification_cb, setup_cb, timeout)
|
2022-07-25 01:16:33 -07:00
|
|
|
return module.run_session(session, request_cb, notification_cb, setup_cb, timeout)
|
2017-12-09 03:26:06 -07:00
|
|
|
end
|
|
|
|
|
2019-08-08 07:03:25 -07:00
|
|
|
function module.stop()
|
2014-10-08 09:56:01 -07:00
|
|
|
session:stop()
|
|
|
|
end
|
|
|
|
|
2019-08-08 07:03:25 -07:00
|
|
|
function module.nvim_prog_abs()
|
2019-05-02 00:56:22 -07:00
|
|
|
-- system(['build/bin/nvim']) does not work for whatever reason. It must
|
|
|
|
-- be executable searched in $PATH or something starting with / or ./.
|
2019-08-08 07:03:25 -07:00
|
|
|
if module.nvim_prog:match('[/\\]') then
|
|
|
|
return module.request('nvim_call_function', 'fnamemodify', {module.nvim_prog, ':p'})
|
2019-05-02 00:56:22 -07:00
|
|
|
else
|
2019-08-08 07:03:25 -07:00
|
|
|
return module.nvim_prog
|
2019-05-02 00:56:22 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-10-22 15:02:22 -07:00
|
|
|
-- Executes an ex-command. VimL errors manifest as client (lua) errors, but
|
|
|
|
-- v:errmsg will not be updated.
|
2019-08-08 07:03:25 -07:00
|
|
|
function module.command(cmd)
|
|
|
|
module.request('nvim_command', cmd)
|
2014-10-08 08:56:28 -07:00
|
|
|
end
|
|
|
|
|
2022-06-08 14:22:50 -07:00
|
|
|
|
2022-07-19 04:36:29 -07:00
|
|
|
-- Use for commands which expect nvim to quit.
|
|
|
|
-- The first argument can also be a timeout.
|
|
|
|
function module.expect_exit(fn_or_timeout, ...)
|
|
|
|
local eof_err_msg = 'EOF was received from Nvim. Likely the Nvim process crashed.'
|
|
|
|
if type(fn_or_timeout) == 'function' then
|
|
|
|
eq(eof_err_msg, module.pcall_err(fn_or_timeout, ...))
|
|
|
|
else
|
|
|
|
eq(eof_err_msg, module.pcall_err(function(timeout, fn, ...)
|
|
|
|
fn(...)
|
|
|
|
while session:next_message(timeout) do
|
|
|
|
end
|
|
|
|
if session.eof_err then
|
|
|
|
error(session.eof_err[2])
|
|
|
|
end
|
|
|
|
end, fn_or_timeout, ...))
|
|
|
|
end
|
2022-06-08 14:22:50 -07:00
|
|
|
end
|
|
|
|
|
2016-10-22 09:15:46 -07:00
|
|
|
-- Evaluates a VimL expression.
|
|
|
|
-- Fails on VimL error, but does not update v:errmsg.
|
2019-08-08 07:03:25 -07:00
|
|
|
function module.eval(expr)
|
|
|
|
return module.request('nvim_eval', expr)
|
2014-10-08 08:56:28 -07:00
|
|
|
end
|
|
|
|
|
2016-10-22 09:15:46 -07:00
|
|
|
-- Executes a VimL function.
|
|
|
|
-- Fails on VimL error, but does not update v:errmsg.
|
2019-08-08 07:03:25 -07:00
|
|
|
function module.call(name, ...)
|
|
|
|
return module.request('nvim_call_function', name, {...})
|
2015-08-23 05:16:46 -07:00
|
|
|
end
|
|
|
|
|
2016-10-22 09:15:46 -07:00
|
|
|
-- Sends user input to Nvim.
|
|
|
|
-- Does not fail on VimL error, but v:errmsg will be updated.
|
2014-11-21 09:06:03 -07:00
|
|
|
local function nvim_feed(input)
|
|
|
|
while #input > 0 do
|
2019-08-08 07:03:25 -07:00
|
|
|
local written = module.request('nvim_input', input)
|
2019-08-09 16:26:43 -07:00
|
|
|
if written == nil then
|
|
|
|
module.assert_alive()
|
2019-09-01 15:51:02 -07:00
|
|
|
error('crash? (nvim_input returned nil)')
|
2019-08-09 16:26:43 -07:00
|
|
|
end
|
2014-11-21 09:06:03 -07:00
|
|
|
input = input:sub(written + 1)
|
2014-10-08 08:56:28 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-08-08 07:03:25 -07:00
|
|
|
function module.feed(...)
|
2014-09-29 05:43:52 -07:00
|
|
|
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
|
|
|
|
|
2019-08-08 07:03:25 -07:00
|
|
|
function module.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
|
|
|
|
|
2019-08-08 07:03:25 -07:00
|
|
|
function module.merge_args(...)
|
2015-08-15 07:25:10 -07:00
|
|
|
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
|
|
|
|
|
2019-04-16 16:08:48 -07:00
|
|
|
-- Removes Nvim startup args from `args` matching items in `args_rm`.
|
|
|
|
--
|
2022-09-24 17:20:47 -07:00
|
|
|
-- - Special case: "-u", "-i", "--cmd" are treated specially: their "values" are also removed.
|
|
|
|
-- - Special case: "runtimepath" will remove only { '--cmd', 'set runtimepath^=…', }
|
|
|
|
--
|
2019-04-16 16:08:48 -07:00
|
|
|
-- Example:
|
|
|
|
-- args={'--headless', '-u', 'NONE'}
|
|
|
|
-- args_rm={'--cmd', '-u'}
|
|
|
|
-- Result:
|
|
|
|
-- {'--headless'}
|
|
|
|
--
|
2022-09-24 17:20:47 -07:00
|
|
|
-- All matching cases are removed.
|
|
|
|
--
|
2019-04-16 16:08:48 -07:00
|
|
|
-- Example:
|
|
|
|
-- args={'--cmd', 'foo', '-N', '--cmd', 'bar'}
|
|
|
|
-- args_rm={'--cmd', '-u'}
|
|
|
|
-- Result:
|
|
|
|
-- {'-N'}
|
|
|
|
local function remove_args(args, args_rm)
|
|
|
|
local new_args = {}
|
|
|
|
local skip_following = {'-u', '-i', '-c', '--cmd', '-s', '--listen'}
|
|
|
|
if not args_rm or #args_rm == 0 then
|
|
|
|
return {unpack(args)}
|
|
|
|
end
|
|
|
|
for _, v in ipairs(args_rm) do
|
|
|
|
assert(type(v) == 'string')
|
|
|
|
end
|
|
|
|
local last = ''
|
|
|
|
for _, arg in ipairs(args) do
|
2019-05-18 07:00:06 -07:00
|
|
|
if tbl_contains(skip_following, last) then
|
2019-04-16 16:08:48 -07:00
|
|
|
last = ''
|
2019-05-18 07:00:06 -07:00
|
|
|
elseif tbl_contains(args_rm, arg) then
|
2019-04-16 16:08:48 -07:00
|
|
|
last = arg
|
2022-09-24 17:20:47 -07:00
|
|
|
elseif arg == runtime_set and tbl_contains(args_rm, 'runtimepath') then
|
|
|
|
table.remove(new_args) -- Remove the preceding "--cmd".
|
|
|
|
last = ''
|
2019-04-16 16:08:48 -07:00
|
|
|
else
|
|
|
|
table.insert(new_args, arg)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return new_args
|
|
|
|
end
|
|
|
|
|
2022-09-20 13:03:16 -07:00
|
|
|
function module.check_close(old_session)
|
|
|
|
local start_time = luv.now()
|
|
|
|
old_session:close()
|
|
|
|
luv.update_time() -- Update cached value of luv.now() (libuv: uv_now()).
|
|
|
|
local end_time = luv.now()
|
|
|
|
local delta = end_time - start_time
|
|
|
|
if delta > 500 then
|
|
|
|
print("nvim took " .. delta .. " milliseconds to exit after last test\n"..
|
|
|
|
"This indicates a likely problem with the test even if it passed!\n")
|
|
|
|
io.stdout:flush()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-04-22 11:56:31 -07:00
|
|
|
--- @param io_extra used for stdin_fd, see :help ui-option
|
|
|
|
function module.spawn(argv, merge, env, keep, io_extra)
|
2021-06-12 14:23:05 -07:00
|
|
|
if session and not keep then
|
2022-09-20 13:03:16 -07:00
|
|
|
module.check_close(session)
|
2021-06-12 14:23:05 -07:00
|
|
|
end
|
|
|
|
|
2016-06-26 08:16:54 -07:00
|
|
|
local child_stream = ChildProcessStream.spawn(
|
2019-08-08 07:03:25 -07:00
|
|
|
merge and module.merge_args(prepend_argv, argv) or argv,
|
2022-04-22 11:56:31 -07:00
|
|
|
env, io_extra)
|
2016-04-13 05:21:32 -07:00
|
|
|
return Session.new(child_stream)
|
2015-03-17 04:45:13 -07:00
|
|
|
end
|
|
|
|
|
2016-06-26 12:35:14 -07:00
|
|
|
-- Creates a new Session connected by domain socket (named pipe) or TCP.
|
2019-08-08 07:03:25 -07:00
|
|
|
function module.connect(file_or_address)
|
2016-06-26 12:35:14 -07:00
|
|
|
local addr, port = string.match(file_or_address, "(.*):(%d+)")
|
|
|
|
local stream = (addr and port) and TcpStream.open(addr, port) or
|
|
|
|
SocketStream.open(file_or_address)
|
|
|
|
return Session.new(stream)
|
|
|
|
end
|
|
|
|
|
2016-12-14 08:56:00 -07:00
|
|
|
-- Calls fn() until it succeeds, up to `max` times or until `max_ms`
|
|
|
|
-- milliseconds have passed.
|
2019-08-08 07:03:25 -07:00
|
|
|
function module.retry(max, max_ms, fn)
|
2018-11-10 03:12:04 -07:00
|
|
|
assert(max == nil or max > 0)
|
|
|
|
assert(max_ms == nil or max_ms > 0)
|
2016-12-14 08:56:00 -07:00
|
|
|
local tries = 1
|
2018-11-10 03:12:04 -07:00
|
|
|
local timeout = (max_ms and max_ms or 10000)
|
2016-12-14 08:56:00 -07:00
|
|
|
local start_time = luv.now()
|
|
|
|
while true do
|
|
|
|
local status, result = pcall(fn)
|
|
|
|
if status then
|
|
|
|
return result
|
2016-12-07 05:01:03 -07:00
|
|
|
end
|
2017-12-04 14:18:11 -07:00
|
|
|
luv.update_time() -- Update cached value of luv.now() (libuv: uv_now()).
|
2016-12-14 08:56:00 -07:00
|
|
|
if (max and tries >= max) or (luv.now() - start_time > timeout) then
|
2019-10-19 14:15:07 -07:00
|
|
|
busted.fail(string.format("retry() attempts: %d\n%s", tries, tostring(result)), 2)
|
2016-12-14 08:56:00 -07:00
|
|
|
end
|
|
|
|
tries = tries + 1
|
2018-04-20 14:56:50 -07:00
|
|
|
luv.sleep(20) -- Avoid hot loop...
|
2016-12-07 05:01:03 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-29 22:21:45 -07:00
|
|
|
-- Starts a new global Nvim session.
|
2019-04-16 16:08:48 -07:00
|
|
|
--
|
2018-05-29 22:21:45 -07:00
|
|
|
-- Parameters are interpreted as startup args, OR a map with these keys:
|
2019-04-16 16:08:48 -07:00
|
|
|
-- args: List: Args appended to the default `nvim_argv` set.
|
|
|
|
-- args_rm: List: Args removed from the default set. All cases are
|
|
|
|
-- removed, e.g. args_rm={'--cmd'} removes all cases of "--cmd"
|
|
|
|
-- (and its value) from the default set.
|
|
|
|
-- env: Map: Defines the environment of the new session.
|
2018-05-29 22:21:45 -07:00
|
|
|
--
|
|
|
|
-- Example:
|
|
|
|
-- clear('-e')
|
2019-04-16 16:08:48 -07:00
|
|
|
-- clear{args={'-e'}, args_rm={'-i'}, env={TERM=term}}
|
2019-08-08 07:03:25 -07:00
|
|
|
function module.clear(...)
|
2022-04-22 11:56:31 -07:00
|
|
|
local argv, env, io_extra = module.new_argv(...)
|
|
|
|
module.set_session(module.spawn(argv, nil, env, nil, io_extra))
|
2019-08-09 01:23:57 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Builds an argument list for use in clear().
|
|
|
|
--
|
2021-08-22 13:55:28 -07:00
|
|
|
---@see clear() for parameters.
|
2019-08-09 01:23:57 -07:00
|
|
|
function module.new_argv(...)
|
2019-08-08 07:03:25 -07:00
|
|
|
local args = {unpack(module.nvim_argv)}
|
2019-04-16 16:38:59 -07:00
|
|
|
table.insert(args, '--headless')
|
2022-06-01 11:28:14 -07:00
|
|
|
if _G._nvim_test_id then
|
|
|
|
-- Set the server name to the test-id for logging. #8519
|
|
|
|
table.insert(args, '--listen')
|
|
|
|
table.insert(args, _G._nvim_test_id)
|
|
|
|
end
|
2016-06-26 08:16:54 -07:00
|
|
|
local new_args
|
2022-04-22 11:56:31 -07:00
|
|
|
local io_extra
|
2022-06-01 11:28:14 -07:00
|
|
|
local env = nil
|
2016-06-26 08:16:54 -07:00
|
|
|
local opts = select(1, ...)
|
2022-05-23 21:44:15 -07:00
|
|
|
if type(opts) ~= 'table' then
|
|
|
|
new_args = {...}
|
|
|
|
else
|
2019-04-16 16:08:48 -07:00
|
|
|
args = remove_args(args, opts.args_rm)
|
2016-06-26 08:16:54 -07:00
|
|
|
if opts.env then
|
2022-05-23 21:44:15 -07:00
|
|
|
local env_opt = {}
|
2016-06-26 08:16:54 -07:00
|
|
|
for k, v in pairs(opts.env) do
|
|
|
|
assert(type(k) == 'string')
|
|
|
|
assert(type(v) == 'string')
|
2022-05-23 21:44:15 -07:00
|
|
|
env_opt[k] = v
|
2016-06-26 08:16:54 -07:00
|
|
|
end
|
|
|
|
for _, k in ipairs({
|
|
|
|
'HOME',
|
|
|
|
'ASAN_OPTIONS',
|
2019-08-20 17:30:18 -07:00
|
|
|
'TSAN_OPTIONS',
|
|
|
|
'MSAN_OPTIONS',
|
2019-04-16 16:08:48 -07:00
|
|
|
'LD_LIBRARY_PATH',
|
|
|
|
'PATH',
|
2016-06-26 08:16:54 -07:00
|
|
|
'NVIM_LOG_FILE',
|
2016-08-15 20:25:02 -07:00
|
|
|
'NVIM_RPLUGIN_MANIFEST',
|
2019-06-14 04:39:57 -07:00
|
|
|
'GCOV_ERROR_FILE',
|
2020-11-30 16:15:12 -07:00
|
|
|
'XDG_DATA_DIRS',
|
2019-10-06 13:26:54 -07:00
|
|
|
'TMPDIR',
|
2022-01-07 14:37:41 -07:00
|
|
|
'VIMRUNTIME',
|
2016-06-26 08:16:54 -07:00
|
|
|
}) do
|
2022-06-01 11:28:14 -07:00
|
|
|
-- Set these from the environment unless the caller defined them.
|
2022-05-23 21:44:15 -07:00
|
|
|
if not env_opt[k] then
|
|
|
|
env_opt[k] = os.getenv(k)
|
2017-02-02 20:18:16 -07:00
|
|
|
end
|
2016-06-26 08:16:54 -07:00
|
|
|
end
|
2022-06-01 11:28:14 -07:00
|
|
|
env = {}
|
2022-05-23 21:44:15 -07:00
|
|
|
for k, v in pairs(env_opt) do
|
2016-06-26 08:16:54 -07:00
|
|
|
env[#env + 1] = k .. '=' .. v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
new_args = opts.args or {}
|
2022-04-22 11:56:31 -07:00
|
|
|
io_extra = opts.io_extra
|
2016-06-26 08:16:54 -07:00
|
|
|
end
|
|
|
|
for _, arg in ipairs(new_args) do
|
2016-06-23 02:15:08 -07:00
|
|
|
table.insert(args, arg)
|
2015-08-29 08:10:06 -07:00
|
|
|
end
|
2022-04-22 11:56:31 -07:00
|
|
|
return args, env, io_extra
|
2014-11-06 19:54:49 -07:00
|
|
|
end
|
|
|
|
|
2019-08-08 07:03:25 -07:00
|
|
|
function module.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>')
|
2019-08-08 07:03:25 -07:00
|
|
|
module.rawfeed(escaped)
|
2014-12-08 18:31:45 -07:00
|
|
|
end
|
|
|
|
nvim_feed('<ESC>')
|
2014-09-29 05:43:52 -07:00
|
|
|
end
|
|
|
|
|
2016-10-22 09:15:46 -07:00
|
|
|
-- Executes an ex-command by user input. Because nvim_input() is used, VimL
|
|
|
|
-- errors will not manifest as client (lua) errors. Use command() for that.
|
2019-08-08 07:03:25 -07:00
|
|
|
function module.feed_command(...)
|
2014-09-29 05:43:52 -07:00
|
|
|
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
|
|
|
|
|
2022-03-27 10:25:55 -07:00
|
|
|
-- @deprecated use nvim_exec()
|
2019-08-08 07:03:25 -07:00
|
|
|
function module.source(code)
|
2022-03-27 10:25:55 -07:00
|
|
|
module.exec(dedent(code))
|
2014-11-11 02:12:19 -07:00
|
|
|
end
|
|
|
|
|
2019-12-31 12:21:57 -07:00
|
|
|
function module.has_powershell()
|
|
|
|
return module.eval('executable("'..(iswin() and 'powershell' or 'pwsh')..'")') == 1
|
|
|
|
end
|
|
|
|
|
2022-06-22 05:51:52 -07:00
|
|
|
--- Sets Nvim shell to powershell.
|
|
|
|
---
|
|
|
|
--- @param fake (boolean) If true, a fake will be used if powershell is not
|
|
|
|
--- found on the system.
|
|
|
|
--- @returns true if powershell was found on the system, else false.
|
|
|
|
function module.set_shell_powershell(fake)
|
|
|
|
local found = module.has_powershell()
|
|
|
|
if not fake then
|
|
|
|
assert(found)
|
|
|
|
end
|
|
|
|
local shell = found and (iswin() and 'powershell' or 'pwsh') or module.testprg('pwsh-test')
|
2021-07-02 05:15:40 -07:00
|
|
|
local set_encoding = '[Console]::InputEncoding=[Console]::OutputEncoding=[System.Text.Encoding]::UTF8;'
|
|
|
|
local cmd = set_encoding..'Remove-Item -Force '..table.concat(iswin()
|
2019-10-10 21:30:20 -07:00
|
|
|
and {'alias:cat', 'alias:echo', 'alias:sleep'}
|
|
|
|
or {'alias:echo'}, ',')..';'
|
2022-03-27 10:25:55 -07:00
|
|
|
module.exec([[
|
2019-10-10 01:16:02 -07:00
|
|
|
let &shell = ']]..shell..[['
|
2021-07-02 05:15:40 -07:00
|
|
|
set shellquote= shellxquote=
|
2019-10-10 01:16:02 -07:00
|
|
|
let &shellcmdflag = '-NoLogo -NoProfile -ExecutionPolicy RemoteSigned -Command ]]..cmd..[['
|
2021-11-09 07:22:21 -07:00
|
|
|
let &shellpipe = '2>&1 | Out-File -Encoding UTF8 %s; exit $LastExitCode'
|
|
|
|
let &shellredir = '-RedirectStandardOutput %s -NoNewWindow -Wait'
|
2017-01-11 21:08:19 -07:00
|
|
|
]])
|
2022-06-22 05:51:52 -07:00
|
|
|
return found
|
2017-01-11 21:08:19 -07:00
|
|
|
end
|
|
|
|
|
2019-08-08 07:03:25 -07:00
|
|
|
function module.nvim(method, ...)
|
|
|
|
return module.request('nvim_'..method, ...)
|
2014-10-08 09:56:01 -07:00
|
|
|
end
|
|
|
|
|
2016-06-08 02:26:06 -07:00
|
|
|
local function ui(method, ...)
|
2019-08-08 07:03:25 -07:00
|
|
|
return module.request('nvim_ui_'..method, ...)
|
2016-06-08 02:26:06 -07:00
|
|
|
end
|
|
|
|
|
2019-08-08 07:03:25 -07:00
|
|
|
function module.nvim_async(method, ...)
|
2016-06-28 12:45:19 -07:00
|
|
|
session:notify('nvim_'..method, ...)
|
2015-07-27 04:39:38 -07:00
|
|
|
end
|
|
|
|
|
2019-08-08 07:03:25 -07:00
|
|
|
function module.buffer(method, ...)
|
|
|
|
return module.request('nvim_buf_'..method, ...)
|
2014-10-08 09:56:01 -07:00
|
|
|
end
|
|
|
|
|
2019-08-08 07:03:25 -07:00
|
|
|
function module.window(method, ...)
|
|
|
|
return module.request('nvim_win_'..method, ...)
|
2014-10-08 09:56:01 -07:00
|
|
|
end
|
|
|
|
|
2019-08-08 07:03:25 -07:00
|
|
|
function module.tabpage(method, ...)
|
|
|
|
return module.request('nvim_tabpage_'..method, ...)
|
2014-10-08 09:56:01 -07:00
|
|
|
end
|
|
|
|
|
2019-08-08 07:03:25 -07:00
|
|
|
function module.curbuf(method, ...)
|
2014-10-08 09:56:01 -07:00
|
|
|
if not method then
|
2019-08-08 07:03:25 -07:00
|
|
|
return module.nvim('get_current_buf')
|
2014-10-08 09:56:01 -07:00
|
|
|
end
|
2019-08-08 07:03:25 -07:00
|
|
|
return module.buffer(method, 0, ...)
|
2014-10-08 09:56:01 -07:00
|
|
|
end
|
|
|
|
|
2020-10-19 11:17:51 -07:00
|
|
|
function module.poke_eventloop()
|
|
|
|
-- Execute 'nvim_eval' (a deferred function) to
|
|
|
|
-- force at least one main_loop iteration
|
2017-03-13 07:02:37 -07:00
|
|
|
session:request('nvim_eval', '1')
|
2015-02-12 09:29:48 -07:00
|
|
|
end
|
|
|
|
|
2020-01-08 10:32:49 -07:00
|
|
|
function module.buf_lines(bufnr)
|
|
|
|
return module.exec_lua("return vim.api.nvim_buf_get_lines((...), 0, -1, false)", bufnr)
|
|
|
|
end
|
|
|
|
|
2021-08-22 13:55:28 -07:00
|
|
|
---@see buf_lines()
|
2019-08-08 07:03:25 -07:00
|
|
|
function module.curbuf_contents()
|
2020-10-19 11:17:51 -07:00
|
|
|
module.poke_eventloop() -- Before inspecting the buffer, do whatever.
|
2019-08-08 07:03:25 -07:00
|
|
|
return table.concat(module.curbuf('get_lines', 0, -1, true), '\n')
|
2014-10-08 09:56:01 -07:00
|
|
|
end
|
|
|
|
|
2019-08-08 07:03:25 -07:00
|
|
|
function module.curwin(method, ...)
|
2014-10-08 09:56:01 -07:00
|
|
|
if not method then
|
2019-08-08 07:03:25 -07:00
|
|
|
return module.nvim('get_current_win')
|
2014-10-08 09:56:01 -07:00
|
|
|
end
|
2019-08-08 07:03:25 -07:00
|
|
|
return module.window(method, 0, ...)
|
2014-10-08 09:56:01 -07:00
|
|
|
end
|
|
|
|
|
2019-08-08 07:03:25 -07:00
|
|
|
function module.curtab(method, ...)
|
2014-10-08 09:56:01 -07:00
|
|
|
if not method then
|
2019-08-08 07:03:25 -07:00
|
|
|
return module.nvim('get_current_tabpage')
|
2014-10-08 09:56:01 -07:00
|
|
|
end
|
2019-08-08 07:03:25 -07:00
|
|
|
return module.tabpage(method, 0, ...)
|
2014-10-08 09:56:01 -07:00
|
|
|
end
|
|
|
|
|
2019-08-08 07:03:25 -07:00
|
|
|
function module.expect(contents)
|
|
|
|
return eq(dedent(contents), module.curbuf_contents())
|
2014-11-21 09:06:03 -07:00
|
|
|
end
|
|
|
|
|
2019-08-08 07:03:25 -07:00
|
|
|
function module.expect_any(contents)
|
2017-02-28 01:34:02 -07:00
|
|
|
contents = dedent(contents)
|
2019-08-08 07:03:25 -07:00
|
|
|
return ok(nil ~= string.find(module.curbuf_contents(), contents, 1, true))
|
2017-02-28 01:34:02 -07:00
|
|
|
end
|
|
|
|
|
2020-09-21 01:37:28 -07:00
|
|
|
function module.expect_events(expected, received, kind)
|
|
|
|
local inspect = require'vim.inspect'
|
|
|
|
if not pcall(eq, expected, received) then
|
|
|
|
local msg = 'unexpected '..kind..' received.\n\n'
|
|
|
|
|
|
|
|
msg = msg .. 'received events:\n'
|
|
|
|
for _, e in ipairs(received) do
|
|
|
|
msg = msg .. ' ' .. inspect(e) .. ';\n'
|
|
|
|
end
|
|
|
|
msg = msg .. '\nexpected events:\n'
|
|
|
|
for _, e in ipairs(expected) do
|
|
|
|
msg = msg .. ' ' .. inspect(e) .. ';\n'
|
|
|
|
end
|
|
|
|
fail(msg)
|
|
|
|
end
|
|
|
|
return received
|
|
|
|
end
|
|
|
|
|
2019-08-09 16:26:43 -07:00
|
|
|
-- Checks that the Nvim session did not terminate.
|
|
|
|
function module.assert_alive()
|
2019-09-01 15:51:02 -07:00
|
|
|
assert(2 == module.eval('1+1'), 'crash? request failed')
|
2019-08-09 16:26:43 -07:00
|
|
|
end
|
|
|
|
|
2020-01-13 01:19:20 -07:00
|
|
|
-- Asserts that buffer is loaded and visible in the current tabpage.
|
|
|
|
function module.assert_visible(bufnr, visible)
|
|
|
|
assert(type(visible) == 'boolean')
|
|
|
|
eq(visible, module.bufmeths.is_loaded(bufnr))
|
|
|
|
if visible then
|
|
|
|
assert(-1 ~= module.funcs.bufwinnr(bufnr),
|
|
|
|
'expected buffer to be visible in current tabpage: '..tostring(bufnr))
|
|
|
|
else
|
|
|
|
assert(-1 == module.funcs.bufwinnr(bufnr),
|
|
|
|
'expected buffer NOT visible in current tabpage: '..tostring(bufnr))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-18 14:43:32 -07:00
|
|
|
local function do_rmdir(path)
|
2019-09-06 09:19:57 -07:00
|
|
|
local mode, errmsg, errcode = lfs.attributes(path, 'mode')
|
|
|
|
if mode == nil then
|
|
|
|
if errcode == 2 then
|
|
|
|
-- "No such file or directory", don't complain.
|
|
|
|
return
|
|
|
|
end
|
|
|
|
error(string.format('rmdir: %s (%d)', errmsg, errcode))
|
|
|
|
end
|
|
|
|
if mode ~= 'directory' then
|
|
|
|
error(string.format('rmdir: not a directory: %s', path))
|
2015-07-20 07:51:53 -07:00
|
|
|
end
|
|
|
|
for file in lfs.dir(path) do
|
2016-03-06 15:53:55 -07:00
|
|
|
if file ~= '.' and file ~= '..' then
|
2016-06-04 23:51:50 -07:00
|
|
|
local abspath = path..'/'..file
|
|
|
|
if lfs.attributes(abspath, 'mode') == 'directory' then
|
2017-04-10 13:57:49 -07:00
|
|
|
do_rmdir(abspath) -- recurse
|
2016-06-04 23:51:50 -07:00
|
|
|
else
|
|
|
|
local ret, err = os.remove(abspath)
|
|
|
|
if not ret then
|
2017-04-10 13:57:49 -07:00
|
|
|
if not session then
|
2017-04-10 10:12:56 -07:00
|
|
|
error('os.remove: '..err)
|
2017-04-10 13:57:49 -07:00
|
|
|
else
|
|
|
|
-- Try Nvim delete(): it handles `readonly` attribute on Windows,
|
|
|
|
-- and avoids Lua cross-version/platform incompatibilities.
|
2019-08-08 07:03:25 -07:00
|
|
|
if -1 == module.call('delete', abspath) then
|
2019-09-04 06:58:04 -07:00
|
|
|
local hint = (is_os('win')
|
2017-04-10 13:57:49 -07:00
|
|
|
and ' (hint: try :%bwipeout! before rmdir())' or '')
|
|
|
|
error('delete() failed'..hint..': '..abspath)
|
|
|
|
end
|
2017-04-10 10:12:56 -07:00
|
|
|
end
|
2016-06-04 23:51:50 -07:00
|
|
|
end
|
2016-03-06 15:53:55 -07:00
|
|
|
end
|
2015-07-20 07:51:53 -07:00
|
|
|
end
|
|
|
|
end
|
2016-09-19 18:02:48 -07:00
|
|
|
local ret, err = lfs.rmdir(path)
|
2015-07-20 07:51:53 -07:00
|
|
|
if not ret then
|
2016-09-19 18:02:48 -07:00
|
|
|
error('lfs.rmdir('..path..'): '..err)
|
2015-07-20 07:51:53 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-08-08 07:03:25 -07:00
|
|
|
function module.rmdir(path)
|
2016-09-19 18:02:48 -07:00
|
|
|
local ret, _ = pcall(do_rmdir, path)
|
2019-09-04 06:58:04 -07:00
|
|
|
if not ret and is_os('win') then
|
2017-01-03 22:35:21 -07:00
|
|
|
-- Maybe "Permission denied"; try again after changing the nvim
|
|
|
|
-- process to the top-level directory.
|
2019-08-08 07:03:25 -07:00
|
|
|
module.command([[exe 'cd '.fnameescape(']]..start_dir.."')")
|
2017-01-03 22:35:21 -07:00
|
|
|
ret, _ = pcall(do_rmdir, path)
|
|
|
|
end
|
2016-09-18 14:43:32 -07:00
|
|
|
-- During teardown, the nvim process may not exit quickly enough, then rmdir()
|
|
|
|
-- will fail (on Windows).
|
|
|
|
if not ret then -- Try again.
|
|
|
|
sleep(1000)
|
|
|
|
do_rmdir(path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-08-08 07:03:25 -07:00
|
|
|
function module.exc_exec(cmd)
|
|
|
|
module.command(([[
|
2015-09-18 15:53:58 -07:00
|
|
|
try
|
|
|
|
execute "%s"
|
|
|
|
catch
|
|
|
|
let g:__exception = v:exception
|
|
|
|
endtry
|
|
|
|
]]):format(cmd:gsub('\n', '\\n'):gsub('[\\"]', '\\%0')))
|
2019-08-08 07:03:25 -07:00
|
|
|
local ret = module.eval('get(g:, "__exception", 0)')
|
|
|
|
module.command('unlet! g:__exception')
|
2015-09-18 15:53:58 -07:00
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
2019-08-08 07:03:25 -07:00
|
|
|
function module.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
|
|
|
|
|
2016-08-15 16:42:12 -07:00
|
|
|
-- Helper to skip tests. Returns true in Windows systems.
|
2017-01-03 20:10:38 -07:00
|
|
|
-- pending_fn is pending() from busted
|
2019-08-08 07:03:25 -07:00
|
|
|
function module.pending_win32(pending_fn)
|
2019-09-01 21:21:26 -07:00
|
|
|
if iswin() then
|
2017-01-03 20:10:38 -07:00
|
|
|
if pending_fn ~= nil then
|
|
|
|
pending_fn('FIXME: Windows', function() end)
|
2016-08-15 16:42:12 -07:00
|
|
|
end
|
|
|
|
return true
|
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-01-03 20:10:38 -07:00
|
|
|
-- Calls pending() and returns `true` if the system is too slow to
|
|
|
|
-- run fragile or expensive tests. Else returns `false`.
|
2019-08-08 07:03:25 -07:00
|
|
|
function module.skip_fragile(pending_fn, cond)
|
2017-01-03 20:10:38 -07:00
|
|
|
if pending_fn == nil or type(pending_fn) ~= type(function()end) then
|
|
|
|
error("invalid pending_fn")
|
|
|
|
end
|
|
|
|
if cond then
|
|
|
|
pending_fn("skipped (test is fragile on this system)", function() end)
|
|
|
|
return true
|
|
|
|
elseif os.getenv("TEST_SKIP_FRAGILE") then
|
|
|
|
pending_fn("skipped (TEST_SKIP_FRAGILE)", function() end)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2019-08-08 07:03:25 -07:00
|
|
|
module.funcs = module.create_callindex(module.call)
|
|
|
|
module.meths = module.create_callindex(module.nvim)
|
2019-09-08 05:57:45 -07:00
|
|
|
module.async_meths = module.create_callindex(module.nvim_async)
|
2019-08-08 07:03:25 -07:00
|
|
|
module.uimeths = module.create_callindex(ui)
|
|
|
|
module.bufmeths = module.create_callindex(module.buffer)
|
|
|
|
module.winmeths = module.create_callindex(module.window)
|
|
|
|
module.tabmeths = module.create_callindex(module.tabpage)
|
|
|
|
module.curbufmeths = module.create_callindex(module.curbuf)
|
|
|
|
module.curwinmeths = module.create_callindex(module.curwin)
|
|
|
|
module.curtabmeths = module.create_callindex(module.curtab)
|
2015-09-26 16:49:48 -07:00
|
|
|
|
2020-09-08 00:47:10 -07:00
|
|
|
function module.exec(code)
|
|
|
|
return module.meths.exec(code, false)
|
|
|
|
end
|
|
|
|
|
|
|
|
function module.exec_capture(code)
|
|
|
|
return module.meths.exec(code, true)
|
|
|
|
end
|
|
|
|
|
2019-08-08 07:03:25 -07:00
|
|
|
function module.exec_lua(code, ...)
|
2019-12-02 01:46:46 -07:00
|
|
|
return module.meths.exec_lua(code, {...})
|
2019-06-23 11:10:28 -07:00
|
|
|
end
|
|
|
|
|
2019-08-08 07:03:25 -07:00
|
|
|
function module.get_pathsep()
|
2019-09-01 21:21:26 -07:00
|
|
|
return iswin() and '\\' or '/'
|
2016-11-05 11:34:22 -07:00
|
|
|
end
|
|
|
|
|
2022-06-22 05:51:52 -07:00
|
|
|
--- Gets the filesystem root dir, namely "/" or "C:/".
|
2019-08-08 07:03:25 -07:00
|
|
|
function module.pathroot()
|
2018-01-24 03:01:14 -07:00
|
|
|
local pathsep = package.config:sub(1,1)
|
2019-09-01 21:21:26 -07:00
|
|
|
return iswin() and (module.nvim_dir:sub(1,2)..pathsep) or '/'
|
2018-01-24 03:01:14 -07:00
|
|
|
end
|
|
|
|
|
2022-06-22 05:51:52 -07:00
|
|
|
--- Gets the full `…/build/bin/{name}` path of a test program produced by
|
|
|
|
--- `test/functional/fixtures/CMakeLists.txt`.
|
|
|
|
---
|
|
|
|
--- @param name (string) Name of the test program.
|
|
|
|
function module.testprg(name)
|
|
|
|
local ext = module.iswin() and '.exe' or ''
|
|
|
|
return ('%s/%s%s'):format(module.nvim_dir, name, ext)
|
|
|
|
end
|
|
|
|
|
2022-05-03 06:08:35 -07:00
|
|
|
-- Returns a valid, platform-independent Nvim listen address.
|
2017-11-26 15:15:17 -07:00
|
|
|
-- Useful for communicating with child instances.
|
2019-08-08 07:03:25 -07:00
|
|
|
function module.new_pipename()
|
2017-11-26 15:15:17 -07:00
|
|
|
-- HACK: Start a server temporarily, get the name, then stop it.
|
2019-08-08 07:03:25 -07:00
|
|
|
local pipename = module.eval('serverstart()')
|
|
|
|
module.funcs.serverstop(pipename)
|
2017-11-26 15:15:17 -07:00
|
|
|
return pipename
|
|
|
|
end
|
|
|
|
|
2019-08-08 07:03:25 -07:00
|
|
|
function module.missing_provider(provider)
|
2020-09-05 15:02:46 -07:00
|
|
|
if provider == 'ruby' or provider == 'node' or provider == 'perl' then
|
|
|
|
local e = module.funcs['provider#'..provider..'#Detect']()[2]
|
|
|
|
return e ~= '' and e or false
|
2017-05-12 10:03:05 -07:00
|
|
|
elseif provider == 'python' or provider == 'python3' then
|
|
|
|
local py_major_version = (provider == 'python3' and 3 or 2)
|
2020-09-05 15:02:46 -07:00
|
|
|
local e = module.funcs['provider#pythonx#Detect'](py_major_version)[2]
|
|
|
|
return e ~= '' and e or false
|
2017-05-12 10:03:05 -07:00
|
|
|
else
|
2020-09-05 15:02:46 -07:00
|
|
|
assert(false, 'Unknown provider: '..provider)
|
2017-05-12 10:03:05 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-08-08 07:03:25 -07:00
|
|
|
function module.alter_slashes(obj)
|
2019-09-01 21:21:26 -07:00
|
|
|
if not iswin() then
|
2017-05-22 14:46:57 -07:00
|
|
|
return obj
|
|
|
|
end
|
|
|
|
if type(obj) == 'string' then
|
|
|
|
local ret = obj:gsub('/', '\\')
|
|
|
|
return ret
|
|
|
|
elseif type(obj) == 'table' then
|
|
|
|
local ret = {}
|
|
|
|
for k, v in pairs(obj) do
|
2019-08-08 07:03:25 -07:00
|
|
|
ret[k] = module.alter_slashes(v)
|
2017-05-22 14:46:57 -07:00
|
|
|
end
|
|
|
|
return ret
|
|
|
|
else
|
2020-01-26 15:26:01 -07:00
|
|
|
assert(false, 'expected string or table of strings, got '..type(obj))
|
2017-05-22 14:46:57 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-08-09 06:32:38 -07:00
|
|
|
local load_factor = 1
|
|
|
|
if global_helpers.isCI() then
|
|
|
|
-- Compute load factor only once (but outside of any tests).
|
|
|
|
module.clear()
|
|
|
|
module.request('nvim_command', 'source src/nvim/testdir/load.vim')
|
|
|
|
load_factor = module.request('nvim_eval', 'g:test_load_factor')
|
|
|
|
end
|
2019-08-08 07:03:25 -07:00
|
|
|
function module.load_adjust(num)
|
2019-01-16 13:21:10 -07:00
|
|
|
return math.ceil(num * load_factor)
|
|
|
|
end
|
|
|
|
|
2019-08-08 07:03:25 -07:00
|
|
|
function module.parse_context(ctx)
|
2019-07-17 13:00:50 -07:00
|
|
|
local parsed = {}
|
2019-09-14 18:52:16 -07:00
|
|
|
for _, item in ipairs({'regs', 'jumps', 'bufs', 'gvars'}) do
|
2019-07-17 13:00:50 -07:00
|
|
|
parsed[item] = filter(function(v)
|
|
|
|
return type(v) == 'table'
|
2019-08-08 07:03:25 -07:00
|
|
|
end, module.call('msgpackparse', ctx[item]))
|
2019-07-17 13:00:50 -07:00
|
|
|
end
|
2019-09-14 18:52:16 -07:00
|
|
|
parsed['bufs'] = parsed['bufs'][1]
|
2019-07-17 13:00:50 -07:00
|
|
|
return map(function(v)
|
|
|
|
if #v == 0 then
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
return v
|
|
|
|
end, parsed)
|
|
|
|
end
|
|
|
|
|
2019-08-28 13:47:54 -07:00
|
|
|
function module.add_builddir_to_rtp()
|
|
|
|
-- Add runtime from build dir for doc/tags (used with :help).
|
|
|
|
module.command(string.format([[set rtp+=%s/runtime]], module.test_build_dir))
|
|
|
|
end
|
|
|
|
|
2019-09-11 18:26:35 -07:00
|
|
|
-- Kill process with given pid
|
|
|
|
function module.os_kill(pid)
|
|
|
|
return os.execute((iswin()
|
|
|
|
and 'taskkill /f /t /pid '..pid..' > nul'
|
|
|
|
or 'kill -9 '..pid..' > /dev/null'))
|
|
|
|
end
|
|
|
|
|
2021-06-12 10:09:57 -07:00
|
|
|
-- Create folder with non existing parents
|
2021-05-31 04:35:13 -07:00
|
|
|
function module.mkdir_p(path)
|
2021-06-12 10:09:57 -07:00
|
|
|
return os.execute((iswin()
|
|
|
|
and 'mkdir '..path
|
|
|
|
or 'mkdir -p '..path))
|
2021-05-31 04:35:13 -07:00
|
|
|
end
|
|
|
|
|
2019-01-17 16:44:35 -07:00
|
|
|
module = global_helpers.tbl_extend('error', module, global_helpers)
|
2017-01-22 06:13:10 -07:00
|
|
|
|
2016-04-23 16:53:11 -07:00
|
|
|
return function(after_each)
|
|
|
|
if after_each then
|
2016-11-04 08:20:58 -07:00
|
|
|
after_each(function()
|
|
|
|
check_logs()
|
|
|
|
check_cores('build/bin/nvim')
|
2018-12-01 08:44:36 -07:00
|
|
|
if session then
|
|
|
|
local msg = session:next_message(0)
|
|
|
|
if msg then
|
|
|
|
if msg[1] == "notification" and msg[2] == "nvim_error_event" then
|
|
|
|
error(msg[3][2])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-11-04 08:20:58 -07:00
|
|
|
end)
|
2016-04-23 16:53:11 -07:00
|
|
|
end
|
2017-01-28 14:52:48 -07:00
|
|
|
return module
|
2016-04-23 16:53:11 -07:00
|
|
|
end
|