robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3local Screen =require ( 'test.functional.ui.screen' ) 4 5local describe , it , before_each =t . describe , t . it , t . before_each 6local clear =n . clear 7local eq =t . eq 8local eval =n . eval 9local command =n . command 10local pcall_err =t . pcall_err 11local feed =n . feed 12local poke_eventloop =n . poke_eventloop 13local is_os =t . is_os 14local api =n . api 15local async_meths =n . async_meths 16local testprg =n . testprg 17local assert_alive =n . assert_alive 18 19describe ( 'terminal channel is closed and later released if' , function () 20local screen 21 22before_each ( function () 23clear () 24screen =Screen . new () 25end ) 26 27it ( 'opened by nvim_open_term() and deleted by :bdelete!' , function () 28command ( [[let id = nvim_open_term(0, {})]] ) 29local chans =eval ( 'len(nvim_list_chans())' ) 30-- Channel hasn't been released yet. 31eq ( 32"Vim(call):Can't send data to closed stream" , 33pcall_err ( command , [[bdelete! | call chansend(id, 'test')]] ) 34) 35-- Channel has been released after processing free_channel_event(). 36eq ( chans - 1 , eval ( 'len(nvim_list_chans())' )) 37 38command ( 'autocmd BufWipeout * ++once let id2 = nvim_open_term(str2nr(expand("<abuf>")), {})' ) 39-- Channel hasn't been released yet. 40eq ( 41"Vim(call):Can't send data to closed stream" , 42pcall_err ( command , [[bdelete! | call chansend(id2, 'test')]] ) 43) 44-- Channel has been released after processing free_channel_event(). 45eq ( chans - 1 , eval ( 'len(nvim_list_chans())' )) 46end ) 47 48it ( 'opened by nvim_open_term(), closed by chanclose(), and deleted by pressing a key' , function () 49command ( 'let id = nvim_open_term(0, {})' ) 50local chans =eval ( 'len(nvim_list_chans())' ) 51-- Channel has been closed but not released. 52eq ( 53"Vim(call):Can't send data to closed stream" , 54pcall_err ( command , [[call chanclose(id) | call chansend(id, 'test')]] ) 55) 56screen : expect ( { any ='%[Terminal closed%]' } ) 57eq ( chans , eval ( 'len(nvim_list_chans())' )) 58feed ( 'i<CR>' ) -- Delete terminal. 59poke_eventloop () -- Process pending input. 60-- Channel has been released after processing free_channel_event(). 61eq ( chans - 1 , eval ( 'len(nvim_list_chans())' )) 62end ) 63 64it ( 'opened by nvim_open_term(), closed by chanclose(), and deleted by :bdelete' , function () 65command ( 'let id = nvim_open_term(0, {})' ) 66local chans =eval ( 'len(nvim_list_chans())' ) 67-- Channel has been closed but not released. 68eq ( 69"Vim(call):Can't send data to closed stream" , 70pcall_err ( command , [[call chanclose(id) | call chansend(id, 'test')]] ) 71) 72screen : expect ( { any ='%[Terminal closed%]' } ) 73eq ( chans , eval ( 'len(nvim_list_chans())' )) 74-- Channel still hasn't been released yet. 75eq ( 76"Vim(call):Can't send data to closed stream" , 77pcall_err ( command , [[bdelete | call chansend(id, 'test')]] ) 78) 79-- Channel has been released after processing free_channel_event(). 80eq ( chans - 1 , eval ( 'len(nvim_list_chans())' )) 81end ) 82 83it ( 'opened by jobstart(…,{term=true}), exited, and deleted by pressing a key' , function () 84command ( [[let id = jobstart('echo',{'term':v:true})]] ) 85local chans =eval ( 'len(nvim_list_chans())' ) 86-- Wait for process to exit. 87screen : expect ( { any ='%[Process exited 0%]' } ) 88-- Process has exited but channel has't been released. 89eq ( 90"Vim(call):Can't send data to closed stream" , 91pcall_err ( command , [[call chansend(id, 'test')]] ) 92) 93eq ( chans , eval ( 'len(nvim_list_chans())' )) 94feed ( 'i<CR>' ) -- Delete terminal. 95poke_eventloop () -- Process pending input. 96poke_eventloop () -- Process term_delayed_free(). 97-- Channel has been released after processing free_channel_event(). 98eq ( chans - 1 , eval ( 'len(nvim_list_chans())' )) 99end ) 100 101-- This indirectly covers #16264 102it ( 'opened by jobstart(…,{term=true}), exited, and deleted by :bdelete' , function () 103command ( [[let id = jobstart('echo', {'term':v:true})]] ) 104local chans =eval ( 'len(nvim_list_chans())' ) 105-- Wait for process to exit. 106screen : expect ( { any ='%[Process exited 0%]' } ) 107-- Process has exited but channel hasn't been released. 108eq ( 109"Vim(call):Can't send data to closed stream" , 110pcall_err ( command , [[call chansend(id, 'test')]] ) 111) 112eq ( chans , eval ( 'len(nvim_list_chans())' )) 113-- Channel still hasn't been released yet. 114eq ( 115"Vim(call):Can't send data to closed stream" , 116pcall_err ( command , [[bdelete | call chansend(id, 'test')]] ) 117) 118poke_eventloop () -- Process term_delayed_free(). 119-- Channel has been released after processing free_channel_event(). 120eq ( chans - 1 , eval ( 'len(nvim_list_chans())' )) 121end ) 122end ) 123 124it ( 'chansend sends lines to terminal channel in proper order' , function () 125clear ( { args ={ '--cmd' , 'set laststatus=2' } } ) 126local screen =Screen . new ( 100 , 20 ) 127screen . _default_attr_ids =nil 128local shells =is_os ( 'win' ) and { 'cmd.exe' , 'pwsh.exe -nop' } or { 'sh' } 129-- Prompt which indicates the shell is ready to read. 130local prompt =is_os ( 'win' ) and '>' or '%$ ' 131for _ , sh in ipairs ( shells ) do 132command ( [[let id = jobstart(']] .. sh .. [[', {'term':v:true})]] ) 133screen : expect ( { any =prompt , attr_ids ={} } ) 134command ( [[call chansend(id, ['echo "hello"', 'echo "world"', ''])]] ) 135-- With PowerShell the command may be highlighted, so specify attr_ids = {}. 136screen : expect ( { any =[[echo "hello".*echo "world"]] , attr_ids ={} } ) 137command ( 'bdelete!' ) 138screen : expect ( { any ='%[No Name%]' } ) 139end 140end ) 141 142--- @param event string 143--- @param extra_tests fun(table, table)? 144local function test_autocmd_no_crash ( event , extra_tests ) 145local env ={} 146-- Use REPFAST for immediately output after start. 147local term_args ={ testprg ( 'shell-test' ), 'REPFAST' , '50' , 'TEST' } 148 149before_each ( function () 150clear () 151env . screen =Screen . new ( 60 , 4 ) 152command ( [[file Xoldbuf | call setline(1, 'OLDBUF') | enew]] ) 153-- Wait before :bwipe to avoid closing PTY master before the child calls setsid(), 154-- as that will cause SIGHUP to be also sent to the parent. 155-- Use vim.uv.sleep() which blocks the event loop. 156n . exec ( [[ 157func Wipe() 158lua vim.uv.sleep(5) 159bwipe! 160endfunc 161]] ) 162end ) 163 164local input_prompt_screen =[[ 165| 166{1:~ }|*2 167^ | 168]] 169local oldbuf_screen =[[ 170^OLDBUF | 171{1:~ }|*2 172| 173]] 174 175it ( 'processes job exit event when using jobstart(…,{term=true})' , function () 176api . nvim_create_autocmd ( event , { command ="call input('')" } ) 177async_meths . nvim_call_function ( 'jobstart' , { term_args , { term =true } } ) 178env . screen : expect ( input_prompt_screen ) 179vim . uv . sleep ( 20 ) 180feed ( '<CR>' ) 181env . screen : expect ( [[ 182^0: TEST | 1831: TEST | 1842: TEST | 185| 186]] ) 187feed ( 'i' ) 188env . screen : expect ( [[ 18948: TEST | 19049: TEST | 191^[Process exited 0] | 192{5:-- TERMINAL --} | 193]] ) 194feed ( '<CR>' ) 195env . screen : expect ( oldbuf_screen ) 196assert_alive () 197end ) 198 199it ( 'wipes buffer and processes events when using jobstart(…,{term=true})' , function () 200api . nvim_create_autocmd ( event , { command ="call Wipe() | call input('')" } ) 201async_meths . nvim_call_function ( 'jobstart' , { term_args , { term =true } } ) 202env . screen : expect ( input_prompt_screen ) 203vim . uv . sleep ( 20 ) 204feed ( '<CR>' ) 205env . screen : expect ( oldbuf_screen ) 206assert_alive () 207eq ( 'Xoldbuf' , eval ( 'bufname()' )) 208eq ( 0 , eval ( [[exists('b:term_title')]] )) 209end ) 210 211it ( 'processes :bwipe from TermClose when using jobstart(…,{term=true})' , function () 212local term_buf =api . nvim_get_current_buf () 213api . nvim_create_autocmd ( 'TermClose' , { command =( 'bwipe! %d' ): format ( term_buf ) } ) 214api . nvim_create_autocmd ( event , { command ="call input('')" , nested =true } ) 215async_meths . nvim_call_function ( 'jobstart' , { term_args , { term =true } } ) 216env . screen : expect ( input_prompt_screen ) 217vim . uv . sleep ( 20 ) 218feed ( '<CR>' ) 219env . screen : expect ( oldbuf_screen ) 220assert_alive () 221eq ( 'Xoldbuf' , eval ( 'bufname()' )) 222eq ( 0 , eval ( [[exists('b:term_title')]] )) 223end ) 224 225it ( 'only wipes buffer when using jobstart(…,{term=true})' , function () 226api . nvim_create_autocmd ( event , { command ='call Wipe()' } ) 227async_meths . nvim_call_function ( 'jobstart' , { term_args , { term =true } } ) 228env . screen : expect ( oldbuf_screen ) 229assert_alive () 230eq ( 'Xoldbuf' , eval ( 'bufname()' )) 231eq ( 0 , eval ( [[exists('b:term_title')]] )) 232end ) 233 234if extra_tests then 235extra_tests ( env , term_args ) 236end 237end 238 239describe ( 'no crash when TermOpen autocommand' , function () 240test_autocmd_no_crash ( 'TermOpen' , function ( env , term_args ) 241it ( 'wipes buffer and processes events when using nvim_open_term()' , function () 242api . nvim_create_autocmd ( 'TermOpen' , { command ="call Wipe() | call input('')" } ) 243async_meths . nvim_open_term ( 0 , {} ) 244env . screen : expect ( [[ 245| 246{1:~ }|*2 247^ | 248]] ) 249feed ( '<CR>' ) 250env . screen : expect ( [[ 251^OLDBUF | 252{1:~ }|*2 253| 254]] ) 255assert_alive () 256end ) 257 258it ( 'wipes buffer when using jobstart(…,{term=true}) during Nvim exit' , function () 259n . expect_exit ( n . exec_lua , function () 260vim . schedule ( function () 261vim . fn . jobstart ( term_args , { term =true } ) 262end ) 263vim . api . nvim_create_autocmd ( 'TermOpen' , { command ='call Wipe()' } ) 264vim . cmd ( 'qall!' ) 265end ) 266end ) 267end ) 268end ) 269 270describe ( 'no crash when BufFilePre autocommand' , function () 271test_autocmd_no_crash ( 'BufFilePre' ) 272end ) 273 274describe ( 'no crash when BufFilePost autocommand' , function () 275test_autocmd_no_crash ( 'BufFilePost' ) 276end ) 277 278describe ( 'nvim_open_term' , function () 279local screen 280 281before_each ( function () 282clear () 283screen =Screen . new ( 8 , 10 ) 284end ) 285 286it ( 'with force_crlf=true converts newlines' , function () 287local win =api . nvim_get_current_win () 288local buf =api . nvim_create_buf ( false , true ) 289local term =api . nvim_open_term ( buf , { force_crlf =true } ) 290api . nvim_win_set_buf ( win , buf ) 291api . nvim_chan_send ( term , 'here\nthere\nfoo\r\nbar\n\ntest' ) 292screen : expect ( [[ 293^here | 294there | 295foo | 296bar | 297| 298test | 299|*4 300]] ) 301api . nvim_chan_send ( term , '\nfirst' ) 302screen : expect ( [[ 303^here | 304there | 305foo | 306bar | 307| 308test | 309first | 310|*3 311]] ) 312end ) 313 314it ( 'with force_crlf=false does not convert newlines' , function () 315local win =api . nvim_get_current_win () 316local buf =api . nvim_create_buf ( false , true ) 317local term =api . nvim_open_term ( buf , { force_crlf =false } ) 318api . nvim_win_set_buf ( win , buf ) 319api . nvim_chan_send ( term , 'here\nthere' ) 320screen : expect ( [[ 321^here | 322there | 323|*8 324]] ) 325end ) 326end )