robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
3b00ddb601
master
1-- Specs for :cd, :tcd, :lcd and getcwd() 2 3local t =require ( 'test.testutil' ) 4local n =require ( 'test.functional.testnvim' )() 5local Screen =require ( 'test.functional.ui.screen' ) 6 7local describe , it , before_each , after_each =t . describe , t . it , t . before_each , t . after_each 8local eq =t . eq 9local neq =t . neq 10local exec_lua =n . exec_lua 11local pcall_err =t . pcall_err 12local call =n . call 13local clear =n . clear 14local command =n . command 15local pathsep =n . get_pathsep () 16local skip =t . skip 17local is_os =t . is_os 18local mkdir =t . mkdir 19 20-- These directories will be created for testing 21local directories ={ 22tab ='Xtest-functional-ex_cmds-cd_spec.tab' , -- Tab 23window ='Xtest-functional-ex_cmds-cd_spec.window' , -- Window 24buffer ='Xtest-functional-ex_cmds-cd_spec.buffer' , -- Buffer 25global ='Xtest-functional-ex_cmds-cd_spec.global' , -- New global 26} 27 28local tmpfile ='Xtest-functional-ex_cmds-cd_spec-tmpfile' 29local startdir ---@type string `getcwd()` at session start (set by `before_each`) 30 31local function join ( ... ) 32return table . concat ( { ... } , pathsep ) 33end 34 35-- Get the current working directory. 36local cwd =function ( ... ) 37return call ( 'getcwd' , ... ) 38end -- effective working dir 39local wcwd =function () 40return cwd ( 0 ) 41end -- window dir 42local bcwd =function () 43return cwd ( - 1 , - 1 , 0 ) 44end -- buffer dir 45local tcwd =function () 46return cwd ( - 1 , 0 ) 47end -- tab dir 48 49-- Same, except these tell us if there is a working directory at all 50local lwd =function ( ... ) 51return call ( 'haslocaldir' , ... ) 52end -- effective working dir 53local wlwd =function () 54return lwd ( 0 ) 55end -- window dir 56local blwd =function () 57return lwd ( - 1 , - 1 , 0 ) 58end -- buffer dir 59local tlwd =function () 60return lwd ( - 1 , 0 ) 61end -- tab dir 62--local glwd = function() return eval('haslocaldir(-1, -1)') end -- global dir 63 64before_each ( function () 65clear () 66for _ , d in pairs ( directories ) do 67mkdir ( d ) 68end 69startdir =cwd () 70end ) 71 72after_each ( function () 73for _ , d in pairs ( directories ) do 74n . rmdir ( d ) 75end 76end ) 77 78-- Test both the `cd` and `chdir` variants 79for _ , cmd in ipairs { 'cd' , 'chdir' } do 80describe ( ':' .. cmd , function () 81describe ( 'using explicit scope' , function () 82it ( 'for window' , function () 83local globalDir =startdir 84local globalwin =call ( 'winnr' ) 85local tabnr =call ( 'tabpagenr' ) 86 87-- Everything matches globalDir to start 88eq ( globalDir , cwd ( globalwin )) 89eq ( globalDir , cwd ( globalwin , tabnr )) 90eq ( 0 , lwd ( globalwin )) 91eq ( 0 , lwd ( globalwin , tabnr )) 92 93command ( 'bot split' ) 94local localwin =call ( 'winnr' ) 95-- Initial window is still using globalDir 96eq ( globalDir , cwd ( localwin )) 97eq ( globalDir , cwd ( localwin , tabnr )) 98eq ( 0 , lwd ( globalwin )) 99eq ( 0 , lwd ( globalwin , tabnr )) 100 101command ( 'silent l' .. cmd .. ' ' .. directories . window ) 102-- From window with local dir, the original window 103-- is still reporting the global dir 104eq ( globalDir , cwd ( globalwin )) 105eq ( globalDir , cwd ( globalwin , tabnr )) 106eq ( 0 , lwd ( globalwin )) 107eq ( 0 , lwd ( globalwin , tabnr )) 108 109-- Window with local dir reports as such 110eq ( globalDir .. pathsep .. directories . window , cwd ( localwin )) 111eq ( globalDir .. pathsep .. directories . window , cwd ( localwin , tabnr )) 112eq ( 1 , lwd ( localwin )) 113eq ( 1 , lwd ( localwin , tabnr )) 114 115command ( 'tabnew' ) 116-- From new tabpage, original window reports global dir 117eq ( globalDir , cwd ( globalwin , tabnr )) 118eq ( 0 , lwd ( globalwin , tabnr )) 119 120-- From new tabpage, local window reports as such 121eq ( globalDir .. pathsep .. directories . window , cwd ( localwin , tabnr )) 122eq ( 1 , lwd ( localwin , tabnr )) 123end ) 124 125it ( 'for tabpage' , function () 126local globalDir =startdir 127local globaltab =call ( 'tabpagenr' ) 128 129-- Everything matches globalDir to start 130eq ( globalDir , cwd ( - 1 , 0 )) 131eq ( globalDir , cwd ( - 1 , globaltab )) 132eq ( 0 , lwd ( - 1 , 0 )) 133eq ( 0 , lwd ( - 1 , globaltab )) 134 135command ( 'tabnew' ) 136command ( 'silent t' .. cmd .. ' ' .. directories . tab ) 137local localtab =call ( 'tabpagenr' ) 138 139-- From local tabpage, original tab reports globalDir 140eq ( globalDir , cwd ( - 1 , globaltab )) 141eq ( 0 , lwd ( - 1 , globaltab )) 142 143-- new tab reports local 144eq ( globalDir .. pathsep .. directories . tab , cwd ( - 1 , 0 )) 145eq ( globalDir .. pathsep .. directories . tab , cwd ( - 1 , localtab )) 146eq ( 1 , lwd ( - 1 , 0 )) 147eq ( 1 , lwd ( - 1 , localtab )) 148 149command ( 'tabnext' ) 150-- From original tabpage, local reports as such 151eq ( globalDir .. pathsep .. directories . tab , cwd ( - 1 , localtab )) 152eq ( 1 , lwd ( - 1 , localtab )) 153end ) 154 155it ( 'for buffer' , function () 156local globalDir =startdir 157-- Create two buffers 158command (( 'e %s1' ): format ( tmpfile )) 159command (( 'e %s%s%s2' ): format ( directories . buffer , pathsep , tmpfile )) 160 161-- Initially matches globalDir 162eq ( globalDir , cwd ()) 163eq ( 0 , lwd ()) 164 165-- Change buffer-local directory to subdirectory 166command ( 'bcd ' .. directories . buffer ) 167eq ( join ( globalDir , directories . buffer ), cwd ()) 168eq ( 1 , blwd ()) 169 170-- ":bcd -" changes to the previous directory, still buffer-scoped. 171command ( 'bcd -' ) 172eq ( globalDir , cwd ()) 173eq ( 1 , blwd ()) 174command ( 'bcd -' ) 175eq ( join ( globalDir , directories . buffer ), cwd ()) 176eq ( 1 , blwd ()) 177 178-- Verify Other buffer is unchanged 179command ( 'b# ' ) 180eq ( globalDir , cwd ()) 181eq ( 0 , blwd ()) 182 183-- A new buffer created with :edit does not inherit the buffer-local directory. 184command ( 'b# ' ) 185command (( 'e %s3' ): format ( tmpfile )) 186eq ( 0 , blwd ()) 187eq ( globalDir , cwd ()) 188 189-- getcwd({winnr}) reports the window's own scope chain: the buffer scope is separate, and 190-- visible only via the {bufnr} form. 191command (( 'split %s%s%s2' ): format ( directories . buffer , pathsep , tmpfile )) 192local bufnr =call ( 'winbufnr' , 1 ) 193command ( 'wincmd p' ) 194eq ( globalDir , cwd ()) 195eq ( { globalDir , globalDir } , { cwd ( 1 ), cwd ( 2 ) } ) 196eq ( join ( globalDir , directories . buffer ), cwd ( - 1 , - 1 , bufnr )) 197 198-- The {bufnr} form skips window and tab: a buffer belongs to no particular window/tabpage. 199command ( 'lcd ' .. directories . window ) 200command ( 'tcd ' .. join ( '..' , directories . tab )) 201eq ( 202{ join ( globalDir , directories . tab ), join ( globalDir , directories . tab ) } , 203{ cwd ( 0 ), tcwd () } 204) 205eq ( globalDir , cwd ( - 1 , - 1 , 0 )) 206end ) 207end ) 208 209describe ( 'getcwd(-1, -1)' , function () 210it ( 'works' , function () 211eq ( startdir , cwd ( - 1 , - 1 )) 212eq ( 0 , lwd ( - 1 , - 1 )) 213end ) 214 215it ( 'with tab-local dir' , function () 216command ( 'silent t' .. cmd .. ' ' .. directories . tab ) 217eq ( startdir , cwd ( - 1 , - 1 )) 218eq ( 0 , lwd ( - 1 , - 1 )) 219end ) 220 221it ( 'with window-local dir' , function () 222command ( 'silent l' .. cmd .. ' ' .. directories . window ) 223eq ( startdir , cwd ( - 1 , - 1 )) 224eq ( 0 , lwd ( - 1 , - 1 )) 225end ) 226 227it ( 'with buffer-local dir' , function () 228command (( 'silent b%s %s' ): format ( cmd , directories . buffer )) 229eq ( startdir , cwd ( - 1 , - 1 )) 230eq ( 0 , lwd ( - 1 , - 1 )) 231 232-- Must behave the same if bufnr is -1 233eq ( startdir , cwd ( - 1 , - 1 , - 1 )) 234eq ( 0 , lwd ( - 1 , - 1 , - 1 )) 235end ) 236end ) 237 238describe ( 'Local directory gets inherited' , function () 239it ( 'by tabs' , function () 240local globalDir =startdir 241 242-- Create a new tab and change directory 243command ( 'tabnew' ) 244command ( 'silent t' .. cmd .. ' ' .. directories . tab ) 245eq ( globalDir .. pathsep .. directories . tab , tcwd ()) 246 247-- Create a new tab and verify it has inherited the directory 248command ( 'tabnew' ) 249eq ( globalDir .. pathsep .. directories . tab , tcwd ()) 250 251-- Change tab and change back, verify that directories are correct 252command ( 'tabnext' ) 253eq ( globalDir , tcwd ()) 254command ( 'tabprevious' ) 255eq ( globalDir .. pathsep .. directories . tab , tcwd ()) 256end ) 257end ) 258 259it ( 'works' , function () 260local globalDir =startdir 261-- Create a new tab first and verify that is has the same working dir 262command ( 'tabnew' ) 263eq ( globalDir , cwd ()) 264eq ( globalDir , tcwd ()) -- has no tab-local directory 265eq ( 0 , tlwd ()) 266eq ( globalDir , wcwd ()) -- has no window-local directory 267eq ( 0 , wlwd ()) 268 269-- Change tab-local working directory and verify it is different 270command ( 'silent t' .. cmd .. ' ' .. directories . tab ) 271eq ( globalDir .. pathsep .. directories . tab , cwd ()) 272eq ( cwd (), tcwd ()) -- working directory matches tab directory 273eq ( 1 , tlwd ()) 274eq ( cwd (), wcwd ()) -- still no window-directory 275eq ( 0 , wlwd ()) 276 277-- Create a new window in this tab to test `:lcd` 278command ( 'new' ) 279eq ( 1 , tlwd ()) -- Still tab-local working directory 280eq ( 0 , wlwd ()) -- Still no window-local working directory 281eq ( globalDir .. pathsep .. directories . tab , cwd ()) 282command ( 'silent l' .. cmd .. ' ../' .. directories . window ) 283eq ( globalDir .. pathsep .. directories . window , cwd ()) 284eq ( globalDir .. pathsep .. directories . tab , tcwd ()) 285eq ( 1 , wlwd ()) 286 287-- Verify the first window still has the tab local directory 288command ( 'wincmd w' ) 289eq ( globalDir .. pathsep .. directories . tab , cwd ()) 290eq ( globalDir .. pathsep .. directories . tab , tcwd ()) 291eq ( 0 , wlwd ()) -- No window-local directory 292 293-- Change back to initial tab and verify working directory has stayed 294command ( 'tabnext' ) 295eq ( globalDir , cwd ()) 296eq ( 0 , tlwd ()) 297eq ( 0 , wlwd ()) 298 299-- Verify global changes don't affect local ones 300command ( 'silent ' .. cmd .. ' ' .. directories . global ) 301eq ( globalDir .. pathsep .. directories . global , cwd ()) 302command ( 'tabnext' ) 303eq ( globalDir .. pathsep .. directories . tab , cwd ()) 304eq ( globalDir .. pathsep .. directories . tab , tcwd ()) 305eq ( 0 , wlwd ()) -- Still no window-local directory in this window 306 307-- Unless the global change happened in a tab with local directory 308command ( 'silent ' .. cmd .. ' ..' ) 309eq ( globalDir , cwd ()) 310eq ( 0 , tlwd ()) 311eq ( 0 , wlwd ()) 312-- Which also affects the first tab 313command ( 'tabnext' ) 314eq ( globalDir , cwd ()) 315 316-- But not in a window with its own local directory 317command ( 'tabnext | wincmd w' ) 318eq ( globalDir .. pathsep .. directories . window , cwd ()) 319eq ( 0 , tlwd ()) 320eq ( globalDir .. pathsep .. directories . window , wcwd ()) 321end ) 322 323it ( 'works when mixing tab-local and buffer-local directories' , function () 324local globalDir =startdir 325 326-- Create two buffers for testing. One in each tab 327command (( 'e %s1' ): format ( tmpfile )) 328command ( 'tabnew' ) 329command (( 'e %s2' ): format ( tmpfile )) 330 331-- Verify that buffer 2 has the same working directory 332eq ( globalDir , cwd ()) 333eq ( globalDir , tcwd ()) -- Has no tab-local directory 334eq ( 0 , tlwd ()) 335eq ( globalDir , bcwd ()) -- Has no buffer-local directory 336eq ( 0 , blwd ()) 337 338-- Change tab-local working directory and verify it is different 339command (( 'silent t%s %s' ): format ( cmd , directories . tab )) 340eq ( join ( globalDir , directories . tab ), cwd ()) 341eq ( cwd (), tcwd ()) -- Working directory matches tab directory 342eq ( 1 , tlwd ()) 343eq ( globalDir , bcwd ()) -- Still no buffer-directory: the buffer form skips the tab scope 344eq ( 0 , blwd ()) 345 346-- Change buffer 2's buffer-local directory 347command (( 'silent b%s ..%s%s' ): format ( cmd , pathsep , directories . buffer )) 348eq ( join ( globalDir , directories . buffer ), cwd ()) 349eq ( join ( globalDir , directories . tab ), tcwd ()) 350eq ( cwd (), bcwd ()) -- Has no buffer-directory 351eq ( 1 , blwd ()) 352 353-- Verify the first tab has no local-directory 354command ( 'tabfirst' ) 355eq ( 0 , tlwd ()) 356 357-- Verify buffer 2 has buffer-local directory even in first tab 358command (( 'b %s2' ): format ( tmpfile )) -- Switch to buffer 2 359eq ( join ( globalDir , directories . buffer ), bcwd ()) 360eq ( 0 , tlwd ()) -- Still no tab-local directory 361 362-- Verify buffer 1 did not have its directory changed 363command ( 'b#' ) -- Switch to buffer 1 364eq ( globalDir , cwd ()) 365eq ( 0 , blwd ()) -- No window-buffer directory 366end ) 367it ( 'works when mixing window local and buffer local directories' , function () 368local globalDir =startdir 369-- Create a new window first and verify that is has the same working directory 370command ( 'new' ) 371eq ( globalDir , cwd ()) 372eq ( globalDir , wcwd ()) -- Has no window-local directory 373eq ( 0 , tlwd ()) 374eq ( globalDir , bcwd ()) -- Has no buffer-local directory 375eq ( 0 , blwd ()) 376 377-- Create a buffer in current window 378command ( 'e ' .. tmpfile ) 379 380-- Change buffer-local working directory and verify it is different 381command (( 'silent b%s %s' ): format ( cmd , directories . buffer )) 382eq ( join ( globalDir , directories . buffer ), cwd ()) 383eq ( cwd (), bcwd ()) -- Working directory matches buffer directory 384eq ( 1 , blwd ()) 385eq ( globalDir , wcwd ()) -- Still no window-directory 386eq ( 0 , wlwd ()) 387 388-- :lcd sets the window-local dir WITHOUT clearing the narrower buffer-local one. 389command (( 'silent l%s ../%s' ): format ( cmd , directories . window )) 390eq ( join ( globalDir , directories . buffer ), cwd ()) 391eq ( { 1 , 1 } , { blwd (), wlwd () } ) 392eq ( join ( globalDir , directories . window ), wcwd ()) 393 394-- Window-local dir applies if the buffer-local one is unset. 395command (( 'silent b%s!' ): format ( cmd )) 396eq ( 0 , blwd ()) 397eq ( join ( globalDir , directories . window ), cwd ()) 398 399-- Verify other window is unaffected. 400command ( 'wincmd w' ) 401command ( 'b ' .. tmpfile ) 402eq ( globalDir , cwd ()) 403command ( 'wincmd w' ) 404eq ( join ( globalDir , directories . window ), cwd ()) 405end ) 406end ) 407end 408 409for _ , cmd in ipairs { 'bcd' , 'bchdir' } do 410describe ( ':' .. cmd , function () 411it ( 'works after deleting the only buffer' , function () 412command (( '%s %s' ): format ( cmd , directories . buffer )) 413command ( 'bd' ) -- delete buffer 414end ) 415 416it ( 'buffer-local directory is NOT sticky/inherited' , function () 417local bufdir =join ( startdir , directories . buffer ) 418 419command ( 'edit ' .. tmpfile ) 420command (( '%s %s' ): format ( cmd , directories . buffer )) 421eq ( bufdir , cwd ()) 422 423-- A new buffer starts without a buffer-local directory. 424command ( 'new' ) 425eq ( startdir , cwd ()) 426eq ( 0 , blwd ()) 427command ( 'close' ) 428eq ( bufdir , cwd ()) 429command ( 'enew' ) 430eq ( startdir , cwd ()) 431eq ( 0 , blwd ()) 432command ( 'b# ' ) 433eq ( bufdir , cwd ()) 434 435-- Recycling an empty unnamed buffer (:edit) drops its directory with it. 436command ( 'enew' ) 437command (( '%s %s' ): format ( cmd , directories . buffer )) 438eq ( bufdir , cwd ()) 439command ( 'edit ' .. tmpfile .. '2' ) 440eq ( startdir , cwd ()) 441eq ( 0 , blwd ()) 442 443-- But re-editing the same buffer keeps it, like any other buffer-local state. #41213 444command (( '%s %s' ): format ( cmd , directories . buffer )) 445command ( 'let b:kept = 1' ) 446command ( 'edit!' ) 447eq ( { 1 , bufdir , 1 } , { blwd (), cwd (), n . eval ( 'get(b:, "kept", 0)' ) } ) 448end ) 449 450it ( 'is not cleared or overridden by :lcd/:tcd/:cd' , function () 451local bufdir =join ( startdir , directories . buffer ) 452 453command ( 'edit ' .. tmpfile ) 454command (( '%s %s' ): format ( cmd , directories . buffer )) 455-- Paths are relative to `bufdir`, which stays in effect throughout. 456command ( 'lcd ' .. join ( '..' , directories . window )) 457eq ( { 1 , bufdir , join ( startdir , directories . window ) } , { blwd (), cwd (), wcwd () } ) 458 459command ( 'tcd ' .. join ( '..' , directories . tab )) 460eq ( { 1 , bufdir , join ( startdir , directories . tab ) } , { blwd (), cwd (), tcwd () } ) 461 462command ( 'cd ..' ) 463eq ( { 1 , bufdir , startdir } , { blwd (), cwd (), cwd ( - 1 , - 1 ) } ) 464 465-- The overridden ":cd" still reset the window/tab scopes, so ":bcd!" lands on the global dir. 466command (( '%s!' ): format ( cmd )) 467eq ( { 0 , 0 , 0 , startdir } , { blwd (), wlwd (), tlwd (), cwd () } ) 468end ) 469end ) 470end 471 472describe ( ':lcd!/:tcd!/:bcd! (bang)' , function () 473it ( 'clear only their own scope' , function () 474local bufdir =join ( startdir , directories . buffer ) 475local windir =join ( startdir , directories . window ) 476local tabdir =join ( startdir , directories . tab ) 477 478command ( 'tcd ' .. directories . tab ) 479command ( 'lcd ' .. join ( '..' , directories . window )) 480command ( 'bcd ' .. join ( '..' , directories . buffer )) 481eq ( { 1 , 1 , 1 } , { blwd (), wlwd (), tlwd () } ) 482eq ( bufdir , cwd ()) 483 484command ( 'bcd!' ) -- Buffer scope gone: the window-local dir applies. 485eq ( { 0 , 1 , 1 } , { blwd (), wlwd (), tlwd () } ) 486eq ( windir , cwd ()) 487 488command ( 'lcd!' ) -- Window scope gone: the tab-local dir applies. 489eq ( { 0 , 0 , 1 } , { blwd (), wlwd (), tlwd () } ) 490eq ( tabdir , cwd ()) 491 492command ( 'tcd!' ) -- Tab scope gone: back to the global dir. 493eq ( { 0 , 0 , 0 } , { blwd (), wlwd (), tlwd () } ) 494eq ( startdir , cwd ()) 495 496command ( 'lcd!' ) -- No-op when the scope has no local directory. 497eq ( startdir , cwd ()) 498 499-- :cd! does not unset. 500command ( 'lcd ' .. directories . window ) 501command ( 'cd! ' .. join ( '..' , directories . global )) 502eq ( join ( startdir , directories . global ), cwd ()) 503eq ( 0 , wlwd ()) -- ":cd" cleared it, as always. 504 505-- Legacy: bang WITH arg ":lcd! {path}" is just ":lcd {path}". 506command ( 'lcd! ' .. join ( '..' , directories . window )) 507eq ( { 1 , join ( startdir , directories . window ) } , { wlwd (), cwd () } ) 508end ) 509end ) 510 511describe ( 'cd during temp context-switch' , function () 512it ( ':bcd/:tcd/:lcd persists in target scope, does not leak into original context' , function () 513local exec_lua =n . exec_lua 514local bufdir =join ( startdir , directories . buffer ) 515local windir =join ( startdir , directories . window ) 516local tabdir =join ( startdir , directories . tab ) 517 518--- Creates a loaded, hidden buffer. 519local function hidden_buf ( name ) 520local b =call ( 'bufadd' , name ) 521call ( 'bufload' , b ) 522return b 523end 524 525--- Runs `vim.cmd[cmd](dir)` with buffer `b` as temporary curbuf. 526local function cd_in_buf_call ( b , cmd , dir ) 527exec_lua ( function ( b_ , cmd_ , d ) 528vim . api . nvim_buf_call ( b_ , function () 529vim . cmd [ cmd_ ]( d ) 530end ) 531end , b , cmd , dir ) 532end 533 534-- :bcd on a hidden buffer via nvim_buf_call() persists; the caller's cwd is unchanged. 535local hidden =hidden_buf ( 'Xtest-cd-hidden' ) 536cd_in_buf_call ( hidden , 'bcd' , bufdir ) 537eq ( { 1 , bufdir , startdir } , { lwd ( - 1 , - 1 , hidden ), cwd ( - 1 , - 1 , hidden ), cwd () } ) 538 539-- :lcd targets the temporary window, which is discarded; the caller's cwd is unchanged. 540cd_in_buf_call ( hidden , 'lcd' , windir ) 541eq ( { 0 , startdir } , { wlwd (), cwd () } ) 542 543-- :lcd via win_execute() persists on the target window; the CWD outside it is unchanged. 544command ( 'split' ) 545call ( 'win_execute' , call ( 'win_getid' , 2 ), ( 'lcd %s' ): format ( windir )) 546eq ( { 1 , windir , startdir } , { lwd ( 2 ), cwd ( 2 ), cwd () } ) 547command ( 'only' ) 548 549-- Entering a hidden buffer must not consume the global dir. 550command ( 'lcd ' .. windir ) 551exec_lua ( function ( b ) 552vim . api . nvim_buf_call ( b , function () end ) 553end , hidden ) 554eq ( { windir , startdir } , { cwd (), cwd ( - 1 , - 1 ) } ) 555command ( 'lcd!' ) 556 557-- An autocmd handler targeting a hidden buffer can set its buffer-local dir; the caller's 558-- cwd is unchanged. 559local hidden2 =hidden_buf ( 'Xtest-cd-hidden2' ) 560exec_lua ( function ( b , d ) 561vim . api . nvim_create_autocmd ( 'TermRequest' , { 562buffer =b , 563once =true , 564callback =function () 565vim . cmd . bcd ( d ) 566end , 567} ) 568vim . api . nvim_exec_autocmds ( 'TermRequest' , { buffer =b , data ={ sequence ='x' } } ) 569end , hidden2 , bufdir ) 570eq ( { 1 , bufdir , startdir } , { lwd ( - 1 , - 1 , hidden2 ), cwd ( - 1 , - 1 , hidden2 ), cwd () } ) 571 572-- :tcd targets the tabpage, shared with the temp context: it persists and changes the caller's 573-- cwd. `globaldir` (the pre-switch cwd, set by :tcd) is kept, so :tcd! can return to it. 574cd_in_buf_call ( hidden , 'tcd' , tabdir ) 575eq ( { 1 , tabdir , tabdir , startdir } , { tlwd (), tcwd (), cwd (), cwd ( - 1 , - 1 ) } ) 576end ) 577 578it ( 'buffer names/statusline updated after switch back #41424' , function () 579local screen =Screen . new ( 40 , 8 ) 580local bufdir =join ( startdir , directories . buffer ) 581command ( 'edit ' .. join ( bufdir , tmpfile )) 582command ( 'bcd ' .. bufdir ) 583command ( 'split Xtest-cd-other' ) -- Buffer with no local dir. 584local otherwin =call ( 'win_getid' ) 585command ( 'wincmd p' ) 586eq ( { bufdir , tmpfile } , { cwd (), call ( 'bufname' , '%' ) } ) 587 588-- 'statusline' shows buffer names, so it must be redrawn exactly when they change. 589exec_lua ( [[ 590_G.evals = 0 591function _G.stl() 592_G.evals = _G.evals + 1 593return 'STL' 594end 595vim.o.laststatus = 2 596vim.o.statusline = '%!v:lua.stl()' 597]] ) 598screen : expect ( { any ='STL' } ) 599 600-- Entering a window leaves `bufdir`, since the buffer there has no local dir. 601exec_lua ( 602[[ 603vim._with({ win = ... }, function() 604vim.cmd('split | close') 605vim.api.nvim__redraw({ flush = true }) -- Paints the names relative to the other CWD. 606_G.inner = _G.evals 607end) 608vim.api.nvim__redraw({ flush = true }) 609]] , 610otherwin 611) 612 613-- Names relative to the old CWD would cause ":write" to target nonsense. 614eq ( { bufdir , tmpfile } , { cwd (), call ( 'bufname' , '%' ) } ) 615command ( 'write' ) 616 617-- The switch moved the CWD, so the names changed and back: 'statusline' follows them. 618neq ( exec_lua ( 'return _G.inner' ), exec_lua ( 'return _G.evals' )) 619 620-- Without a local dir the switch doesn't move the CWD, so nothing needs a redraw. #41561 621command ( 'bcd!' ) 622exec_lua ( 'vim.api.nvim__redraw({ flush = true })' ) 623local evals =exec_lua ( 'return _G.evals' ) 624call ( 'win_execute' , otherwin , 'let g:x = 1' ) 625exec_lua ( 'vim.api.nvim__redraw({ flush = true })' ) 626eq ( evals , exec_lua ( 'return _G.evals' )) 627end ) 628 629it ( "nvim_open_win / nvim_win_set_buf keep the caller's cwd" , function () 630local bufdir =join ( startdir , directories . buffer ) 631command ( 'bcd ' .. bufdir ) 632eq ( bufdir , cwd ()) 633 634-- A transient switch to a scratch float must not reset the caller's cwd. 635local float =call ( 'nvim_open_win' , call ( 'nvim_create_buf' , true , true ), false , { 636relative ='editor' , 637width =20 , 638height =5 , 639row =1 , 640col =1 , 641} ) 642eq ( bufdir , cwd ()) 643 644call ( 'nvim_win_set_buf' , float , call ( 'nvim_create_buf' , true , true )) 645eq ( bufdir , cwd ()) 646 647call ( 'nvim_win_close' , float , true ) 648eq ( bufdir , cwd ()) 649command ( 'bcd!' ) 650 651-- Switching to a window with no local dir must not consume the global dir. #41238 652local windir =join ( startdir , directories . window ) 653command ( 'split' ) 654command ( 'lcd ' .. windir ) 655call ( 'nvim_win_set_buf' , call ( 'win_getid' , 2 ), call ( 'nvim_create_buf' , true , true )) 656eq ( { windir , startdir } , { cwd (), cwd ( - 1 , - 1 ) } ) 657command ( 'wincmd w' ) 658eq ( { startdir , startdir } , { cwd (), cwd ( - 1 , - 1 ) } ) 659command ( 'only' ) 660 661-- Setting a :bcd buffer into another window, should not modify the caller's CWD. 662local bcdbuf =call ( 'nvim_create_buf' , true , true ) 663n . exec_lua ( function ( b , d ) 664vim . api . nvim_buf_call ( b , function () 665vim . cmd . bcd ( d ) 666end ) 667end , bcdbuf , bufdir ) 668command ( 'split' ) 669call ( 'nvim_win_set_buf' , call ( 'win_getid' , 2 ), bcdbuf ) 670eq ( { bufdir , startdir , startdir } , { cwd ( - 1 , - 1 , bcdbuf ), cwd (), cwd ( - 1 , - 1 ) } ) 671command ( 'only' ) 672end ) 673end ) 674 675-- Test legal parameters for 'getcwd' and 'haslocaldir' 676for _ , cmd in ipairs { 'getcwd' , 'haslocaldir' } do 677describe ( cmd .. '()' , function () 678it ( 'validation' , function () 679local err474 ='Vim:E474: Invalid argument' 680eq ( err474 , pcall_err ( call , cmd , 'some string' )) 681eq ( err474 , pcall_err ( call , cmd , 1.5 )) 682eq ( err474 , pcall_err ( call , cmd , { 1 , 2 } )) 683eq ( err474 , pcall_err ( call , cmd , { key ='value' } )) 684eq ( err474 , pcall_err ( call , cmd , - 2 )) 685-- Funcref is not representable over RPC. 686eq ( 687'Vim(call):E474: Invalid argument' , 688pcall_err ( command , ( 'call %s(function("tr"))' ): format ( cmd )) 689) 690 691-- -1 preceded by an argument >= 0 692local err5001 ='Vim:E5001: Argument cannot be -1 if preceding argument is >= 0.' 693eq ( err5001 , pcall_err ( call , cmd , 0 , - 1 )) 694eq ( err5001 , pcall_err ( call , cmd , 2 , 3 , - 1 )) 695eq ( err5001 , pcall_err ( call , cmd , - 1 , 0 , - 1 )) 696eq ( err5001 , pcall_err ( call , cmd , 0 , - 1 , 0 )) 697-- Buffer scope requires window and tab args to be -1. 698local err5006 ='Vim:E5006: Window and tab scope must be -1 when using buffer scope' 699eq ( err5006 , pcall_err ( call , cmd , 0 , 0 , 0 )) 700eq ( err5006 , pcall_err ( call , cmd , 1 , 2 , 3 )) 701eq ( 'Vim:E5007: Cannot find buffer number.' , pcall_err ( call , cmd , - 1 , - 1 , 99999 )) 702eq ( 703( 'Vim:E118: Too many arguments for function: %s' ): format ( cmd ), 704pcall_err ( call , cmd , 0 , 0 , 0 , 0 ) 705) 706end ) 707end ) 708end 709 710describe ( 'getcwd()' , function () 711it ( 'returns empty string if working directory does not exist' , function () 712skip ( is_os ( 'win' ), 'N/A for Windows' ) 713command ( 'cd ' .. directories . global ) 714command ( "call delete('../" .. directories . global .. "', 'd')" ) 715eq ( '' , n . eval ( 'getcwd()' )) 716end ) 717 718it ( "works with 'autochdir' after local directory was set (#9892)" , function () 719local curdir =cwd () 720command ( 'lcd ' .. directories . global ) 721command ( 'lcd -' ) 722command ( 'set autochdir' ) 723command ( 'edit ' .. directories . global .. '/foo' ) 724eq ( curdir .. pathsep .. directories . global , cwd ()) 725eq ( curdir , wcwd ()) 726call ( 'mkdir' , 'bar' ) 727command ( 'edit ' .. 'bar/foo' ) 728eq ( curdir .. pathsep .. directories . global .. pathsep .. 'bar' , cwd ()) 729eq ( curdir , wcwd ()) 730command ( 'lcd ..' ) 731eq ( curdir .. pathsep .. directories . global , cwd ()) 732eq ( curdir .. pathsep .. directories . global , wcwd ()) 733command ( 'edit' ) 734eq ( curdir .. pathsep .. directories . global .. pathsep .. 'bar' , cwd ()) 735eq ( curdir .. pathsep .. directories . global , wcwd ()) 736end ) 737end )