robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
d9b4fb1273
master
1local n =require ( 'test.functional.testnvim' )() 2 3local clear =n . clear 4local exec_lua =n . exec_lua 5local run =n . run 6local stop =n . stop 7local api =n . api 8local NIL =vim . NIL 9 10local M ={} 11 12function M . clear_notrace () 13-- problem: here be dragons 14-- solution: don't look too closely for dragons 15clear { 16env ={ 17NVIM_LUA_NOTRACK ='1' , 18NVIM_APPNAME ='nvim_lsp_test' , 19VIMRUNTIME =os . getenv 'VIMRUNTIME' , 20} , 21} 22end 23 24M . create_tcp_echo_server =function () 25--- Create a TCP server that echos the first message it receives. 26--- @param host string 27--- @return integer 28function _G . _create_tcp_server ( host ) 29local uv =vim . uv 30local server =assert ( uv . new_tcp ()) 31local on_read =require ( 'vim.lsp.rpc' ). create_read_loop ( 32function ( body ) 33vim . rpcnotify ( 1 , 'body' , body ) 34end , 35nil , 36function ( err , code ) 37vim . rpcnotify ( 1 , 'error' , err , code ) 38end 39) 40server : bind ( host , 0 ) 41server : listen ( 127 , function ( e ) 42assert ( not e , e ) 43local socket =assert ( uv . new_tcp ()) 44server : accept ( socket ) 45socket : read_start ( function ( err , chunk ) 46on_read ( err , chunk ) 47socket : shutdown () 48socket : close () 49server : shutdown () 50server : close () 51end ) 52end ) 53return server : getsockname (). port 54end 55function _G . _send_msg_to_server ( msg ) 56local port =_G . _create_tcp_server ( '127.0.0.1' ) 57local client =assert ( vim . uv . new_tcp ()) 58client : connect ( '127.0.0.1' , port , function () 59client : write ( msg , function () 60client : shutdown () 61client : close () 62end ) 63end ) 64end 65end 66 67M . create_server_definition =function () 68---@class test.functional.plugin.lsp.testutil._create_server.Opts 69---@field capabilities lsp.ServerCapabilities? 70---@field handlers table<vim.lsp.protocol.Method.ClientToServer.Request, fun(method: vim.lsp.protocol.Method.ClientToServer.Request, params: table?, callback: fun(err?: lsp.ResponseError, result: any))>? 71 72---@class test.functional.plugin.lsp.testutil._create_server.Message 73---@field method vim.lsp.protocol.Method.ClientToServer 74---@field params table? 75 76---@class test.functional.plugin.lsp.testutil._create_server.Server 77---@field messages test.functional.plugin.lsp.testutil._create_server.Message[] 78---@field cmd fun(dispatchers: vim.lsp.rpc.Dispatchers, config: vim.lsp.ClientConfig): vim.lsp.rpc.Client 79 80---@param opts test.functional.plugin.lsp.testutil._create_server.Opts? 81---@return test.functional.plugin.lsp.testutil._create_server.Server 82function _G . _create_server ( opts ) 83opts =opts or {} 84local server ={} 85server . messages ={} 86 87function server . cmd ( dispatchers , _config ) 88local closing =false 89local handlers =opts . handlers or {} 90local srv ={} 91 92function srv . request ( method , params , callback ) 93table . insert ( server . messages , { 94method =method , 95params =params , 96} ) 97local handler =handlers [ method ] 98if handler then 99handler ( method , params , callback ) 100elseif method == 'initialize' then 101callback ( nil , { 102capabilities =opts . capabilities or {} , 103} ) 104elseif method == 'shutdown' then 105callback ( nil , nil ) 106end 107local request_id =# server . messages 108return true , request_id 109end 110 111function srv . notify ( method , params ) 112table . insert ( server . messages , { 113method =method , 114params =params , 115} ) 116if method == 'exit' then 117dispatchers . on_exit ( 0 , 15 ) 118end 119return true 120end 121 122function srv . is_closing () 123return closing 124end 125 126function srv . terminate () 127closing =true 128dispatchers . on_exit ( 0 , 15 ) 129end 130 131return srv 132end 133 134return server 135end 136end 137 138-- Fake LSP server. 139M . fake_lsp_code ='test/functional/fixtures/fake-lsp-server.lua' 140M . fake_lsp_logfile ='Xtest-fake-lsp.log' 141 142local function fake_lsp_server_setup ( test_name , timeout_ms , options , settings ) 143exec_lua ( function ( fake_lsp_code , fake_lsp_logfile , timeout ) 144options =options or {} 145settings =settings or {} 146_G . lsp =require ( 'vim.lsp' ) 147_G . TEST_RPC_CLIENT_ID =_G . lsp . start_client { 148cmd_env ={ 149NVIM_LOG_FILE =fake_lsp_logfile , 150NVIM_LUA_NOTRACK ='1' , 151NVIM_APPNAME ='nvim_lsp_test' , 152} , 153cmd ={ 154vim . v . progpath , 155'-l' , 156fake_lsp_code , 157test_name , 158tostring ( timeout ), 159} , 160handlers =setmetatable ( {} , { 161__index =function ( _t , _method ) 162return function ( ... ) 163return vim . rpcrequest ( 1 , 'handler' , ... ) 164end 165end , 166} ), 167workspace_folders ={ 168{ 169uri ='file://' .. vim . uv . cwd (), 170name ='test_folder' , 171} , 172} , 173before_init =function ( _params , _config ) 174vim . schedule ( function () 175vim . rpcrequest ( 1 , 'setup' ) 176end ) 177end , 178on_init =function ( client , result ) 179_G . TEST_RPC_CLIENT =client 180vim . rpcrequest ( 1 , 'init' , result ) 181end , 182flags ={ 183allow_incremental_sync =options . allow_incremental_sync or false , 184debounce_text_changes =options . debounce_text_changes or 0 , 185} , 186settings =settings , 187on_exit =function ( ... ) 188vim . rpcnotify ( 1 , 'exit' , ... ) 189end , 190} 191end , M . fake_lsp_code , M . fake_lsp_logfile , timeout_ms or 1e3 ) 192end 193 194--- @class test.lsp.Config 195--- @field test_name string 196--- @field timeout_ms? integer 197--- @field options? table 198--- @field settings? table 199--- 200--- @field on_setup? fun() 201--- @field on_init? fun(client: vim.lsp.Client, ...) 202--- @field on_handler? fun(...) 203--- @field on_exit? fun(code: integer, signal: integer) 204 205--- @param config test.lsp.Config 206function M . test_rpc_server ( config ) 207if config . test_name then 208M . clear_notrace () 209fake_lsp_server_setup ( 210config . test_name , 211config . timeout_ms or 1e3 , 212config . options , 213config . settings 214) 215end 216local client =setmetatable ( {} , { 217__index =function ( t , name ) 218-- Workaround for not being able to yield() inside __index for Lua 5.1 :( 219-- Otherwise I would just return the value here. 220return function ( arg1 , ... ) 221local ismethod =arg1 == t 222return exec_lua ( function ( ... ) 223local client =_G . TEST_RPC_CLIENT 224if type ( client [ name ]) == 'function' then 225return client [ name ]( ismethod and client or arg1 , ... ) 226end 227return client [ name ] 228end , ... ) 229end 230end , 231} ) 232--- @type integer, integer 233local code , signal 234local busy =0 235local exited =false 236local function on_request ( method , args ) 237busy =busy + 1 238if method == 'setup' and config . on_setup then 239config . on_setup () 240end 241if method == 'init' and config . on_init then 242config . on_init ( client , unpack ( args )) 243end 244if method == 'handler' and config . on_handler then 245config . on_handler ( unpack ( args )) 246end 247busy =busy - 1 248if busy == 0 and exited then 249stop () 250end 251return NIL 252end 253local function on_notify ( method , args ) 254if method == 'exit' then 255code , signal =unpack ( args ) 256exited =true 257if busy == 0 then 258stop () 259end 260end 261return NIL 262end 263-- TODO specify timeout? 264-- run(on_request, on_notify, nil, 1000) 265run ( on_request , on_notify , nil ) 266if config . on_exit then 267config . on_exit ( code , signal ) 268end 269stop () 270if config . test_name then 271api . nvim_exec_autocmds ( 'VimLeavePre' , { modeline =false } ) 272end 273end 274 275return M