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 , after_each =t . describe , t . it , t . before_each , t . after_each 5local clear =n . clear 6local command =n . command 7local eq =t . eq 8local exec_capture =n . exec_capture 9local exec_lua =n . exec_lua 10local expect =n . expect 11local fn =n . fn 12local insert =n . insert 13local nvim_prog =n . nvim_prog 14local neq =t . neq 15local set_session =n . set_session 16local tmpname =t . tmpname 17local write_file =t . write_file 18 19describe ( 'Remote' , function () 20local fname , other_fname 21local contents ='The call is coming from outside the process' 22local other_contents ="A second file's contents" 23 24before_each ( function () 25fname =tmpname () .. ' with spaces in the filename' 26other_fname =tmpname () 27write_file ( fname , contents ) 28write_file ( other_fname , other_contents ) 29end ) 30 31describe ( 'connect to server and' , function () 32local server 33before_each ( function () 34server =n . clear () 35end ) 36 37after_each ( function () 38server : close () 39end ) 40 41-- Run a `nvim --remote*` command and return { stdout, stderr } of the process 42local function run_remote ( ... ) 43set_session ( server ) 44local addr =fn . serverlist ()[ 1 ] 45 46-- Create an nvim instance just to run the remote-invoking nvim. We want 47-- to wait for the remote instance to exit and calling jobwait blocks 48-- the event loop. If the server event loop is blocked, it can't process 49-- our incoming --remote calls. 50local client_starter =n . new_session ( true ) 51set_session ( client_starter ) 52-- Call jobstart() and jobwait() in the same RPC request to reduce flakiness. 53eq ( 54{ 0 } , 55exec_lua ( 56[[return vim.fn.jobwait({ vim.fn.jobstart({...}, { 57stdout_buffered = true, 58stderr_buffered = true, 59on_stdout = function(_, data, _) 60_G.Remote_stdout = table.concat(data, '\n') 61end, 62on_stderr = function(_, data, _) 63_G.Remote_stderr = table.concat(data, '\n') 64end, 65}) })]] , 66nvim_prog , 67'--clean' , 68'--headless' , 69'--server' , 70addr , 71... 72) 73) 74local res =exec_lua ( [[return { _G.Remote_stdout, _G.Remote_stderr }]] ) 75client_starter : close () 76set_session ( server ) 77return res 78end 79 80it ( 'edit a single file' , function () 81eq ( { '' , '' } , run_remote ( '--remote' , fname )) 82expect ( contents ) 83eq ( 1 , # fn . getbufinfo ()) 84end ) 85 86it ( 'tab edit a single file with a non-changed buffer' , function () 87eq ( { '' , '' } , run_remote ( '--remote-tab' , fname )) 88expect ( contents ) 89eq ( 1 , # fn . gettabinfo ()) 90end ) 91 92it ( 'tab edit a single file with a changed buffer' , function () 93insert ( 'hello' ) 94eq ( { '' , '' } , run_remote ( '--remote-tab' , fname )) 95expect ( contents ) 96eq ( 2 , # fn . gettabinfo ()) 97end ) 98 99it ( 'edit multiple files' , function () 100eq ( { '' , '' } , run_remote ( '--remote' , fname , other_fname )) 101expect ( contents ) 102command ( 'next' ) 103expect ( other_contents ) 104eq ( 2 , # fn . getbufinfo ()) 105end ) 106 107it ( 'send keys' , function () 108eq ( { '' , '' } , run_remote ( '--remote-send' , ':edit ' .. fname .. '<CR><C-W>v' )) 109expect ( contents ) 110eq ( 2 , # fn . getwininfo ()) 111-- Only a single buffer as we're using edit and not drop like --remote does 112eq ( 1 , # fn . getbufinfo ()) 113end ) 114 115it ( 'evaluate expressions' , function () 116eq ( { '0' , '' } , run_remote ( '--remote-expr' , 'setline(1, "Yo")' )) 117eq ( { 'Yo' , '' } , run_remote ( '--remote-expr' , 'getline(1)' )) 118expect ( 'Yo' ) 119eq ( { ( 'k' ): rep ( 1234 ), '' } , run_remote ( '--remote-expr' , 'repeat("k", 1234)' )) 120eq ( { '1.25' , '' } , run_remote ( '--remote-expr' , '1.25' )) 121eq ( { 'no' , '' } , run_remote ( '--remote-expr' , '0z6E6F' )) 122eq ( { '\t' , '' } , run_remote ( '--remote-expr' , '"\t"' )) 123end ) 124end ) 125 126it ( 'creates server if not found' , function () 127clear ( '--remote' , fname ) 128expect ( contents ) 129eq ( 1 , # fn . getbufinfo ()) 130-- Since we didn't pass silent, we should get a complaint 131neq ( nil , string . find ( exec_capture ( 'messages' ), 'E247:' )) 132end ) 133 134it ( 'creates server if not found with tabs' , function () 135clear ( '--remote-tab-silent' , fname , other_fname ) 136expect ( contents ) 137eq ( 2 , # fn . gettabinfo ()) 138eq ( 2 , # fn . getbufinfo ()) 139-- We passed silent, so no message should be issued about the server not being found 140eq ( nil , string . find ( exec_capture ( 'messages' ), 'E247:' )) 141end ) 142 143describe ( 'exits with error on' , function () 144local function run_and_check_exit_code ( ... ) 145local p =n . spawn_wait { args ={ ... } } 146eq ( 2 , p . status ) 147end 148it ( 'bogus subcommand' , function () 149run_and_check_exit_code ( '--remote-bogus' ) 150end ) 151 152it ( 'send without server' , function () 153run_and_check_exit_code ( '--remote-send' , 'i' ) 154end ) 155 156it ( 'expr without server' , function () 157run_and_check_exit_code ( '--remote-expr' , 'setline(1, "Yo")' ) 158end ) 159it ( 'wait subcommand' , function () 160run_and_check_exit_code ( '--remote-wait' , fname ) 161end ) 162end ) 163end )