robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
fbcb7a056c
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3 4local describe , it , before_each =t . describe , t . it , t . before_each 5local assert_alive =n . assert_alive 6local command =n . command 7local feed_command =n . feed_command 8local feed =n . feed 9local eval =n . eval 10local eq =t . eq 11local run =n . run 12local pcall_err =t . pcall_err 13local exec_capture =n . exec_capture 14local poke_eventloop =n . poke_eventloop 15 16describe ( 'exit:' , function () 17local cid 18 19before_each ( function () 20n . clear () 21cid =n . api . nvim_get_chan_info ( 0 ). id 22end ) 23 24it ( 'v:exiting defaults to v:null' , function () 25eq ( 1 , eval ( 'v:exiting is v:null' )) 26eq ( '' , eval ( 'v:exitreason' )) 27end ) 28 29local function test_exiting ( setup_fn ) 30local function on_setup () 31command (( 'autocmd QuitPre * call rpcrequest(%d, "exit", "QuitPre")' ): format ( cid )) 32command (( 'autocmd ExitPre * call rpcrequest(%d, "exit", "ExitPre")' ): format ( cid )) 33command (( 'autocmd VimLeavePre * call rpcrequest(%d, "exit", "VimLeavePre")' ): format ( cid )) 34command (( 'autocmd VimLeave * call rpcrequest(%d, "exit", "VimLeave")' ): format ( cid )) 35setup_fn () 36end 37local received ={} 38local function on_request ( name , args ) 39eq ( 'exit' , name ) 40table . insert ( received , args ) 41eq ( 'quit' , eval ( 'v:exitreason' )) 42if args [ 1 ] == 'VimLeavePre' or args [ 1 ] == 'VimLeave' then 43eq ( 0 , eval ( 'v:exiting' )) 44end 45return '' 46end 47run ( on_request , nil , on_setup ) 48eq ( { { 'QuitPre' } , { 'ExitPre' } , { 'VimLeavePre' } , { 'VimLeave' } } , received ) 49end 50 51it ( 'v:exiting=0, v:exitreason=quit on normal exit' , function () 52test_exiting ( function () 53command ( 'quit' ) 54end ) 55end ) 56 57it ( 'v:exiting=0, v:exitreason=quit on exit from Ex mode try-catch vim-patch:8.0.0184' , function () 58test_exiting ( function () 59feed ( '1q:' ) 60feed_command ( 'try' , 'call NoFunction()' , 'catch' , 'echo "bye"' , 'endtry' , 'quit' ) 61end ) 62end ) 63 64it ( 'resets v:exitreason if quit is cancelled' , function () 65n . api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'modified' } ) 66pcall_err ( command , 'quit' ) 67eq ( '' , eval ( 'v:exitreason' )) 68end ) 69end ) 70 71describe ( ':cquit' , function () 72local function test_cq ( cmdline , exit_code , redir_msg ) 73if redir_msg then 74n . clear () 75eq ( 76redir_msg , 77pcall_err ( function () 78return exec_capture ( cmdline ) 79end ) 80) 81poke_eventloop () 82assert_alive () 83n . check_close () 84else 85local p =n . spawn_wait ( '--cmd' , cmdline ) 86eq ( exit_code , p . status ) 87end 88end 89 90it ( 'exits with non-zero after :cquit' , function () 91test_cq ( 'cquit' , 1 , nil ) 92end ) 93 94it ( 'exits with non-zero after :cquit 123' , function () 95test_cq ( 'cquit 123' , 123 , nil ) 96end ) 97 98it ( 'exits with non-zero after :123 cquit' , function () 99test_cq ( '123 cquit' , 123 , nil ) 100end ) 101 102it ( 'exits with 0 after :cquit 0' , function () 103test_cq ( 'cquit 0' , 0 , nil ) 104end ) 105 106it ( 'exits with 0 after :0 cquit' , function () 107test_cq ( '0 cquit' , 0 , nil ) 108end ) 109 110it ( 'exits with redir msg for multiple exit codes after :cquit 1 2' , function () 111test_cq ( 112'cquit 1 2' , 113nil , 114'nvim_exec2(), line 1: Vim(cquit):E488: Trailing characters: 2: cquit 1 2' 115) 116end ) 117 118it ( 'exits with redir msg for non-number exit code after :cquit X' , function () 119test_cq ( 120'cquit X' , 121nil , 122'nvim_exec2(), line 1: Vim(cquit):E488: Trailing characters: X: cquit X' 123) 124end ) 125 126it ( 'exits with redir msg for negative exit code after :cquit -1' , function () 127test_cq ( 128'cquit -1' , 129nil , 130'nvim_exec2(), line 1: Vim(cquit):E488: Trailing characters: -1: cquit -1' 131) 132end ) 133end ) 134 135describe ( 'when piping to stdin, no crash during exit' , function () 136before_each ( function () 137n . clear () 138end ) 139 140it ( 'after :quit non-last window in vim.schedule() callback #14379' , function () 141n . fn . system ( { 142n . nvim_prog , 143'-es' , 144'--cmd' , 145"lua vim.schedule(function() vim.cmd('vsplit | quit') end)" , 146'+quit' , 147} , '' ) 148eq ( 0 , n . api . nvim_get_vvar ( 'shell_error' )) 149end ) 150 151it ( 'after :quit non-last window in vim.defer_fn() callback #14379' , function () 152n . fn . system ( { 153n . nvim_prog , 154'-es' , 155'--cmd' , 156"lua vim.defer_fn(function() vim.cmd('vsplit | quit') end, 0)" , 157'+quit' , 158} , '' ) 159eq ( 0 , n . api . nvim_get_vvar ( 'shell_error' )) 160end ) 161 162it ( 'after closing v:stderr channel' , function () 163n . fn . system ( { 164n . nvim_prog , 165'-es' , 166'--cmd' , 167'call chanclose(v:stderr)' , 168'+quit' , 169} , '' ) 170eq ( 0 , n . api . nvim_get_vvar ( 'shell_error' )) 171end ) 172end )