robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
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 , setup , teardown , finally = 6t . describe , t . it , t . before_each , t . after_each , t . setup , t . teardown , t . finally 7local assert_alive =n . assert_alive 8local assert_log =t . assert_log 9local clear =n . clear 10local command =n . command 11local ok =t . ok 12local eq =t . eq 13local matches =t . matches 14local eval =n . eval 15local exec =n . exec 16local exec_capture =n . exec_capture 17local exec_lua =n . exec_lua 18local feed =n . feed 19local fn =n . fn 20local pesc =vim . pesc 21local mkdir =t . mkdir 22local mkdir_p =n . mkdir_p 23local nvim_prog =n . nvim_prog 24local nvim_set =n . nvim_set 25local read_file =t . read_file 26local retry =t . retry 27local rmdir =n . rmdir 28local sleep =vim . uv . sleep 29local startswith =vim . startswith 30local write_file =t . write_file 31local api =n . api 32local is_os =t . is_os 33local dedent =t . dedent 34local tbl_map =vim . tbl_map 35local tbl_filter =vim . tbl_filter 36local endswith =vim . endswith 37local check_close =n . check_close 38 39local testlog ='Xtest-startupspec-log' 40 41describe ( 'startup' , function () 42it ( '--clean' , function () 43clear () 44matches ( vim . pesc ( fn . stdpath ( 'config' )), api . nvim_get_option_value ( 'runtimepath' , {} )) 45 46clear ( '--clean' ) 47ok ( not api . nvim_get_option_value ( 'runtimepath' , {} ): match ( vim . pesc ( fn . stdpath ( 'config' )))) 48end ) 49 50it ( 'prevents remote UI infinite loop' , function () 51clear () 52local screen =Screen . new ( 84 , 3 ) 53fn . jobstart ( 54{ nvim_prog , '-u' , 'NONE' , '--server' , eval ( 'v:servername' ), '--remote-ui' } , 55{ term =true } 56) 57screen : expect ( [[ 58^Cannot attach UI of :terminal child to its parent. (Unset $NVIM to skip this check) | 59[Process exited 1] | 60| 61]] ) 62end ) 63 64it ( '--startuptime' , function () 65local testfile ='Xtest_startuptime' 66finally ( function () 67os . remove ( testfile ) 68end ) 69clear ( { args ={ '--startuptime' , testfile } } ) 70assert_log ( 'Embedded' , testfile , 100 ) 71assert_log ( 'sourcing' , testfile , 100 ) 72assert_log ( "require%('vim%._core.editor'%)" , testfile , 100 ) 73end ) 74 75it ( '--startuptime does not crash on error #31125' , function () 76local p =n . spawn_wait ( '--startuptime' , '.' , '-c' , '42cquit' ) 77eq ( "E484: Can't open file ." , p . stderr ) 78eq ( 42 , p . status ) 79end ) 80 81it ( '-D does not hang #12647' , function () 82clear () 83local screen =Screen . new ( 60 , 7 ) 84-- not the same colors on windows for some reason 85screen . _default_attr_ids =nil 86local id =fn . jobstart ( { 87nvim_prog , 88'-u' , 89'NONE' , 90'-i' , 91'NONE' , 92'--cmd' , 93'set noruler' , 94'-D' , 95} , { 96term =true , 97env ={ 98VIMRUNTIME =os . getenv ( 'VIMRUNTIME' ), 99} , 100} ) 101screen : expect ( { any =pesc ( 'Entering Debug mode. Type "cont" to continue.' ) } ) 102fn . chansend ( id , 'cont\n' ) 103screen : expect ( [[ 104^ | 105~ |*3 106[No Name] | 107|*2 108]] ) 109end ) 110 111it ( ':filetype detect enables filetype detection with -u NONE' , function () 112clear () 113eq ( 'filetype detection:OFF plugin:OFF indent:OFF' , exec_capture ( 'filetype' )) 114command ( 'filetype detect' ) 115eq ( 'filetype detection:ON plugin:OFF indent:OFF' , exec_capture ( 'filetype' )) 116end ) 117end ) 118 119describe ( 'startup' , function () 120before_each ( clear ) 121 122after_each ( function () 123check_close () 124os . remove ( testlog ) 125end ) 126 127describe ( '-l Lua' , function () 128local function assert_l_out ( expected , nvim_args , lua_args , script , input ) 129local args ={ nvim_prog } 130vim . list_extend ( args , nvim_args or {} ) 131vim . list_extend ( args , { '-l' , ( script or 'test/functional/fixtures/startup.lua' ) } ) 132vim . list_extend ( args , lua_args or {} ) 133local out =fn . system ( args , input ): gsub ( '\r\n' , '\n' ) 134if type ( expected ) == 'function' then 135return expected ( out ) 136else 137return eq ( dedent ( expected ), out ) 138end 139end 140 141it ( 'outputs the EOF as LF (not CRLF) #36853' , function () 142local args ={ nvim_prog , '-l' , '-' } 143local input ='print("foo")' 144local out =fn . system ( args , input ) 145eq ( 'foo\n' , out ) 146end ) 147 148it ( 'failure modes' , function () 149-- nvim -l <empty> 150local proc =n . spawn_wait ( '-l' ) 151matches ( 'nvim%.?e?x?e?: Argument missing after: "%-l"' , proc . stderr ) 152eq ( 1 , proc . status ) 153end ) 154 155it ( 'os.exit() sets Nvim exitcode' , function () 156-- nvim -l foo.lua -arg1 -- a b c 157assert_l_out ( 158[[ 159bufs: 160nvim args: 7 161lua args: { "-arg1", "--exitcode", "73", "--arg2", 162[0] = "test/functional/fixtures/startup.lua" 163} 164]] , 165{} , 166{ '-arg1' , '--exitcode' , '73' , '--arg2' } 167) 168eq ( 73 , eval ( 'v:shell_error' )) 169end ) 170 171it ( 'os.exit() runs Nvim teardown' , function () 172local exit_file =t . tmpname ( false ) 173finally ( function () 174os . remove ( exit_file ) 175end ) 176 177fn . system ( 178{ 179nvim_prog , 180'-u' , 181'NONE' , 182'-i' , 183'NONE' , 184'--cmd' , 185'set shada=' , 186'-l' , 187'-' , 188} , 189( [[ 190vim.api.nvim_create_autocmd('VimLeave', { 191callback = function() 192vim.fn.writefile({ tostring(vim.v.exiting) }, %s) 193end, 194}) 195os.exit(73) 196]] ): format ( vim . inspect ( exit_file )) 197) 198 199eq ( 73 , eval ( 'v:shell_error' )) 200eq ( '73\n' , read_file ( exit_file )) 201end ) 202 203it ( 'os.exit() fails from libuv process callback #39783' , function () 204local out =fn . system ( 205{ nvim_prog , '--clean' , '-l' , '-' } , 206( [[ 207local callback_called = false 208local handle 209handle = assert(vim.uv.spawn(%q, { args = { 'EXIT', '0' } }, function() 210handle:close() 211callback_called = true 212os.exit(73) 213error('os.exit() returned') 214end)) 215assert(vim.wait(1000, function() 216return callback_called 217end)) 218]] ): format ( n . testprg ( 'shell-test' )) 219) 220 221eq ( 0 , eval ( 'v:shell_error' )) 222matches ( 'E5560: os%.exit must not be called in a fast event context' , out ) 223end ) 224 225it ( 'Lua-error sets Nvim exitcode' , function () 226local proc =n . spawn_wait ( '-l' , 'test/functional/fixtures/startup-fail.lua' ) 227matches ( 'E5113: .* my pearls!!' , ( proc : output ())) 228eq ( 0 , proc . signal ) 229eq ( 1 , proc . status ) 230 231eq ( 0 , eval ( 'v:shell_error' )) 232matches ( 233'E5113: .* %[string "error%("whoa"%)"%]:1: whoa' , 234fn . system ( { nvim_prog , '-l' , '-' } , 'error("whoa")' ) 235) 236eq ( 1 , eval ( 'v:shell_error' )) 237end ) 238 239it ( 'executes stdin "-"' , function () 240assert_l_out ( 241'arg0=- args=2 whoa\n' , 242nil , 243{ 'arg1' , 'arg 2' } , 244'-' , 245"print(('arg0=%s args=%d %s'):format(_G.arg[0], #_G.arg, 'whoa'))" 246) 247assert_l_out ( 248'biiig input: 1000042\n' , 249nil , 250nil , 251'-' , 252( 'print("biiig input: "..("%s"):len())' ): format ( string . rep ( 'x' , ( 1000 * 1000 ) + 42 )) 253) 254eq ( 0 , eval ( 'v:shell_error' )) 255end ) 256 257it ( 'does not truncate long print() message' , function () 258assert_l_out (( 'k' ): rep ( 1234 ) .. '\n' , nil , nil , '-' , "print(('k'):rep(1234))" ) 259end ) 260 261it ( 'does add the expected number of newlines' , function () 262assert_l_out ( '' , nil , nil , '-' , '' ) 263 264assert_l_out ( '\n' , nil , nil , '-' , [[print()]] ) 265assert_l_out ( '\n\n' , nil , nil , '-' , [[print('\n')]] ) 266 267assert_l_out ( 'foobar\n' , nil , nil , '-' , [[print('foobar')]] ) 268assert_l_out ( 'foobar\n\n' , nil , nil , '-' , [[print('foobar\n')]] ) 269 270assert_l_out ( 'foo\nbar\n' , nil , nil , '-' , [[print('foo')print('bar')]] ) 271assert_l_out ( 'foo\n\nbar\n' , nil , nil , '-' , [[print('foo')print()print('bar')]] ) 272end ) 273 274it ( 'preserves escape sequences' , function () 275assert_l_out ( 276'this is \027[31mRED FOR AN AMAZING REASON\027[0m\n' , 277nil , 278nil , 279'-' , 280[[print('this is \027[31mRED FOR AN AMAZING REASON\027[0m')]] 281) 282end ) 283 284it ( 'sets _G.arg' , function () 285-- nvim -l foo.lua 286assert_l_out ( 287[[ 288bufs: 289nvim args: 3 290lua args: { 291[0] = "test/functional/fixtures/startup.lua" 292} 293]] , 294{} , 295{} 296) 297eq ( 0 , eval ( 'v:shell_error' )) 298 299-- nvim -l foo.lua [args] 300assert_l_out ( 301[[ 302bufs: 303nvim args: 7 304lua args: { "-arg1", "--arg2", "--", "arg3", 305[0] = "test/functional/fixtures/startup.lua" 306} 307]] , 308{} , 309{ '-arg1' , '--arg2' , '--' , 'arg3' } 310) 311eq ( 0 , eval ( 'v:shell_error' )) 312 313-- nvim file1 file2 -l foo.lua -arg1 -- file3 file4 314assert_l_out ( 315[[ 316bufs: file1 file2 317nvim args: 10 318lua args: { "-arg1", "arg 2", "--", "file3", "file4", 319[0] = "test/functional/fixtures/startup.lua" 320} 321]] , 322{ 'file1' , 'file2' } , 323{ '-arg1' , 'arg 2' , '--' , 'file3' , 'file4' } 324) 325eq ( 0 , eval ( 'v:shell_error' )) 326 327-- nvim -l foo.lua <vim args> 328assert_l_out ( 329[[ 330bufs: 331nvim args: 5 332lua args: { "-c", "set wrap?", 333[0] = "test/functional/fixtures/startup.lua" 334} 335]] , 336{} , 337{ '-c' , 'set wrap?' } 338) 339eq ( 0 , eval ( 'v:shell_error' )) 340 341-- nvim <vim args> -l foo.lua <vim args> 342assert_l_out ( 343[[ 344wrap 345bufs: 346nvim args: 7 347lua args: { "-c", "set wrap?", 348[0] = "test/functional/fixtures/startup.lua" 349} 350]] , 351{ '-c' , 'set wrap?' } , 352{ '-c' , 'set wrap?' } 353) 354eq ( 0 , eval ( 'v:shell_error' )) 355end ) 356 357it ( 'disables swapfile/shada/config/plugins unless overridden' , function () 358local script =[[print(('updatecount=%d shadafile=%s loadplugins=%s scripts=%d'):format( 359vim.o.updatecount, vim.o.shadafile, tostring(vim.o.loadplugins), math.max(1, #vim.fn.getscriptinfo())))]] 360finally ( function () 361os . remove ( 'Xtest_shada' ) 362end ) 363 364assert_l_out ( function ( out ) 365-- Accept scripts=2 for PUC Lua where `vim._core.util` is sourced from disk instead of a preload blob. 366return matches ( 'updatecount=0 shadafile=NONE loadplugins=false scripts=[12]\n' , out ) 367end , nil , nil , '-' , script ) 368 369-- User can override. 370assert_l_out ( 371function ( out ) 372return matches ( 'updatecount=99 shadafile=Xtest_shada loadplugins=true scripts=2%d\n' , out ) 373end , 374{ '+set updatecount=99' , '-i' , 'Xtest_shada' , '+set loadplugins' , '-u' , 'NORC' } , 375nil , 376'-' , 377script 378) 379end ) 380end ) 381 382it ( '--cmd/-c/+ do not truncate long Lua print() message with --headless' , function () 383local out =fn . system ( { 384nvim_prog , 385'-u' , 386'NONE' , 387'-i' , 388'NONE' , 389'--headless' , 390'--cmd' , 391'lua print(("A"):rep(1234))' , 392'-c' , 393'lua print(("B"):rep(1234))' , 394'+lua print(("C"):rep(1234))' , 395'+q' , 396} ) 397 398eq (( 'A' ): rep ( 1234 ) .. '\n' .. ( 'B' ): rep ( 1234 ) .. '\n' .. ( 'C' ): rep ( 1234 ) .. '\n' , out ) 399end ) 400 401it ( 'pipe at both ends: has("ttyin")==0 has("ttyout")==0' , function () 402-- system() puts a pipe at both ends. 403local out =fn . system ( { 404nvim_prog , 405'-u' , 406'NONE' , 407'-i' , 408'NONE' , 409'--headless' , 410'--cmd' , 411nvim_set , 412'-c' , 413[[echo has('ttyin') has('ttyout')]] , 414'+q' , 415} ) 416eq ( '0 0' , out ) 417end ) 418 419it ( 'with --embed: has("ttyin")==0 has("ttyout")==0' , function () 420local screen =Screen . new ( 25 , 3 ) 421-- Remote UI connected by --embed. 422-- TODO: a lot of tests in this file already use the new default color scheme. 423-- once we do the batch update of tests to use it, remove this workaround 424screen . _default_attr_ids =nil 425command ( [[echo has('ttyin') has('ttyout')]] ) 426screen : expect ( [[ 427^ | 428~ | 4290 0 | 430]] ) 431end ) 432 433it ( 'in a TTY: has("ttyin")==1 has("ttyout")==1' , function () 434local screen =Screen . new ( 25 , 4 ) 435screen . _default_attr_ids =nil 436if is_os ( 'win' ) then 437command ( [[set shellcmdflag=/s\ /c shellxquote=\"]] ) 438end 439-- Running in :terminal 440fn . jobstart ( { 441nvim_prog , 442'-u' , 443'NONE' , 444'-i' , 445'NONE' , 446'--cmd' , 447nvim_set , 448'-c' , 449'echo has("ttyin") has("ttyout")' , 450} , { 451term =true , 452env ={ 453VIMRUNTIME =os . getenv ( 'VIMRUNTIME' ), 454} , 455} ) 456screen : expect ( [[ 457^ | 458~ | 4591 1 | 460| 461]] ) 462end ) 463 464it ( 'output to pipe: has("ttyin")==1 has("ttyout")==0' , function () 465clear ( { env ={ NVIM_LOG_FILE =testlog } } ) 466if is_os ( 'win' ) then 467command ( [[set shellcmdflag=/s\ /c shellxquote=\"]] ) 468end 469os . remove ( 'Xtest_startup_ttyout' ) 470finally ( function () 471os . remove ( 'Xtest_startup_ttyout' ) 472end ) 473-- Running in :terminal 474fn . jobstart ( 475( 476[["%s" -u NONE -i NONE --cmd "%s"]] 477.. [[ -c "call writefile([has('ttyin'), has('ttyout')], 'Xtest_startup_ttyout')"]] 478.. [[ -c q | cat -v]] 479): format ( nvim_prog , nvim_set ), 480{ 481term =true , 482env ={ 483VIMRUNTIME =os . getenv ( 'VIMRUNTIME' ), 484} , 485} 486) 487retry ( nil , 3000 , function () 488sleep ( 1 ) 489eq ( 490'1\n0\n' , -- stdin is a TTY, stdout is a pipe 491read_file ( 'Xtest_startup_ttyout' ) 492) 493end ) 494end ) 495 496it ( 'input from pipe: has("ttyin")==0 has("ttyout")==1' , function () 497clear ( { env ={ NVIM_LOG_FILE =testlog } } ) 498if is_os ( 'win' ) then 499command ( [[set shellcmdflag=/s\ /c shellxquote=\"]] ) 500end 501os . remove ( 'Xtest_startup_ttyout' ) 502finally ( function () 503os . remove ( 'Xtest_startup_ttyout' ) 504end ) 505-- Running in :terminal 506fn . jobstart ( 507( 508[[echo foo | ]] -- Input from a pipe. 509.. [["%s" -u NONE -i NONE --cmd "%s"]] 510.. [[ -c "call writefile([has('ttyin'), has('ttyout')], 'Xtest_startup_ttyout')"]] 511.. [[ -c q -- -]] 512): format ( nvim_prog , nvim_set ), 513{ 514term =true , 515env ={ 516VIMRUNTIME =os . getenv ( 'VIMRUNTIME' ), 517} , 518} 519) 520retry ( nil , 3000 , function () 521sleep ( 1 ) 522eq ( 523'0\n1\n' , -- stdin is a pipe, stdout is a TTY 524read_file ( 'Xtest_startup_ttyout' ) 525) 526end ) 527end ) 528 529it ( 'input from pipe (implicit) #7679' , function () 530clear ( { env ={ NVIM_LOG_FILE =testlog } } ) 531local screen =Screen . new ( 25 , 4 ) 532screen . _default_attr_ids =nil 533if is_os ( 'win' ) then 534command ( [[set shellcmdflag=/s\ /c shellxquote=\"]] ) 535end 536-- Running in :terminal 537fn . jobstart ( 538( 539[[echo foo | ]] 540.. [["%s" -u NONE -i NONE --cmd "%s"]] 541.. [[ -c "echo has('ttyin') has('ttyout')"]] 542): format ( nvim_prog , nvim_set ), 543{ 544term =true , 545env ={ 546VIMRUNTIME =os . getenv ( 'VIMRUNTIME' ), 547} , 548} 549) 550screen : expect ( [[ 551^foo | 552~ | 5530 1 | 554| 555]] ) 556if not is_os ( 'win' ) then 557assert_log ( 'Failed to get flags on descriptor 3: Bad file descriptor' , testlog , 100 ) 558end 559end ) 560 561it ( 'input from pipe + file args #7679' , function () 562eq ( 563'ohyeah\n0 0 bufs=3' , 564fn . system ( { 565nvim_prog , 566'-n' , 567'-u' , 568'NONE' , 569'-i' , 570'NONE' , 571'--headless' , 572'+.print' , 573"+echo has('ttyin') has('ttyout') 'bufs='.bufnr('$')" , 574'+qall!' , 575'-' , 576'test/functional/fixtures/tty-test.c' , 577'test/functional/fixtures/shell-test.c' , 578} , { 'ohyeah' , '' } ) 579) 580end ) 581 582it ( 'if stdin is empty: selects buffer 2, deletes buffer 1 #8561' , function () 583eq ( 584'\n 2 %a "file1" line 0\n 3 "file2" line 0' , 585fn . system ( { 586nvim_prog , 587'-n' , 588'-u' , 589'NONE' , 590'-i' , 591'NONE' , 592'--headless' , 593'+ls!' , 594'+qall!' , 595'-' , 596'file1' , 597'file2' , 598} , { '' } ) 599) 600end ) 601 602it ( 'if stdin is empty and - is last: selects buffer 1, deletes buffer 3 #35269' , function () 603eq ( 604'\n 1 %a "file1" line 0\n 2 "file2" line 0' , 605fn . system ( { 606nvim_prog , 607'-n' , 608'-u' , 609'NONE' , 610'-i' , 611'NONE' , 612'--headless' , 613'+ls!' , 614'+qall!' , 615'file1' , 616'file2' , 617'-' , 618} , { '' } ) 619) 620end ) 621 622it ( "empty stdin with terminal split doesn't crash #35681" , function () 623eq ( 624'nocrash' , 625fn . system ( { 626nvim_prog , 627'-n' , 628'-u' , 629'NONE' , 630'-i' , 631'NONE' , 632'--headless' , 633'--cmd' , 634'term' , 635'+split' , 636'+quit!' , 637'+bw!' , 638'+bw!' , 639'+echo "nocrash"' , 640"+call timer_start(1, { -> execute('qa') })" , -- need to let event handling happen 641'-' , 642} , { '' } ) 643) 644end ) 645 646it ( 'stdin with -es/-Es #7679' , function () 647local input ={ 'append' , 'line1' , 'line2' , '.' , '%print' , '' } 648local inputstr =table . concat ( input , '\n' ) 649 650-- 651-- -Es: read stdin as text 652-- 653eq ( 654'partylikeits1999\n' , 655fn . system ( { 656nvim_prog , 657'-n' , 658'-u' , 659'NONE' , 660'-i' , 661'NONE' , 662'-Es' , 663'+.print' , 664'test/functional/fixtures/tty-test.c' , 665} , { 'partylikeits1999' , '' } ) 666) 667eq ( inputstr , fn . system ( { nvim_prog , '-i' , 'NONE' , '-Es' , '+%print' , '-' } , input )) 668-- with `-u NORC` 669eq ( 670'thepartycontinues\n' , 671fn . system ( { nvim_prog , '-n' , '-u' , 'NORC' , '-Es' , '+.print' } , { 'thepartycontinues' , '' } ) 672) 673-- without `-u` 674eq ( 675'thepartycontinues\n' , 676fn . system ( { nvim_prog , '-n' , '-Es' , '+.print' } , { 'thepartycontinues' , '' } ) 677) 678 679-- 680-- -es: read stdin as ex-commands 681-- 682eq ( 683' encoding=utf-8\n' , 684fn . system ( { 685nvim_prog , 686'-n' , 687'-u' , 688'NONE' , 689'-i' , 690'NONE' , 691'-es' , 692'test/functional/fixtures/tty-test.c' , 693} , { 'set encoding' , '' } ) 694) 695eq ( 'line1\nline2\n' , fn . system ( { nvim_prog , '-i' , 'NONE' , '-es' , '-' } , input )) 696-- with `-u NORC` 697eq ( 698' encoding=utf-8\n' , 699fn . system ( { nvim_prog , '-n' , '-u' , 'NORC' , '-es' } , { 'set encoding' , '' } ) 700) 701-- without `-u` 702eq ( ' encoding=utf-8\n' , fn . system ( { nvim_prog , '-n' , '-es' } , { 'set encoding' , '' } )) 703-- CRLF input (e.g. Windows pipes) works like LF. 704eq ( ' encoding=utf-8\n' , fn . system ( { nvim_prog , '-n' , '-es' } , 'set encoding\r\n' )) 705-- Stdin is not consumed as typeahead: `:lua` can read the next stdin line as data. 706eq ( 707'thedata\n' , 708fn . system ( 709{ nvim_prog , '-n' , '-u' , 'NONE' , '-i' , 'NONE' , '-es' } , 710{ 'lua vim.g.d = io.read()' , 'thedata' , 'put =g:d' , 'print' , '' } 711) 712) 713 714-- Cursor starts at the last line (POSIX ex): ed-style "a" appends at end-of-file. 715write_file ( 'Xesfile' , 'L1\nL2\n' ) 716finally ( function () 717os . remove ( 'Xesfile' ) 718end ) 719eq ( 720'L1\nL2\nX\n' , 721fn . system ( 722{ nvim_prog , '-n' , '-u' , 'NONE' , '-i' , 'NONE' , '-es' , 'Xesfile' } , 723{ 'a' , 'X' , '.' , '%print' , '' } 724) 725) 726 727-- In a terminal, "-es" executes +cmds and exits; it doesn't wait for input. 728local screen =Screen . new ( 60 , 6 ) 729fn . jobstart ( { nvim_prog , '-u' , 'NONE' , '-i' , 'NONE' , '-es' , '-V1' , '+echo "hello"' } , { 730term =true , 731env ={ VIMRUNTIME =os . getenv ( 'VIMRUNTIME' ) } , 732} ) 733-- "nvim -es" output ends with a final newline. 734screen : expect ( [[ 735^hello | 736| 737[Process exited 0] | 738|*3 739]] ) 740end ) 741 742it ( '-es/-Es disables swapfile/shada/config #8540' , function () 743for _ , arg in ipairs ( { '-es' , '-Es' } ) do 744local out =fn . system ( { 745nvim_prog , 746arg , 747'+set updatecount? shadafile? loadplugins?' , 748'+put =map(getscriptinfo(), {-> v:val.name})' , 749'+%print' , 750} ) 751local line1 =string . match ( out , '^.-\n' ) 752-- updatecount=0 means swapfile was disabled. 753eq ( ' updatecount=0 shadafile=NONE loadplugins\n' , line1 ) 754-- Standard plugins were loaded, but not user config. #31878 755local nrlines =# vim . split ( out , '\n' ) 756ok ( nrlines > 20 , '>20' , nrlines ) 757ok ( string . find ( out , 'man.lua' ) ~= nil ) 758ok ( string . find ( out , 'init.vim' ) == nil ) 759end 760end ) 761 762it ( 'fails on --embed with -es/-Es/-l' , function () 763matches ( 764'nvim[.exe]*: %-%-embed conflicts with %-es/%-Es/%-l' , 765n . spawn_wait ( '--embed' , '-es' ). stderr 766) 767matches ( 768'nvim[.exe]*: %-%-embed conflicts with %-es/%-Es/%-l' , 769n . spawn_wait ( '--embed' , '-Es' ). stderr 770) 771matches ( 772'nvim[.exe]*: %-%-embed conflicts with %-es/%-Es/%-l' , 773n . spawn_wait ( '--embed' , '-l' , 'foo.lua' ). stderr 774) 775end ) 776 777it ( '-es does not exit early with closed stdin' , function () 778write_file ( 'Xinput.txt' , 'line1\nline2\nline3\nline4\n' ) 779write_file ( 'Xoutput.txt' , 'OUT\n' ) 780finally ( function () 781os . remove ( 'Xinput.txt' ) 782os . remove ( 'Xoutput.txt' ) 783end ) 784-- Use system() without input so that stdin is closed. 785fn . system ( { 786nvim_prog , 787'--clean' , 788'-es' , 789'-c' , 790[[g/^/.w >>Xoutput.txt]] , 791'Xinput.txt' , 792} ) 793eq ( 'OUT\nline1\nline2\nline3\nline4\n' , read_file ( 'Xoutput.txt' )) 794end ) 795 796it ( 'ENTER dismisses early message #7967' , function () 797local screen 798screen =Screen . new ( 60 , 6 ) 799screen . _default_attr_ids =nil 800local id =fn . jobstart ( { 801nvim_prog , 802'-u' , 803'NONE' , 804'-i' , 805'NONE' , 806'--cmd' , 807'set noruler' , 808'--cmd' , 809'let g:foo = g:bar' , 810} , { 811term =true , 812env ={ 813VIMRUNTIME =os . getenv ( 'VIMRUNTIME' ), 814} , 815} ) 816screen : expect ( [[ 817^ | 818| 819Error in pre-vimrc command line: | 820E121: Undefined variable: g:bar | 821Press ENTER or type command to continue | 822| 823]] ) 824fn . chansend ( id , '\n' ) 825screen : expect ( [[ 826^ | 827~ |*2 828[No Name] | 829|*2 830]] ) 831end ) 832 833it ( '-r works without --headless in PTY #23294' , function () 834exec ( [[ 835func Normalize(data) abort 836" Windows: remove ^M and term escape sequences 837return mapnew(a:data, 'substitute(substitute(v:val, "\r", "", "g"), "\x1b\\%(\\]\\d\\+;.\\{-}\x07\\|\\[.\\{-}[\x40-\x7E]\\)", "", "g")') 838endfunc 839func OnOutput(id, data, event) dict 840let g:stdout = Normalize(a:data) 841endfunc 842call jobstart([v:progpath, '-u', 'NONE', '-i', 'NONE', '-r'], { 843\ 'pty': v:true, 844\ 'stdout_buffered': v:true, 845\ 'on_stdout': function('OnOutput'), 846\ }) 847]] ) 848retry ( nil , nil , function () 849eq ( 'Swap files found:' , eval ( 'g:stdout[0]' )) 850end ) 851end ) 852 853it ( 'fixed hang issue with --headless (#11386)' , function () 854local expected ='' 855local period =100 856for i =1 , period - 1 do 857expected =expected .. i .. '\n' 858end 859expected =expected .. period 860eq ( 861expected , 862-- FIXME(codehex): We should really set a timeout for the system function. 863-- If this test fails, there will be a waiting input state. 864fn . system ( { 865nvim_prog , 866'-u' , 867'NONE' , 868'-c' , 869'for i in range(1, 100) | echo i | endfor | quit' , 870'--headless' , 871} ) 872) 873end ) 874 875it ( 'get command line arguments from v:argv' , function () 876local p =n . spawn_wait ( '--cmd' , nvim_set , '-c' , [[echo v:argv[-1:] len(v:argv) > 1]] , '+q' ) 877eq ( "['+q'] 1" , p . stderr ) 878end ) 879end ) 880 881describe ( 'startup' , function () 882it ( '-e/-E interactive #7679' , function () 883clear ( '-e' ) 884local screen =Screen . new ( 40 , 12 ) 885feed ( "put ='from -e'<CR>" ) 886screen : expect ( [[ 887| 888from -e | 889{1:~ }|*6 890{2:[No Name] [+] }| 891{1::}^ | 892{3:[Ex mode] }| 893{5:-- INSERT --} | 894]] ) 895 896clear ( '-E' ) 897screen =Screen . new ( 40 , 12 ) 898feed ( "put ='from -E'<CR>" ) 899screen : expect ( [[ 900| 901from -E | 902{1:~ }|*6 903{2:[No Name] [+] }| 904{1::}^ | 905{3:[Ex mode] }| 906{5:-- INSERT --} | 907]] ) 908end ) 909 910it ( '-e enters Ex mode at startup' , function () 911clear ( '-e' ) 912local screen =Screen . new ( 40 , 12 ) 913-- The keep-open cmdwin REPL is focused, in Insert mode. #40962 914screen : expect ( { any =vim . pesc ( '[Ex mode]' ) } ) 915eq ( ':' , fn . getcmdwintype ()) 916feed ( 'visual<CR>' ) 917eq ( 'n' , fn . mode ( 1 )) 918eq ( '' , fn . getcmdwintype ()) 919 920-- Scripts can detect batch mode ("-es"): mode() is "cv". 921eq ( 'cv\n' , fn . system ( { nvim_prog , '-n' , '-es' } , { 'put =mode(1)' , 'print' , '' } )) 922end ) 923 924it ( '-d does not diff non-arglist windows #13720 #21289' , function () 925write_file ( 926'Xdiff.vim' , 927[[ 928let bufnr = nvim_create_buf(0, 1) 929let config = { 930\ 'relative': 'editor', 931\ 'focusable': v:false, 932\ 'width': 1, 933\ 'height': 1, 934\ 'row': 3, 935\ 'col': 3 936\ } 937autocmd WinEnter * let g:float_win = nvim_open_win(bufnr, v:false, config) 938]] 939) 940finally ( function () 941os . remove ( 'Xdiff.vim' ) 942end ) 943clear { args ={ '-u' , 'Xdiff.vim' , '-d' , 'Xdiff.vim' , 'Xdiff.vim' } } 944eq ( true , api . nvim_get_option_value ( 'diff' , { win =fn . win_getid ( 1 ) } )) 945eq ( true , api . nvim_get_option_value ( 'diff' , { win =fn . win_getid ( 2 ) } )) 946local float_win =eval ( 'g:float_win' ) 947eq ( 'editor' , api . nvim_win_get_config ( float_win ). relative ) 948eq ( false , api . nvim_get_option_value ( 'diff' , { win =float_win } )) 949end ) 950 951it ( 'still opens arglist windows if init only opens floating windows' , function () 952write_file ( 953'Xfloat.vim' , 954[[ 955let config = { 956\ 'relative': 'editor', 957\ 'focusable': v:false, 958\ 'width': 1, 959\ 'height': 1, 960\ 'row': 1, 961\ 'col': 1 962\ } 963call nvim_open_win(0, v:false, config) 964]] 965) 966finally ( function () 967os . remove ( 'Xfloat.vim' ) 968end ) 969clear { args ={ '-u' , 'Xfloat.vim' , '-d' , 'Xfloat.vim' , 'foo' } } 970local screen =Screen . new ( 40 , 5 ) 971screen : add_extra_attr_ids ( { 972[ 101 ] ={ 973bold =true , 974background =Screen . colors . LightBlue , 975foreground =Screen . colors . Brown , 976} , 977[ 102 ] ={ foreground =Screen . colors . SlateBlue , background =Screen . colors . LightBlue } , 978[ 103 ] ={ foreground =Screen . colors . Magenta , background =Screen . colors . LightBlue } , 979[ 104 ] ={ foreground =Screen . colors . DarkCyan , background =Screen . colors . LightBlue } , 980} ) 981screen : expect ( [[ 982{7: }{101:^let}{22: config }{101:=}{22: }{102:{}{22: }│{23:-}| 983{7: }{101:l}{22: }{102:\}{22: }{103:'relative'}{22:: }{103:'editor'}{22:, }│{23:-}| 984{7: }{22: }{102:\}{22: }{103:'focusable'}{22:: }{104:v:}{22:false, }│ | 985{3:Xfloat.vim }{2:<}| 986| 987]] ) 988end ) 989 990it ( "default 'diffopt' is applied with -d" , function () 991clear ( { 992args ={ 993'-d' , 994'test/functional/fixtures/diff/startup_old.txt' , 995'test/functional/fixtures/diff/startup_new.txt' , 996'--cmd' , 997'set laststatus=0' , 998} , 999} ) 1000local screen =Screen . new ( 80 , 24 ) 1001screen : expect ( [[ 1002{7:+ }{13:^+-- 15 lines: package main············}│{7:+ }{13:+-- 15 lines: package main···········}| 1003{7: } │{7: } | 1004{7: }func printCharacters(str string) strin│{7: }func printCharacters(str string) stri| 1005{7: } return str │{7: } return str | 1006{7: }} │{7: }} | 1007{7: } │{7: } | 1008{7: }func main() { │{7: }func main() { | 1009{7: }{23:--------------------------------------}│{7: }{22: hello := "Hello, World!" }| 1010{7: }{23:--------------------------------------}│{7: }{22: }| 1011{7: }{4: fmt.Print}{27:Ln}{4:(compressString("aa}│{7: }{4: fmt.Print(compressString("aaa}| 1012{7: }{4: fmt.Print}{27:Ln}{4:(compressString("go}│{7: }{4: fmt.Print(compressString("gol}| 1013{7: }{4: fmt.Print}{27:Ln}{4:(removeDuplicate("a}│{7: }{4: fmt.Print(removeDuplicate("aa}| 1014{7: }{4: fmt.Print}{27:Ln}{4:(reverseAndDouble("}│{7: }{4: fmt.Print(reverseAndDouble("a}| 1015{7: }{4: fmt.Print}{27:Ln}{4:(printCharacters("g}│{7: }{4: fmt.Print(printCharacters("go}| 1016{7: }{23:--------------------------------------}│{7: }{22: }| 1017{7: }{23:--------------------------------------}│{7: }{22: fmt.Println(hello) }| 1018{7: }} │{7: }} | 1019{1:~ }│{1:~ }|*6 1020| 1021]] ) 1022command ( 'let &diffopt = &diffopt' ) 1023screen : expect_unchanged () 1024end ) 1025 1026it ( 'does not crash if --embed is given twice' , function () 1027clear { args ={ '--embed' } } 1028assert_alive () 1029end ) 1030 1031it ( 'does not crash when expanding cdpath during early_init' , function () 1032clear { env ={ CDPATH ='~doesnotexist' } } 1033assert_alive () 1034eq ( ',~doesnotexist' , eval ( '&cdpath' )) 1035end ) 1036 1037it ( "sets 'shortmess' when loading other tabs" , function () 1038clear ( { args ={ '-p' , 'a' , 'b' , 'c' } } ) 1039local screen =Screen . new ( 25 , 4 ) 1040screen : expect ( [[ 1041{5: a }{24: b c }{2: }{24:X}| 1042^ | 1043{1:~ }| 1044| 1045]] ) 1046end ) 1047 1048describe ( 'opening a terminal before buffers are loaded #30765' , function () 1049local lines ={} --- @type string[] 1050for i =1 , 50 do 1051lines [ # lines + 1 ] =( 'line%d' ): format ( i ) 1052end 1053 1054setup ( function () 1055write_file ( 'Xsomefile' , table . concat ( lines , '\n' ) .. '\n' ) 1056end ) 1057 1058teardown ( function () 1059os . remove ( 'Xsomefile' ) 1060end ) 1061 1062it ( 'sends buffer content to terminal with nvim_open_term()' , function () 1063clear ( { 1064args_rm ={ '--headless' } , 1065args ={ 1066'Xsomefile' , 1067'--cmd' , 1068'let g:chan = nvim_open_term(0, {}) | startinsert' , 1069'--cmd' , 1070'call chansend(g:chan, "new_line1\nnew_line2\nnew_line3")' , 1071} , 1072} ) 1073local screen =Screen . new ( 50 , 7 ) 1074screen : expect ( [[ 1075line48 | 1076line49 | 1077line50 | 1078new_line1 | 1079new_line2 | 1080new_line3^ | 1081{5:-- TERMINAL --} | 1082]] ) 1083eq ( lines , api . nvim_buf_get_lines ( 0 , 0 , # lines , true )) 1084end ) 1085 1086it ( 'does not error with jobstart(…,{term=true})' , function () 1087clear ( { 1088args_rm ={ '--headless' } , 1089args ={ 1090'Xsomefile' , 1091'--cmd' , 1092( 'lua vim.fn.jobstart({%q}, {term = true})' ): format ( n . testprg ( 'tty-test' )), 1093} , 1094} ) 1095local screen =Screen . new ( 50 , 7 ) 1096screen : expect ( [[ 1097^tty ready | 1098|*6 1099]] ) 1100end ) 1101end ) 1102end ) 1103 1104describe ( 'startup' , function () 1105local function pack_clear ( cmd ) 1106-- add packages after config dir in rtp but before config/after 1107clear { 1108args ={ 1109'--cmd' , 1110'set packpath=test/functional/fixtures' , 1111'--cmd' , 1112'let paths=split(&rtp, ",")' , 1113'--cmd' , 1114'let &rtp = paths[0]..",test/functional/fixtures,test/functional/fixtures/middle,"..join(paths[1:],",")' , 1115'--cmd' , 1116cmd , 1117} , 1118env ={ XDG_CONFIG_HOME ='test/functional/fixtures/' } , 1119args_rm ={ 'runtimepath' } , 1120} 1121end 1122 1123it ( 'handles &packpath during startup' , function () 1124pack_clear [[ 1125let g:x = bar#test() 1126let g:y = leftpad#pad("heyya") 1127]] 1128eq ( - 3 , eval 'g:x' ) 1129eq ( ' heyya' , eval 'g:y' ) 1130 1131pack_clear [[ lua _G.y = require'bar'.doit() _G.z = require'leftpad''howdy' ]] 1132eq ( { 9003 , '\thowdy' } , exec_lua [[ return { _G.y, _G.z } ]] ) 1133end ) 1134 1135it ( 'handles require from &packpath in an async handler' , function () 1136-- NO! you cannot just speed things up by calling async functions during startup! 1137-- It doesn't make anything actually faster! NOOOO! 1138pack_clear [[ lua require'async_leftpad'('brrrr', 'async_res') ]] 1139 1140-- haha, async leftpad go brrrrr 1141eq ( '\tbrrrr' , exec_lua [[ return _G.async_res ]] ) 1142end ) 1143 1144it ( 'handles :packadd during startup' , function () 1145-- control group: opt/bonus is not available by default 1146pack_clear [[ 1147try 1148let g:x = bonus#secret() 1149catch 1150let g:err = v:exception 1151endtry 1152]] 1153eq ( 'Vim(let):E117: Unknown function: bonus#secret' , eval 'g:err' ) 1154 1155pack_clear [[ lua _G.test = {pcall(function() require'bonus'.launch() end)} ]] 1156eq ( 1157{ false , [[[string ":lua"]:1: module 'bonus' not found:]] } , 1158exec_lua [[ _G.test[2] = string.gsub(_G.test[2], '[\r\n].*', '') return _G.test ]] 1159) 1160 1161-- ok, time to launch the nukes: 1162pack_clear [[ packadd! bonus | let g:x = bonus#secret() ]] 1163eq ( 'halloj' , eval 'g:x' ) 1164 1165pack_clear [[ packadd! bonus | lua _G.y = require'bonus'.launch() ]] 1166eq ( 'CPE 1704 TKS' , exec_lua [[ return _G.y ]] ) 1167end ) 1168 1169it ( 'handles the correct order with start packages and after/' , function () 1170pack_clear [[ lua _G.test_loadorder = {} vim.cmd "runtime! filen.lua" ]] 1171eq ( 1172{ 'ordinary' , 'FANCY' , 'mittel' , 'FANCY after' , 'ordinary after' } , 1173exec_lua [[ return _G.test_loadorder ]] 1174) 1175end ) 1176 1177it ( 'handles the correct order with start packages and after/ after startup' , function () 1178pack_clear [[ lua _G.test_loadorder = {} ]] 1179command [[ runtime! filen.lua ]] 1180eq ( 1181{ 'ordinary' , 'FANCY' , 'mittel' , 'FANCY after' , 'ordinary after' } , 1182exec_lua [[ return _G.test_loadorder ]] 1183) 1184end ) 1185 1186it ( 'handles the correct order with globpath(&rtp, ...)' , function () 1187pack_clear [[ set loadplugins | lua _G.test_loadorder = {} ]] 1188command [[ 1189for x in globpath(&rtp, "filen.lua",1,1) 1190call v:lua.dofile(x) 1191endfor 1192]] 1193eq ( 1194{ 'ordinary' , 'FANCY' , 'mittel' , 'FANCY after' , 'ordinary after' } , 1195exec_lua [[ return _G.test_loadorder ]] 1196) 1197 1198local rtp =api . nvim_get_option_value ( 'rtp' , {} ) 1199ok ( 1200startswith ( 1201rtp , 1202'test/functional/fixtures/nvim,test/functional/fixtures/pack/*/start/*,test/functional/fixtures/start/*,test/functional/fixtures,test/functional/fixtures/middle,' 1203), 1204'startswith(…)' , 1205'rtp=' .. rtp 1206) 1207end ) 1208 1209it ( 'handles the correct order with opt packages and after/' , function () 1210pack_clear [[ lua _G.test_loadorder = {} vim.cmd "packadd! superspecial\nruntime! filen.lua" ]] 1211eq ( { 1212'ordinary' , 1213'SuperSpecial' , 1214'FANCY' , 1215'mittel' , 1216'FANCY after' , 1217'SuperSpecial after' , 1218'ordinary after' , 1219} , exec_lua [[ return _G.test_loadorder ]] ) 1220end ) 1221 1222it ( 'handles the correct order with opt packages and after/ after startup' , function () 1223pack_clear [[ lua _G.test_loadorder = {} ]] 1224command [[ 1225packadd! superspecial 1226runtime! filen.lua 1227]] 1228eq ( { 1229'ordinary' , 1230'SuperSpecial' , 1231'FANCY' , 1232'mittel' , 1233'FANCY after' , 1234'SuperSpecial after' , 1235'ordinary after' , 1236} , exec_lua [[ return _G.test_loadorder ]] ) 1237end ) 1238 1239it ( 'does an incremental update for packadd' , function () 1240pack_clear [[ lua _G.test_loadorder = {} ]] 1241command [[ 1242" need to use the runtime to make the initial cache: 1243runtime! non_exist_ent 1244" this should now incrementally update it: 1245packadd! superspecial 1246]] 1247 1248local check =api . nvim__runtime_inspect () 1249local check_copy =vim . deepcopy ( check ) 1250local any_incremental =false 1251for _ , item in ipairs ( check_copy ) do 1252any_incremental =any_incremental or item . pack_inserted 1253item . pack_inserted =nil 1254end 1255eq ( true , any_incremental , 'no pack_inserted in ' .. vim . inspect ( check )) 1256 1257command [[ 1258let &rtp = &rtp 1259runtime! phantom_ghost 1260]] 1261 1262local new_check =api . nvim__runtime_inspect () 1263eq ( check_copy , new_check ) 1264 1265command [[ runtime! filen.lua ]] 1266eq ( { 1267'ordinary' , 1268'SuperSpecial' , 1269'FANCY' , 1270'mittel' , 1271'FANCY after' , 1272'SuperSpecial after' , 1273'ordinary after' , 1274} , exec_lua [[ return _G.test_loadorder ]] ) 1275end ) 1276 1277it ( 'handles the correct order with opt packages and globpath(&rtp, ...)' , function () 1278pack_clear [[ set loadplugins | lua _G.test_loadorder = {} ]] 1279command [[ 1280packadd! superspecial 1281for x in globpath(&rtp, "filen.lua",1,1) 1282call v:lua.dofile(x) 1283endfor 1284]] 1285eq ( { 1286'ordinary' , 1287'SuperSpecial' , 1288'FANCY' , 1289'mittel' , 1290'SuperSpecial after' , 1291'FANCY after' , 1292'ordinary after' , 1293} , exec_lua [[ return _G.test_loadorder ]] ) 1294end ) 1295 1296it ( 'handles the correct order with a package that changes packpath' , function () 1297pack_clear [[ lua _G.test_loadorder = {} vim.cmd "packadd! funky\nruntime! filen.lua" ]] 1298eq ( 1299{ 'ordinary' , 'funky!' , 'FANCY' , 'mittel' , 'FANCY after' , 'ordinary after' } , 1300exec_lua [[ return _G.test_loadorder ]] 1301) 1302eq ( { 'ordinary' , 'funky!' , 'mittel' , 'ordinary after' } , exec_lua [[ return _G.nested_order ]] ) 1303end ) 1304 1305it ( 'handles the correct order when prepending packpath' , function () 1306clear { 1307args ={ 1308'--cmd' , 1309'set packpath^=test/functional/fixtures' , 1310'--cmd' , 1311[[ lua _G.test_loadorder = {} vim.cmd "runtime! filen.lua" ]] , 1312} , 1313env ={ XDG_CONFIG_HOME ='test/functional/fixtures/' } , 1314} 1315eq ( 1316{ 'ordinary' , 'FANCY' , 'FANCY after' , 'ordinary after' } , 1317exec_lua [[ return _G.test_loadorder ]] 1318) 1319end ) 1320 1321it ( 'window widths are correct when modelines set &columns with tabpages' , function () 1322write_file ( 'Xtab1.noft' , 'vim: columns=81' ) 1323write_file ( 'Xtab2.noft' , 'vim: columns=81' ) 1324finally ( function () 1325os . remove ( 'Xtab1.noft' ) 1326os . remove ( 'Xtab2.noft' ) 1327end ) 1328clear ( { args ={ '-p' , 'Xtab1.noft' , 'Xtab2.noft' } } ) 1329eq ( 81 , api . nvim_win_get_width ( 0 )) 1330command ( 'tabnext' ) 1331eq ( 81 , api . nvim_win_get_width ( 0 )) 1332end ) 1333end ) 1334 1335describe ( 'sysinit' , function () 1336local xdgdir ='Xxdg' 1337local vimdir ='Xvim' 1338local xhome ='Xhome' 1339 1340before_each ( function () 1341rmdir ( xdgdir ) 1342rmdir ( vimdir ) 1343rmdir ( xhome ) 1344 1345mkdir ( xdgdir ) 1346mkdir (( '%s/nvim' ): format ( xdgdir )) 1347write_file ( 1348( '%s/nvim/sysinit.vim' ): format ( xdgdir ), 1349[[ 1350let g:loaded = get(g:, "loaded", 0) + 1 1351let g:xdg = 1 1352]] 1353) 1354 1355mkdir ( vimdir ) 1356write_file ( 1357( '%s/sysinit.vim' ): format ( vimdir ), 1358[[ 1359let g:loaded = get(g:, "loaded", 0) + 1 1360let g:vim = 1 1361]] 1362) 1363 1364mkdir ( xhome ) 1365end ) 1366after_each ( function () 1367rmdir ( xdgdir ) 1368rmdir ( vimdir ) 1369rmdir ( xhome ) 1370end ) 1371 1372it ( 'prefers XDG_CONFIG_DIRS over VIM' , function () 1373clear { 1374args ={ '--cmd' , 'set nomore undodir=. directory=. belloff=' } , 1375args_rm ={ '-u' , '--cmd' } , 1376env ={ HOME =xhome , XDG_CONFIG_DIRS =xdgdir , VIM =vimdir } , 1377} 1378eq ( 1379'loaded 1 xdg 1 vim 0' , 1380eval ( 'printf("loaded %d xdg %d vim %d", g:loaded, get(g:, "xdg", 0), get(g:, "vim", 0))' ) 1381) 1382end ) 1383 1384it ( 'uses VIM if XDG_CONFIG_DIRS unset' , function () 1385clear { 1386args ={ '--cmd' , 'set nomore undodir=. directory=. belloff=' } , 1387args_rm ={ '-u' , '--cmd' } , 1388env ={ HOME =xhome , XDG_CONFIG_DIRS ='' , VIM =vimdir } , 1389} 1390eq ( 1391'loaded 1 xdg 0 vim 1' , 1392eval ( 'printf("loaded %d xdg %d vim %d", g:loaded, get(g:, "xdg", 0), get(g:, "vim", 0))' ) 1393) 1394end ) 1395 1396it ( 'respects NVIM_APPNAME in XDG_CONFIG_DIRS' , function () 1397local appname ='mysysinitapp' 1398mkdir (( '%s/%s' ): format ( xdgdir , appname )) 1399write_file (( '%s/%s/sysinit.vim' ): format ( xdgdir , appname ), [[let g:appname_sysinit = 1]] ) 1400clear { 1401args_rm ={ '-u' } , 1402env ={ HOME =xhome , XDG_CONFIG_DIRS =xdgdir , NVIM_APPNAME =appname } , 1403} 1404eq ( 1 , eval ( 'g:appname_sysinit' )) 1405-- Should not load from nvim/ subdir (which has the default sysinit.vim from before_each) 1406eq ( 0 , eval ( 'get(g:, "xdg", 0)' )) 1407end ) 1408end ) 1409 1410describe ( 'user config init' , function () 1411local xhome ='Xhome' 1412local xconfig =( '%s/Xconfig' ): format ( xhome ) 1413local xdata =( '%s/Xdata' ): format ( xhome ) 1414local init_lua_path =( '%s/nvim/init.lua' ): format ( xconfig ) 1415local xenv ={ XDG_CONFIG_HOME =xconfig , XDG_DATA_HOME =xdata } 1416 1417before_each ( function () 1418rmdir ( xhome ) 1419 1420mkdir_p (( '%s/nvim' ): format ( xconfig )) 1421mkdir_p ( xdata ) 1422 1423write_file ( init_lua_path , [[vim.g.lua_rc = 1]] ) 1424end ) 1425 1426after_each ( function () 1427rmdir ( xhome ) 1428end ) 1429 1430it ( 'loads init.lua from XDG config home by default' , function () 1431clear { args_rm ={ '-u' } , env =xenv } 1432 1433eq ( 1 , eval ( 'g:lua_rc' )) 1434eq ( fn . fnamemodify ( init_lua_path , ':p' ), eval ( '$MYVIMRC' )) 1435end ) 1436 1437describe ( 'loads existing' , function () 1438local exrc_path ='.exrc' 1439local xstate ='Xstate' 1440local xstateenv ={ XDG_CONFIG_HOME =xconfig , XDG_DATA_HOME =xdata , XDG_STATE_HOME =xstate } 1441 1442local function setup_exrc_file ( filename ) 1443exrc_path =filename 1444 1445if string . find ( exrc_path , '%.lua$' ) then 1446write_file ( 1447exrc_path , 1448string . format ( 1449[[ 1450vim.g.exrc_file = "%s" 1451vim.g.exrc_path = debug.getinfo(1, 'S').source:gsub('^@', '') 1452vim.g.exrc_count = (vim.g.exrc_count or 0) + 1 1453]] , 1454exrc_path 1455) 1456) 1457else 1458write_file ( 1459exrc_path , 1460string . format ( 1461[[ 1462let g:exrc_file = "%s" 1463" let g:exrc_path = ?? 1464let g:exrc_count = get(g:, 'exrc_count', 0) + 1 1465]] , 1466exrc_path 1467) 1468) 1469end 1470end 1471 1472before_each ( function () 1473for _ , file in ipairs ( { '.exrc' , '.nvimrc' , '.nvim.lua' } ) do 1474os . remove ( '../' .. file ) 1475os . remove ( file ) 1476end 1477write_file ( 1478init_lua_path , 1479[[ 1480vim.o.exrc = true 1481vim.g.exrc_file = '---' 1482]] 1483) 1484mkdir_p (( '%s/%s' ): format ( xstate , is_os ( 'win' ) and 'nvim-data' or 'nvim' )) 1485end ) 1486 1487after_each ( function () 1488for _ , file in ipairs ( { '.exrc' , '.nvimrc' , '.nvim.lua' } ) do 1489os . remove ( '../' .. file ) 1490os . remove ( file ) 1491end 1492rmdir ( xstate ) 1493end ) 1494 1495for _ , filename in ipairs ( { '.exrc' , '.nvimrc' , '.nvim.lua' , '../.nvim.lua' , '../.nvimrc' } ) do 1496it ( filename .. ' from cwd' , function () 1497setup_exrc_file ( filename ) 1498 1499clear { args_rm ={ '-u' } , env =xstateenv } 1500-- The 'exrc' file is not trusted, and the prompt is skipped because there is no UI. 1501eq ( '---' , eval ( 'g:exrc_file' )) 1502 1503local screen =Screen . new ( 50 , 8 ) 1504screen . _default_attr_ids =nil 1505fn . jobstart ( { nvim_prog } , { 1506term =true , 1507env ={ 1508VIMRUNTIME =os . getenv ( 'VIMRUNTIME' ), 1509} , 1510} ) 1511screen : expect ( { any =pesc ( '[i]gnore, (v)iew, (d)eny:' ) } ) 1512-- `i` to enter Terminal mode, `v` to view then `:trust` 1513feed ( 'iv' ) 1514feed ( ':trust<CR>' ) 1515feed ( ':q<CR>' ) 1516screen : expect ( [[ 1517^ | 1518~ |*4 1519[No Name] 0,0-1 All| 1520| 1521-- TERMINAL -- | 1522]] ) 1523feed ( ':echo g:exrc_file<CR>' ) 1524screen : expect ( string . format ( 1525[[ 1526^ | 1527~ |*4 1528[No Name] 0,0-1 All| 1529%s%s| 1530-- TERMINAL -- | 1531]] , 1532'---' , 1533string . rep ( ' ' , 50 - # '---' ) 1534)) 1535 1536clear { args_rm ={ '-u' } , env =xstateenv } 1537if string . find ( exrc_path , '%.lua$' ) then 1538eq ( 1539vim . fs . normalize ( vim . fs . abspath ( filename )), 1540vim . fs . normalize ( vim . fs . abspath ( eval ( 'g:exrc_path' ))) 1541) 1542end 1543-- The 'exrc' file is now trusted. 1544eq ( filename , eval ( 'g:exrc_file' )) 1545end ) 1546end 1547 1548it ( 'exrc from parent directories' , function () 1549setup_exrc_file ( '.nvim.lua' ) 1550setup_exrc_file ( '../.exrc' ) 1551clear { args_rm ={ '-u' } , env =xstateenv } 1552-- use a screen wide width to avoid wrapping the word `.exrc`, `.nvim.lua` below. 1553local screen =Screen . new ( 500 , 8 ) 1554screen . _default_attr_ids =nil 1555fn . jobstart ( { nvim_prog } , { 1556term =true , 1557env ={ 1558VIMRUNTIME =os . getenv ( 'VIMRUNTIME' ), 1559} , 1560} ) 1561-- current directory exrc is found first 1562screen : expect ( { any ='.nvim.lua' } ) 1563screen : expect ( { any =pesc ( '[i]gnore, (v)iew, (d)eny:' ), unchanged =true } ) 1564feed ( 'iv' ) 1565 1566-- after that the exrc in the parent directory 1567screen : expect ( { any ='.exrc' , unchanged =true } ) 1568screen : expect ( { any =pesc ( '[i]gnore, (v)iew, (d)eny:' ), unchanged =true } ) 1569feed ( 'v' ) 1570 1571-- trust .exrc 1572feed ( ':trust<CR>' ) 1573screen : expect ( { any ='Allowed in trust database: ".*/%.exrc"' } ) 1574feed ( ':q<CR>' ) 1575-- trust .nvim.lua 1576feed ( ':trust<CR>' ) 1577screen : expect ( { any ='Allowed in trust database: ".*/%.nvim%.lua"' } ) 1578feed ( ':q<CR>' ) 1579-- no exrc file is executed 1580feed ( ':echo g:exrc_count<CR>' ) 1581screen : expect ( { any ='E121: Undefined variable: g:exrc_count' } ) 1582 1583-- restart nvim 1584feed ( ':restart<CR>' ) 1585screen : expect ( [[ 1586^{MATCH: +}| 1587~{MATCH: +}|*4 1588[No Name]{MATCH: +}0,0-1{MATCH: +}All| 1589{MATCH: +}| 1590-- TERMINAL --{MATCH: +}| 1591]] ) 1592 1593-- a total of 2 exrc files are executed 1594feed ( ':echo g:exrc_count<CR>' ) 1595screen : expect ( [[ 1596^{MATCH: +}| 1597~{MATCH: +}|*4 1598[No Name]{MATCH: +}0,0-1{MATCH: +}All| 15992{MATCH: +}| 1600-- TERMINAL --{MATCH: +}| 1601]] ) 1602 1603-- The server is now detached and needs to be quit explicitly. 1604feed ( ':qall!<CR>' ) 1605screen : expect ( { any =vim . pesc ( '[Process exited 0]' ) } ) 1606end ) 1607end ) 1608 1609describe ( 'with explicitly provided config' , function () 1610local custom_lua_path =( '%s/custom.lua' ): format ( xhome ) 1611before_each ( function () 1612write_file ( 1613custom_lua_path , 1614[[ 1615vim.g.custom_lua_rc = 1 1616]] 1617) 1618end ) 1619 1620it ( 'loads custom lua config and does not set $MYVIMRC' , function () 1621clear { args ={ '-u' , custom_lua_path } , env =xenv } 1622eq ( 1 , eval ( 'g:custom_lua_rc' )) 1623eq ( '' , eval ( '$MYVIMRC' )) 1624end ) 1625end ) 1626 1627describe ( 'VIMRC also exists' , function () 1628before_each ( function () 1629write_file ( 1630( '%s/nvim/init.vim' ): format ( xconfig ), 1631[[ 1632let g:vim_rc = 1 1633]] 1634) 1635end ) 1636 1637it ( 'loads default lua config, but shows an error' , function () 1638clear { args_rm ={ '-u' } , env =xenv } 1639eq ( 1 , eval ( 'g:lua_rc' )) 1640t . matches ( 1641'E5422: Conflicting configs: "Xhome.Xconfig.nvim.init.lua" "Xhome.Xconfig.nvim.init.vim"' , 1642eval ( 'v:errmsg' ) 1643) 1644end ) 1645end ) 1646 1647describe ( 'from XDG_CONFIG_DIRS' , function () 1648local xdgdir ='Xxdgconfigdirs' 1649 1650before_each ( function () 1651-- Remove init.lua from XDG_CONFIG_HOME so nvim falls back to XDG_CONFIG_DIRS 1652os . remove ( init_lua_path ) 1653rmdir ( xdgdir ) 1654mkdir_p (( '%s/nvim' ): format ( xdgdir )) 1655end ) 1656 1657after_each ( function () 1658rmdir ( xdgdir ) 1659end ) 1660 1661it ( 'loads init.lua from XDG_CONFIG_DIRS when no config in XDG_CONFIG_HOME' , function () 1662write_file (( '%s/nvim/init.lua' ): format ( xdgdir ), [[vim.g.xdg_config_dirs_lua = 1]] ) 1663clear { 1664args_rm ={ '-u' } , 1665env ={ XDG_CONFIG_HOME =xconfig , XDG_DATA_HOME =xdata , XDG_CONFIG_DIRS =xdgdir } , 1666} 1667eq ( 1 , eval ( 'g:xdg_config_dirs_lua' )) 1668eq ( fn . fnamemodify (( '%s/nvim/init.lua' ): format ( xdgdir ), ':p' ), eval ( '$MYVIMRC' )) 1669end ) 1670 1671it ( 'prefers init.lua over init.vim, shows E5422' , function () 1672write_file (( '%s/nvim/init.lua' ): format ( xdgdir ), [[vim.g.xdg_lua = 1]] ) 1673write_file (( '%s/nvim/init.vim' ): format ( xdgdir ), [[let g:xdg_vim = 1]] ) 1674clear { 1675args_rm ={ '-u' } , 1676env ={ XDG_CONFIG_HOME =xconfig , XDG_DATA_HOME =xdata , XDG_CONFIG_DIRS =xdgdir } , 1677} 1678eq ( 1 , eval ( 'g:xdg_lua' )) 1679eq ( 0 , eval ( 'get(g:, "xdg_vim", 0)' )) 1680t . matches ( 'E5422: Conflicting configs:' , eval ( 'v:errmsg' )) 1681end ) 1682 1683it ( 'falls back to init.vim when no init.lua' , function () 1684write_file (( '%s/nvim/init.vim' ): format ( xdgdir ), [[let g:xdg_vim = 1]] ) 1685clear { 1686args_rm ={ '-u' } , 1687env ={ XDG_CONFIG_HOME =xconfig , XDG_DATA_HOME =xdata , XDG_CONFIG_DIRS =xdgdir } , 1688} 1689eq ( 1 , eval ( 'g:xdg_vim' )) 1690end ) 1691 1692it ( 'respects NVIM_APPNAME' , function () 1693local appname ='mytestapp' 1694mkdir_p (( '%s/%s' ): format ( xdgdir , appname )) 1695-- Also create nvim/ with a config that should NOT be loaded 1696write_file (( '%s/nvim/init.lua' ): format ( xdgdir ), [[vim.g.wrong = 1]] ) 1697write_file (( '%s/%s/init.lua' ): format ( xdgdir , appname ), [[vim.g.appname_lua = 1]] ) 1698clear { 1699args_rm ={ '-u' } , 1700env ={ 1701XDG_CONFIG_HOME =xconfig , 1702XDG_DATA_HOME =xdata , 1703XDG_CONFIG_DIRS =xdgdir , 1704NVIM_APPNAME =appname , 1705} , 1706} 1707eq ( 1 , eval ( 'g:appname_lua' )) 1708eq ( 0 , eval ( 'get(g:, "wrong", 0)' )) 1709eq ( fn . fnamemodify (( '%s/%s/init.lua' ): format ( xdgdir , appname ), ':p' ), eval ( '$MYVIMRC' )) 1710end ) 1711end ) 1712end ) 1713 1714describe ( 'runtime:' , function () 1715local xhome ='Xhome' 1716local xconfig =( '%s/Xconfig' ): format ( xhome ) 1717local xdata =( '%s/Xdata' ): format ( xhome ) 1718local xenv ={ XDG_CONFIG_HOME =xconfig , XDG_DATA_HOME =xdata } 1719 1720setup ( function () 1721rmdir ( xhome ) 1722mkdir_p (( '%s/nvim' ): format ( xconfig )) 1723mkdir_p ( xdata ) 1724end ) 1725 1726teardown ( function () 1727rmdir ( xhome ) 1728end ) 1729 1730it ( 'loads plugin/*.lua from XDG config home' , function () 1731local plugin_folder_path =( '%s/nvim/plugin' ): format ( xconfig ) 1732local plugin_file_path =( '%s/plugin.lua' ): format ( plugin_folder_path ) 1733mkdir_p ( plugin_folder_path ) 1734finally ( function () 1735rmdir ( plugin_folder_path ) 1736end ) 1737write_file ( plugin_file_path , [[ vim.g.lua_plugin = 1 ]] ) 1738 1739clear { args_rm ={ '-u' } , env =xenv } 1740 1741eq ( 1 , eval ( 'g:lua_plugin' )) 1742end ) 1743 1744it ( 'loads plugin/*.lua from start packages' , function () 1745local plugin_path =( '%s/nvim/pack/category/start/test_plugin' ): format ( xconfig ) 1746local plugin_folder_path =( '%s/plugin' ): format ( plugin_path ) 1747local plugin_file_path =( '%s/plugin.lua' ): format ( plugin_folder_path ) 1748local profiler_file ='test_startuptime.log' 1749mkdir_p ( plugin_folder_path ) 1750finally ( function () 1751os . remove ( profiler_file ) 1752rmdir ( plugin_path ) 1753end ) 1754 1755write_file ( plugin_file_path , [[vim.g.lua_plugin = 2]] ) 1756 1757clear { args_rm ={ '-u' } , args ={ '--startuptime' , profiler_file } , env =xenv } 1758 1759eq ( 2 , eval ( 'g:lua_plugin' )) 1760-- Check if plugin_file_path is listed in getscriptinfo() 1761local scripts =tbl_map ( function ( s ) 1762return s . name 1763end , fn . getscriptinfo ()) 1764ok ( # tbl_filter ( function ( s ) 1765return endswith ( s , plugin_file_path ) 1766end , scripts ) > 0 ) 1767 1768-- Check if plugin_file_path is listed in startup profile 1769local profile_reader =io . open ( profiler_file , 'r' ) 1770local profile_log =profile_reader : read ( '*a' ) 1771profile_reader : close () 1772ok ( profile_log : find ( plugin_file_path ) ~= nil ) 1773end ) 1774 1775it ( 'loads plugin/*.lua from site packages' , function () 1776local nvimdata =is_os ( 'win' ) and 'nvim-data' or 'nvim' 1777local plugin_path =( '%s/%s/site/pack/xa/start/yb' ): format ( xdata , nvimdata ) 1778local plugin_folder_path =( '%s/plugin' ): format ( plugin_path ) 1779local plugin_after_path =( '%s/after/plugin' ): format ( plugin_path ) 1780local plugin_file_path =( '%s/plugin.lua' ): format ( plugin_folder_path ) 1781local plugin_after_file_path =( '%s/helloo.lua' ): format ( plugin_after_path ) 1782mkdir_p ( plugin_folder_path ) 1783mkdir_p ( plugin_after_path ) 1784finally ( function () 1785rmdir ( plugin_path ) 1786end ) 1787 1788write_file ( plugin_file_path , [[table.insert(_G.lista, "unos")]] ) 1789write_file ( plugin_after_file_path , [[table.insert(_G.lista, "dos")]] ) 1790 1791clear { args_rm ={ '-u' } , args ={ '--cmd' , 'lua _G.lista = {}' } , env =xenv } 1792 1793eq ( { 'unos' , 'dos' } , exec_lua 'return _G.lista' ) 1794end ) 1795 1796it ( 'no crash setting &rtp in plugins with :packloadall called before #18315' , function () 1797local plugin_folder_path =( '%s/nvim/plugin' ): format ( xconfig ) 1798mkdir_p ( plugin_folder_path ) 1799finally ( function () 1800rmdir ( plugin_folder_path ) 1801end ) 1802 1803write_file ( 1804( '%s/plugin.vim' ): format ( plugin_folder_path ), 1805[[ 1806let &runtimepath = &runtimepath 1807let g:vim_plugin = 1 1808]] 1809) 1810write_file ( 1811( '%s/plugin.lua' ): format ( plugin_folder_path ), 1812[[ 1813vim.o.runtimepath = vim.o.runtimepath 1814vim.g.lua_plugin = 1 1815]] 1816) 1817 1818clear { args_rm ={ '-u' } , args ={ '--cmd' , 'packloadall' } , env =xenv } 1819 1820eq ( 1 , eval ( 'g:vim_plugin' )) 1821eq ( 1 , eval ( 'g:lua_plugin' )) 1822end ) 1823 1824it ( "loads ftdetect/*.{vim,lua} respecting 'rtp' order" , function () 1825local rtp_folder =( '%s/nvim' ): format ( xconfig ) 1826local after_rtp_folder =( '%s/after' ): format ( rtp_folder ) 1827local ftdetect_folder =( '%s/ftdetect' ): format ( rtp_folder ) 1828local after_ftdetect_folder =( '%s/ftdetect' ): format ( after_rtp_folder ) 1829mkdir_p ( ftdetect_folder ) 1830mkdir_p ( after_ftdetect_folder ) 1831finally ( function () 1832rmdir ( ftdetect_folder ) 1833rmdir ( after_ftdetect_folder ) 1834end ) 1835write_file (( '%s/scripts.vim' ): format ( rtp_folder ), [[let g:aseq ..= 'S']] ) 1836write_file (( '%s/scripts.vim' ): format ( after_rtp_folder ), [[let g:aseq ..= 's']] ) 1837-- A .lua file is loaded after a .vim file if they only differ in extension. 1838-- All files in after/ftdetect/ are loaded after all files in ftdetect/. 1839write_file ( 1840( '%s/new-ft.vim' ): format ( ftdetect_folder ), 1841[[ 1842let g:seq ..= 'A' 1843autocmd BufRead,BufNewFile FTDETECT let g:aseq ..= 'A' 1844]] 1845) 1846write_file ( 1847( '%s/new-ft.lua' ): format ( ftdetect_folder ), 1848[[ 1849vim.g.seq = vim.g.seq .. 'B' 1850vim.api.nvim_create_autocmd({ 'BufRead', 'BufNewFile' }, { 1851pattern = 'FTDETECT', 1852command = "let g:aseq ..= 'B'", 1853}) 1854]] 1855) 1856write_file ( 1857( '%s/new-ft.vim' ): format ( after_ftdetect_folder ), 1858[[ 1859let g:seq ..= 'a' 1860autocmd BufRead,BufNewFile FTDETECT let g:aseq ..= 'a' 1861]] 1862) 1863write_file ( 1864( '%s/new-ft.lua' ): format ( after_ftdetect_folder ), 1865[[ 1866vim.g.seq = vim.g.seq .. 'b' 1867vim.api.nvim_create_autocmd({ 'BufRead', 'BufNewFile' }, { 1868pattern = 'FTDETECT', 1869command = "let g:aseq ..= 'b'", 1870}) 1871]] 1872) 1873clear { args_rm ={ '-u' } , args ={ '--cmd' , 'let g:seq = ""' } , env =xenv } 1874eq ( 'ABab' , eval ( 'g:seq' )) 1875command ( 'let g:aseq = ""' ) 1876command ( 'edit FTDETECT' ) 1877eq ( 'SsABab' , eval ( 'g:aseq' )) 1878end ) 1879 1880it ( 'no crash for recursive search_path build #39815' , function () 1881clear () 1882local screen =Screen . new () 1883fn . jobstart ( { 1884nvim_prog , 1885'--clean' , 1886'+lua require("vim._core.ui2").enable()' , 1887'+set rtp+=$FOO | set syntax' , 1888} , { term =true , env ={ VIMRUNTIME =os . getenv ( 'VIMRUNTIME' ) } } ) 1889screen : expect ( { any ='syntax' , none ='Process exited 1' } ) 1890end ) 1891end ) 1892 1893describe ( 'user session' , function () 1894local xhome ='Xhome' 1895local session_file =( '%s/session.lua' ): format ( xhome ) 1896 1897before_each ( function () 1898rmdir ( xhome ) 1899 1900mkdir ( xhome ) 1901write_file ( 1902session_file , 1903[[ 1904vim.g.lua_session = 1 1905]] 1906) 1907end ) 1908 1909after_each ( function () 1910rmdir ( xhome ) 1911end ) 1912 1913it ( 'loads session from the provided lua file' , function () 1914clear { args ={ '-S' , session_file } , env ={ HOME =xhome } } 1915eq ( 1 , eval ( 'g:lua_session' )) 1916end ) 1917end )