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 , after_each , pending , finally = 5t . describe , t . it , t . after_each , t . pending , t . finally 6local uv =vim . uv 7 8local api =n . api 9local feed =n . feed 10local eq =t . eq 11local neq =t . neq 12local clear =n . clear 13local ok =t . ok 14local fn =n . fn 15local nvim_prog =n . nvim_prog 16local retry =t . retry 17local write_file =t . write_file 18local assert_log =t . assert_log 19local check_close =n . check_close 20local is_os =t . is_os 21 22local testlog ='Xtest-embed-log' 23 24local function test_embed ( ext_linegrid ) 25local screen 26local function startup ( ... ) 27clear { args_rm ={ '--headless' } , args ={ ... } } 28 29-- attach immediately after startup, for early UI 30screen =Screen . new ( 60 , 8 , { ext_linegrid =ext_linegrid } ) 31screen : add_extra_attr_ids { 32[ 100 ] ={ foreground =Screen . colors . NvimDarkCyan } , 33[ 101 ] ={ foreground =Screen . colors . NvimDarkRed } , 34[ 102 ] ={ 35background =Screen . colors . NvimLightGrey4 , 36foreground =Screen . colors . NvimDarkGrey2 , 37} , 38} 39end 40 41it ( 'can display errors' , function () 42startup ( '--cmd' , 'echoerr invalid+' ) 43screen : expect ( [[ 44|*4 45{3: }| 46{9:Error in pre-vimrc command line:} | 47{9:E121: Undefined variable: invalid} | 48{6:Press ENTER or type command to continue}^ | 49]] ) 50 51feed ( '<cr>' ) 52screen : expect ( [[ 53^ | 54{1:~ }|*6 55| 56]] ) 57end ) 58 59it ( "doesn't erase output when setting color scheme" , function () 60if t . is_os ( 'openbsd' ) then 61pending ( 'FIXME #10804' ) 62end 63startup ( '--cmd' , 'echoerr "foo"' , '--cmd' , 'color default' , '--cmd' , 'echoerr "bar"' ) 64screen : expect ( [[ 65|*3 66{102: }| 67{9:Error in pre-vimrc command line:} | 68{9:foo} | 69{101:bar} | 70{100:Press ENTER or type command to continue}^ | 71]] ) 72end ) 73 74it ( "doesn't erase output when setting Normal colors" , function () 75startup ( '--cmd' , 'echoerr "foo"' , '--cmd' , 'hi Normal guibg=Green' , '--cmd' , 'echoerr "bar"' ) 76screen : expect { 77grid =[[ 78|*3 79{3: }| 80{9:Error in pre-vimrc command line:} | 81{9:foo} | 82{9:bar} | 83{6:Press ENTER or type command to continue}^ | 84]] , 85condition =function () 86eq ( Screen . colors . Green , screen . default_colors . rgb_bg ) 87end , 88} 89end ) 90end 91 92describe ( '--embed UI on startup (ext_linegrid=true)' , function () 93test_embed ( true ) 94end ) 95describe ( '--embed UI on startup (ext_linegrid=false)' , function () 96test_embed ( false ) 97end ) 98 99describe ( '--embed UI' , function () 100after_each ( function () 101check_close () 102os . remove ( testlog ) 103end ) 104 105it ( 'can pass stdin' , function () 106local pipe =assert ( uv . pipe ()) 107 108local writer =assert ( uv . new_pipe ( false )) 109writer : open ( pipe . write ) 110 111clear { args_rm ={ '--headless' } , io_extra =pipe . read , env ={ NVIM_LOG_FILE =testlog } } 112 113-- attach immediately after startup, for early UI 114-- rpc_async: Avoid hanging. #24888 115local screen =Screen . new ( 40 , 8 , { stdin_fd =3 } , false ) 116screen . rpc_async =true -- Avoid hanging. #24888 117screen : attach () 118 119writer : write 'hello nvim\nfrom external input\n' 120writer : shutdown ( function () 121writer : close () 122end ) 123 124screen : expect [[ 125^hello nvim | 126from external input | 127{1:~ }|*5 128| 129]] 130 131-- stdin (rpc input) still works 132feed 'o' 133screen : expect [[ 134hello nvim | 135^ | 136from external input | 137{1:~ }|*4 138{5:-- INSERT --} | 139]] 140 141if not is_os ( 'win' ) then 142assert_log ( 'Failed to get flags on descriptor 3: Bad file descriptor' , testlog , 100 ) 143end 144end ) 145 146it ( 'can pass stdin to -q - #17523' , function () 147write_file ( 148'Xbadfile.c' , 149[[ 150/* some file with an error */ 151main() { 152functionCall(arg; arg, arg); 153return 666 154} 155]] 156) 157finally ( function () 158os . remove ( 'Xbadfile.c' ) 159end ) 160 161local pipe =assert ( uv . pipe ()) 162 163local writer =assert ( uv . new_pipe ( false )) 164writer : open ( pipe . write ) 165 166clear { args_rm ={ '--headless' } , args ={ '-q' , '-' } , io_extra =pipe . read } 167 168-- attach immediately after startup, for early UI 169local screen =Screen . new ( 60 , 8 , { stdin_fd =3 } , false ) 170screen . rpc_async =true -- Avoid hanging. #24888 171screen : attach () 172 173writer : write [[Xbadfile.c:4:12: error: expected ';' before '}' token]] 174writer : shutdown ( function () 175writer : close () 176end ) 177 178screen : expect [[ 179/* some file with an error */ | 180main() { | 181functionCall(arg; arg, arg); | 182return 66^6 | 183} | 184{1:~ }|*2 185(1 of 1): error: expected ';' before '}' token | 186]] 187 188-- stdin (rpc input) still works 189feed 'A' 190screen : expect [[ 191/* some file with an error */ | 192main() { | 193functionCall(arg; arg, arg); | 194return 666^ | 195} | 196{1:~ }|*2 197{5:-- INSERT --} | 198]] 199 200eq ( '-' , api . nvim_get_option_value ( 'errorfile' , {} )) 201end ) 202 203it ( 'only sets background colors once even if overridden' , function () 204local screen , current , seen 205local function handle_default_colors_set ( _ , _ , rgb_bg , _ , _ , _ ) 206seen [ rgb_bg ] =true 207current =rgb_bg 208end 209local function startup ( ... ) 210seen ={} 211current =nil 212clear { args_rm ={ '--headless' } , args ={ ... } } 213 214-- attach immediately after startup, for early UI 215screen =Screen . new ( 40 , 8 ) 216screen . _handle_default_colors_set =handle_default_colors_set 217end 218 219startup () 220screen : expect { 221condition =function () 222eq ( 16777215 , current ) 223end , 224} 225eq ( { [ 16777215 ] =true } , seen ) 226 227-- NB: by accident how functional/testutil.lua currently handles the default color scheme, the 228-- above is sufficient to test the behavior. But in case that workaround is removed, we need 229-- a test with an explicit override like below, so do it to remain safe. 230startup ( '--cmd' , 'hi NORMAL guibg=#FF00FF' ) 231screen : expect { 232condition =function () 233eq ( 16711935 , current ) 234end , 235} 236eq ( { [ 16711935 ] =true } , seen ) -- we only saw the last one, despite 16777215 was set internally earlier 237end ) 238 239it ( 'updates cwd of attached UI #21771' , function () 240clear { args_rm ={ '--headless' } } 241api . nvim_set_current_dir ( t . paths . test_source_path ) 242 243local screen =Screen . new ( 40 , 8 ) 244 245screen : expect { 246condition =function () 247eq ( t . paths . test_source_path , screen . pwd ) 248end , 249} 250 251-- Change global cwd 252n . command ( string . format ( 'cd %s/src/nvim' , t . paths . test_source_path )) 253 254screen : expect { 255condition =function () 256eq ( string . format ( '%s/src/nvim' , t . paths . test_source_path ), screen . pwd ) 257end , 258} 259 260-- Split the window and change the cwd in the split 261n . command ( 'new' ) 262n . command ( string . format ( 'lcd %s/test' , t . paths . test_source_path )) 263 264screen : expect { 265condition =function () 266eq ( string . format ( '%s/test' , t . paths . test_source_path ), screen . pwd ) 267end , 268} 269 270-- Move to the original window 271n . command ( 'wincmd p' ) 272 273screen : expect { 274condition =function () 275eq ( string . format ( '%s/src/nvim' , t . paths . test_source_path ), screen . pwd ) 276end , 277} 278 279-- Change global cwd again 280n . command ( string . format ( 'cd %s' , t . paths . test_source_path )) 281 282screen : expect { 283condition =function () 284eq ( t . paths . test_source_path , screen . pwd ) 285end , 286} 287end ) 288 289it ( 'closing stdio with another remote UI does not leak memory #36392' , function () 290clear ( { args_rm ={ '--headless' } } ) 291Screen . new () 292eq ( 1 , # api . nvim_list_uis ()) 293local server =api . nvim_get_vvar ( 'servername' ) 294local other_session =n . connect ( server ) 295Screen . new ( nil , nil , nil , other_session ) 296eq ( 2 , # api . nvim_list_uis ()) 297check_close () 298other_session : close () 299end ) 300end ) 301 302describe ( '--embed --listen UI' , function () 303it ( 'waits for connection on listening address' , function () 304clear () 305local child_server =assert ( n . new_pipename ()) 306fn . jobstart ( { 307nvim_prog , 308'--embed' , 309'--listen' , 310child_server , 311'--clean' , 312'--cmd' , 313'colorscheme vim' , 314} ) 315retry ( nil , nil , function () 316neq ( nil , uv . fs_stat ( child_server )) 317end ) 318 319local child_session =n . connect ( child_server ) 320 321local info_ok , api_info =child_session : request ( 'nvim_get_api_info' ) 322ok ( info_ok ) 323eq ( 2 , # api_info ) 324ok ( api_info [ 1 ] > 2 , 'channel_id > 2' , api_info [ 1 ]) 325 326child_session : request ( 327'nvim_exec2' , 328[[ 329let g:evs = [] 330autocmd UIEnter * call add(g:evs, $"UIEnter:{v:event.chan}") 331autocmd VimEnter * call add(g:evs, "VimEnter") 332]] , 333{} 334) 335 336-- VimEnter and UIEnter shouldn't be triggered until after attach 337local var_ok , var =child_session : request ( 'nvim_get_var' , 'evs' ) 338ok ( var_ok ) 339eq ( {} , var ) 340 341local child_screen =Screen . new ( 40 , 6 , nil , child_session ) 342child_screen : expect { 343grid =[[ 344^ | 345{1:~ }|*3 346{2:[No Name] 0,0-1 All}| 347| 348]] , 349attr_ids ={ 350[ 1 ] ={ foreground =Screen . colors . Blue , bold =true } , 351[ 2 ] ={ reverse =true , bold =true } , 352} , 353} 354 355-- VimEnter and UIEnter should now be triggered 356var_ok , var =child_session : request ( 'nvim_get_var' , 'evs' ) 357ok ( var_ok ) 358eq ( { 'VimEnter' , ( 'UIEnter:%d' ): format ( api_info [ 1 ]) } , var ) 359end ) 360end )