robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f907614088
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3 4local describe , it , before_each , finally =t . describe , t . it , t . before_each , t . finally 5local clear , eq , eval , next_msg , ok , source =n . clear , t . eq , n . eval , n . next_msg , t . ok , n . source 6local command , fn , api =n . command , n . fn , n . api 7local feed =n . feed 8local exec_lua =n . exec_lua 9local matches =t . matches 10local sleep =vim . uv . sleep 11local get_session , set_session =n . get_session , n . set_session 12local nvim_prog =n . nvim_prog 13local is_os =t . is_os 14local retry =t . retry 15local expect_twostreams =n . expect_twostreams 16local assert_alive =n . assert_alive 17local pcall_err =t . pcall_err 18local skip =t . skip 19 20describe ( 'channels' , function () 21local init =[[ 22function! Normalize(data) abort 23" Windows: remove ^M 24return type([]) == type(a:data) 25\ ? mapnew(a:data, 'substitute(v:val, "\r", "", "g")') 26\ : a:data 27endfunction 28function! OnEvent(id, data, event) dict 29call rpcnotify(1, a:event, a:id, a:data) 30endfunction 31]] 32before_each ( function () 33clear () 34source ( init ) 35end ) 36 37it ( 'can connect to socket' , function () 38local server =n . new_session ( true ) 39set_session ( server ) 40local address =fn . serverlist ()[ 1 ] 41local client =n . new_session ( true ) 42set_session ( client ) 43source ( init ) 44 45api . nvim_set_var ( 'address' , address ) 46command ( "let g:id = sockconnect('pipe', address, {'on_data':'OnEvent'})" ) 47local id =eval ( 'g:id' ) 48ok ( id > 0 ) 49 50command ( "call chansend(g:id, msgpackdump([[2,'nvim_set_var',['code',23]]]))" ) 51set_session ( server ) 52retry ( nil , 1000 , function () 53eq ( 23 , api . nvim_get_var ( 'code' )) 54end ) 55set_session ( client ) 56 57command ( "call chansend(g:id, msgpackdump([[0,0,'nvim_eval',['2+3']]]))" ) 58 59local res =eval ( 'msgpackdump([[1,0,v:null,5]])' ) 60eq ( { '\148\001\n\192\005' } , res ) 61eq ( { 'notification' , 'data' , { id , res } } , next_msg ()) 62command ( "call chansend(g:id, msgpackdump([[2,'nvim_command',['quit']]]))" ) 63eq ( { 'notification' , 'data' , { id , { '' } } } , next_msg ()) 64end ) 65 66it ( 'emits ChanClose when RPC socket reaches EOF' , function () 67local client =get_session () 68local server =n . new_session ( true ) 69finally ( function () 70set_session ( client ) 71if server then 72server : close () 73end 74end ) 75set_session ( server ) 76local address =fn . serverlist ()[ 1 ] 77set_session ( client ) 78 79api . nvim_set_var ( 'address' , address ) 80exec_lua ( function () 81_G . closed_event =nil 82vim . api . nvim_create_autocmd ( 'ChanClose' , { 83callback =function () 84_G . closed_event =vim . deepcopy ( vim . v . event . info ) 85end , 86} ) 87end ) 88command ( "let g:id = sockconnect('pipe', address, {'rpc': v:true})" ) 89local id =eval ( 'g:id' ) 90ok ( id > 0 ) 91eq ( 5 , eval ( "rpcrequest(g:id, 'nvim_eval', '2+3')" )) 92 93server : close () 94server =nil 95set_session ( client ) 96retry ( nil , 3000 , function () 97local info =exec_lua ( function () 98return _G . closed_event 99end ) 100eq ( id , info . id ) 101eq ( 'socket' , info . stream ) 102eq ( 'rpc' , info . mode ) 103end ) 104end ) 105 106it ( 'dont crash due to garbage in rpc #23781' , function () 107local client =get_session () 108local server =n . new_session ( true ) 109set_session ( server ) 110local address =fn . serverlist ()[ 1 ] 111set_session ( client ) 112 113api . nvim_set_var ( 'address' , address ) 114command ( "let g:id = sockconnect('pipe', address, {'on_data':'OnEvent'})" ) 115local id =eval ( 'g:id' ) 116ok ( id > 0 ) 117 118command ( "call chansend(g:id, 'F')" ) 119eq ( { 'notification' , 'data' , { id , { '' } } } , next_msg ()) 120set_session ( server ) 121assert_alive () 122 123set_session ( client ) 124command ( 'call chanclose(g:id)' ) 125command ( "let g:id = sockconnect('pipe', address, {'on_data':'OnEvent'})" ) 126id =eval ( 'g:id' ) 127ok ( id > 0 ) 128 129command ( "call chansend(g:id, msgpackdump([[2, 'redraw', 'F']], 'B')[:-4])" ) 130set_session ( server ) 131assert_alive () 132set_session ( client ) 133command ( "call chansend(g:id, 'F')" ) 134eq ( { 'notification' , 'data' , { id , { '' } } } , next_msg ()) 135 136set_session ( server ) 137assert_alive () 138set_session ( client ) 139command ( 'call chanclose(g:id)' ) 140server : close () 141end ) 142 143it ( 'can use stdio channel' , function () 144source ( [[ 145let g:job_opts = { 146\ 'on_stdout': function('OnEvent'), 147\ 'on_stderr': function('OnEvent'), 148\ 'on_exit': function('OnEvent'), 149\ } 150]] ) 151api . nvim_set_var ( 'nvim_prog' , nvim_prog ) 152api . nvim_set_var ( 153'code' , 154[[ 155function! OnEvent(id, data, event) dict 156let text = string([a:id, a:data, a:event]) 157call chansend(g:x, text) 158 159if a:data == [''] 160call chansend(v:stderr, "*dies*") 161quit 162endif 163endfunction 164let g:x = stdioopen({'on_stdin':'OnEvent'}) 165call chansend(x, "hello") 166]] 167) 168command ( 169"let g:id = jobstart([ g:nvim_prog, '-u', 'NONE', '-i', 'NONE', '--cmd', 'set noswapfile', '--headless', '--cmd', g:code], g:job_opts)" 170) 171local id =eval ( 'g:id' ) 172ok ( id > 0 ) 173 174eq ( { 'notification' , 'stdout' , { id , { 'hello' } } } , next_msg ()) 175 176command ( "call chansend(id, 'howdy')" ) 177eq ( { 'notification' , 'stdout' , { id , { "[1, ['howdy'], 'stdin']" } } } , next_msg ()) 178 179command ( 'call chansend(id, 0z686f6c61)' ) 180eq ( { 'notification' , 'stdout' , { id , { "[1, ['hola'], 'stdin']" } } } , next_msg ()) 181 182command ( "call chanclose(id, 'stdin')" ) 183expect_twostreams ( { 184{ 'notification' , 'stdout' , { id , { "[1, [''], 'stdin']" } } } , 185{ 'notification' , 'stdout' , { id , { '' } } } , 186} , { 187{ 'notification' , 'stderr' , { id , { '*dies*' } } } , 188{ 'notification' , 'stderr' , { id , { '' } } } , 189} ) 190eq ( { 'notification' , 'exit' , { 3 , 0 } } , next_msg ()) 191end ) 192 193it ( 'can use stdio channel and on_print callback' , function () 194source ( [[ 195let g:job_opts = { 196\ 'on_stdout': function('OnEvent'), 197\ 'on_stderr': function('OnEvent'), 198\ 'on_exit': function('OnEvent'), 199\ } 200]] ) 201api . nvim_set_var ( 'nvim_prog' , nvim_prog ) 202api . nvim_set_var ( 203'code' , 204[[ 205function! OnStdin(id, data, event) dict 206echo string([a:id, a:data, a:event]) 207if a:data == [''] 208quit 209endif 210endfunction 211function! OnPrint(text) dict 212call chansend(g:x, ['OnPrint:' .. a:text]) 213endfunction 214let g:x = stdioopen({'on_stdin': funcref('OnStdin'), 'on_print':'OnPrint'}) 215call chansend(x, "hello") 216]] 217) 218command ( 219"let g:id = jobstart([ g:nvim_prog, '-u', 'NONE', '-i', 'NONE', '--cmd', 'set noswapfile', '--headless', '--cmd', g:code], g:job_opts)" 220) 221local id =eval ( 'g:id' ) 222ok ( id > 0 ) 223 224eq ( { 'notification' , 'stdout' , { id , { 'hello' } } } , next_msg ()) 225 226command ( "call chansend(id, 'howdy')" ) 227eq ( { 'notification' , 'stdout' , { id , { "OnPrint:[1, ['howdy'], 'stdin']" } } } , next_msg ()) 228end ) 229 230-- Helper to accumulate PTY stdout data until expected string is received. 231-- PTY reads are non-atomic and may deliver data in chunks. 232local function expect_stdout ( id , expected ) 233local accumulated ='' 234while # accumulated < # expected do 235local msg =next_msg ( 1000 ) 236if not msg then 237break 238end 239eq ( 'notification' , msg [ 1 ]) 240eq ( 'stdout' , msg [ 2 ]) 241eq ( id , msg [ 3 ][ 1 ]) 242accumulated =accumulated .. table . concat ( msg [ 3 ][ 2 ], '\n' ) 243-- Windows: strip terminal escapes injected by ConPTY (CSI/OSC). 244accumulated =accumulated : gsub ( '\27%[[%d;?]*[%a~]' , '' ): gsub ( '\27%][^\7]*\7' , '' ) 245end 246eq ( expected , accumulated ) 247end 248 249it ( 'can use stdio channel with pty' , function () 250source ( [[ 251let g:job_opts = { 252\ 'on_stdout': function('OnEvent'), 253\ 'on_exit': function('OnEvent'), 254\ 'pty': v:true, 255\ } 256]] ) 257api . nvim_set_var ( 'nvim_prog' , nvim_prog ) 258api . nvim_set_var ( 259'code' , 260[[ 261function! OnEvent(id, data, event) dict 262let text = string([a:id, a:data, a:event]) 263call chansend(g:x, text) 264endfunction 265let g:x = stdioopen({'on_stdin':'OnEvent'}) 266]] 267) 268command ( 269"let g:id = jobstart([ g:nvim_prog, '-u', 'NONE', '-i', 'NONE', '--cmd', 'set noswapfile', '--headless', '--cmd', g:code], g:job_opts)" 270) 271local id =eval ( 'g:id' ) 272ok ( id > 0 ) 273 274-- Windows ConPTY does not echo input to the master, unlike POSIX ptys. 275local echo_text =is_os ( 'win' ) and '' or 'TEXT\r\n' 276local echo_blobs =is_os ( 'win' ) and '' or 'Blobs!\r\n' 277 278command ( "call chansend(id, 'TEXT\n')" ) 279expect_stdout ( id , echo_text .. "[1, ['TEXT', ''], 'stdin']" ) 280 281command ( 'call chansend(id, 0z426c6f6273210a)' ) 282expect_stdout ( id , echo_blobs .. "[1, ['Blobs!', ''], 'stdin']" ) 283 284-- Windows: the assertions below depend on POSIX pty canonical-mode behavior (echo, backspace handling, EOT-as-EOF). 285if is_os ( 'win' ) then 286return 287end 288 289command ( "call chansend(id, 'neovan')" ) 290expect_stdout ( id , 'neovan' ) 291command ( "call chansend(id, '\127\127im\n')" ) 292expect_stdout ( id , "\b \b\b \bim\r\n[1, ['neovim', ''], 'stdin']" ) 293 294command ( "call chansend(id, 'incomplet\004')" ) 295 296local bsdlike =is_os ( 'bsd' ) or is_os ( 'mac' ) 297local extra =bsdlike and '^D\008\008' or '' 298expect_stdout ( id , 'incomplet' .. extra .. "[1, ['incomplet'], 'stdin']" ) 299 300command ( "call chansend(id, '\004')" ) 301expect_stdout ( id , extra .. "[1, [''], 'stdin']" ) 302 303-- channel is still open 304command ( "call chansend(id, 'hi again!\n')" ) 305expect_stdout ( id , 'hi again!\r\n' ) 306end ) 307 308it ( 'stdio channel can use rpc and stderr simultaneously' , function () 309source ( [[ 310let g:job_opts = { 311\ 'on_stderr': function('OnEvent'), 312\ 'on_exit': function('OnEvent'), 313\ 'rpc': v:true, 314\ } 315]] ) 316api . nvim_set_var ( 'nvim_prog' , nvim_prog ) 317api . nvim_set_var ( 318'code' , 319[[ 320let id = stdioopen({'rpc':v:true}) 321call rpcnotify(id,"nvim_call_function", "rpcnotify", [1, "message", "hi there!", id]) 322call chansend(v:stderr, "trouble!") 323]] 324) 325command ( 326"let id = jobstart([ g:nvim_prog, '-u', 'NONE', '-i', 'NONE', '--cmd', 'set noswapfile', '--headless', '--cmd', g:code], g:job_opts)" 327) 328-- On Windows the stderr write may be delivered before the RPC notify. 329local notif1 ={ 'notification' , 'message' , { 'hi there!' , 1 } } 330local notif2 ={ 'notification' , 'stderr' , { 3 , { 'trouble!' } } } 331n . expect_msg_seq ( { notif1 , notif2 } , { notif2 , notif1 } ) 332 333eq ( 30 , eval ( "rpcrequest(id, 'nvim_eval', '[chansend(v:stderr, \"math??\"), 5*6][1]')" )) 334eq ( { 'notification' , 'stderr' , { 3 , { 'math??' } } } , next_msg ()) 335 336local _ , err = 337pcall ( command , "call rpcrequest(id, 'nvim_command', 'call chanclose(v:stderr, \"stdin\")')" ) 338matches ( 'E906: invalid stream for channel' , err ) 339 340eq ( 1 , eval ( "rpcrequest(id, 'nvim_eval', 'chanclose(v:stderr, \"stderr\")')" )) 341eq ( { 'notification' , 'stderr' , { 3 , { '' } } } , next_msg ()) 342 343command ( "call rpcnotify(id, 'nvim_command', 'quit')" ) 344eq ( { 'notification' , 'exit' , { 3 , 0 } } , next_msg ()) 345end ) 346 347it ( 'stdio channel works with stdout redirected to file #30509' , function () 348t . write_file ( 349'Xstdio_write.vim' , 350[[ 351let chan = stdioopen({}) 352call chansend(chan, 'foo') 353call chansend(chan, 'bar') 354qall! 355]] 356) 357local fd =assert ( vim . uv . fs_open ( 'Xstdio_redir' , 'w' , 420 )) 358local exit_code , exit_signal 359local handle =vim . uv . spawn ( nvim_prog , { 360args ={ '-u' , 'NONE' , '-i' , 'NONE' , '--headless' , '-S' , 'Xstdio_write.vim' } , 361-- Simulate shell redirection: "nvim ... > Xstdio_redir". #30509 362stdio ={ nil , fd , nil } , 363} , function ( code , signal ) 364vim . uv . stop () 365exit_code , exit_signal =code , signal 366end ) 367finally ( function () 368handle : close () 369vim . uv . fs_close ( fd ) 370os . remove ( 'Xstdio_write.vim' ) 371os . remove ( 'Xstdio_redir' ) 372end ) 373vim . uv . run ( 'default' ) 374eq ( { 0 , 0 } , { exit_code , exit_signal } ) 375eq ( 'foobar' , t . read_file ( 'Xstdio_redir' )) 376end ) 377 378it ( 'can use buffered output mode' , function () 379skip ( fn . executable ( 'grep' ) == 0 , 'missing "grep" command' ) 380source ( [[ 381let g:job_opts = { 382\ 'on_stdout': function('OnEvent'), 383\ 'on_exit': function('OnEvent'), 384\ 'stdout_buffered': v:true, 385\ } 386]] ) 387command ( "let id = jobstart(['grep', '^[0-9]'], g:job_opts)" ) 388local id =eval ( 'g:id' ) 389 390command ( [[call chansend(id, "stuff\n10 PRINT \"NVIM\"\nxx")]] ) 391sleep ( 10 ) 392command ( [[call chansend(id, "xx\n20 GOTO 10\nzz\n")]] ) 393command ( "call chanclose(id, 'stdin')" ) 394 395eq ( { 396'notification' , 397'stdout' , 398{ id , { '10 PRINT "NVIM"' , '20 GOTO 10' , '' } } , 399} , next_msg ()) 400eq ( { 'notification' , 'exit' , { id , 0 } } , next_msg ()) 401 402command ( "let id = jobstart(['grep', '^[0-9]'], g:job_opts)" ) 403id =eval ( 'g:id' ) 404 405command ( [[call chansend(id, "is no number\nnot at all")]] ) 406command ( "call chanclose(id, 'stdin')" ) 407 408-- works correctly with no output 409eq ( { 'notification' , 'stdout' , { id , { '' } } } , next_msg ()) 410eq ( { 'notification' , 'exit' , { id , 1 } } , next_msg ()) 411end ) 412 413it ( 'can use buffered output mode with no stream callback' , function () 414skip ( fn . executable ( 'grep' ) == 0 , 'missing "grep" command' ) 415source ( [[ 416function! OnEvent(id, data, event) dict 417call rpcnotify(1, a:event, a:id, a:data, self.stdout) 418endfunction 419let g:job_opts = { 420\ 'on_exit': function('OnEvent'), 421\ 'stdout_buffered': v:true, 422\ } 423]] ) 424command ( "let id = jobstart(['grep', '^[0-9]'], g:job_opts)" ) 425local id =eval ( 'g:id' ) 426 427command ( [[call chansend(id, "stuff\n10 PRINT \"NVIM\"\nxx")]] ) 428sleep ( 10 ) 429command ( [[call chansend(id, "xx\n20 GOTO 10\nzz\n")]] ) 430command ( "call chanclose(id, 'stdin')" ) 431 432eq ( { 433'notification' , 434'exit' , 435{ id , 0 , { '10 PRINT "NVIM"' , '20 GOTO 10' , '' } } , 436} , next_msg ()) 437 438-- if dict is reused the new value is not stored, 439-- but nvim also does not crash 440command ( "let id = jobstart(['cat'], g:job_opts)" ) 441id =eval ( 'g:id' ) 442 443command ( [[call chansend(id, "cat text\n")]] ) 444sleep ( 10 ) 445command ( "call chanclose(id, 'stdin')" ) 446 447-- old value was not overwritten 448eq ( { 449'notification' , 450'exit' , 451{ id , 0 , { '10 PRINT "NVIM"' , '20 GOTO 10' , '' } } , 452} , next_msg ()) 453 454-- and an error was thrown. 455eq ( 456"E5210: dict key 'stdout' already set for buffered stream in channel " .. id , 457eval ( 'v:errmsg' ) 458) 459 460-- reset dictionary 461source ( [[ 462let g:job_opts = { 463\ 'on_exit': function('OnEvent'), 464\ 'stdout_buffered': v:true, 465\ } 466]] ) 467command ( "let id = jobstart(['grep', '^[0-9]'], g:job_opts)" ) 468id =eval ( 'g:id' ) 469 470command ( [[call chansend(id, "is no number\nnot at all")]] ) 471command ( "call chanclose(id, 'stdin')" ) 472 473-- works correctly with no output 474eq ( { 'notification' , 'exit' , { id , 1 , { '' } } } , next_msg ()) 475end ) 476 477it ( 'ChanOpen works with vim.wait() from another autocommand #32706' , function () 478exec_lua ( [[ 479vim.api.nvim_create_autocmd('ChanOpen', { 480callback = function(ev) 481_G.chan = vim.v.event.info.id 482end, 483}) 484vim.api.nvim_create_autocmd('InsertEnter', { 485buf = 0, 486callback = function() 487local chan = vim.fn.jobstart({ 'cat' }) 488_G.result = vim.wait(3000, function() 489return _G.chan == chan 490end) 491end, 492}) 493]] ) 494feed ( 'i' ) 495retry ( nil , 4000 , function () 496eq ( true , exec_lua ( 'return _G.result' )) 497end ) 498end ) 499 500it ( 'ChanInfo works with vim.wait() from another autocommand #32706' , function () 501exec_lua ( [[ 502vim.api.nvim_create_autocmd('ChanInfo', { 503callback = function(ev) 504_G.foo = vim.v.event.info.client.attributes.foo 505end, 506}) 507vim.api.nvim_create_autocmd('InsertEnter', { 508buf = 0, 509callback = function() 510local chan = vim.fn.jobstart({ 'cat' }) 511_G.result = vim.wait(3000, function() 512return _G.foo == 'bar' 513end) 514end, 515}) 516]] ) 517feed ( 'i' ) 518api . nvim_set_client_info ( 'test' , {} , 'remote' , {} , { foo ='bar' } ) 519retry ( nil , 4000 , function () 520eq ( true , exec_lua ( 'return _G.result' )) 521end ) 522end ) 523 524describe ( 'sockconnect() reports error when connection fails' , function () 525it ( 'in "pipe" mode' , function () 526eq ( 527'Vim:connection failed: connection refused' , 528pcall_err ( fn . sockconnect , 'pipe' , n . new_pipename ()) 529) 530end ) 531 532it ( 'in "tcp" mode' , function () 533eq ( 534'Vim:connection failed: connection refused' , 535pcall_err ( fn . sockconnect , 'tcp' , '127.0.0.1:0' ) 536) 537end ) 538 539it ( 'with another connection accepted while polling #37807' , function () 540local server =api . nvim_get_vvar ( 'servername' ) 541local invalid_pipe =n . new_pipename () 542exec_lua ( function () 543vim . defer_fn ( function () 544vim . uv . sleep ( 50 ) -- Block the uv event loop. 545vim . fn . sockconnect ( 'pipe' , invalid_pipe ) 546end , 10 ) 547end ) 548vim . uv . sleep ( 20 ) 549-- The server uv event loop is currently blocked, so the connection will 550-- be accepted when sockconnect() polls. 551local other_session =n . connect ( server ) 552eq ( { true , { 1000 } } , { other_session : request ( 'nvim_list_wins' ) } ) 553other_session : close () 554matches ( 555'^vim.schedule callback: Vim:connection failed: connection refused\n' , 556api . nvim_get_vvar ( 'errmsg' ) 557) 558end ) 559end ) 560 561it ( 'no stack-buffer-overflow with Nvim exit during connection #39387' , function () 562local nvim0 =n . get_session () 563-- Need a valid pipe so that connecting to it doesn't fail immediately. 564local server =fn . serverstart () 565finally ( function () 566nvim0 : close () 567end ) 568 569n . set_session ( n . new_session ( true )) 570exec_lua ( function () 571vim . defer_fn ( function () 572vim . uv . sleep ( 50 ) -- Block the uv event loop. 573vim . fn . sockconnect ( 'pipe' , server ) 574end , 10 ) 575end ) 576vim . uv . sleep ( 20 ) 577-- The server uv event loop is currently blocked, so the channel close will 578-- be processed when sockconnect() polls. 579n . check_close () 580end ) 581end ) 582 583describe ( 'loopback' , function () 584before_each ( function () 585clear () 586command ( "let chan = sockconnect('pipe', v:servername, {'rpc': v:true})" ) 587end ) 588 589it ( 'does not crash when sending raw data' , function () 590eq ( 591"Vim(call):Can't send raw data to rpc channel" , 592pcall_err ( command , "call chansend(chan, 'test')" ) 593) 594assert_alive () 595end ) 596 597it ( 'are released when closed' , function () 598local chans =eval ( 'len(nvim_list_chans())' ) 599command ( 'call chanclose(chan)' ) 600n . poke_eventloop () -- Process rpc_close_event(). 601-- Channel has been released after processing free_channel_event(). 602eq ( chans - 1 , eval ( 'len(nvim_list_chans())' )) 603end ) 604end )