From 7035125b2b26aa68fcfb7cda39377ac79926a0f9 Mon Sep 17 00:00:00 2001 From: dundargoc Date: Mon, 8 Apr 2024 11:03:20 +0200 Subject: test: improve test conventions Work on https://github.com/neovim/neovim/issues/27004. --- test/functional/plugin/ccomplete_spec.lua | 14 +- test/functional/plugin/cfilter_spec.lua | 10 +- test/functional/plugin/editorconfig_spec.lua | 30 +-- test/functional/plugin/health_spec.lua | 32 +-- test/functional/plugin/lsp/codelens_spec.lua | 10 +- test/functional/plugin/lsp/completion_spec.lua | 8 +- test/functional/plugin/lsp/diagnostic_spec.lua | 14 +- test/functional/plugin/lsp/handler_spec.lua | 10 +- test/functional/plugin/lsp/helpers.lua | 219 --------------------- .../plugin/lsp/incremental_sync_spec.lua | 12 +- test/functional/plugin/lsp/inlay_hint_spec.lua | 18 +- .../functional/plugin/lsp/semantic_tokens_spec.lua | 28 +-- test/functional/plugin/lsp/snippet_spec.lua | 10 +- test/functional/plugin/lsp/testutil.lua | 219 +++++++++++++++++++++ test/functional/plugin/lsp/utils_spec.lua | 12 +- test/functional/plugin/lsp_spec.lua | 72 +++---- test/functional/plugin/man_spec.lua | 24 +-- test/functional/plugin/matchparen_spec.lua | 12 +- test/functional/plugin/msgpack_spec.lua | 16 +- test/functional/plugin/shada_spec.lua | 16 +- test/functional/plugin/tohtml_spec.lua | 16 +- test/functional/plugin/tutor_spec.lua | 10 +- test/functional/plugin/vim_syntax_spec.lua | 10 +- 23 files changed, 411 insertions(+), 411 deletions(-) delete mode 100644 test/functional/plugin/lsp/helpers.lua create mode 100644 test/functional/plugin/lsp/testutil.lua (limited to 'test/functional/plugin') diff --git a/test/functional/plugin/ccomplete_spec.lua b/test/functional/plugin/ccomplete_spec.lua index 903f16fc73..2133f28a2e 100644 --- a/test/functional/plugin/ccomplete_spec.lua +++ b/test/functional/plugin/ccomplete_spec.lua @@ -1,10 +1,10 @@ -local helpers = require('test.functional.helpers')(after_each) -local clear = helpers.clear -local command = helpers.command -local eq = helpers.eq -local eval = helpers.eval -local feed = helpers.feed -local write_file = helpers.write_file +local t = require('test.functional.testutil')(after_each) +local clear = t.clear +local command = t.command +local eq = t.eq +local eval = t.eval +local feed = t.feed +local write_file = t.write_file describe('ccomplete#Complete', function() setup(function() diff --git a/test/functional/plugin/cfilter_spec.lua b/test/functional/plugin/cfilter_spec.lua index 37261d59df..917e666ab0 100644 --- a/test/functional/plugin/cfilter_spec.lua +++ b/test/functional/plugin/cfilter_spec.lua @@ -1,8 +1,8 @@ -local helpers = require('test.functional.helpers')(after_each) -local clear = helpers.clear -local command = helpers.command -local eq = helpers.eq -local fn = helpers.fn +local t = require('test.functional.testutil')(after_each) +local clear = t.clear +local command = t.command +local eq = t.eq +local fn = t.fn describe('cfilter.lua', function() before_each(function() diff --git a/test/functional/plugin/editorconfig_spec.lua b/test/functional/plugin/editorconfig_spec.lua index c8151d2005..cd524373da 100644 --- a/test/functional/plugin/editorconfig_spec.lua +++ b/test/functional/plugin/editorconfig_spec.lua @@ -1,11 +1,11 @@ -local helpers = require('test.functional.helpers')(after_each) -local clear = helpers.clear -local command = helpers.command -local eq = helpers.eq -local pathsep = helpers.get_pathsep() -local fn = helpers.fn -local api = helpers.api -local exec_lua = helpers.exec_lua +local t = require('test.functional.testutil')(after_each) +local clear = t.clear +local command = t.command +local eq = t.eq +local pathsep = t.get_pathsep() +local fn = t.fn +local api = t.api +local exec_lua = t.exec_lua local testdir = 'Xtest-editorconfig' @@ -20,8 +20,8 @@ local function test_case(name, expected) end setup(function() - helpers.mkdir_p(testdir) - helpers.write_file( + t.mkdir_p(testdir) + t.write_file( testdir .. pathsep .. '.editorconfig', [[ root = true @@ -96,7 +96,7 @@ setup(function() end) teardown(function() - helpers.rmdir(testdir) + t.rmdir(testdir) end) describe('editorconfig', function() @@ -178,18 +178,18 @@ But not this one -- luacheck: pop local trimmed = untrimmed:gsub('%s+\n', '\n') - helpers.write_file(filename, untrimmed) + t.write_file(filename, untrimmed) command('edit ' .. filename) command('write') command('bdelete') - eq(trimmed, helpers.read_file(filename)) + eq(trimmed, t.read_file(filename)) filename = testdir .. pathsep .. 'no_trim.txt' - helpers.write_file(filename, untrimmed) + t.write_file(filename, untrimmed) command('edit ' .. filename) command('write') command('bdelete') - eq(untrimmed, helpers.read_file(filename)) + eq(untrimmed, t.read_file(filename)) end) it('sets textwidth', function() diff --git a/test/functional/plugin/health_spec.lua b/test/functional/plugin/health_spec.lua index 8416f0e5f7..3fe9102886 100644 --- a/test/functional/plugin/health_spec.lua +++ b/test/functional/plugin/health_spec.lua @@ -1,15 +1,15 @@ -local helpers = require('test.functional.helpers')(after_each) +local t = require('test.functional.testutil')(after_each) local Screen = require('test.functional.ui.screen') -local clear = helpers.clear -local curbuf_contents = helpers.curbuf_contents -local command = helpers.command -local eq, neq, matches = helpers.eq, helpers.neq, helpers.matches -local getcompletion = helpers.fn.getcompletion -local insert = helpers.insert -local source = helpers.source -local fn = helpers.fn -local api = helpers.api +local clear = t.clear +local curbuf_contents = t.curbuf_contents +local command = t.command +local eq, neq, matches = t.eq, t.neq, t.matches +local getcompletion = t.fn.getcompletion +local insert = t.insert +local source = t.source +local fn = t.fn +local api = t.api describe(':checkhealth', function() it('detects invalid $VIMRUNTIME', function() @@ -53,7 +53,7 @@ describe('health.vim', function() describe(':checkhealth', function() it('functions report_*() render correctly', function() command('checkhealth full_render') - helpers.expect([[ + t.expect([[ ============================================================================== test_plug.full_render: require("test_plug.full_render.health").check() @@ -76,7 +76,7 @@ describe('health.vim', function() it('concatenates multiple reports', function() command('checkhealth success1 success2 test_plug') - helpers.expect([[ + t.expect([[ ============================================================================== test_plug: require("test_plug.health").check() @@ -106,7 +106,7 @@ describe('health.vim', function() it('lua plugins submodules', function() command('checkhealth test_plug.submodule') - helpers.expect([[ + t.expect([[ ============================================================================== test_plug.submodule: require("test_plug.submodule.health").check() @@ -121,7 +121,7 @@ describe('health.vim', function() it('... including empty reports', function() command('checkhealth test_plug.submodule_empty') - helpers.expect([[ + t.expect([[ ============================================================================== test_plug.submodule_empty: require("test_plug.submodule_empty.health").check() @@ -180,7 +180,7 @@ describe('health.vim', function() it('gracefully handles invalid healthcheck', function() command('checkhealth non_existent_healthcheck') -- luacheck: ignore 613 - helpers.expect([[ + t.expect([[ ============================================================================== non_existent_healthcheck: @@ -192,7 +192,7 @@ describe('health.vim', function() it('does not use vim.health as a healtcheck', function() -- vim.health is not a healthcheck command('checkhealth vim') - helpers.expect([[ + t.expect([[ ERROR: No healthchecks found.]]) end) end) diff --git a/test/functional/plugin/lsp/codelens_spec.lua b/test/functional/plugin/lsp/codelens_spec.lua index 29daf7a066..a618854e2f 100644 --- a/test/functional/plugin/lsp/codelens_spec.lua +++ b/test/functional/plugin/lsp/codelens_spec.lua @@ -1,14 +1,14 @@ -local helpers = require('test.functional.helpers')(after_each) +local t = require('test.functional.testutil')(after_each) -local exec_lua = helpers.exec_lua -local eq = helpers.eq +local exec_lua = t.exec_lua +local eq = t.eq describe('vim.lsp.codelens', function() before_each(function() - helpers.clear() + t.clear() exec_lua('require("vim.lsp")') end) - after_each(helpers.clear) + after_each(t.clear) it('on_codelens_stores_and_displays_lenses', function() local fake_uri = 'file:///fake/uri' diff --git a/test/functional/plugin/lsp/completion_spec.lua b/test/functional/plugin/lsp/completion_spec.lua index 655eb76be6..fd29a11ead 100644 --- a/test/functional/plugin/lsp/completion_spec.lua +++ b/test/functional/plugin/lsp/completion_spec.lua @@ -1,7 +1,7 @@ ---@diagnostic disable: no-unknown -local helpers = require('test.functional.helpers')(after_each) -local eq = helpers.eq -local exec_lua = helpers.exec_lua +local t = require('test.functional.testutil')(after_each) +local eq = t.eq +local exec_lua = t.exec_lua --- Convert completion results. --- @@ -41,7 +41,7 @@ local function complete(line, candidates, lnum) end describe('vim.lsp._completion', function() - before_each(helpers.clear) + before_each(t.clear) -- https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_completion it('prefers textEdit over label as word', function() diff --git a/test/functional/plugin/lsp/diagnostic_spec.lua b/test/functional/plugin/lsp/diagnostic_spec.lua index 72531db021..ffff677ad6 100644 --- a/test/functional/plugin/lsp/diagnostic_spec.lua +++ b/test/functional/plugin/lsp/diagnostic_spec.lua @@ -1,12 +1,12 @@ -local helpers = require('test.functional.helpers')(after_each) -local lsp_helpers = require('test.functional.plugin.lsp.helpers') +local t = require('test.functional.testutil')(after_each) +local t_lsp = require('test.functional.plugin.lsp.testutil') -local clear = helpers.clear -local exec_lua = helpers.exec_lua -local eq = helpers.eq -local neq = require('test.helpers').neq +local clear = t.clear +local exec_lua = t.exec_lua +local eq = t.eq +local neq = t.neq -local create_server_definition = lsp_helpers.create_server_definition +local create_server_definition = t_lsp.create_server_definition describe('vim.lsp.diagnostic', function() local fake_uri diff --git a/test/functional/plugin/lsp/handler_spec.lua b/test/functional/plugin/lsp/handler_spec.lua index 56e29e7337..04e4fc8b8e 100644 --- a/test/functional/plugin/lsp/handler_spec.lua +++ b/test/functional/plugin/lsp/handler_spec.lua @@ -1,9 +1,9 @@ -local helpers = require('test.functional.helpers')(after_each) +local t = require('test.functional.testutil')(after_each) -local eq = helpers.eq -local exec_lua = helpers.exec_lua -local pcall_err = helpers.pcall_err -local matches = helpers.matches +local eq = t.eq +local exec_lua = t.exec_lua +local pcall_err = t.pcall_err +local matches = t.matches describe('lsp-handlers', function() describe('vim.lsp._with_extend', function() diff --git a/test/functional/plugin/lsp/helpers.lua b/test/functional/plugin/lsp/helpers.lua deleted file mode 100644 index fb93746480..0000000000 --- a/test/functional/plugin/lsp/helpers.lua +++ /dev/null @@ -1,219 +0,0 @@ -local helpers = require('test.functional.helpers')(nil) - -local clear = helpers.clear -local exec_lua = helpers.exec_lua -local run = helpers.run -local stop = helpers.stop -local api = helpers.api -local NIL = vim.NIL - -local M = {} - -function M.clear_notrace() - -- problem: here be dragons - -- solution: don't look too closely for dragons - clear { - env = { - NVIM_LUA_NOTRACK = '1', - NVIM_APPNAME = 'nvim_lsp_test', - VIMRUNTIME = os.getenv 'VIMRUNTIME', - }, - } -end - -M.create_server_definition = [[ - function _create_server(opts) - opts = opts or {} - local server = {} - server.messages = {} - - function server.cmd(dispatchers) - local closing = false - local handlers = opts.handlers or {} - local srv = {} - - function srv.request(method, params, callback) - table.insert(server.messages, { - method = method, - params = params, - }) - local handler = handlers[method] - if handler then - local response, err = handler(method, params) - callback(err, response) - elseif method == 'initialize' then - callback(nil, { - capabilities = opts.capabilities or {} - }) - elseif method == 'shutdown' then - callback(nil, nil) - end - local request_id = #server.messages - return true, request_id - end - - function srv.notify(method, params) - table.insert(server.messages, { - method = method, - params = params - }) - if method == 'exit' then - dispatchers.on_exit(0, 15) - end - end - - function srv.is_closing() - return closing - end - - function srv.terminate() - closing = true - end - - return srv - end - - return server - end -]] - --- Fake LSP server. -M.fake_lsp_code = 'test/functional/fixtures/fake-lsp-server.lua' -M.fake_lsp_logfile = 'Xtest-fake-lsp.log' - -local function fake_lsp_server_setup(test_name, timeout_ms, options, settings) - exec_lua( - [=[ - lsp = require('vim.lsp') - local test_name, fake_lsp_code, fake_lsp_logfile, timeout, options, settings = ... - TEST_RPC_CLIENT_ID = lsp.start_client { - cmd_env = { - NVIM_LOG_FILE = fake_lsp_logfile; - NVIM_LUA_NOTRACK = "1"; - NVIM_APPNAME = "nvim_lsp_test"; - }; - cmd = { - vim.v.progpath, '-l', fake_lsp_code, test_name, tostring(timeout), - }; - handlers = setmetatable({}, { - __index = function(t, method) - return function(...) - return vim.rpcrequest(1, 'handler', ...) - end - end; - }); - workspace_folders = {{ - uri = 'file://' .. vim.uv.cwd(), - name = 'test_folder', - }}; - before_init = function(params, config) - vim.schedule(function() - vim.rpcrequest(1, "setup") - end) - end, - on_init = function(client, result) - TEST_RPC_CLIENT = client - vim.rpcrequest(1, "init", result) - end; - flags = { - allow_incremental_sync = options.allow_incremental_sync or false; - debounce_text_changes = options.debounce_text_changes or 0; - }; - settings = settings; - on_exit = function(...) - vim.rpcnotify(1, "exit", ...) - end; - } - ]=], - test_name, - M.fake_lsp_code, - M.fake_lsp_logfile, - timeout_ms or 1e3, - options or {}, - settings or {} - ) -end - ---- @class test.lsp.Config ---- @field test_name string ---- @field timeout_ms? integer ---- @field options? table ---- @field settings? table ---- ---- @field on_setup? fun() ---- @field on_init? fun(client: vim.lsp.Client, ...) ---- @field on_handler? fun(...) ---- @field on_exit? fun(code: integer, signal: integer) - ---- @param config test.lsp.Config -function M.test_rpc_server(config) - if config.test_name then - M.clear_notrace() - fake_lsp_server_setup( - config.test_name, - config.timeout_ms or 1e3, - config.options, - config.settings - ) - end - local client = setmetatable({}, { - __index = function(_, name) - -- Workaround for not being able to yield() inside __index for Lua 5.1 :( - -- Otherwise I would just return the value here. - return function(...) - return exec_lua( - [=[ - local name = ... - if type(TEST_RPC_CLIENT[name]) == 'function' then - return TEST_RPC_CLIENT[name](select(2, ...)) - else - return TEST_RPC_CLIENT[name] - end - ]=], - name, - ... - ) - end - end, - }) - --- @type integer, integer - local code, signal - local function on_request(method, args) - if method == 'setup' then - if config.on_setup then - config.on_setup() - end - return NIL - end - if method == 'init' then - if config.on_init then - config.on_init(client, unpack(args)) - end - return NIL - end - if method == 'handler' then - if config.on_handler then - config.on_handler(unpack(args)) - end - end - return NIL - end - local function on_notify(method, args) - if method == 'exit' then - code, signal = unpack(args) - return stop() - end - end - -- TODO specify timeout? - -- run(on_request, on_notify, nil, 1000) - run(on_request, on_notify, nil) - if config.on_exit then - config.on_exit(code, signal) - end - stop() - if config.test_name then - api.nvim_exec_autocmds('VimLeavePre', { modeline = false }) - end -end - -return M diff --git a/test/functional/plugin/lsp/incremental_sync_spec.lua b/test/functional/plugin/lsp/incremental_sync_spec.lua index bd1842ceb5..eed182150e 100644 --- a/test/functional/plugin/lsp/incremental_sync_spec.lua +++ b/test/functional/plugin/lsp/incremental_sync_spec.lua @@ -1,11 +1,11 @@ -- Test suite for testing interactions with the incremental sync algorithms powering the LSP client -local helpers = require('test.functional.helpers')(after_each) +local t = require('test.functional.testutil')(after_each) -local api = helpers.api -local clear = helpers.clear -local eq = helpers.eq -local exec_lua = helpers.exec_lua -local feed = helpers.feed +local api = t.api +local clear = t.clear +local eq = t.eq +local exec_lua = t.exec_lua +local feed = t.feed before_each(function() clear() diff --git a/test/functional/plugin/lsp/inlay_hint_spec.lua b/test/functional/plugin/lsp/inlay_hint_spec.lua index e9c347bcc5..dceaf45bca 100644 --- a/test/functional/plugin/lsp/inlay_hint_spec.lua +++ b/test/functional/plugin/lsp/inlay_hint_spec.lua @@ -1,15 +1,15 @@ -local helpers = require('test.functional.helpers')(after_each) -local lsp_helpers = require('test.functional.plugin.lsp.helpers') +local t = require('test.functional.testutil')(after_each) +local t_lsp = require('test.functional.plugin.lsp.testutil') local Screen = require('test.functional.ui.screen') -local eq = helpers.eq -local dedent = helpers.dedent -local exec_lua = helpers.exec_lua -local insert = helpers.insert -local api = helpers.api +local eq = t.eq +local dedent = t.dedent +local exec_lua = t.exec_lua +local insert = t.insert +local api = t.api -local clear_notrace = lsp_helpers.clear_notrace -local create_server_definition = lsp_helpers.create_server_definition +local clear_notrace = t_lsp.clear_notrace +local create_server_definition = t_lsp.create_server_definition local text = dedent([[ auto add(int a, int b) { return a + b; } diff --git a/test/functional/plugin/lsp/semantic_tokens_spec.lua b/test/functional/plugin/lsp/semantic_tokens_spec.lua index 12f6e09e7e..82450e404e 100644 --- a/test/functional/plugin/lsp/semantic_tokens_spec.lua +++ b/test/functional/plugin/lsp/semantic_tokens_spec.lua @@ -1,19 +1,19 @@ -local helpers = require('test.functional.helpers')(after_each) -local lsp_helpers = require('test.functional.plugin.lsp.helpers') +local t = require('test.functional.testutil')(after_each) +local t_lsp = require('test.functional.plugin.lsp.testutil') local Screen = require('test.functional.ui.screen') -local command = helpers.command -local dedent = helpers.dedent -local eq = helpers.eq -local exec_lua = helpers.exec_lua -local feed = helpers.feed -local feed_command = helpers.feed_command -local insert = helpers.insert -local matches = helpers.matches -local api = helpers.api - -local clear_notrace = lsp_helpers.clear_notrace -local create_server_definition = lsp_helpers.create_server_definition +local command = t.command +local dedent = t.dedent +local eq = t.eq +local exec_lua = t.exec_lua +local feed = t.feed +local feed_command = t.feed_command +local insert = t.insert +local matches = t.matches +local api = t.api + +local clear_notrace = t_lsp.clear_notrace +local create_server_definition = t_lsp.create_server_definition before_each(function() clear_notrace() diff --git a/test/functional/plugin/lsp/snippet_spec.lua b/test/functional/plugin/lsp/snippet_spec.lua index ba8bc7fe04..d62e188dab 100644 --- a/test/functional/plugin/lsp/snippet_spec.lua +++ b/test/functional/plugin/lsp/snippet_spec.lua @@ -1,13 +1,13 @@ -local helpers = require('test.functional.helpers')(after_each) +local t = require('test.functional.testutil')(after_each) local snippet = require('vim.lsp._snippet_grammar') local type = snippet.NodeType -local eq = helpers.eq -local exec_lua = helpers.exec_lua +local eq = t.eq +local exec_lua = t.exec_lua describe('vim.lsp._snippet_grammar', function() - before_each(helpers.clear) - after_each(helpers.clear) + before_each(t.clear) + after_each(t.clear) local parse = function(...) local res = exec_lua('return require("vim.lsp._snippet_grammar").parse(...)', ...) diff --git a/test/functional/plugin/lsp/testutil.lua b/test/functional/plugin/lsp/testutil.lua new file mode 100644 index 0000000000..7dc7bd9925 --- /dev/null +++ b/test/functional/plugin/lsp/testutil.lua @@ -0,0 +1,219 @@ +local t = require('test.functional.testutil')(nil) + +local clear = t.clear +local exec_lua = t.exec_lua +local run = t.run +local stop = t.stop +local api = t.api +local NIL = vim.NIL + +local M = {} + +function M.clear_notrace() + -- problem: here be dragons + -- solution: don't look too closely for dragons + clear { + env = { + NVIM_LUA_NOTRACK = '1', + NVIM_APPNAME = 'nvim_lsp_test', + VIMRUNTIME = os.getenv 'VIMRUNTIME', + }, + } +end + +M.create_server_definition = [[ + function _create_server(opts) + opts = opts or {} + local server = {} + server.messages = {} + + function server.cmd(dispatchers) + local closing = false + local handlers = opts.handlers or {} + local srv = {} + + function srv.request(method, params, callback) + table.insert(server.messages, { + method = method, + params = params, + }) + local handler = handlers[method] + if handler then + local response, err = handler(method, params) + callback(err, response) + elseif method == 'initialize' then + callback(nil, { + capabilities = opts.capabilities or {} + }) + elseif method == 'shutdown' then + callback(nil, nil) + end + local request_id = #server.messages + return true, request_id + end + + function srv.notify(method, params) + table.insert(server.messages, { + method = method, + params = params + }) + if method == 'exit' then + dispatchers.on_exit(0, 15) + end + end + + function srv.is_closing() + return closing + end + + function srv.terminate() + closing = true + end + + return srv + end + + return server + end +]] + +-- Fake LSP server. +M.fake_lsp_code = 'test/functional/fixtures/fake-lsp-server.lua' +M.fake_lsp_logfile = 'Xtest-fake-lsp.log' + +local function fake_lsp_server_setup(test_name, timeout_ms, options, settings) + exec_lua( + [=[ + lsp = require('vim.lsp') + local test_name, fake_lsp_code, fake_lsp_logfile, timeout, options, settings = ... + TEST_RPC_CLIENT_ID = lsp.start_client { + cmd_env = { + NVIM_LOG_FILE = fake_lsp_logfile; + NVIM_LUA_NOTRACK = "1"; + NVIM_APPNAME = "nvim_lsp_test"; + }; + cmd = { + vim.v.progpath, '-l', fake_lsp_code, test_name, tostring(timeout), + }; + handlers = setmetatable({}, { + __index = function(t, method) + return function(...) + return vim.rpcrequest(1, 'handler', ...) + end + end; + }); + workspace_folders = {{ + uri = 'file://' .. vim.uv.cwd(), + name = 'test_folder', + }}; + before_init = function(params, config) + vim.schedule(function() + vim.rpcrequest(1, "setup") + end) + end, + on_init = function(client, result) + TEST_RPC_CLIENT = client + vim.rpcrequest(1, "init", result) + end; + flags = { + allow_incremental_sync = options.allow_incremental_sync or false; + debounce_text_changes = options.debounce_text_changes or 0; + }; + settings = settings; + on_exit = function(...) + vim.rpcnotify(1, "exit", ...) + end; + } + ]=], + test_name, + M.fake_lsp_code, + M.fake_lsp_logfile, + timeout_ms or 1e3, + options or {}, + settings or {} + ) +end + +--- @class test.lsp.Config +--- @field test_name string +--- @field timeout_ms? integer +--- @field options? table +--- @field settings? table +--- +--- @field on_setup? fun() +--- @field on_init? fun(client: vim.lsp.Client, ...) +--- @field on_handler? fun(...) +--- @field on_exit? fun(code: integer, signal: integer) + +--- @param config test.lsp.Config +function M.test_rpc_server(config) + if config.test_name then + M.clear_notrace() + fake_lsp_server_setup( + config.test_name, + config.timeout_ms or 1e3, + config.options, + config.settings + ) + end + local client = setmetatable({}, { + __index = function(_, name) + -- Workaround for not being able to yield() inside __index for Lua 5.1 :( + -- Otherwise I would just return the value here. + return function(...) + return exec_lua( + [=[ + local name = ... + if type(TEST_RPC_CLIENT[name]) == 'function' then + return TEST_RPC_CLIENT[name](select(2, ...)) + else + return TEST_RPC_CLIENT[name] + end + ]=], + name, + ... + ) + end + end, + }) + --- @type integer, integer + local code, signal + local function on_request(method, args) + if method == 'setup' then + if config.on_setup then + config.on_setup() + end + return NIL + end + if method == 'init' then + if config.on_init then + config.on_init(client, unpack(args)) + end + return NIL + end + if method == 'handler' then + if config.on_handler then + config.on_handler(unpack(args)) + end + end + return NIL + end + local function on_notify(method, args) + if method == 'exit' then + code, signal = unpack(args) + return stop() + end + end + -- TODO specify timeout? + -- run(on_request, on_notify, nil, 1000) + run(on_request, on_notify, nil) + if config.on_exit then + config.on_exit(code, signal) + end + stop() + if config.test_name then + api.nvim_exec_autocmds('VimLeavePre', { modeline = false }) + end +end + +return M diff --git a/test/functional/plugin/lsp/utils_spec.lua b/test/functional/plugin/lsp/utils_spec.lua index bb9cdb8390..c7b874d443 100644 --- a/test/functional/plugin/lsp/utils_spec.lua +++ b/test/functional/plugin/lsp/utils_spec.lua @@ -1,12 +1,12 @@ -local helpers = require('test.functional.helpers')(after_each) +local t = require('test.functional.testutil')(after_each) local Screen = require('test.functional.ui.screen') -local feed = helpers.feed +local feed = t.feed -local eq = helpers.eq -local exec_lua = helpers.exec_lua +local eq = t.eq +local exec_lua = t.exec_lua describe('vim.lsp.util', function() - before_each(helpers.clear) + before_each(t.clear) describe('stylize_markdown', function() local stylize_markdown = function(content, opts) @@ -142,7 +142,7 @@ describe('vim.lsp.util', function() local screen before_each(function() - helpers.clear() + t.clear() screen = Screen.new(80, 80) screen:attach() feed('79i') -- fill screen with empty lines diff --git a/test/functional/plugin/lsp_spec.lua b/test/functional/plugin/lsp_spec.lua index c65cca6cb7..a7dd0537c3 100644 --- a/test/functional/plugin/lsp_spec.lua +++ b/test/functional/plugin/lsp_spec.lua @@ -1,36 +1,36 @@ -local helpers = require('test.functional.helpers')(after_each) -local lsp_helpers = require('test.functional.plugin.lsp.helpers') - -local assert_log = helpers.assert_log -local buf_lines = helpers.buf_lines -local clear = helpers.clear -local command = helpers.command -local dedent = helpers.dedent -local exec_lua = helpers.exec_lua -local eq = helpers.eq -local eval = helpers.eval -local matches = helpers.matches -local pcall_err = helpers.pcall_err +local t = require('test.functional.testutil')(after_each) +local t_lsp = require('test.functional.plugin.lsp.testutil') + +local assert_log = t.assert_log +local buf_lines = t.buf_lines +local clear = t.clear +local command = t.command +local dedent = t.dedent +local exec_lua = t.exec_lua +local eq = t.eq +local eval = t.eval +local matches = t.matches +local pcall_err = t.pcall_err local pesc = vim.pesc -local insert = helpers.insert -local fn = helpers.fn -local retry = helpers.retry -local stop = helpers.stop +local insert = t.insert +local fn = t.fn +local retry = t.retry +local stop = t.stop local NIL = vim.NIL -local read_file = helpers.read_file -local write_file = helpers.write_file -local is_ci = helpers.is_ci -local api = helpers.api -local is_os = helpers.is_os -local skip = helpers.skip -local mkdir = helpers.mkdir -local tmpname = helpers.tmpname - -local clear_notrace = lsp_helpers.clear_notrace -local create_server_definition = lsp_helpers.create_server_definition -local fake_lsp_code = lsp_helpers.fake_lsp_code -local fake_lsp_logfile = lsp_helpers.fake_lsp_logfile -local test_rpc_server = lsp_helpers.test_rpc_server +local read_file = t.read_file +local write_file = t.write_file +local is_ci = t.is_ci +local api = t.api +local is_os = t.is_os +local skip = t.skip +local mkdir = t.mkdir +local tmpname = t.tmpname + +local clear_notrace = t_lsp.clear_notrace +local create_server_definition = t_lsp.create_server_definition +local fake_lsp_code = t_lsp.fake_lsp_code +local fake_lsp_logfile = t_lsp.fake_lsp_logfile +local test_rpc_server = t_lsp.test_rpc_server local function get_buf_option(name, bufnr) bufnr = bufnr or 'BUFFER' @@ -249,7 +249,7 @@ describe('LSP', function() if is_ci() then pending('hangs the build on CI #14028, re-enable with freeze timeout #14204') return - elseif helpers.skip_fragile(pending) then + elseif t.skip_fragile(pending) then return end local expected_handlers = { @@ -1481,7 +1481,7 @@ describe('LSP', function() end, on_handler = function(err, result, ctx) if ctx.method == 'start' then - helpers.command('normal! 1Go') + t.command('normal! 1Go') client.notify('finish') end eq(table.remove(expected_handlers), { err, result, ctx }, 'expected handler') @@ -2368,7 +2368,7 @@ describe('LSP', function() end) describe('lsp.util.rename', function() - local pathsep = helpers.get_pathsep() + local pathsep = t.get_pathsep() it('Can rename an existing file', function() local old = tmpname() @@ -2404,7 +2404,7 @@ describe('LSP', function() os.remove(old_dir) os.remove(new_dir) - helpers.mkdir_p(old_dir) + t.mkdir_p(old_dir) local file = 'file.txt' write_file(old_dir .. pathsep .. file, 'Test content') @@ -2440,7 +2440,7 @@ describe('LSP', function() local new = tmpname() os.remove(old) os.remove(new) - helpers.mkdir_p(old) + t.mkdir_p(old) local result = exec_lua( [[ diff --git a/test/functional/plugin/man_spec.lua b/test/functional/plugin/man_spec.lua index 34006d5906..862cb13146 100644 --- a/test/functional/plugin/man_spec.lua +++ b/test/functional/plugin/man_spec.lua @@ -1,17 +1,17 @@ -local helpers = require('test.functional.helpers')(after_each) +local t = require('test.functional.testutil')(after_each) local Screen = require('test.functional.ui.screen') -local command, feed = helpers.command, helpers.feed -local clear = helpers.clear -local exec_lua = helpers.exec_lua -local fn = helpers.fn -local nvim_prog = helpers.nvim_prog -local matches = helpers.matches -local write_file = helpers.write_file -local tmpname = helpers.tmpname -local eq = helpers.eq +local command, feed = t.command, t.feed +local clear = t.clear +local exec_lua = t.exec_lua +local fn = t.fn +local nvim_prog = t.nvim_prog +local matches = t.matches +local write_file = t.write_file +local tmpname = t.tmpname +local eq = t.eq local pesc = vim.pesc -local skip = helpers.skip -local is_ci = helpers.is_ci +local skip = t.skip +local is_ci = t.is_ci -- Collects all names passed to find_path() after attempting ":Man foo". local function get_search_history(name) diff --git a/test/functional/plugin/matchparen_spec.lua b/test/functional/plugin/matchparen_spec.lua index 619c43f2c9..93d747519a 100644 --- a/test/functional/plugin/matchparen_spec.lua +++ b/test/functional/plugin/matchparen_spec.lua @@ -1,11 +1,11 @@ -local helpers = require('test.functional.helpers')(after_each) +local t = require('test.functional.testutil')(after_each) local Screen = require('test.functional.ui.screen') -local clear = helpers.clear -local command = helpers.command -local api = helpers.api -local feed = helpers.feed -local eq = helpers.eq +local clear = t.clear +local command = t.command +local api = t.api +local feed = t.feed +local eq = t.eq describe('matchparen', function() local screen --- @type test.functional.ui.screen diff --git a/test/functional/plugin/msgpack_spec.lua b/test/functional/plugin/msgpack_spec.lua index 8511e6c703..76feca7911 100644 --- a/test/functional/plugin/msgpack_spec.lua +++ b/test/functional/plugin/msgpack_spec.lua @@ -1,11 +1,11 @@ -local helpers = require('test.functional.helpers')(after_each) -local clear = helpers.clear -local api = helpers.api -local eq = helpers.eq -local nvim_eval = helpers.eval -local nvim_command = helpers.command -local exc_exec = helpers.exc_exec -local ok = helpers.ok +local t = require('test.functional.testutil')(after_each) +local clear = t.clear +local api = t.api +local eq = t.eq +local nvim_eval = t.eval +local nvim_command = t.command +local exc_exec = t.exc_exec +local ok = t.ok local NIL = vim.NIL describe('autoload/msgpack.vim', function() diff --git a/test/functional/plugin/shada_spec.lua b/test/functional/plugin/shada_spec.lua index 1c20548321..9312f1f2f0 100644 --- a/test/functional/plugin/shada_spec.lua +++ b/test/functional/plugin/shada_spec.lua @@ -1,13 +1,13 @@ -local helpers = require('test.functional.helpers')(after_each) +local t = require('test.functional.testutil')(after_each) local Screen = require('test.functional.ui.screen') -local clear = helpers.clear +local clear = t.clear local eq, api, nvim_eval, nvim_command, exc_exec, fn, nvim_feed = - helpers.eq, helpers.api, helpers.eval, helpers.command, helpers.exc_exec, helpers.fn, helpers.feed -local neq = helpers.neq -local read_file = helpers.read_file + t.eq, t.api, t.eval, t.command, t.exc_exec, t.fn, t.feed +local neq = t.neq +local read_file = t.read_file -local shada_helpers = require('test.functional.shada.helpers') -local get_shada_rw = shada_helpers.get_shada_rw +local t_shada = require('test.functional.shada.testutil') +local get_shada_rw = t_shada.get_shada_rw local function reset(shada_file) clear { args = { '-u', 'NORC', '-i', shada_file or 'NONE' } } @@ -2612,7 +2612,7 @@ end) describe('plugin/shada.vim', function() local epoch = os.date('%Y-%m-%dT%H:%M:%S', 0) - local eol = helpers.is_os('win') and '\r\n' or '\n' + local eol = t.is_os('win') and '\r\n' or '\n' before_each(function() -- Note: reset() is called explicitly in each test. os.remove(fname) diff --git a/test/functional/plugin/tohtml_spec.lua b/test/functional/plugin/tohtml_spec.lua index 2ac0fe1fa3..9d947fe44a 100644 --- a/test/functional/plugin/tohtml_spec.lua +++ b/test/functional/plugin/tohtml_spec.lua @@ -1,13 +1,13 @@ local Screen = require('test.functional.ui.screen') -local helpers = require('test.functional.helpers')(after_each) +local t = require('test.functional.testutil')(after_each) -local clear = helpers.clear -local exec = helpers.exec -local exec_lua = helpers.exec_lua -local eq = helpers.eq -local fn = helpers.fn -local api = helpers.api -local insert = helpers.insert +local clear = t.clear +local exec = t.exec +local exec_lua = t.exec_lua +local eq = t.eq +local fn = t.fn +local api = t.api +local insert = t.insert local function html_syntax_match() local styles = diff --git a/test/functional/plugin/tutor_spec.lua b/test/functional/plugin/tutor_spec.lua index 46a117f298..d9ba8365d6 100644 --- a/test/functional/plugin/tutor_spec.lua +++ b/test/functional/plugin/tutor_spec.lua @@ -1,9 +1,9 @@ local Screen = require('test.functional.ui.screen') -local helpers = require('test.functional.helpers')(after_each) -local clear = helpers.clear -local command = helpers.command -local feed = helpers.feed -local is_os = helpers.is_os +local t = require('test.functional.testutil')(after_each) +local clear = t.clear +local command = t.command +local feed = t.feed +local is_os = t.is_os describe(':Tutor', function() local screen --- @type test.functional.ui.screen diff --git a/test/functional/plugin/vim_syntax_spec.lua b/test/functional/plugin/vim_syntax_spec.lua index c5858f8bc0..c07c5646a6 100644 --- a/test/functional/plugin/vim_syntax_spec.lua +++ b/test/functional/plugin/vim_syntax_spec.lua @@ -1,15 +1,15 @@ -local helpers = require('test.functional.helpers')(after_each) +local t = require('test.functional.testutil')(after_each) local Screen = require('test.functional.ui.screen') -local clear = helpers.clear -local exec = helpers.exec -local api = helpers.api +local clear = t.clear +local exec = t.exec +local api = t.api describe('Vimscript syntax highlighting', function() local screen --- @type test.functional.ui.screen before_each(function() clear() - helpers.add_builddir_to_rtp() + t.add_builddir_to_rtp() exec([[ setfiletype vim syntax on -- cgit