robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f907614088
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3local Screen =require ( 'test.functional.ui.screen' ) 4local describe , it , before_each =t . describe , t . it , t . before_each 5local uv =vim . uv 6 7local clear , command , testprg =n . clear , n . command , n . testprg 8local eval , eq , neq , retry =n . eval , t . eq , t . neq , t . retry 9local exec_lua =n . exec_lua 10local matches =t . matches 11local ok =t . ok 12local feed =n . feed 13local api =n . api 14local pcall_err =t . pcall_err 15local assert_alive =n . assert_alive 16local skip =t . skip 17local is_os =t . is_os 18 19describe ( 'autocmd TermClose' , function () 20before_each ( function () 21clear () 22api . nvim_set_option_value ( 'shell' , testprg ( 'shell-test' ), {} ) 23command ( 'set shellcmdflag=EXE shellredir= shellpipe= shellquote= shellxquote=' ) 24command ( 'autocmd! nvim.terminal TermClose' ) 25end ) 26 27local function test_termclose_delete_own_buf () 28-- The terminal process needs to keep running so that TermClose isn't triggered immediately. 29api . nvim_set_option_value ( 'shell' , string . format ( '"%s" INTERACT' , testprg ( 'shell-test' )), {} ) 30command ( 'terminal' ) 31local termbuf =api . nvim_get_current_buf () 32command (( 'autocmd TermClose * bdelete! %d' ): format ( termbuf )) 33matches ( 34'^TermClose Autocommands for "%*": Vim%(bdelete%):E937: Attempt to delete a buffer that is in use: term://' , 35pcall_err ( command , 'bdelete!' ) 36) 37assert_alive () 38end 39 40it ( 'TermClose deleting its own buffer, altbuf = buffer 1 #10386' , function () 41test_termclose_delete_own_buf () 42end ) 43 44it ( 'TermClose deleting its own buffer, altbuf NOT buffer 1 #10386' , function () 45command ( 'edit foo1' ) 46test_termclose_delete_own_buf () 47end ) 48 49it ( 'TermClose deleting all other buffers' , function () 50local oldbuf =api . nvim_get_current_buf () 51-- The terminal process needs to keep running so that TermClose isn't triggered immediately. 52api . nvim_set_option_value ( 'shell' , string . format ( '"%s" INTERACT' , testprg ( 'shell-test' )), {} ) 53command (( 'autocmd TermClose * bdelete! %d' ): format ( oldbuf )) 54command ( 'horizontal terminal' ) 55neq ( oldbuf , api . nvim_get_current_buf ()) 56command ( 'bdelete!' ) 57feed ( '<C-G>' ) -- This shouldn't crash due to having a 0-line buffer. 58assert_alive () 59end ) 60 61it ( 'TermClose switching back to terminal buffer' , function () 62local buf =api . nvim_get_current_buf () 63api . nvim_open_term ( buf , {} ) 64command (( 'autocmd TermClose * buffer %d | new' ): format ( buf )) 65eq ( 66'TermClose Autocommands for "*": Vim(buffer):E1546: Cannot switch to a closing buffer' , 67pcall_err ( command , 'bwipe!' ) 68) 69assert_alive () 70end ) 71 72it ( 'triggers when fast-exiting terminal job stops' , function () 73command ( 'autocmd TermClose * let g:test_termclose = 23' ) 74command ( 'terminal' ) 75-- shell-test exits immediately. 76retry ( nil , nil , function () 77neq ( - 1 , eval ( 'jobwait([&channel], 0)[0]' )) 78end ) 79retry ( nil , nil , function () 80eq ( 23 , eval ( 'g:test_termclose' )) 81end ) 82end ) 83 84it ( 'triggers when long-running terminal job gets stopped' , function () 85api . nvim_set_option_value ( 'shell' , is_os ( 'win' ) and 'cmd.exe' or 'sh' , {} ) 86command ( 'autocmd TermClose * let g:test_termclose = 23' ) 87command ( 'terminal' ) 88command ( 'call jobstop(b:terminal_job_id)' ) 89retry ( nil , nil , function () 90eq ( 23 , eval ( 'g:test_termclose' )) 91end ) 92end ) 93 94it ( 'kills job trapping SIGTERM' , function () 95skip ( is_os ( 'win' ), 'N/A for Windows' ) 96api . nvim_set_option_value ( 'shell' , 'sh' , {} ) 97api . nvim_set_option_value ( 'shellcmdflag' , '-c' , {} ) 98command ( 99[[ let g:test_job = jobstart('trap "" TERM && echo 1 && sleep 60', { ]] 100.. [[ 'on_stdout': {-> execute('let g:test_job_started = 1')}, ]] 101.. [[ 'on_exit': {-> execute('let g:test_job_exited = 1')}}) ]] 102) 103retry ( nil , nil , function () 104eq ( 1 , eval ( 'get(g:, "test_job_started", 0)' )) 105end ) 106 107uv . update_time () 108local start =uv . now () 109command ( 'call jobstop(g:test_job)' ) 110retry ( nil , nil , function () 111eq ( 1 , eval ( 'get(g:, "test_job_exited", 0)' )) 112end ) 113uv . update_time () 114local duration =uv . now () - start 115-- Nvim begins SIGTERM after KILL_TIMEOUT_MS. 116ok ( duration >= 2000 ) 117ok ( duration <= 4000 ) -- Epsilon for slow CI 118end ) 119 120it ( 'kills PTY job trapping SIGHUP and SIGTERM' , function () 121skip ( is_os ( 'win' ), 'N/A for Windows' ) 122api . nvim_set_option_value ( 'shell' , 'sh' , {} ) 123api . nvim_set_option_value ( 'shellcmdflag' , '-c' , {} ) 124command ( 125[[ let g:test_job = jobstart('trap "" HUP TERM && echo 1 && sleep 60', { ]] 126.. [[ 'pty': 1,]] 127.. [[ 'on_stdout': {-> execute('let g:test_job_started = 1')}, ]] 128.. [[ 'on_exit': {-> execute('let g:test_job_exited = 1')}}) ]] 129) 130retry ( nil , nil , function () 131eq ( 1 , eval ( 'get(g:, "test_job_started", 0)' )) 132end ) 133 134uv . update_time () 135local start =uv . now () 136command ( 'call jobstop(g:test_job)' ) 137retry ( nil , nil , function () 138eq ( 1 , eval ( 'get(g:, "test_job_exited", 0)' )) 139end ) 140uv . update_time () 141local duration =uv . now () - start 142-- Nvim begins SIGKILL after (2 * KILL_TIMEOUT_MS). 143ok ( duration >= 4000 ) 144ok ( duration <= 7000 ) -- Epsilon for slow CI 145end ) 146 147it ( 'reports the correct <abuf>' , function () 148command ( 'set hidden' ) 149command ( 'set shellcmdflag=EXE' ) 150command ( 'autocmd TermClose * let g:abuf = expand("<abuf>")' ) 151command ( 'edit foo' ) 152command ( 'edit bar' ) 153eq ( 2 , eval ( 'bufnr("%")' )) 154 155command ( 'terminal ls' ) 156retry ( nil , nil , function () 157eq ( 3 , eval ( 'bufnr("%")' )) 158end ) 159 160command ( 'buffer 1' ) 161retry ( nil , nil , function () 162eq ( 1 , eval ( 'bufnr("%")' )) 163end ) 164 165command ( '3bdelete!' ) 166retry ( nil , nil , function () 167eq ( '3' , eval ( 'g:abuf' )) 168end ) 169feed ( '<c-c>' ) 170n . poke_eventloop () -- Wait for input to be flushed 171n . expect_exit ( n . load_adjust ( 1000 ), feed , ':qa!<cr>' ) 172end ) 173 174it ( 'exposes v:event.status' , function () 175command ( 'set shellcmdflag=EXIT' ) 176command ( 'autocmd TermClose * let g:status = v:event.status' ) 177 178command ( 'terminal 0' ) 179retry ( nil , nil , function () 180eq ( 0 , eval ( 'g:status' )) 181end ) 182 183command ( 'terminal 42' ) 184retry ( nil , nil , function () 185eq ( 42 , eval ( 'g:status' )) 186end ) 187 188command ( 'set shellcmdflag= | terminal INTERACT' ) 189retry ( nil , nil , function () 190matches ( '^interact %$ ?$' , api . nvim_buf_get_lines ( 0 , 0 , 1 , true )[ 1 ]) 191end ) 192command ( 'bwipe!' ) 193eq ( - 1 , eval ( 'g:status' )) 194end ) 195end ) 196 197it ( 'autocmd TermEnter, TermLeave' , function () 198clear () 199command ( 'let g:evs = []' ) 200command ( 'autocmd TermOpen * call add(g:evs, ["TermOpen", mode()])' ) 201command ( 'autocmd TermClose * call add(g:evs, ["TermClose", mode()])' ) 202command ( 'autocmd TermEnter * call add(g:evs, ["TermEnter", mode()])' ) 203command ( 'autocmd TermLeave * call add(g:evs, ["TermLeave", mode()])' ) 204command ( 'terminal' ) 205 206feed ( 'i' ) 207eq ( { { 'TermOpen' , 'n' } , { 'TermEnter' , 't' } } , eval ( 'g:evs' )) 208feed ( [[<C-\><C-n>]] ) 209feed ( 'A' ) 210eq ( 211{ { 'TermOpen' , 'n' } , { 'TermEnter' , 't' } , { 'TermLeave' , 'n' } , { 'TermEnter' , 't' } } , 212eval ( 'g:evs' ) 213) 214 215-- TermLeave is also triggered by :quit. 216command ( 'split foo' ) 217feed ( '<Ignore>' ) -- Add input to separate two RPC requests 218command ( 'wincmd w' ) 219feed ( 'i' ) 220command ( 'q!' ) 221feed ( '<Ignore>' ) -- Add input to separate two RPC requests 222eq ( { 223{ 'TermOpen' , 'n' } , 224{ 'TermEnter' , 't' } , 225{ 'TermLeave' , 'n' } , 226{ 'TermEnter' , 't' } , 227{ 'TermLeave' , 'n' } , 228{ 'TermEnter' , 't' } , 229{ 'TermClose' , 't' } , 230{ 'TermLeave' , 'n' } , 231} , eval ( 'g:evs' )) 232end ) 233 234describe ( 'autocmd TextChangedT,WinResized' , function () 235before_each ( clear ) 236 237it ( 'TextChangedT works' , function () 238local screen =Screen . new ( 50 , 7 ) 239screen : set_default_attr_ids ( { 240[ 1 ] ={ bold =true } , 241[ 31 ] ={ foreground =Screen . colors . Gray100 , background =Screen . colors . DarkGreen } , 242[ 32 ] ={ 243foreground =Screen . colors . Gray100 , 244bold =true , 245background =Screen . colors . DarkGreen , 246} , 247} ) 248 249local term , term_unfocused =exec_lua ( function () 250-- Split windows before opening terminals so TextChangedT doesn't fire an additional time due 251-- to the inner terminal being resized (which is usually deferred too). 252vim . cmd . vnew () 253local term_unfocused =vim . api . nvim_open_term ( 0 , {} ) 254vim . cmd . wincmd 'p' 255local term =vim . api . nvim_open_term ( 0 , {} ) 256vim . cmd . startinsert () 257return term , term_unfocused 258end ) 259eq ( 't' , eval ( 'mode()' )) 260 261exec_lua ( function () 262_G . n_triggered =0 263vim . api . nvim_create_autocmd ( 'TextChanged' , { 264callback =function () 265_G . n_triggered =_G . n_triggered + 1 266end , 267} ) 268_G . t_triggered =0 269vim . api . nvim_create_autocmd ( 'TextChangedT' , { 270callback =function () 271_G . t_triggered =_G . t_triggered + 1 272end , 273} ) 274end ) 275 276api . nvim_chan_send ( term , 'a' ) 277retry ( nil , nil , function () 278eq ( 1 , exec_lua ( 'return _G.t_triggered' )) 279end ) 280api . nvim_chan_send ( term , 'b' ) 281retry ( nil , nil , function () 282eq ( 2 , exec_lua ( 'return _G.t_triggered' )) 283end ) 284 285-- Not triggered by changes in a non-current terminal. 286api . nvim_chan_send ( term_unfocused , 'hello' ) 287screen : expect ( [[ 288hello │ab^ | 289│ |*4 290{31:[Scratch] [-] }{32:[Scratch] [-] }| 291{1:-- TERMINAL --} | 292]] ) 293eq ( 2 , exec_lua ( 'return _G.t_triggered' )) 294 295-- Not triggered by unflushed redraws. 296api . nvim__redraw ( { valid =false , flush =false } ) 297eq ( 2 , exec_lua ( 'return _G.t_triggered' )) 298 299-- Not triggered when not in terminal mode. 300command ( 'stopinsert' ) 301eq ( 'n' , eval ( 'mode()' )) 302eq ( 2 , exec_lua ( 'return _G.t_triggered' )) 303eq ( 0 , exec_lua ( 'return _G.n_triggered' )) -- Nothing we did was in Normal mode yet. 304 305api . nvim_chan_send ( term , 'c' ) 306screen : expect ( [[ 307hello │a^bc | 308│ |*4 309{31:[Scratch] [-] }{32:[Scratch] [-] }| 310| 311]] ) 312eq ( 1 , exec_lua ( 'return _G.n_triggered' )) -- Happened in Normal mode. 313end ) 314 315it ( 'no crash when deleting terminal buffer' , function () 316-- Using nvim_open_term over :terminal as the former can free the terminal immediately on 317-- close, causing the crash. 318 319-- WinResized 320local buf1 , term1 =exec_lua ( function () 321vim . cmd . new () 322local buf =vim . api . nvim_get_current_buf () 323local term =vim . api . nvim_open_term ( 0 , { 324on_input =function () 325vim . cmd . wincmd '_' 326end , 327} ) 328vim . api . nvim_create_autocmd ( 'WinResized' , { 329once =true , 330command ='bwipeout!' , 331} ) 332return buf , term 333end ) 334feed ( 'ii' ) 335eq ( false , api . nvim_buf_is_valid ( buf1 )) 336eq ( 'n' , eval ( 'mode()' )) 337eq ( {} , api . nvim_get_chan_info ( term1 )) -- Channel should've been cleaned up. 338 339-- TextChangedT 340local buf2 , term2 =exec_lua ( function () 341vim . cmd . new () 342local buf =vim . api . nvim_get_current_buf () 343local term =vim . api . nvim_open_term ( 0 , { 344on_input =function ( _ , chan ) 345vim . api . nvim_chan_send ( chan , 'sup' ) 346end , 347} ) 348vim . api . nvim_create_autocmd ( 'TextChangedT' , { 349once =true , 350command ='bwipeout!' , 351} ) 352return buf , term 353end ) 354feed ( 'ii' ) 355-- refresh_terminal is deferred, so TextChangedT may not trigger immediately. 356retry ( nil , nil , function () 357eq ( false , api . nvim_buf_is_valid ( buf2 )) 358end ) 359eq ( 'n' , eval ( 'mode()' )) 360eq ( {} , api . nvim_get_chan_info ( term2 )) -- Channel should've been cleaned up. 361end ) 362end ) 363 364describe ( 'no crash if :bwipe from TermClose is processed by' , function () 365local oldwin --- @type integer 366local chan --- @type integer 367 368before_each ( function () 369clear () 370command ( 'autocmd! nvim.terminal' ) 371oldwin =api . nvim_get_current_win () 372command ( 'new' ) 373local buf =api . nvim_get_current_buf () 374chan =api . nvim_open_term ( buf , {} ) 375api . nvim_set_var ( 'chan' , chan ) 376command (( 'autocmd TermClose <buffer> bwipe! %d' ): format ( buf )) 377command ( 'let g:done = 0' ) 378feed ( 'i' ) 379eq ( { mode ='t' , blocking =false } , api . nvim_get_mode ()) 380end ) 381 382--- @param event string Event name. 383--- @param trigger_cmd string The Ex command to trigger the event. 384local function test_case ( event , trigger_cmd ) 385api . nvim_create_autocmd ( 386event , 387{ nested =true , once =true , command ='sleep 40m | let g:done = 1' } 388) 389exec_lua ( function () 390vim . cmd ( trigger_cmd ) 391vim . defer_fn ( function () 392vim . fn . chanclose ( chan ) 393end , 25 ) 394end ) 395retry ( nil , 1000 , function () 396eq ( 1 , api . nvim_get_var ( 'done' )) 397end ) 398assert_alive () 399eq ( { mode ='n' , blocking =false } , api . nvim_get_mode ()) 400eq ( { oldwin } , api . nvim_list_wins ()) 401feed ( '<Ignore>' ) -- Add input to separate two RPC requests. 402-- Channel should have been released. 403eq ( {} , api . nvim_get_chan_info ( chan )) 404end 405 406it ( 'WinResized autocommand in Terminal mode' , function () 407test_case ( 'WinResized' , 'vsplit' ) 408end ) 409 410it ( 'TextChangedT autocommand in Terminal mode' , function () 411test_case ( 'TextChangedT' , [[call chansend(g:chan, "foo\r\nbar")]] ) 412end ) 413 414it ( 'TermRequest autocommand in Terminal mode' , function () 415test_case ( 'TermRequest' , [[call chansend(g:chan, "\x1b]11;?\x1b\\")]] ) 416end ) 417 418it ( 'TermRequest autocommand in Normal mode' , function () 419feed ( [[<C-\><C-N>]] ) 420eq ( { mode ='nt' , blocking =false } , api . nvim_get_mode ()) 421test_case ( 'TermRequest' , [[call chansend(g:chan, "\x1b]11;?\x1b\\")]] ) 422end ) 423end )