robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
e16d577f16
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 , after_each , finally = 6t . describe , t . it , t . before_each , t . after_each , t . finally 7local assert_alive =n . assert_alive 8local clear , poke_eventloop =n . clear , n . poke_eventloop 9local testprg , source , eq , neq =n . testprg , n . source , t . eq , t . neq 10local feed =n . feed 11local eval =n . eval 12local fn =n . fn 13local api =n . api 14local exec_lua =n . exec_lua 15local retry =t . retry 16local ok =t . ok 17local command =n . command 18local skip =t . skip 19local is_os =t . is_os 20 21describe ( ':terminal' , function () 22local screen 23 24before_each ( function () 25clear () 26screen =Screen . new ( 50 , 4 , { rgb =false } ) 27screen . _default_attr_ids =nil 28end ) 29 30it ( 'does not interrupt Press-ENTER prompt #2748' , function () 31-- Ensure that :messages shows Press-ENTER. 32source ( [[ 33echomsg "msg1" 34echomsg "msg2" 35echomsg "msg3" 36]] ) 37-- Invoke a command that emits frequent terminal activity. 38feed ( [[:terminal "]] .. testprg ( 'shell-test' ) .. [[" REP 9999 !terminal_output!<cr>]] ) 39feed ( [[<C-\><C-N>]] ) 40poke_eventloop () 41-- Wait for some terminal activity. 42retry ( nil , 4000 , function () 43ok ( fn . line ( '$' ) > 6 ) 44end ) 45feed ( ':messages<CR>' ) 46screen : expect ( [[ 47msg1 | 48msg2 | 49msg3 | 50Press ENTER or type command to continue^ | 51]] ) 52end ) 53 54it ( 'reads output buffer on terminal reporting #4151' , function () 55skip ( is_os ( 'win' )) 56if is_os ( 'win' ) then 57command ( 58[[terminal powershell -NoProfile -NoLogo -Command Write-Host -NoNewline "\"$([char]27)[6n\""; Start-Sleep -Milliseconds 500 ]] 59) 60else 61command ( [[terminal printf '\e[6n'; sleep 0.5 ]] ) 62end 63screen : expect { any ='%^%[%[1;1R' } 64end ) 65 66it ( 'in normal-mode :split does not move cursor' , function () 67if is_os ( 'win' ) then 68command ( 69[[terminal for /L \\%I in (1,0,2) do ( echo foo & ping -w 100 -n 1 127.0.0.1 > nul )]] 70) 71else 72command ( [[terminal while true; do echo foo; sleep .1; done]] ) 73end 74feed ( [[<C-\><C-N>M]] ) -- move cursor away from last line 75poke_eventloop () 76eq ( 3 , eval ( "line('$')" )) -- window height 77eq ( 2 , eval ( "line('.')" )) -- cursor is in the middle 78feed ( ':vsplit<CR>' ) 79eq ( 2 , eval ( "line('.')" )) -- cursor stays where we put it 80feed ( ':split<CR>' ) 81eq ( 2 , eval ( "line('.')" )) -- cursor stays where we put it 82end ) 83 84it ( 'Enter/Leave does not increment jumplist #3723' , function () 85feed ( ':terminal<CR>' ) 86local function enter_and_leave () 87local lines_before =fn . line ( '$' ) 88-- Create a new line (in the shell). For a normal buffer this 89-- increments the jumplist; for a terminal-buffer it should not. #3723 90feed ( 'i' ) 91poke_eventloop () 92feed ( '<CR><CR><CR><CR>' ) 93poke_eventloop () 94feed ( [[<C-\><C-N>]] ) 95poke_eventloop () 96-- Wait for >=1 lines to be created. 97retry ( nil , 4000 , function () 98ok ( fn . line ( '$' ) > lines_before ) 99end ) 100end 101enter_and_leave () 102enter_and_leave () 103enter_and_leave () 104ok ( fn . line ( '$' ) > 6 ) -- Verify assumption. 105local jumps =fn . split ( fn . execute ( 'jumps' ), '\n' ) 106eq ( ' jump line col file/text' , jumps [ 1 ]) 107eq ( 3 , # jumps ) 108end ) 109 110it ( "triggers 'autowrite'" , function () 111api . nvim_set_option_value ( 'autowrite' , true , {} ) 112command ( 'vnew Xtermautowritetestfile | setlocal fileformat=unix' ) 113finally ( function () 114os . remove ( 'Xtermautowritetestfile' ) 115end ) 116fn . setline ( 1 , 'test content' ) 117command ( 'terminal' ) 118eq ( 'test content\n' , t . read_file ( 'Xtermautowritetestfile' )) 119end ) 120 121it ( 'nvim_get_mode() in :terminal' , function () 122command ( 'terminal' ) 123eq ( { blocking =false , mode ='nt' } , api . nvim_get_mode ()) 124feed ( 'i' ) 125eq ( { blocking =false , mode ='t' } , api . nvim_get_mode ()) 126feed ( [[<C-\><C-N>]] ) 127eq ( { blocking =false , mode ='nt' } , api . nvim_get_mode ()) 128end ) 129 130it ( ':stopinsert RPC request exits terminal-mode #7807' , function () 131command ( 'terminal' ) 132feed ( 'i[tui] insert-mode' ) 133eq ( { blocking =false , mode ='t' } , api . nvim_get_mode ()) 134command ( 'stopinsert' ) 135feed ( '<Ignore>' ) -- Add input to separate two RPC requests 136eq ( { blocking =false , mode ='nt' } , api . nvim_get_mode ()) 137end ) 138 139it ( ":stopinsert in normal mode doesn't break insert mode #9889" , function () 140command ( 'terminal' ) 141eq ( { blocking =false , mode ='nt' } , api . nvim_get_mode ()) 142command ( 'stopinsert' ) 143feed ( '<Ignore>' ) -- Add input to separate two RPC requests 144eq ( { blocking =false , mode ='nt' } , api . nvim_get_mode ()) 145feed ( 'a' ) 146eq ( { blocking =false , mode ='t' } , api . nvim_get_mode ()) 147end ) 148 149it ( 'switching to terminal buffer in Insert mode goes to Terminal mode #7164' , function () 150command ( 'terminal' ) 151command ( 'vnew' ) 152feed ( 'i' ) 153command ( 'let g:events = []' ) 154command ( 'autocmd InsertLeave * let g:events += ["InsertLeave"]' ) 155command ( 'autocmd TermEnter * let g:events += ["TermEnter"]' ) 156command ( 'inoremap <F2> <Cmd>wincmd p<CR>' ) 157eq ( { blocking =false , mode ='i' } , api . nvim_get_mode ()) 158feed ( '<F2>' ) 159eq ( { blocking =false , mode ='t' } , api . nvim_get_mode ()) 160eq ( { 'InsertLeave' , 'TermEnter' } , eval ( 'g:events' )) 161end ) 162 163it ( 'switching to terminal buffer immediately after :stopinsert #27031' , function () 164command ( 'terminal' ) 165command ( 'vnew' ) 166feed ( 'i' ) 167eq ( { blocking =false , mode ='i' } , api . nvim_get_mode ()) 168command ( 'stopinsert | wincmd p' ) 169feed ( '<Ignore>' ) -- Add input to separate two RPC requests 170eq ( { blocking =false , mode ='nt' } , api . nvim_get_mode ()) 171end ) 172 173it ( 'switching to another terminal buffer in Terminal mode' , function () 174command ( 'terminal' ) 175local buf0 =api . nvim_get_current_buf () 176command ( 'terminal' ) 177local buf1 =api . nvim_get_current_buf () 178command ( 'terminal' ) 179local buf2 =api . nvim_get_current_buf () 180neq ( buf0 , buf1 ) 181neq ( buf0 , buf2 ) 182neq ( buf1 , buf2 ) 183feed ( 'i' ) 184eq ( { blocking =false , mode ='t' } , api . nvim_get_mode ()) 185api . nvim_set_current_buf ( buf1 ) 186eq ( { blocking =false , mode ='t' } , api . nvim_get_mode ()) 187api . nvim_set_current_buf ( buf0 ) 188eq ( { blocking =false , mode ='t' } , api . nvim_get_mode ()) 189exec_lua ( function () 190vim . api . nvim_set_current_buf ( buf1 ) 191vim . api . nvim_buf_delete ( buf0 , { force =true } ) 192end ) 193eq ( { blocking =false , mode ='t' } , api . nvim_get_mode ()) 194api . nvim_set_current_buf ( buf2 ) 195eq ( { blocking =false , mode ='t' } , api . nvim_get_mode ()) 196api . nvim_set_current_buf ( buf1 ) 197eq ( { blocking =false , mode ='t' } , api . nvim_get_mode ()) 198end ) 199end ) 200 201local function test_terminal_with_fake_shell ( backslash ) 202-- shell-test.c is a fake shell that prints its arguments and exits. 203local shell_path =testprg ( 'shell-test' ) 204if backslash then 205shell_path =shell_path : gsub ( '/' , [[\]] ) 206end 207 208local screen 209 210before_each ( function () 211clear () 212screen =Screen . new ( 50 , 4 , { rgb =false } ) 213screen . _default_attr_ids =nil 214api . nvim_set_option_value ( 'shell' , shell_path , {} ) 215api . nvim_set_option_value ( 'shellcmdflag' , 'EXE' , {} ) 216api . nvim_set_option_value ( 'shellxquote' , '' , {} ) -- win: avoid extra quotes 217t . mkdir ( 'Xsomedir' ) 218t . write_file ( 'Xsomedir/Xuniquefile' , '' ) 219end ) 220 221after_each ( function () 222n . rmdir ( 'Xsomedir' ) 223end ) 224 225it ( 'with no argument, acts like jobstart(…,{term=true})' , function () 226command ( 'autocmd! nvim.terminal TermClose' ) 227command ( 'terminal' ) 228screen : expect ( [[ 229^ready $ | 230| 231|*2 232]] ) 233end ) 234 235it ( "with no argument, and 'shell' is set to empty string" , function () 236api . nvim_set_option_value ( 'shell' , '' , {} ) 237eq ( "Vim(terminal):E91: 'shell' option is empty" , t . pcall_err ( command , 'terminal' )) 238end ) 239 240it ( "with no argument, but 'shell' has arguments, acts like jobstart(…,{term=true})" , function () 241api . nvim_set_option_value ( 'shell' , shell_path .. ' INTERACT' , {} ) 242command ( 'terminal' ) 243screen : expect ( [[ 244^interact $ | 245|*3 246]] ) 247end ) 248 249it ( 'executes a given command through the shell' , function () 250command ( 'terminal echo hi' ) 251screen : expect ( [[ 252^ready $ echo hi | 253| 254[Process exited 0] | 255| 256]] ) 257end ) 258 259it ( "executes a given command through the shell, when 'shell' has arguments" , function () 260api . nvim_set_option_value ( 'shell' , shell_path .. ' -t jeff' , {} ) 261command ( 'terminal echo hi' ) 262screen : expect ( [[ 263^jeff $ echo hi | 264| 265[Process exited 0] | 266| 267]] ) 268end ) 269 270it ( 'spawns in CWD effective at time of invocation' , function () 271-- Run "echo" so the default TermClose handler does not auto-delete an exitcode=0 shell. 272command ( 'terminal echo' ) 273local dir =fn . bufname (): match ( '^term://(.-)//' ) 274local parentdir =fn . fnamemodify ( fn . getcwd (), ':h' ) -- Absolute, so 'cdpath' cannot interfere. 275command (( 'bcd %s' ): format ( fn . fnameescape ( parentdir ))) -- :terminal should use this CWD. 276command ( 'terminal echo' ) 277eq ( fn . fnamemodify ( dir , ':h' ), fn . bufname (): match ( '^term://(.-)//' )) 278end ) 279 280it ( 'allows quotes and slashes' , function () 281command ( [[terminal echo 'hello' \ "world"]] ) 282screen : expect ( [[ 283^ready $ echo 'hello' \ "world" | 284| 285[Process exited 0] | 286| 287]] ) 288end ) 289 290it ( 'ex_terminal() double-free #4554' , function () 291source ( [[ 292autocmd BufNew * set shell=foo 293terminal]] ) 294-- Verify that BufNew actually fired (else the test is invalid). 295eq ( 'foo' , eval ( '&shell' )) 296end ) 297 298it ( 'ignores writes if the backing stream closes' , function () 299command ( 'autocmd! nvim.terminal TermClose' ) 300command ( 'terminal' ) 301feed ( 'iiXXXXXXX' ) 302poke_eventloop () 303-- Race: Though the shell exited (and streams were closed by SIGCHLD 304-- handler), :terminal cleanup is pending on the main-loop. 305-- This write should be ignored (not crash, #5445). 306feed ( 'iiYYYYYYY' ) 307assert_alive () 308end ) 309 310it ( 'works with findfile()' , function () 311command ( 'autocmd! nvim.terminal TermClose' ) 312command ( 'terminal' ) 313eq ( 'term://' , string . match ( eval ( 'bufname("%")' ), '^term://' )) 314eq ( 'Xsomedir/Xuniquefile' , eval ( 'findfile("Xsomedir/Xuniquefile", ".")' )) 315end ) 316 317it ( 'works with :find' , function () 318command ( 'autocmd! nvim.terminal TermClose' ) 319command ( 'terminal' ) 320screen : expect ( [[ 321^ready $ | 322| 323|*2 324]] ) 325eq ( 'term://' , string . match ( eval ( 'bufname("%")' ), '^term://' )) 326feed ( [[<C-\><C-N>]] ) 327command ( [[find */Xuniquefile]] ) 328eq ( 'Xsomedir/Xuniquefile' , eval ( 'bufname("%")' )) 329end ) 330 331it ( 'works with gf' , function () 332command ( [[terminal echo "Xsomedir/Xuniquefile"]] ) 333screen : expect ( [[ 334^ready $ echo "Xsomedir/Xuniquefile" | 335| 336[Process exited 0] | 337| 338]] ) 339feed ( [[<C-\><C-N>]] ) 340eq ( 'term://' , string . match ( eval ( 'bufname("%")' ), '^term://' )) 341feed ( [[ggf"lgf]] ) 342eq ( 'Xsomedir/Xuniquefile' , eval ( 'bufname("%")' )) 343end ) 344 345it ( 'with bufhidden=delete #3958' , function () 346command ( 'set hidden' ) 347eq ( 1 , eval ( '&hidden' )) 348command ( 'autocmd BufNew * setlocal bufhidden=delete' ) 349for _ =1 , 5 do 350source ( [[ 351execute 'edit '.reltimestr(reltime()) 352terminal]] ) 353end 354end ) 355 356describe ( 'exit does not have long delay #27615' , function () 357for _ , ut in ipairs ( { 5 , 50 , 500 , 5000 , 50000 , 500000 } ) do 358it (( 'with updatetime=%d' ): format ( ut ), function () 359api . nvim_set_option_value ( 'updatetime' , ut , {} ) 360api . nvim_set_option_value ( 'shellcmdflag' , 'EXIT' , {} ) 361command ( 'terminal 42' ) 362screen : expect ( [[ 363^ | 364[Process exited 42] | 365|*2 366]] ) 367end ) 368end 369end ) 370end 371 372describe ( ':terminal (fake shell)' , function () 373test_terminal_with_fake_shell ( false ) 374if is_os ( 'win' ) then 375describe ( "when 'shell' uses backslashes" , function () 376test_terminal_with_fake_shell ( true ) 377end ) 378end 379end )